class Amorail::Entity

Core class for all Amo entities (company, contact, etc)

Attributes

amo_name[R]
amo_response_name[R]

Public Class Methods

amo_field(*vars, **hargs) click to toggle source
# File lib/amorail/entity.rb, line 29
def amo_field(*vars, **hargs)
  vars.each { |v| attributes[v] = :default }
  hargs.each { |k, v| attributes[k] = v }
  attr_accessor(*(vars + hargs.keys))
end
amo_names(name, response_name = nil) click to toggle source
# File lib/amorail/entity.rb, line 24
def amo_names(name, response_name = nil)
  @amo_name = @amo_response_name = name
  @amo_response_name = response_name unless response_name.nil?
end
amo_property(name, options = {}) click to toggle source
# File lib/amorail/entity.rb, line 35
def amo_property(name, options = {})
  properties[name] = options
  attr_accessor(options.fetch(:method_name, name))
end
attributes() click to toggle source
# File lib/amorail/entity.rb, line 40
def attributes
  @attributes ||=
    superclass.respond_to?(:attributes) ? superclass.attributes.dup : {}
end
inherited(subclass) click to toggle source

copy Amo names

# File lib/amorail/entity.rb, line 20
def inherited(subclass)
  subclass.amo_names amo_name, amo_response_name
end
new(attributes = {}) click to toggle source
Calls superclass method
# File lib/amorail/entity.rb, line 61
def initialize(attributes = {})
  super(attributes)
  self.last_modified = Time.now.to_i if last_modified.nil?
end
properties() click to toggle source
# File lib/amorail/entity.rb, line 45
def properties
  @properties ||=
    superclass.respond_to?(:properties) ? superclass.properties.dup : {}
end
remote_url(action) click to toggle source
# File lib/amorail/entity.rb, line 50
def remote_url(action)
  File.join(Amorail.config.api_path, amo_name, action)
end

Public Instance Methods

reload_model(info) click to toggle source
# File lib/amorail/entity.rb, line 70
def reload_model(info)
  merge_params(info)
  merge_custom_fields(info['custom_fields'])
  self
end

Private Instance Methods

commit_request(attrs) click to toggle source
# File lib/amorail/entity.rb, line 117
def commit_request(attrs)
  client.safe_request(
    :post,
    remote_url('set'),
    normalize_params(attrs)
  )
end
custom_field_name(field) click to toggle source
# File lib/amorail/entity.rb, line 101
def custom_field_name(field)
  fname = field['code'] || field['name']
  return if fname.nil?

  fname = self.class.properties
              .fetch(fname.downcase, {})[:method_name] || fname
  fname
end
handle_response(response, method) click to toggle source

We can have response with 200 or 204 here. 204 response has no body, so we don't want to parse it.

# File lib/amorail/entity.rb, line 127
def handle_response(response, method)
  return false if response.status == 204

  data = send(
    "extract_data_#{method}",
    response.body['response'][self.class.amo_response_name]
  )
  reload_model(data)
rescue InvalidRecord
  false
end
merge_custom_fields(fields) click to toggle source
# File lib/amorail/entity.rb, line 88
def merge_custom_fields(fields)
  return if fields.nil?

  fields.each do |f|
    fname = custom_field_name(f)
    next if fname.nil?

    fname = "#{fname.downcase}="
    fval = f.fetch('values').first.fetch('value')
    send(fname, fval) if respond_to?(fname)
  end
end
merge_params(attrs) click to toggle source
# File lib/amorail/entity.rb, line 78
def merge_params(attrs)
  attrs.each do |k, v|
    action = "#{k}="
    next unless respond_to?(action)

    send(action, v)
  end
  self
end
push(method) click to toggle source

call safe method <safe_request>. safe_request call authorize if current session undefined or expires.

# File lib/amorail/entity.rb, line 112
def push(method)
  response = commit_request(create_params(method))
  handle_response(response, method)
end