Class: Hypermodel::Responder

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(resource_name, action, record, controller) ⇒ Responder

Returns a new instance of Responder.



28
29
30
31
32
# File 'lib/hypermodel/responder.rb', line 28

def initialize(resource_name, action, record, controller)
  @resource_name = resource_name
  @action        = action
  @resource      = Resource.new(record, controller)
end

Class Method Details

.call(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/hypermodel/responder.rb', line 17

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



34
35
36
# File 'lib/hypermodel/responder.rb', line 34

def to_json(*opts)
  @resource.to_json(*opts)
end