flon 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/flon/api.rb +42 -16
- data/lib/flon/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d77f8f763e06566ba8fd67310e4bf2eb7cf25cf1a2927685973c8ab8310a673
|
4
|
+
data.tar.gz: f693eedb16f5dbff5505c00408ff728b4dbf8911294dea690d36139b811027db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
56
|
-
|
57
|
-
|
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