class AgileCRMWrapper::Contact

Constants

CONTACT_FIELDS
SYSTEM_PROPERTIES

Public Class Methods

all() click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 10
def all
  response = AgileCRMWrapper.connection.get('contacts')
  if response.status == 200
    return response.body.map { |body| new body }
  else
    return response
  end
end
create(options = {}) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 53
def create(options = {})
  payload = parse_contact_fields(options)
  response = AgileCRMWrapper.connection.post('contacts', payload)
  if response && response.status == 200
    contact = new(response.body)
  end
  contact
end
delete(arg) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 62
def delete(arg)
  AgileCRMWrapper.connection.delete("contacts/#{arg}")
end
find(id) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 19
def find(id)
  response = AgileCRMWrapper.connection.get("contacts/#{id}")
  if response.status == 200
    new(response.body)
  elsif response.status == 204
    fail(AgileCRMWrapper::NotFound.new(response))
  end
end
parse_contact_fields(options) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 66
def parse_contact_fields(options)
  payload = { 'properties' => [] }
  options.each do |key, value|
    if contact_field?(key)
      payload[key.to_s] = value
    else
      payload['properties'] << parse_property(key, value)
    end
  end
  payload
end
search_by_email(*emails) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 28
def search_by_email(*emails)
  emails = emails.flatten.compact.uniq
  response = AgileCRMWrapper.connection.post(
    'contacts/search/email', "email_ids=#{emails}",
    'content-type' => 'application/x-www-form-urlencoded'
  )
  if response.body.size > 1
    response.body.reject(&:nil?).map { |body| new body }
  else
    res = new(response.body.first)
    res.empty? ? nil : res
  end
end

Private Class Methods

contact_field?(key) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 89
def contact_field?(key)
  CONTACT_FIELDS.include?(key.to_s)
end
parse_property(key, value) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 80
def parse_property(key, value)
  type = system_propety?(key) ? 'SYSTEM' : 'CUSTOM'
  { 'type' => type, 'name' => key.to_s, 'value' => value }
end
system_propety?(key) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 85
def system_propety?(key)
  SYSTEM_PROPERTIES.include?(key.to_s)
end

Public Instance Methods

delete_tags(tags) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 98
def delete_tags(tags)
  email = get_property('email')
  response = AgileCRMWrapper.connection.post(
    'contacts/email/tags/delete', "email=#{email}&tags=#{tags}",
    'content-type' => 'application/x-www-form-urlencoded'
  )
  self.tags = self.tags - tags if response.status == 204
end
destroy() click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 94
def destroy
  self.class.delete(id)
end
get_property(property_name) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 120
def get_property(property_name)
  return unless respond_to?(:properties)
  arr = properties.select { |a| a['name'] == property_name.to_s }
  if arr.length > 1
    arr.map {|i| i['value'] }
  else
    arr.first['value'] if arr.first
  end
end
notes() click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 107
def notes
  response = AgileCRMWrapper.connection.get("contacts/#{id}/notes")
  response.body.map { |note| AgileCRMWrapper::Note.new(note) }
end
update(options = {}) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 112
def update(options = {})
  payload = self.class.parse_contact_fields(options)
  payload['properties'] = merge_properties(payload['properties'])
  merge!(payload)
  response = AgileCRMWrapper.connection.put('contacts', self)
  merge!(response.body)
end

Private Instance Methods

merge_properties(new_properties) click to toggle source
# File lib/agilecrm-wrapper/contact.rb, line 132
def merge_properties(new_properties)
  properties.map do |h|
    new_properties.delete_if do |h2|
      if h['name'] == h2['name']
        h['value'] = h2['value']
        true
      end
    end
    h
  end + new_properties
end