Class: Routemaster::Responses::HateoasResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/routemaster/responses/hateoas_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, client: nil) ⇒ HateoasResponse

Returns a new instance of HateoasResponse.



16
17
18
19
# File 'lib/routemaster/responses/hateoas_response.rb', line 16

def initialize(response, client: nil)
  @response = response
  @client = client || Routemaster::APIClient.new(response_class: Routemaster::Responses::HateoasResponse)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/routemaster/responses/hateoas_response.rb', line 21

def method_missing(m, *args, &block)
  method_name = m.to_s
  normalized_method_name = method_name == '_self' ? 'self' : method_name

  if _links.keys.include?(normalized_method_name)
    unless respond_to?(method_name)
      resource = Resources::RestResource.new(_links[normalized_method_name]['href'], client: @client)

      self.class.send(:define_method, method_name) do |*m_args|
        resource
      end

      resource
    end
  else
    super
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



13
14
15
# File 'lib/routemaster/responses/hateoas_response.rb', line 13

def response
  @response
end

Instance Method Details



40
41
42
# File 'lib/routemaster/responses/hateoas_response.rb', line 40

def body_without_links
  body.reject { |key, _| ['_links'].include?(key) }
end