module Spigot::ActiveRecord::ClassMethods

Public Instance Methods

create_by_api(params = {}) click to toggle source
#create_by_api(params)

Insert mapped data into the calling model's table. Does not perform any checks on existing content already present in the database

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 27
def create_by_api(params = {})
  create_by_translator params_to_translator(params)
end
create_or_update_by_api(params = {}) click to toggle source
#create_or_update_by_api(params)

Queries the database to find an existing record. If that record is found it updates it with passed api_data and returns the record. Otherwise it creates a new record and returns the newly created record.

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 60
def create_or_update_by_api(params = {})
  babel = params_to_translator(params)
  record = find_by_translator(babel).first
  record.present? ? update_by_translator(babel, record) : create_by_translator(babel)
end
find_all_by_api(params = {}) click to toggle source
#find_all_by_api(params)

Build a query based on the defined map for this resource to find all matching records in the database.

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 18
def find_all_by_api(params = {})
  find_by_translator params_to_translator(params)
end
find_by_api(params = {}) click to toggle source
#find_by_api(params)

Build a query based on the defined map for this resource to find a single matching record in the database

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 9
def find_by_api(params = {})
  find_all_by_api(params).first
end
find_or_create_by_api(params = {}) click to toggle source
#find_or_create_by_api(params)

Queries the database to find an existing record. If that record is found simply return it, otherwise return a newly created record. This does not update any existing record. If you want that, use `create_or_update_by_api`

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 49
def find_or_create_by_api(params = {})
  babel = params_to_translator(params)
  find_by_translator(babel).first || create_by_translator(babel)
end
update_by_api(params = {}) click to toggle source
#update_by_api(params)

Queries the database to find an existing record. If a record is found, it updates that record with any new formatted data received by the API

@param params [Hash] Data as received from the api with optional service key

# File lib/spigot/active_record.rb, line 37
def update_by_api(params = {})
  babel = params_to_translator(params)
  record = find_by_translator(babel).first
  update_by_translator(babel, record) if record.present?
end

Private Instance Methods

create_by_translator(translator) click to toggle source
# File lib/spigot/active_record.rb, line 83
def create_by_translator(translator)
  Record.create(translator.service, self, translator.format)
end
find_by_translator(translator) click to toggle source
# File lib/spigot/active_record.rb, line 73
def find_by_translator(translator)
  if invalid_primary_keys?(translator)
    message = "The #{translator.primary_key} column does not exist on #{to_s}"
    raise Spigot::InvalidSchemaError, message
  end

  return [] if translator.conditions.blank?
  where(translator.conditions)
end
invalid_primary_keys?(translator) click to toggle source
# File lib/spigot/active_record.rb, line 92
def invalid_primary_keys?(translator)
  [*translator.primary_key].each do |key|
    return true unless column_names.include?(key.to_s)
  end
  false
end
params_to_translator(params) click to toggle source
# File lib/spigot/active_record.rb, line 68
def params_to_translator(params)
  service, data = Spigot::Map::Service.extract(params)
  Translator.new(self, service, data)
end
update_by_translator(translator, record) click to toggle source
# File lib/spigot/active_record.rb, line 87
def update_by_translator(translator, record)
  Record.update(translator.service, self, record, translator.format)
  record
end