Class: Gecko::Record::BaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/gecko/record/base_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:



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

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/gecko/record/base_adapter.rb', line 6

def client
  @client
end

#last_responseObject (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

Examples:

new_order = client.Order.build(company_id: 123, order_number: 1234)
new_order = client.Order.build
new_order.order_number = 1234

Parameters:

  • initial (#to_hash)

    attributes to set up the record

Returns:



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.

Examples:

client.Product.count

Parameters:

  • params (#to_hash) (defaults to: {})

Returns:

  • (Integer)

    Total number of available records



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

Returns:

  • (Hash)


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

Returns:

  • Hash



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.

Examples:

client.Product.fetch(12)

Parameters:

  • id (Integer)

    ID of record

Returns:



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.

Examples:

client.Product.find(12)

Parameters:

  • id (Integer)

    ID of record

Returns:

  • (Gecko::Record::Base)

    if a record was found either in the identity map or via the API

  • (nil)

    If no record was found



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

Examples:

client.Product.find_many([12, 13, 14])

Parameters:

  • ids (Array<Integer>)

    IDs of records to fetch

Returns:

  • (Array<Gecko::Record::Base>)

    Records for the ids either in the identity map or via the API



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

Examples:

Fetch via ID

client.Product.first

Fetch via date

client.Product.first(updated_at_min: "2014-03-03T21:09:00")

Search

client.Product.first(q: "gecko")

Parameters:

  • params (#to_hash) (defaults to: {})

Options Hash (params):

  • :q (String)

    Search query

  • :ids (Array<Integer>)

    IDs to search for

  • :updated_at_min (String)

    Last updated_at minimum

  • :updated_at_max (String)

    Last updated_at maximum

  • :order (String)

    Sort order i.e ‘name asc’

  • :status (String, Array<String>)

    Record status/es

Returns:



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

Examples:

client.Product.has_record_for_id?(12)

Returns:

  • (Boolean)

    if a record was found in the identity map.



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

Returns:



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

Returns:



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_allArray<Gecko::Record::Base>

Returns all the records currently in the identity map.

Examples:

Return all Products previously fetched

client.Product.peek_all

Returns:



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

Examples:

client.Product.record_for_id(12)

Returns:

Raises:



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

Parameters:

  • opts (Hash) (defaults to: {})

    the options to make the request with

Options Hash (opts):

  • :idempotency_key (Hash)

    A unique identifier for this action

Returns:

  • (Boolean)

    whether the save was successful. If false the record will contain an errors hash



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

#sizeInteger

Returns the total count for a record type. Reads from the last request or makes a new request if not available.

Examples:

client.Product.size

Returns:

  • (Integer)

    Total number of available records



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

Examples:

Fetch via ID

client.Product.where(ids: [1,2])

Fetch via date

client.Product.where(updated_at_min: "2014-03-03T21:09:00")

Search

client.Product.where(q: "gecko")

Parameters:

  • params (#to_hash) (defaults to: {})

Options Hash (params):

  • :q (String)

    Search query

  • :page (Integer) — default: 1

    Page number for pagination

  • :limit (Integer) — default: 100

    Page limit for pagination

  • :ids (Array<Integer>)

    IDs to search for

  • :updated_at_min (String)

    Last updated_at minimum

  • :updated_at_max (String)

    Last updated_at maximum

  • :order (String)

    Sort order i.e ‘name asc’

  • :status (String, Array<String>)

    Record status/es

Returns:



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