Class: Hypermodel::Responder
- Inherits:
-
Object
- Object
- Hypermodel::Responder
- Defined in:
- lib/hypermodel/responder.rb
Overview
Public: Responsible for exposing a resource in JSON-HAL format.
Examples
class PostsController < ApplicationController
respond_to :json
def show
@post = Post.find params[:id]
respond_with(@post, responder: Hypermodel::Responder)
end
end
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(resource_name, action, record, controller) ⇒ Responder
constructor
A new instance of Responder.
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(resource_name, action, record, controller) ⇒ Responder
Returns a new instance of Responder.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/hypermodel/responder.rb', line 30 def initialize(resource_name, action, record, controller) @resource_name = resource_name @action = action if record.respond_to?(:each) @resource = if record.empty? EmptyCollection.new(resource_name, controller) else Collection.new(record, controller) end else @resource = Resource.new(record, controller) end end |
Class Method Details
.call(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hypermodel/responder.rb', line 19 def self.call(*args) controller = args[0] resource = args[1].first resource_name = controller.params["controller"] action = controller.params["action"] responder = new resource_name, action, resource, controller controller.render json: responder end |
Instance Method Details
#to_json(*opts) ⇒ Object
45 46 47 |
# File 'lib/hypermodel/responder.rb', line 45 def to_json(*opts) @resource.to_json(*opts) end |