Class: ZohoCrm::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_crm.rb,
lib/zoho_crm/version.rb

Constant Summary collapse

AUTH_URL =
"https://accounts.zoho.com/apiauthtoken/nb/create?SCOPE=ZohoCRM/crmapi&"
GET_LEADS =
"https://crm.zoho.com/crm/private/json/Leads/getRecords?"
GET_CONTACTS =
"https://crm.zoho.com/crm/private/json/Contacts/getRecords?"
NEW_LEAD =
"https://crm.zoho.com/crm/private/xml/Leads/insertRecords?"
NEW_CONTACT =
"https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?"
UPDATE_LEAD =
"http://crm.zoho.com/crm/private/xml/Leads/updateRecords?"
UPDATE_CONTACT =
"http://crm.zoho.com/crm/private/xml/Contacts/updateRecords?"
DELETE_LEAD =
"http://crm.zoho.com/crm/private/xml/Leads/deleteRecords?"
DELETE_CONTACT =
"http://crm.zoho.com/crm/private/xml/Contacts/deleteRecords?"
GET_FIELDS =
"https://crm.zoho.com/crm/private/json/"
NEW_CONTACTS =
"https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?"
NEW_LEADS =
"https://crm.zoho.com/crm/private/xml/Leads/insertRecords?"
UPDATE_CONTACTS =
"https://crm.zoho.com/crm/private/xml/Contacts/updateRecords?"
UPDATE_LEADS =
"https://crm.zoho.com/crm/private/xml/Leads/updateRecords?"
VERSION =
"1.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



31
32
33
34
# File 'lib/zoho_crm.rb', line 31

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#authenticate_userObject



36
37
38
39
40
41
42
# 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) ⇒ Object



88
89
90
91
92
# 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) ⇒ Object



94
95
96
97
98
# 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) ⇒ Object



100
101
102
103
104
105
# 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) ⇒ Object



107
108
109
110
111
112
113
# 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) ⇒ Object



115
116
117
118
119
120
121
# 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) ⇒ Object



56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



72
73
74
75
76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
86
# 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) ⇒ Object



123
124
125
126
127
128
129
# 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) ⇒ Object



131
132
133
134
135
136
137
# 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