Class: FitApi::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/fit_api/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#actionObject

Returns the value of attribute action.



7
8
9
# File 'lib/fit_api/controller.rb', line 7

def action
  @action
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/fit_api/controller.rb', line 8

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/fit_api/controller.rb', line 8

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/fit_api/controller.rb', line 8

def request
  @request
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/fit_api/controller.rb', line 7

def response
  @response
end

Class Method Details

.actionsObject



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

Raises:



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