Class: FitApi::Controller
- Inherits:
-
Object
- Object
- FitApi::Controller
- Defined in:
- lib/fit_api/controller.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #halt(*args) ⇒ Object
-
#initialize(request, params) ⇒ Controller
constructor
A new instance of Controller.
- #invoke(action) ⇒ Object
- #json(status = 200, data) ⇒ Object
Constructor Details
#initialize(request, params) ⇒ Controller
Returns a new instance of Controller.
10 11 12 13 14 15 16 17 |
# File 'lib/fit_api/controller.rb', line 10 def initialize(request, params) @request = request @params = params set_default_headers @response = [ 501, @headers, [ 'Not implemented' ] ] end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
7 8 9 |
# File 'lib/fit_api/controller.rb', line 7 def action @action end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/fit_api/controller.rb', line 8 def headers @headers end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/fit_api/controller.rb', line 8 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
8 9 10 |
# File 'lib/fit_api/controller.rb', line 8 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
7 8 9 |
# File 'lib/fit_api/controller.rb', line 7 def response @response end |
Class Method Details
.actions ⇒ Object
20 21 22 |
# File 'lib/fit_api/controller.rb', line 20 def actions @actions ||= Hash.new { |h,k| h[k] = [] } end |
Instance Method Details
#halt(*args) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/fit_api/controller.rb', line 42 def halt(*args) is_int = args.first.is_a?(Integer) status = is_int ? args.first : 400 error = is_int ? (args.count > 1 ? args.last : "") : args.first json(status, error.to_h) raise Halt end |
#invoke(action) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/fit_api/controller.rb', line 51 def invoke(action) return unless respond_to?(action) self.action = action.to_sym trigger_callbacks(:before, action) send(action) trigger_callbacks(:after, action) @response end |
#json(status = 200, data) ⇒ Object
36 37 38 39 40 |
# File 'lib/fit_api/controller.rb', line 36 def json(status = 200, data) data = data.to_h unless data.is_a?(Hash) && data.is_a?(String) json = JSON.pretty_generate(data) @response = [ status, headers, [ json ] ] end |