Skip to content

Instantly share code, notes, and snippets.

@Extrapolator214
Created March 16, 2017 10:35
Show Gist options
  • Save Extrapolator214/915363aacfe42f1869a4ff29f69df5c9 to your computer and use it in GitHub Desktop.
Save Extrapolator214/915363aacfe42f1869a4ff29f69df5c9 to your computer and use it in GitHub Desktop.
Zoho CRM thin API client
class ZohoApiClient
BASE_URL = 'https://crm.zoho.com/crm/private/json'
# https://www.zoho.com/crm/help/api/api-methods.html
METHODS = %w[
getMyRecords
getRecords
getRecordById
getDeletedRecordIds
insertRecords
updateRecords
getSearchRecordsByPDC
deleteRecords
convertLead
getRelatedRecords
getFields
updateRelatedRecords
getUsers
uploadFile
delink
downloadFile
deleteFile
uploadPhoto
downloadPhoto
deletePhoto
getModules
searchRecords
]
def initialize
@api_key = ENV['ZOHO_API_KEY']
@scope = 'crmapi'
end
# IMPORTANT: Soho specifics
# there is a delay of about a minute before changes made in CRM
# become available for the API.
# EXAMPLE USAGE:
# api = ZohoApiClient.new
# api.get_modules(api_module: 'Info')
# api.search_records(api_module: 'Potentials', options: {criteria: '(Stage:"Closed (Won)")'})
# api.get_record_by_id(api_module: 'Accounts', options: {id: some_id})
# api.get_record_by_id(api_module: 'Products', options: {id: some_id})
METHODS.each do |name|
define_method name.underscore do |api_module: 'Potentials', options: {}|
request(name, api_module, options)
end
end
private
def request(method, api_module, options)
url = "#{BASE_URL}/#{api_module}/#{method}?authtoken=#{@api_key}&scope=#{@scope}" # method without params
response = Faraday.get [url, options.map{|k,v,| "#{k.to_s.camelize(:lower)}=#{v}"}].join('&') #pass options as params
JSON(response.body)
rescue JSON::ParserError
response.body # return a string
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment