Class: Gecko::Record::BaseAdapter
- Inherits:
-
Object
- Object
- Gecko::Record::BaseAdapter
- Defined in:
- lib/gecko/record/base_adapter.rb
Direct Known Subclasses
AccountAdapter, AddressAdapter, CompanyAdapter, ContactAdapter, CurrencyAdapter, FulfillmentAdapter, FulfillmentLineItemAdapter, ImageAdapter, InvoiceAdapter, InvoiceLineItemAdapter, LocationAdapter, OrderAdapter, OrderLineItemAdapter, PaymentTermAdapter, ProductAdapter, PurchaseOrderAdapter, PurchaseOrderLineItemAdapter, TaxTypeAdapter, UserAdapter, VariantAdapter
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Gecko::Record::Base
Build a new record.
-
#count ⇒ Integer
Returns the total count for a record type via API request.
-
#extract_collection(json) ⇒ Hash
private
Extract a collection from an API response.
-
#extract_record(json) ⇒ Object
private
Extract a record from an API response.
-
#fetch(id) ⇒ Gecko::Record::Base?
private
Fetch a record via API, regardless of whether it is already in identity map.
-
#find(id) ⇒ Gecko::Record::Base?
Find a record via ID, first searches the Identity Map, then makes an API request.
-
#find_many(ids) ⇒ Array<Gecko::Record::Base>
Find multiple records via IDs, searching the Identity Map, then making an API request for remaining records.
-
#has_record_for_id?(id) ⇒ Boolean
private
Returns whether the Identity Map has a record for a particular ID.
-
#initialize(client, model_name) ⇒ undefined
constructor
private
Instantiates a new Record Adapter.
-
#instantiate_and_register_record(record_json) ⇒ Gecko::Record::Base
private
Instantiates a record from it’s JSON representation and registers it into the identity map.
-
#parse_records(json) ⇒ Array<Gecko::Record::Base>
private
Parse a json collection and instantiate records.
-
#record_for_id(id) ⇒ Gecko::Record::Base
private
Searches the Identity Map for a record via ID.
-
#save(record) ⇒ Boolean
private
Save a record.
-
#size ⇒ Integer
Returns the total count for a record type.
-
#where(params = {}) ⇒ Array<Gecko::Record::Base>
Make an API request with parameters.
Constructor Details
#initialize(client, model_name) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instantiates a new Record Adapter
13 14 15 16 17 |
# File 'lib/gecko/record/base_adapter.rb', line 13 def initialize(client, model_name) @client = client @model_name = model_name @identity_map = {} end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'lib/gecko/record/base_adapter.rb', line 4 def client @client end |
Instance Method Details
#build(attributes = {}) ⇒ Gecko::Record::Base
Build a new record
213 214 215 |
# File 'lib/gecko/record/base_adapter.rb', line 213 def build(attributes={}) model_class.new(@client, attributes) end |
#count ⇒ Integer
Returns the total count for a record type via API request.
126 127 128 129 |
# File 'lib/gecko/record/base_adapter.rb', line 126 def count self.where(limit: 0) @pagination['total_records'] end |
#extract_collection(json) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract a collection from an API response
186 187 188 |
# File 'lib/gecko/record/base_adapter.rb', line 186 def extract_collection(json) json[plural_path] end |
#extract_record(json) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract a record from an API response
195 196 197 |
# File 'lib/gecko/record/base_adapter.rb', line 195 def extract_record(json) json && json[json_root] end |
#fetch(id) ⇒ Gecko::Record::Base?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetch a record via API, regardless of whether it is already in identity map.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gecko/record/base_adapter.rb', line 155 def fetch(id) verify_id_presence!(id) response = request(:get, plural_path + '/' + id.to_s) record_json = extract_record(response.parsed) instantiate_and_register_record(record_json) rescue OAuth2::Error => ex case ex.response.status when 404 record_not_found!(id) else raise end end |
#find(id) ⇒ Gecko::Record::Base?
Find a record via ID, first searches the Identity Map, then makes an API request.
32 33 34 35 36 37 38 |
# File 'lib/gecko/record/base_adapter.rb', line 32 def find(id) if has_record_for_id?(id) record_for_id(id) else fetch(id) end end |
#find_many(ids) ⇒ Array<Gecko::Record::Base>
Find multiple records via IDs, searching the Identity Map, then making an API request for remaining records. May return nulls
78 79 80 81 82 83 84 85 |
# File 'lib/gecko/record/base_adapter.rb', line 78 def find_many(ids) existing, required = ids.partition { |id| has_record_for_id?(id) } if required.any? where(ids: ids) + existing.map { |id| record_for_id(id) } else existing.map { |id| record_for_id(id) } end end |
#has_record_for_id?(id) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the Identity Map has a record for a particular ID
62 63 64 |
# File 'lib/gecko/record/base_adapter.rb', line 62 def has_record_for_id?(id) @identity_map.key?(id) end |
#instantiate_and_register_record(record_json) ⇒ Gecko::Record::Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Instantiates a record from it’s JSON representation and registers it into the identity map
239 240 241 242 243 |
# File 'lib/gecko/record/base_adapter.rb', line 239 def instantiate_and_register_record(record_json) record = model_class.new(@client, record_json) register_record(record) record end |
#parse_records(json) ⇒ Array<Gecko::Record::Base>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parse a json collection and instantiate records
174 175 176 177 178 179 |
# File 'lib/gecko/record/base_adapter.rb', line 174 def parse_records(json) parse_sideloaded_records(json) extract_collection(json).map do |record_json| instantiate_and_register_record(record_json) end end |
#record_for_id(id) ⇒ Gecko::Record::Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Searches the Identity Map for a record via ID
49 50 51 52 |
# File 'lib/gecko/record/base_adapter.rb', line 49 def record_for_id(id) verify_id_presence!(id) @identity_map.fetch(id) { record_not_in_identity_map!(id) } end |
#save(record) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Save a record
225 226 227 228 229 230 231 |
# File 'lib/gecko/record/base_adapter.rb', line 225 def save(record) if record.persisted? update_record(record) else create_record(record) end end |
#size ⇒ Integer
Returns the total count for a record type. Reads from the last request or makes a new request if not available.
140 141 142 |
# File 'lib/gecko/record/base_adapter.rb', line 140 def size (@pagination && @pagination['total_records']) || count end |
#where(params = {}) ⇒ Array<Gecko::Record::Base>
Make an API request with parameters. Parameters vary via Record Type
111 112 113 114 115 116 |
# File 'lib/gecko/record/base_adapter.rb', line 111 def where(params={}) response = request(:get, plural_path, params: params) parsed_response = response.parsed set_pagination(response.headers) parse_records(parsed_response) end |