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.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Gecko::Record::Base
Build a new record.
-
#count(params = {}) ⇒ 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.
-
#first(params = {}) ⇒ Gecko::Record::Base
Fetch the first record for the given parameters.
-
#forty_two(params = {}) ⇒ Object
Fetch the forty-second record for the given parameters.
-
#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>
Fetch a record collection via the API.
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 |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
4 5 6 |
# File 'lib/gecko/record/base_adapter.rb', line 4 def last_response @last_response end |
Instance Method Details
#build(attributes = {}) ⇒ Gecko::Record::Base
Build a new record
248 249 250 |
# File 'lib/gecko/record/base_adapter.rb', line 248 def build(attributes={}) model_class.new(@client, attributes) end |
#count(params = {}) ⇒ Integer
Returns the total count for a record type via API request.
161 162 163 164 |
# File 'lib/gecko/record/base_adapter.rb', line 161 def count(params = {}) self.where(params.merge(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
221 222 223 |
# File 'lib/gecko/record/base_adapter.rb', line 221 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
230 231 232 |
# File 'lib/gecko/record/base_adapter.rb', line 230 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.
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/gecko/record/base_adapter.rb', line 190 def fetch(id) verify_id_presence!(id) response = @last_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 |
#first(params = {}) ⇒ Gecko::Record::Base
Fetch the first record for the given parameters
140 141 142 |
# File 'lib/gecko/record/base_adapter.rb', line 140 def first(params={}) where(params.merge(limit: 1)).first end |
#forty_two(params = {}) ⇒ Object
Fetch the forty-second record for the given parameters
147 148 149 |
# File 'lib/gecko/record/base_adapter.rb', line 147 def forty_two(params={}) where(params.merge(limit: 1, page: 42)).first 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
274 275 276 277 278 |
# File 'lib/gecko/record/base_adapter.rb', line 274 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
209 210 211 212 213 214 |
# File 'lib/gecko/record/base_adapter.rb', line 209 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
260 261 262 263 264 265 266 |
# File 'lib/gecko/record/base_adapter.rb', line 260 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.
175 176 177 |
# File 'lib/gecko/record/base_adapter.rb', line 175 def size (@pagination && @pagination['total_records']) || count end |
#where(params = {}) ⇒ Array<Gecko::Record::Base>
Fetch a record collection via the API. Parameters vary via Record Type
111 112 113 114 115 116 |
# File 'lib/gecko/record/base_adapter.rb', line 111 def where(params={}) response = @last_response = request(:get, plural_path, params: params) parsed_response = response.parsed set_pagination(response.headers) parse_records(parsed_response) end |