Class: Routemaster::Responses::HateoasResponse

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

Direct Known Subclasses

HateoasEnumerableResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Forwardable

_compile_method

Constructor Details

#initialize(response, client: nil) ⇒ HateoasResponse

Returns a new instance of HateoasResponse.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/routemaster/responses/hateoas_response.rb', line 18

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)
      define_singleton_method(method_name) { resource }
      public_send method_name
    end
  else
    super
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/routemaster/responses/hateoas_response.rb', line 10

def response
  @response
end

Instance Method Details



33
34
35
# File 'lib/routemaster/responses/hateoas_response.rb', line 33

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

#has?(link) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/routemaster/responses/hateoas_response.rb', line 37

def has?(link)
  _links.has_key?(link.to_s)
end