Class: AcceptableApi::Action
- Inherits:
-
Object
- Object
- AcceptableApi::Action
- Defined in:
- lib/acceptable_api/action.rb
Instance Method Summary collapse
- #acceptable_mime_types ⇒ Object
- #controller ⇒ Object
- #execute ⇒ Object
-
#initialize(route, request, mappers) ⇒ Action
constructor
A new instance of Action.
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_types ⇒ Object
39 40 41 |
# File 'lib/acceptable_api/action.rb', line 39 def acceptable_mime_types Accepts.new request.env end |
#controller ⇒ Object
35 36 37 |
# File 'lib/acceptable_api/action.rb', line 35 def controller route.controller_for_request request end |
#execute ⇒ Object
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 |