casual-api 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/casual-api +14 -2
  3. data/lib/casual.rb +18 -10
  4. data/lib/dsl.rb +53 -10
  5. metadata +1 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 615e5453eed2773913ee6567ca5e25e8b34e54e5
4
- data.tar.gz: caab53ecf1987013cbc48e5a24a6a1cc7214a68f
3
+ metadata.gz: b1552be82e1bda4452edefb02d6aa9bcf80a3acc
4
+ data.tar.gz: 72e38979a147b0433d378a5c2a622af96e1624ca
5
5
  SHA512:
6
- metadata.gz: 6f90e9b8ad933610cff8933eaf2418a9423da898216e95525b7a85eb81f8ab0412bb74817e3b76979c193ac7a4c45b333182b129407d00a5675200525393d105
7
- data.tar.gz: 4569b89b9554e635dac8e7e004241ccf602f971d351c7ecfad26d1be74e9ad6217303cb05db240d0afc8a43397b0358c174b142abe53b52f4623715d23a1b568
6
+ metadata.gz: abfc8346e7c6caba58546df5d88fc242cd3ff37d7768c215d975ecc1f3bf3c0f4cdc5f2fcd17c8f2d630ec3c1df14e1a3b427eed17e1de2d8b21ed556809c0a9
7
+ data.tar.gz: 1f4471ba83ee2893bae942cee31911ca86452c93cb317f432a187f844066e8e6b6351a1afdb84e43a5ecf203fbd972fe9ef1e711cad4dc19dbf4a2a85f376186
data/bin/casual-api CHANGED
@@ -1,8 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'open3'
3
3
  lib = File.expand_path('../../lib', __FILE__)
4
- port = ARGV[0] || 3000
5
- command = "thin -p #{port} -R #{lib}/config.ru start"
4
+ command = "rackup #{lib}/config.ru #{ ARGV.join(' ') }"
6
5
  puts "casual-api is running, CTRL+C to stop"
7
6
  _, out, err, th = Open3.popen3(command)
7
+ tho = Thread.new{
8
+ while line = out.gets
9
+ STDOUT.puts line
10
+ end
11
+ }
12
+ the = Thread.new{
13
+ while line = err.gets
14
+ STDERR.puts line
15
+ end
16
+ }
17
+
8
18
  th.join
19
+ tho.join
20
+ the.join
data/lib/casual.rb CHANGED
@@ -40,15 +40,23 @@ class Casual
40
40
  actions = parse_actions
41
41
  action = detect_action actions
42
42
  if action
43
- cp = ChildProcess.new(action, parameters, session)
44
- result = cp.execute
45
- @req.session['casual.session'] = result[:session]
46
- res = Rack::Response.new(){|r|
47
- r.status = result[:status_code]
48
- r.write result[:body]
49
- r['Content-Type'] = map_content_type(result[:content_type])
50
- }
51
- res.finish
43
+ begin
44
+ cp = ChildProcess.new(action, parameters, session)
45
+ result = cp.execute
46
+ @req.session['casual.session'] = result[:session]
47
+ res = Rack::Response.new(){|r|
48
+ r.status = result[:status_code]
49
+ r.write result[:body]
50
+ r['Content-Type'] = map_content_type(result[:content_type])
51
+ }
52
+ res.finish
53
+ rescue CommandError => ex
54
+ return [ex.status_code,
55
+ { 'Content-Type' => map_content_type(ex.content_type) },
56
+ [ex.message]]
57
+ rescue => ex
58
+ [500, {}, ["Server Error"]]
59
+ end
52
60
  else
53
61
  [404, {}, []]
54
62
  end
@@ -77,7 +85,7 @@ class Casual
77
85
  }
78
86
  end
79
87
  def make_path action
80
- @prefix + (action[:domain] + [action[:name]]).compact.map{|p|p.to_s}.join('/')
88
+ @prefix + (action[:domain] + action[:names]).compact.map{|p|p.to_s}.join('/')
81
89
  end
82
90
  def parameters
83
91
  @req.params.map{|k,v| {k.to_sym => v} }.inject({}, :merge)
data/lib/dsl.rb CHANGED
@@ -28,6 +28,13 @@ module BodyWriter
28
28
  end
29
29
  end
30
30
 
31
+ class CommandError < StandardError
32
+ def content_type; :txt; end
33
+ def status_code; 400; end
34
+ end
35
+ class InvalidParameterError < CommandError
36
+ end
37
+
31
38
  module Action
32
39
  def self.extended obj
33
40
  obj.instance_variable_set(:@session, {})
@@ -36,6 +43,10 @@ module Action
36
43
  obj.instance_variable_set(:@tempfiles, [])
37
44
  end
38
45
 
46
+ def invalid_parameter *params
47
+ raise InvalidParameterError, "Invalid parameter(s): #{params.map{|p|p.to_s}.join(',')}"
48
+ end
49
+
39
50
  def file? file
40
51
  file.is_a? Hash
41
52
  end
@@ -77,9 +88,7 @@ module Action
77
88
  }.inject({}, :merge)
78
89
  result = execute(obj, ps, action[:action])
79
90
  unless result[:executed]
80
- obj.status_code = 400
81
- obj.body = "Not given enough parameter"
82
- obj.content_type = :txt
91
+ raise InvalidParameterError, "Not given required parameter(s)"
83
92
  end
84
93
  obj.tempfiles.each{|f| f.close }
85
94
  obj
@@ -127,13 +136,47 @@ module ExtExpression
127
136
  end
128
137
 
129
138
  module SyntaxSugar
130
- def get(path, &block); path(:get, path, caller, &block); end
131
- def post(path, &block); path(:post, path, caller, &block); end
139
+ def get(*names, &block); path(:get, *names, &block); end
140
+ def post(*names, &block); path(:post, *names, &block); end
132
141
 
133
- def command(path, cmd)
134
- get path do
135
- write `#{cmd}`
142
+ def command(*names, cmd)
143
+ eval(replace :get, names, "write `#{cmd}`")
144
+ end
145
+
146
+ def text(*names, text)
147
+ eval(replace :get, names, "write \"#{text}\"")
148
+ end
149
+
150
+ def get_command(*names, cmd); command(*names, cmd); end
151
+ def post_command(*names, cmd)
152
+ eval(replace :post, names, "write `#{cmd}`")
153
+ end
154
+
155
+ def get_text(*names, text); text(*names, text); end
156
+ def post_text(*names, text)
157
+ eval(replace :post, names, "write \"#{text}\"")
158
+ end
159
+
160
+ private
161
+ def replace meth, names, cmd,
162
+ ps = cmd.scan(/\$\{(?<p>[^\}]*)\}/).flatten
163
+ fs = cmd.scan(/\$\<(?<p>[^\}]*)\>/).flatten
164
+ c = ps.inject(cmd){|acc,c|
165
+ acc.gsub /\$\{#{c}\}/, "\#{#{c}}"
166
+ }
167
+ c = fs.inject(c){|acc,c|
168
+ acc.gsub /\$\<#{c}\>/, "\#{#{c}}"
169
+ }
170
+ filecheck = fs.inject(""){|acc,f|
171
+ acc << "invalid_parameter :#{f} unless file? #{f}\n"
172
+ acc << "#{f} = tempfile #{f}\n"
173
+ }
174
+ args = (ps + fs).join(',')
175
+ <<PROC
176
+ #{meth.to_s}(#{names.map{|n|":#{n.to_s}"}.join(',')}) do | #{args} |
177
+ #{filecheck}#{c}
136
178
  end
179
+ PROC
137
180
  end
138
181
  end
139
182
 
@@ -165,10 +208,10 @@ module DSL
165
208
  clear_annotaion
166
209
  end
167
210
 
168
- def path(method, name, c=nil, &block)
211
+ def path(method, *names, &block)
169
212
  meta = @context_stack.last.clone
170
213
  meta[:method] = method
171
- meta[:name] = name
214
+ meta[:names] = names
172
215
  meta[:domain] = @domain_stack.clone
173
216
  meta[:action] = block
174
217
  clear_annotaion # annotations effect only a path.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casual-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi Kojima
@@ -10,26 +10,6 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: thin
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.6'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.6.2
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '1.6'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 1.6.2
33
13
  - !ruby/object:Gem::Dependency
34
14
  name: rack
35
15
  requirement: !ruby/object:Gem::Requirement