Class: JsonApiClient::Linking::TopLevelLinks

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/linking/top_level_links.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_class, links) ⇒ TopLevelLinks

Returns a new instance of TopLevelLinks.


7
8
9
10
# File 'lib/json_api_client/linking/top_level_links.rb', line 7

def initialize(record_class, links)
  @links = links
  @record_class = record_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object


16
17
18
19
20
21
22
# File 'lib/json_api_client/linking/top_level_links.rb', line 16

def method_missing(method, *args)
  if respond_to_missing?(method)
    fetch_link(method)
  else
    super
  end
end

Instance Attribute Details

Returns the value of attribute links


5
6
7
# File 'lib/json_api_client/linking/top_level_links.rb', line 5

def links
  @links
end

#record_classObject (readonly)

Returns the value of attribute record_class


5
6
7
# File 'lib/json_api_client/linking/top_level_links.rb', line 5

def record_class
  @record_class
end

Instance Method Details


33
34
35
36
# File 'lib/json_api_client/linking/top_level_links.rb', line 33

def fetch_link(link_name)
  return unless respond_to_missing?(link_name)
  record_class.requestor.linked(link_url_for(link_name))
end

24
25
26
27
28
29
30
31
# File 'lib/json_api_client/linking/top_level_links.rb', line 24

def link_url_for(link_name)
  link_definition = links.fetch(link_name.to_s)
  if link_definition.is_a?(Hash)
    link_definition["href"]
  else
    link_definition
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/json_api_client/linking/top_level_links.rb', line 12

def respond_to_missing?(method, include_private = false)
  links.has_key?(method.to_s) || super
end