fit_api 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -4
- data/lib/fit_api/controller.rb +14 -14
- data/lib/fit_api/router/mapper.rb +14 -4
- data/lib/fit_api/router/params.rb +4 -0
- data/lib/fit_api/router/route.rb +19 -5
- data/lib/fit_api/router.rb +2 -4
- data/lib/fit_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d176cc7557464b1cb5b9756b11feb083c8ec735799d2d89e587d3a271794b86
|
4
|
+
data.tar.gz: 5a0a48d8fbf84fd0abd3e82b004b34f9393a522a2cdec75250d6698840cc8dd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c079a3c4f083fd3543878d9e6d9cf030ee28b9f91ce67a94b4ca31a5d70c6128f3de32c09822b109ce4188ff897b6dc6b7298f8aa73205a7d05f56c67253fc36
|
7
|
+
data.tar.gz: ca6de1c94256a2840192be7e3fea05d5575a4d57a747be8026540590ca22448a6d40f5fcee52b713a3d7b0f84873b685521a45e7f388be66c00b83d239276fe5
|
data/README.md
CHANGED
@@ -88,6 +88,14 @@ delete '/test/:id', to: 'app#test_delete'
|
|
88
88
|
|
89
89
|
### Resources
|
90
90
|
|
91
|
+
You can pass the following options:
|
92
|
+
|
93
|
+
```
|
94
|
+
only
|
95
|
+
except
|
96
|
+
controller
|
97
|
+
```
|
98
|
+
|
91
99
|
**Nested:**
|
92
100
|
|
93
101
|
```ruby
|
@@ -220,12 +228,14 @@ class AppController < FitApi::Controller
|
|
220
228
|
end
|
221
229
|
|
222
230
|
def process_post
|
223
|
-
json
|
231
|
+
json resource, 201
|
224
232
|
end
|
225
233
|
end
|
226
234
|
```
|
227
235
|
|
228
|
-
You have the method `#json` available, which basically sets the response body.
|
236
|
+
You have the method `#json` available, which basically sets the response body.
|
237
|
+
|
238
|
+
Default status code: ```200```
|
229
239
|
|
230
240
|
----
|
231
241
|
|
@@ -321,8 +331,8 @@ headers['Header-Name'] = 'Header Value'
|
|
321
331
|
### Callbacks
|
322
332
|
|
323
333
|
```ruby
|
324
|
-
before_action *actions
|
325
|
-
after_action *actions,
|
334
|
+
before_action *actions, only: %i(index show)
|
335
|
+
after_action *actions, except: %i(destroy)
|
326
336
|
```
|
327
337
|
|
328
338
|
## TODO:
|
data/lib/fit_api/controller.rb
CHANGED
@@ -17,11 +17,12 @@ module FitApi
|
|
17
17
|
end
|
18
18
|
|
19
19
|
%i(before after).each do |callback_type|
|
20
|
-
define_method "#{callback_type}_action" do |*
|
21
|
-
|
22
|
-
|
20
|
+
define_method "#{callback_type}_action" do |*callbacks|
|
21
|
+
executable_actions = callbacks.last.is_a?(Hash) ? callbacks.last : {}
|
22
|
+
|
23
|
+
callbacks.each do |method|
|
23
24
|
unless method.is_a?(Hash)
|
24
|
-
actions[callback_type] << { method: method
|
25
|
+
actions[callback_type] << { method: method }.merge(executable_actions)
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
@@ -29,23 +30,22 @@ module FitApi
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def set_response_headers
|
32
|
-
headers['Date']
|
33
|
-
headers['Content-Type']
|
33
|
+
headers['Date'] ||= Rack::Utils.rfc2822(Time.now)
|
34
|
+
headers['Content-Type'] ||= 'application/json'
|
34
35
|
|
35
36
|
headers.each &response.method(:add_header)
|
36
37
|
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
def json(hash, status: 200)
|
41
|
-
self.response = Rack::Response.new(hash.to_json, status)
|
39
|
+
def json(object, status = 200)
|
40
|
+
self.response = Rack::Response.new(JSON.pretty_generate(object), status)
|
42
41
|
end
|
43
42
|
|
44
43
|
def halt(*args)
|
45
|
-
|
46
|
-
status =
|
47
|
-
error
|
48
|
-
|
44
|
+
is_int = args.first.is_a?(Integer)
|
45
|
+
status = is_int ? args.first : 400
|
46
|
+
error = is_int ? (args.count > 1 ? args.last : '') : args.first
|
47
|
+
|
48
|
+
json(error, status)
|
49
49
|
raise Halt
|
50
50
|
end
|
51
51
|
end
|
@@ -59,20 +59,30 @@ module FitApi
|
|
59
59
|
private
|
60
60
|
|
61
61
|
def set_resource(type, resource, options, &block)
|
62
|
-
options[:only] ||= %i(index show create update destroy)
|
63
62
|
options[:controller] ||= resource
|
64
|
-
|
65
63
|
path = get_path(type, resource)
|
66
64
|
|
67
65
|
@parent = [ type, resource ]
|
66
|
+
@previous = @controller
|
68
67
|
@controller = options[:controller]
|
69
68
|
|
70
69
|
namespace path, options do
|
71
|
-
set_actions type, resource, options
|
70
|
+
set_actions type, resource, get_actions(options)
|
72
71
|
instance_eval &block if block
|
73
72
|
end
|
74
73
|
|
75
|
-
@parent, @controller = nil, nil
|
74
|
+
@parent, @previous, @controller = nil, nil, @previous
|
75
|
+
end
|
76
|
+
|
77
|
+
def get_actions(options)
|
78
|
+
actions = %i(index show create update destroy)
|
79
|
+
only = options[:only]
|
80
|
+
except = options[:except]
|
81
|
+
|
82
|
+
return actions & only if only
|
83
|
+
return actions - except if except
|
84
|
+
|
85
|
+
actions
|
76
86
|
end
|
77
87
|
|
78
88
|
def set_actions(type, resource, actions)
|
data/lib/fit_api/router/route.rb
CHANGED
@@ -22,7 +22,11 @@ module FitApi
|
|
22
22
|
|
23
23
|
run! controller
|
24
24
|
rescue Halt
|
25
|
+
controller.set_response_headers
|
25
26
|
controller.response
|
27
|
+
rescue Exception => ex
|
28
|
+
error = { message: "#{ex.message} (#{ex.class})", backtrace: ex.backtrace }
|
29
|
+
controller.json(ENV['RACK_ENV'] == 'production' ? 'An error has occured' : error, 500)
|
26
30
|
end
|
27
31
|
|
28
32
|
def match?(path)
|
@@ -50,18 +54,28 @@ module FitApi
|
|
50
54
|
run_callbacks! controller, :before
|
51
55
|
controller.send action
|
52
56
|
run_callbacks! controller, :after
|
53
|
-
controller.response
|
54
|
-
ensure
|
55
57
|
controller.set_response_headers
|
58
|
+
controller.response
|
56
59
|
end
|
57
60
|
|
58
61
|
def run_callbacks!(controller, type)
|
59
|
-
controller.class
|
60
|
-
|
61
|
-
|
62
|
+
klass = controller.class
|
63
|
+
|
64
|
+
while klass != Object
|
65
|
+
actions = klass.actions[type].each do |rule|
|
66
|
+
controller.send(rule[:method]) if run?(rule)
|
62
67
|
end
|
68
|
+
klass = klass.superclass
|
63
69
|
end
|
64
70
|
end
|
71
|
+
|
72
|
+
def run?(rule)
|
73
|
+
except, only = rule[:except], rule[:only]
|
74
|
+
|
75
|
+
except && !except.map(&:to_s).include?(action) ||
|
76
|
+
only && only.map(&:to_s).include?(action) ||
|
77
|
+
!only && !except
|
78
|
+
end
|
65
79
|
end
|
66
80
|
|
67
81
|
class Request < Rack::Request
|
data/lib/fit_api/router.rb
CHANGED
@@ -12,14 +12,12 @@ module FitApi
|
|
12
12
|
status = is_root ? 200 : 404
|
13
13
|
res = is_root ? 'fit-api is working!' : 'Action not found'
|
14
14
|
|
15
|
-
[ status, { 'Content-Type' => 'application/json'}, [ res.to_json ] ]
|
15
|
+
[ status, { 'Content-Type' => 'application/json' }, [ res.to_json ] ]
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.find(method, path, find_not_found_action = true)
|
20
|
-
|
21
|
-
route = routes.find { |route| route.match? path }
|
22
|
-
|
20
|
+
route = mapper.routes[method.downcase].find { |route| route.match? path }
|
23
21
|
return route if route
|
24
22
|
|
25
23
|
if find_not_found_action
|
data/lib/fit_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fit_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernardo Castro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|