flon 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dcd15fb29f7a61dc56644751c6b3f3f6a6181e148a386e71b2b9d5197048f3dd
4
- data.tar.gz: f12632af7ac2b14a6ba79a77fcb8cdc0d6905de5ad47bb59bc73a5d5b39d3089
3
+ metadata.gz: 9d77f8f763e06566ba8fd67310e4bf2eb7cf25cf1a2927685973c8ab8310a673
4
+ data.tar.gz: f693eedb16f5dbff5505c00408ff728b4dbf8911294dea690d36139b811027db
5
5
  SHA512:
6
- metadata.gz: c552c94ec88849cdfb0d6e68b584609154f7ff73f9d486ee95860bca47416f2bb15327a6f97890810c5c8de2cb9a0e75c8503985d000448115e9d2e518e66bd7
7
- data.tar.gz: 9051e7d9f471a94b78a3731e2996eee2e61c9d7f10babc144873314a1d4cbc8cd6b2b1d1036ae99da34fd412b56411e0681d2872e91b731b3996fe914628723f
6
+ metadata.gz: 1ff850c5ec62797dfba5deda611c90aa941110e824bc39bb3cb577fa04594e2f1b7e5210874a9e910b9d5b136d9c7ecb40b23077b800d1b3f39a3b82549a011f
7
+ data.tar.gz: 179296c86b0de6ebca2629a1977a78159d1e4f261f03d3b397a834a9f41c89c5cb7cd129825aee22285d1d6ccf7421efa8f1909f834a3aac79a7ff8384d6a3b4
data/README.md CHANGED
@@ -48,12 +48,12 @@ class MyApi
48
48
  namespace '/:index' do
49
49
  get '/'
50
50
  def name_by_index(params)
51
- @names[params[:index]]
51
+ @names[params[:index].to_i]
52
52
  end
53
53
 
54
54
  put '/edit'
55
55
  def change_name(params, body)
56
- @names[params[:index]] = body
56
+ @names[params[:index].to_i] = body
57
57
  end
58
58
  end
59
59
  end
@@ -138,12 +138,12 @@ namespace '/users' do
138
138
  namespace '/:id' do
139
139
  get
140
140
  def user_by_id(params) # GET /users/:id
141
- @users[params[:id]]
141
+ @users[params[:id].to_i]
142
142
  end
143
143
 
144
144
  put '/edit'
145
145
  def edit_user(params, body) # PUT /users/:id/edit
146
- @users[params[:id]] = body
146
+ @users[params[:id].to_i] = body
147
147
  end
148
148
  end
149
149
  end
data/lib/flon/api.rb CHANGED
@@ -34,27 +34,57 @@ module Flon
34
34
  # @return [Array] a rack-suitable response
35
35
  def call(env)
36
36
  request = Rack::Request.new(env)
37
- response = dispatch(request)
37
+
38
+ method = http_method_to_symbol(request.request_method)
39
+ path = request.path_info
40
+
41
+ response = case (match = @router.match(method, path))
42
+ when :bad_path then Response.new(404, '"404 Not Found"')
43
+ when :bad_method then Response.new(405, '"405 Method Not Allowed"')
44
+ else dispatch(method, request, match)
45
+ end
46
+
38
47
  [response.status, { 'Content-Type' => 'application/json' }, [response.body]]
39
48
  end
40
49
 
41
50
  private
42
51
 
43
- def dispatch(request)
44
- method = http_method_to_symbol(request.request_method)
45
- path = request.path_info
52
+ def dispatch(method, request, match)
53
+ params = coalesce_params(request.params, match.params)
54
+
55
+ if receives_body?(method)
56
+ body = normalise_body(request)
57
+ return Response.new(415, '"415 Unsupported Media Type"') if body == :fail
58
+ else
59
+ body = nil
60
+ end
61
+
62
+ call_action(match.action, params, body)
63
+ end
46
64
 
47
- match = @router.match(method, path)
48
- case match
49
- when :bad_path then Response.new(404, '"404 Not Found"')
50
- when :bad_method then Response.new(405, '"405 Method Not Allowed"')
51
- else call_action(request.params, match, receives_body?(method) ? request.body.read : nil)
65
+ def coalesce_params(request_params, route_params)
66
+ request_params.transform_keys(&:to_sym).merge(route_params)
67
+ end
68
+
69
+ def normalise_body(request)
70
+ body = request.body.read
71
+ return body if body.empty?
72
+
73
+ return :fail unless request.get_header('Content-Type') == 'application/json'
74
+
75
+ begin
76
+ JSON.parse(body)
77
+ rescue JSON::JSONError
78
+ :fail
52
79
  end
53
80
  end
54
81
 
55
- def call_action(params, match, body)
56
- params = params.transform_keys(&:to_sym).merge(match.params)
57
- responsify(call_respect_arity(match.action.bind(@api), params, body))
82
+ def receives_body?(http_method)
83
+ http_method != :get && http_method != :delete
84
+ end
85
+
86
+ def call_action(action, params, body)
87
+ responsify(call_respect_arity(action.bind(@api), params, body))
58
88
  end
59
89
 
60
90
  def responsify(result)
@@ -74,10 +104,6 @@ module Flon
74
104
  http_method.downcase.to_sym
75
105
  end
76
106
 
77
- def receives_body?(http_method)
78
- http_method != :get && http_method != :delete
79
- end
80
-
81
107
  def valid_api?(api)
82
108
  api.class.respond_to?(:router) && api.class.router.is_a?(Flon::Router)
83
109
  end
data/lib/flon/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Flon
4
4
  # Flon's version.
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - unleashy