class ZohoCrm::Client

Constants

AUTH_URL
DELETE_CONTACT
DELETE_LEAD
GET_CONTACTS
GET_FIELDS
GET_LEADS
NEW_CONTACT
NEW_CONTACTS
NEW_LEAD
NEW_LEADS
UPDATE_CONTACT
UPDATE_CONTACTS
UPDATE_LEAD
UPDATE_LEADS
VERSION

Public Class Methods

new(username, password) click to toggle source
# File lib/zoho_crm.rb, line 31
def initialize(username, password)
  @username = username
  @password = password
end

Public Instance Methods

authenticate_user() click to toggle source
# File lib/zoho_crm.rb, line 36
def authenticate_user
  token_url = AUTH_URL + "EMAIL_ID=#{@username}&PASSWORD=#{@password}"
  response = HTTParty.post(token_url)
  response_body = response.body
  auth_info = Hash[response_body.split(" ").map { |str| str.split("=") }]
  raise_auth_exception(auth_info["AUTHTOKEN"])
end
delete_contact(auth_token, id) click to toggle source
# File lib/zoho_crm.rb, line 88
def delete_contact(auth_token, id)
  delete_contact = DELETE_CONTACT + "authtoken=#{auth_token}&scope=crmapi&id=#{id}"
  response = HTTParty.delete(delete_contact)
  raise_api_exception(response)
end
delete_lead(auth_token, id) click to toggle source
# File lib/zoho_crm.rb, line 94
def delete_lead(auth_token, id)
  delete_lead = DELETE_LEAD + "authtoken=#{auth_token}&scope=crmapi&id=#{id}"
  response = HTTParty.delete(delete_lead)
  raise_api_exception(response)
end
get_fields(auth_token, module_name) click to toggle source
# File lib/zoho_crm.rb, line 100
def get_fields(auth_token, module_name)
  name = module_name.capitalize
  fields = GET_FIELDS + name + "/getFields?authtoken=#{auth_token}&scope=crmap"
  response = HTTParty.get(fields)
  raise_api_exception(response)
end
multiple_new_contacts(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 107
def multiple_new_contacts(auth_token, data)
  xml_data = format_multiple_contacts(data)
  formatted_data = escape_xml(xml_data)
  new_contacts = NEW_CONTACTS + "newFormat=1&authtoken=#{auth_token}&scope=crmapi&xmlData=#{formatted_data}"
  response = HTTParty.post(new_contacts)
  raise_api_exception(response)
end
multiple_new_leads(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 115
def multiple_new_leads(auth_token, data)
  xml_data = format_multiple_leads(data)
  formatted_data = escape_xml(xml_data)
  new_leads = NEW_LEADS + "newFormat=1&authtoken=#{auth_token}&scope=crmapi&xmlData=#{formatted_data}"
  response = HTTParty.post(new_leads)
  raise_api_exception(response)
end
new_contact(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 56
def new_contact(auth_token, data)
  xml_data = format_contacts(data)
  formatted_data = escape_xml(xml_data)
  new_contact = NEW_CONTACT + "authtoken=#{auth_token}&scope=crmapi&xmlData=#{formatted_data}"
  response = HTTParty.post(new_contact)
  raise_api_exception(response)
end
new_lead(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 64
def new_lead(auth_token, data)
  xml_data = format_leads(data)
  formatted_data = escape_xml(xml_data)
  new_lead = NEW_LEAD + "authtoken=#{auth_token}&scope=crmapi&xmlData=#{formatted_data}"
  response = HTTParty.post(new_lead)
  raise_api_exception(response)
end
retrieve_contacts(auth_token, from_index, to_index) click to toggle source
# File lib/zoho_crm.rb, line 44
def retrieve_contacts(auth_token, from_index, to_index)
  all_contacts = GET_CONTACTS + "authtoken=#{auth_token}&scope=crmapi&fromIndex=#{from_index}&toIndex=#{to_index}"
  response = HTTParty.get(all_contacts)
  raise_api_exception(response)
end
retrieve_leads(auth_token, from_index, to_index) click to toggle source
# File lib/zoho_crm.rb, line 50
def retrieve_leads(auth_token, from_index, to_index)
  all_leads = GET_LEADS + "authtoken=#{auth_token}&scope=crmapi&fromIndex=#{from_index}&toIndex=#{to_index}"
  response = HTTParty.get(all_leads)
  raise_api_exception(response)
end
update_contact(auth_token, data, id) click to toggle source
# File lib/zoho_crm.rb, line 72
def update_contact(auth_token, data, id)
  xml_data = format_contacts(data)
  formatted_data = escape_xml(xml_data)
  update_contact = UPDATE_CONTACT + "authtoken=#{auth_token}&scope=crmapi&newFormat=1&id=#{id}&xmlData=#{formatted_data}"
  response = HTTParty.put(update_contact)
  raise_api_exception(response)
end
update_lead(auth_token, data, id) click to toggle source
# File lib/zoho_crm.rb, line 80
def update_lead(auth_token, data, id)
  xml_data = format_leads(data)
  formatted_data = escape_xml(xml_data)
  update_lead = UPDATE_LEAD + "authtoken=#{auth_token}&scope=crmapi&newFormat=1&id=#{id}&xmlData=#{formatted_data}"
  response = HTTParty.put(update_lead)
  raise_api_exception(response)
end
update_multiple_contacts(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 123
def update_multiple_contacts(auth_token, data)
  xml_data = format_multiple_contacts(data)
  formatted_data = escape_xml(xml_data)
  update_contacts = UPDATE_CONTACTS + "authtoken=#{auth_token}&scope=crmapi&version=4&xmlData=#{formatted_data}"
  response = HTTParty.post(update_contacts)
  raise_api_exception(response)
end
update_multiple_leads(auth_token, data) click to toggle source
# File lib/zoho_crm.rb, line 131
def update_multiple_leads(auth_token, data)
  xml_data = format_multiple_leads(data)
  formatted_data = escape_xml(xml_data)
  update_leads = UPDATE_LEADS + "authtoken=#{auth_token}&scope=crmapi&version=4&xmlData=#{formatted_data}"
  response = HTTParty.post(update_leads)
  raise_api_exception(response)
end

Private Instance Methods

escape_xml(data) click to toggle source
# File lib/zoho_crm.rb, line 189
def escape_xml(data)
  CGI.escape(data)
end
format_contacts(info) click to toggle source
# File lib/zoho_crm.rb, line 141
def format_contacts(info)
  data = "<Contacts><row no='1'>"
  info.each do |key, value|
    data += "<FL val='#{zohoify_key(key)}'>#{value}</FL>"
  end
  data += "</row></Contacts>"
end
format_leads(info) click to toggle source
# File lib/zoho_crm.rb, line 149
def format_leads(info)
  data = "<Leads><row no='1'>"
  info.each do |key, value|
    data += "<FL val='#{zohoify_key(key)}'>#{value}</FL>"
  end
  data += "</row></Leads>"
end
format_multiple_contacts(info) click to toggle source
# File lib/zoho_crm.rb, line 157
def format_multiple_contacts(info)
  data = "<Contacts>"
  row_num = 1
  info.each do |record|
    data += "<row no='#{row_num}'>"
    record.each do |key, value|
      data += "<FL val='#{zohoify_key(key)}'>#{value}</FL>"
    end
    data += "</row>"
    row_num += 1
  end
  data += "</Contacts>"
end
format_multiple_leads(info) click to toggle source
# File lib/zoho_crm.rb, line 171
def format_multiple_leads(info)
  data = "<Leads>"
  row_num = 1
  info.each do |record|
    data += "<row no='#{row_num}'>"
    record.each do |key, value|
      data += "<FL val='#{zohoify_key(key)}'>#{value}</FL>"
    end
    data += "</row>"
    row_num += 1
  end
  data += "</Leads>"
end
raise_api_exception(response) click to toggle source
# File lib/zoho_crm.rb, line 193
def raise_api_exception(response)
  if response["response"].nil?
    response
  elsif response["response"]["error"].nil?
    response
  else
    raise RateLimitExceeded, "You've 'literally' exceeded your API rate limit" if response["response"]["error"]["message"] == "You crossed your license limit"
  end
end
raise_auth_exception(token) click to toggle source
# File lib/zoho_crm.rb, line 203
def raise_auth_exception(token)
  if token.nil?
    raise AuthenticationFailure.new("Good gracious! Incorrect credentials or too many active auth tokens")
  else
    token
  end
end
zohoify_key(key) click to toggle source
# File lib/zoho_crm.rb, line 185
def zohoify_key(key)
  key.to_s.gsub("_", " ").split.map(&:capitalize).join(' ')
end