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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d36a694f0222623e6ef2d22990328dafcca101afb9862bb10911bb5dd8758275
4
- data.tar.gz: c39d8a2d8179cb27390c647265b78068ca9ebbc84ecb2a4a38629563e3b7d4b2
3
+ metadata.gz: 0d176cc7557464b1cb5b9756b11feb083c8ec735799d2d89e587d3a271794b86
4
+ data.tar.gz: 5a0a48d8fbf84fd0abd3e82b004b34f9393a522a2cdec75250d6698840cc8dd7
5
5
  SHA512:
6
- metadata.gz: c573401548394fde83e25e09e132a1c581a2617e3e62815dd853264de96e56743c22b7fd3d549f6354750b7626b9f851fdeeceb5767892069e05d6fd5e432000
7
- data.tar.gz: 2fc7176c773d13da6abd1b51891a92bef7d2873c3f36950a7f9a597099c6eef82e2e58c527e632ee4616140e3899cd45df1486ac03c0e374a1ed00f7d0933de2
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 params
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, only: %i(index show)
334
+ before_action *actions, only: %i(index show)
335
+ after_action *actions, except: %i(destroy)
326
336
  ```
327
337
 
328
338
  ## TODO:
@@ -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 |*methods|
21
- only = methods.last.is_a?(Hash) ? methods.last[:only] : nil
22
- methods.each do |method|
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, only: only }
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'] = Rack::Utils.rfc2822(Time.now)
33
- headers['Content-Type'] = 'application/json'
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
- private
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
- is_integer = args.first.is_a?(Integer)
46
- status = is_integer ? args.first : 400
47
- error = is_integer ? (args.count > 1 ? args.last : '') : args.first
48
- json(error, status: status)
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[:only]
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)
@@ -5,6 +5,10 @@ module FitApi
5
5
  @hash = hash
6
6
  end
7
7
 
8
+ def to_h
9
+ @hash
10
+ end
11
+
8
12
  def to_json
9
13
  @hash.to_json
10
14
  end
@@ -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.actions[type].each do |action|
60
- if action[:only].nil? || action[:only].include?(@action.to_sym)
61
- controller.send action[:method]
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
@@ -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
- routes = mapper.routes[method.downcase]
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
@@ -1,5 +1,5 @@
1
1
  module FitApi
2
2
  def self.version
3
- '1.1.1'
3
+ '1.1.2'
4
4
  end
5
5
  end
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.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-25 00:00:00.000000000 Z
11
+ date: 2019-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack