Class: JsonApiClient::LinkedData
- Inherits:
-
Object
- Object
- JsonApiClient::LinkedData
- Extended by:
- Forwardable
- Defined in:
- lib/json_api_client/linked_data.rb
Instance Attribute Summary collapse
-
#link_definition ⇒ Object
readonly
Returns the value of attribute link_definition.
-
#record_class ⇒ Object
readonly
Returns the value of attribute record_class.
Instance Method Summary collapse
- #add_data(key, data) ⇒ Object
- #data_for(type, ids) ⇒ Object
-
#fetch_data(klass, type, missing_ids) ⇒ Object
make an api request to fetch the missing data.
-
#initialize(data, link_definition, record_class) ⇒ LinkedData
constructor
A new instance of LinkedData.
- #klass_for(type) ⇒ Object
Constructor Details
#initialize(data, link_definition, record_class) ⇒ LinkedData
Returns a new instance of LinkedData.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/json_api_client/linked_data.rb', line 11 def initialize(data, link_definition, record_class) @link_definition = link_definition @record_class = record_class @results_by_type_by_id = {} data.each do |type, results| klass = klass_for(type) add_data(type, results.map{|result| klass.new(result)}) end end |
Instance Attribute Details
#link_definition ⇒ Object (readonly)
Returns the value of attribute link_definition.
5 6 7 |
# File 'lib/json_api_client/linked_data.rb', line 5 def link_definition @link_definition end |
#record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
5 6 7 |
# File 'lib/json_api_client/linked_data.rb', line 5 def record_class @record_class end |
Instance Method Details
#add_data(key, data) ⇒ Object
59 60 61 62 |
# File 'lib/json_api_client/linked_data.rb', line 59 def add_data(key, data) @results_by_type_by_id[key] ||= {} @results_by_type_by_id[key].merge!(data.index_by{|datum| datum["id"]}) end |
#data_for(type, ids) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/json_api_client/linked_data.rb', line 22 def data_for(type, ids) ids = Array(ids) # the name of the linked data is provided by the link definition from the result attr_name = link_definition.attribute_name_for(type) # get any preloaded data from the result type_data = @results_by_type_by_id.fetch(attr_name, {}) # find the associated class for the data klass = klass_for(type) # return all the found records found, missing = ids.partition { |id| type_data[id].present? } # make another api request if there are missing records fetch_data(klass, type, missing) if missing.present? # reload data type_data = @results_by_type_by_id.fetch(attr_name, {}) ids.map do |id| type_data[id] end end |
#fetch_data(klass, type, missing_ids) ⇒ Object
make an api request to fetch the missing data
49 50 51 52 53 54 55 56 57 |
# File 'lib/json_api_client/linked_data.rb', line 49 def fetch_data(klass, type, missing_ids) path = URI(link_definition.url_for(type, missing_ids)).path query = Query::Linked.new(path) results = klass.run_request(query) key = link_definition.attribute_name_for(type).to_s add_data(key, results) end |
#klass_for(type) ⇒ Object
64 65 66 |
# File 'lib/json_api_client/linked_data.rb', line 64 def klass_for(type) Utils.compute_type(record_class, type.to_s.pluralize.classify) end |