class Creditsafe::BaseModel

Public Class Methods

attribute_aliases(aliases = nil) click to toggle source
# File lib/creditsafe/base_model.rb, line 35
def attribute_aliases(aliases = nil)
  @attribute_aliases ||= {}

  return @attribute_aliases unless aliases

  @attribute_aliases.merge!(aliases)
end
attributes(*attributes) click to toggle source
# File lib/creditsafe/base_model.rb, line 25
def attributes(*attributes)
  @attributes ||= []

  return @attributes unless attributes

  attr_accessor(*attributes)

  @attributes += attributes
end
build(json: {}, key_transformer: self.key_transformer) click to toggle source

Sets all the instance variables by reading the JSON from Creditsafe and converting the keys from camelCase to snake_case, as it's the standard in Ruby.

# File lib/creditsafe/base_model.rb, line 61
def build(json: {}, key_transformer: self.key_transformer)
  new.tap do |record|
    attributes.each do |attr|
      key = attribute_aliases.key?(attr) ? attribute_aliases[attr] : attr
      record.public_send("#{attr}=", json[key_transformer.transform(key)])
    end
  end
end
format_url(url, params) click to toggle source
# File lib/creditsafe/base_model.rb, line 43
def format_url(url, params)
  formatted = url.dup.strip

  params.each { |key, value| formatted.sub!(":#{key}", value) }

  formatted
end
get(path, params = {}) click to toggle source
# File lib/creditsafe/base_model.rb, line 9
def get(path, params = {})
  result = Creditsafe.client.get(path.strip, params)

  raise APIKeyError, result.body[:error] if result.status == 401

  result
end
key_transformer() click to toggle source
# File lib/creditsafe/base_model.rb, line 55
def key_transformer
  CamelizerLower
end
new(attributes = {}) click to toggle source
# File lib/creditsafe/base_model.rb, line 71
def initialize(attributes = {})
  attributes.each do |attr, value|
    public_send("#{attr}=", value)
  end
end
post(path, params = {}) click to toggle source
# File lib/creditsafe/base_model.rb, line 17
def post(path, params = {})
  result = Creditsafe.client.post(path.strip, params)

  raise APIKeyError, result.body[:error] if result.status == 401

  result
end
successful_response?(result) click to toggle source
# File lib/creditsafe/base_model.rb, line 51
def successful_response?(result)
  result.status < 400
end

Public Instance Methods

as_json() click to toggle source
# File lib/creditsafe/base_model.rb, line 89
def as_json
  self.class.attributes.each_with_object({}) do |attr, hash|
    value = public_send(attr)

    value = value.as_json if value.respond_to?(:as_json)
    if value.is_a?(Array)
      value = value.map { |v| v.respond_to?(:as_json) ? v.as_json : v }
    end

    key = self.class.key_transformer.transform(attr)

    hash[key] = value
  end
end
format_url(url, params) click to toggle source
# File lib/creditsafe/base_model.rb, line 85
def format_url(url, params)
  self.class.format_url(url, params)
end
get(path, params = {}) click to toggle source
# File lib/creditsafe/base_model.rb, line 77
def get(path, params = {})
  self.class.get(path, params)
end
post(path, params = {}) click to toggle source
# File lib/creditsafe/base_model.rb, line 81
def post(path, params = {})
  self.class.post(path, params)
end