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, NoteAdapter, OrderAdapter, OrderLineItemAdapter, PaymentMethodAdapter, 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
15 16 17 18 19 |
# File 'lib/gecko/record/base_adapter.rb', line 15 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.
6 7 8 |
# File 'lib/gecko/record/base_adapter.rb', line 6 def client @client end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
6 7 8 |
# File 'lib/gecko/record/base_adapter.rb', line 6 def last_response @last_response end |
Instance Method Details
#build(attributes = {}) ⇒ Gecko::Record::Base
Build a new record
262 263 264 |
# File 'lib/gecko/record/base_adapter.rb', line 262 def build(attributes = {}) model_class.new(@client, attributes) end |
#count(params = {}) ⇒ Integer
Returns the total count for a record type via API request.
175 176 177 178 |
# File 'lib/gecko/record/base_adapter.rb', line 175 def count(params = {}) 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
235 236 237 |
# File 'lib/gecko/record/base_adapter.rb', line 235 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
244 245 246 |
# File 'lib/gecko/record/base_adapter.rb', line 244 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.
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/gecko/record/base_adapter.rb', line 204 def fetch(id) # rubocop:disable Metrics/MethodLength 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 => e case e.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.
34 35 36 37 38 39 40 |
# File 'lib/gecko/record/base_adapter.rb', line 34 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
80 81 82 83 84 85 86 87 |
# File 'lib/gecko/record/base_adapter.rb', line 80 def find_many(ids) existing, required = ids.partition { |id| has_record_for_id?(id) } if required.any? where(ids: required) + 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
154 155 156 |
# File 'lib/gecko/record/base_adapter.rb', line 154 def first(params = {}) where(params.merge(limit: 1)).first end |
#forty_two(params = {}) ⇒ Object
Fetch the forty-second record for the given parameters
161 162 163 |
# File 'lib/gecko/record/base_adapter.rb', line 161 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
64 65 66 |
# File 'lib/gecko/record/base_adapter.rb', line 64 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
290 291 292 293 294 |
# File 'lib/gecko/record/base_adapter.rb', line 290 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
223 224 225 226 227 228 |
# File 'lib/gecko/record/base_adapter.rb', line 223 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.
128 129 130 |
# File 'lib/gecko/record/base_adapter.rb', line 128 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
51 52 53 54 |
# File 'lib/gecko/record/base_adapter.rb', line 51 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
276 277 278 279 280 281 282 |
# File 'lib/gecko/record/base_adapter.rb', line 276 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.
189 190 191 |
# File 'lib/gecko/record/base_adapter.rb', line 189 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
113 114 115 116 117 118 |
# File 'lib/gecko/record/base_adapter.rb', line 113 def where(params = {}) response = request(:get, plural_path, params: params) parsed_response = response.parsed set_pagination(response.headers) parse_records(parsed_response) end |