class Retailigence::Model

The base for all API requests and models throughout the Retailigence library.

Attributes

safe_attributes[RW]

Attributes safe for initialization

Public Class Methods

attributes(*attrs) click to toggle source

Creates a list of safe attributes for assign using initialize.

# File lib/retailigence/model.rb, line 21
def attributes(*attrs)
  @safe_attributes ||= []

  attrs.each do |attr_name|
    name = underscore(attr_name.to_s).to_sym

    attr_accessor name
    @safe_attributes << name
  end
end
get(action = nil, params = {}) click to toggle source

Convenience method for performing a GET request. See request

# File lib/retailigence/model.rb, line 49
def get(action = nil, params = {})
  request(:get, action, params)
end
new(params = {}) click to toggle source

Initialize an object with the provided params. For the available params, see the model’s Attributes.

# File lib/retailigence/model.rb, line 9
def initialize(params = {})
  params.each do |key, value|
    mapped_key = underscore(key)
    send("#{mapped_key}=".to_sym, value) if safe_attribute?(mapped_key)
  end
end
request(method = :get, action = nil, params = {}) click to toggle source

Perform a request using Typhoeus.

Arguments

  • method - Symbol for the request method.

  • action - The path to request

  • params - Hash of params to send with the request

# File lib/retailigence/model.rb, line 38
def request(method = :get, action = nil, params = {})
  params[:apikey] = Retailigence.configuration.api_key
  params[:format] = 'JSON'

  url = "http://#{host}/v#{Retailigence::API_VERSION}/#{action}"

  response = Typhoeus.send(method, url, params: params)
  JSON.parse response.body
end
underscore(word) click to toggle source

Convert the camelCase to its underscore/snake_case equivalent.

# File lib/retailigence/model.rb, line 54
def underscore(word)
  word.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('-', '_').downcase
end

Private Class Methods

host() click to toggle source
# File lib/retailigence/model.rb, line 62
def host
  host =
    if Retailigence.configuration.production?
      'api'
    else
      'apitest'
    end

  "#{host}.retailigence.com"
end

Private Instance Methods

safe_attribute?(key) click to toggle source
# File lib/retailigence/model.rb, line 80
def safe_attribute?(key)
  self.class.safe_attributes.include?(key)
end
underscore(word) click to toggle source
# File lib/retailigence/model.rb, line 76
def underscore(word)
  self.class.underscore(word.to_s).to_sym
end