Class: AcceptableApi::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/acceptable_api/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(route, request, mappers) ⇒ Action

Returns a new instance of Action.



12
13
14
15
16
# File 'lib/acceptable_api/action.rb', line 12

def initialize route, request, mappers
  self.route = route
  self.request = request
  self.mappers = mappers
end

Instance Method Details

#acceptable_mime_typesObject



39
40
41
# File 'lib/acceptable_api/action.rb', line 39

def acceptable_mime_types
  Accepts.new request.env
end

#controllerObject



35
36
37
# File 'lib/acceptable_api/action.rb', line 35

def controller
  route.controller_for_request request
end

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/acceptable_api/action.rb', line 18

def execute
  resource = controller.perform_action
  resource_mappers = mappers.from resource.class
  mapper = resource_mappers.to acceptable_mime_types
  if mapper.missing?
    body = {
      :links => resource_mappers.mime_types.map do |mime_type|
        { :rel => "alternative", 'Content-Type' => mime_type, :uri => request.url, :method => request.request_method }
      end
    }
    [ 406, { 'Content-Type' => 'application/json' }, [ JSON.pretty_generate(body) ] ]
  else
    body = mapper.execute resource, request
    [ 200, { 'Content-Type' => mapper.to }, [ body ] ]
  end
end