Class: Routemaster::Responses::HateoasResponse
- Inherits:
-
Object
- Object
- Routemaster::Responses::HateoasResponse
- Extended by:
- Forwardable
- Defined in:
- lib/routemaster/responses/hateoas_response.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #body_without_links ⇒ Object
- #has?(link) ⇒ Boolean
-
#initialize(response, client: nil) ⇒ HateoasResponse
constructor
A new instance of HateoasResponse.
- #method_missing(m, *args, &block) ⇒ Object
Methods included from Forwardable
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
#response ⇒ Object (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
#body_without_links ⇒ Object
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
37 38 39 |
# File 'lib/routemaster/responses/hateoas_response.rb', line 37 def has?(link) _links.has_key?(link.to_s) end |