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, WebhookAdapter
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.
-
#peek_all ⇒ Array<Gecko::Record::Base>
Returns all the records currently in the identity map.
-
#record_for_id(id) ⇒ Gecko::Record::Base
private
Searches the Identity Map for a record via ID.
-
#save(record, opts = {}) ⇒ 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
260 261 262 |
# File 'lib/gecko/record/base_adapter.rb', line 260 def build(attributes={}) model_class.new(@client, attributes) end |
#count(params = {}) ⇒ Integer
Returns the total count for a record type via API request.
173 174 175 176 |
# File 'lib/gecko/record/base_adapter.rb', line 173 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
233 234 235 |
# File 'lib/gecko/record/base_adapter.rb', line 233 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
242 243 244 |
# File 'lib/gecko/record/base_adapter.rb', line 242 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.
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/gecko/record/base_adapter.rb', line 202 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
152 153 154 |
# File 'lib/gecko/record/base_adapter.rb', line 152 def first(params={}) where(params.merge(limit: 1)).first end |
#forty_two(params = {}) ⇒ Object
Fetch the forty-second record for the given parameters
159 160 161 |
# File 'lib/gecko/record/base_adapter.rb', line 159 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
288 289 290 291 292 |
# File 'lib/gecko/record/base_adapter.rb', line 288 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
221 222 223 224 225 226 |
# File 'lib/gecko/record/base_adapter.rb', line 221 def parse_records(json) parse_sideloaded_records(json) extract_collection(json).map do |record_json| instantiate_and_register_record(record_json) end end |
#peek_all ⇒ Array<Gecko::Record::Base>
Returns all the records currently in the identity map.
126 127 128 |
# File 'lib/gecko/record/base_adapter.rb', line 126 def peek_all @identity_map.values 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, opts = {}) ⇒ 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
274 275 276 277 278 279 280 |
# File 'lib/gecko/record/base_adapter.rb', line 274 def save(record, opts = {}) if record.persisted? update_record(record, opts) else create_record(record, opts) 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.
187 188 189 |
# File 'lib/gecko/record/base_adapter.rb', line 187 def size (defined?(@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 |