class ZohoApi::Crm

noinspection RubyStringKeysInHashInspection

Attributes

auth_token[R]

debug_output $stderr

module_fields[R]

debug_output $stderr

Public Class Methods

new(auth_token, modules, ignore_fields, fields = nil) click to toggle source
# File lib/zoho_api.rb, line 30
def initialize(auth_token, modules, ignore_fields, fields = nil)
  @auth_token = auth_token
  @modules = %w(Accounts Contacts Events Leads Potentials Tasks Users).concat(modules).uniq
  @module_fields = fields.nil? ? reflect_module_fields : fields
  @ignore_fields = ignore_fields
end

Public Instance Methods

add_record(module_name, fields_values_hash) click to toggle source
# File lib/zoho_api.rb, line 37
def add_record(module_name, fields_values_hash)
  x = REXML::Document.new
  element = x.add_element module_name
  row = element.add_element 'row', {'no' => '1'}
  fields_values_hash.each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
  r = self.class.post(create_url(module_name, 'insertRecords'),
                      :query => {:newFormat => 1, :authtoken => @auth_token,
                                 :scope => 'crmapi', :xmlData => x, :wfTrigger => 'true'},
                      :headers => {'Content-length' => '0'})
  check_for_errors(r)
  x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
  to_hash(x_r, module_name)[0]
end
attach_file(module_name, record_id, file_path, file_name) click to toggle source
# File lib/zoho_api.rb, line 51
def attach_file(module_name, record_id, file_path, file_name)
  mime_type = (MIME::Types.type_for(file_path)[0] || MIME::Types['application/octet-stream'][0])
  url_path = create_url(module_name, "uploadFile?authtoken=#{@auth_token}&scope=crmapi&id=#{record_id}")
  url = URI.parse(create_url(module_name, url_path))
  io = UploadIO.new(file_path, mime_type, file_name)
  req = Net::HTTP::Post::Multipart.new url_path, 'content' => io
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  res = http.start do |h|
    h.request(req)
  end
  raise(RuntimeError, "[RubyZoho] Attach of file #{file_path} to module #{module_name} failed.") unless res.code == '200'
  res.code
end
check_for_errors(response) click to toggle source
# File lib/zoho_api.rb, line 66
def check_for_errors(response)
  raise(RuntimeError, "Web service call failed with #{response.code}") unless response.code == 200
  x = REXML::Document.new(response.body)

  # updateRelatedRecords returns two codes one in the status tag and another in a success tag, we want the
  # code under the success tag in this case
  code = REXML::XPath.first(x, '//success/code') || REXML::XPath.first(x, '//code')

  # 4422 code is no records returned, not really an error
  # TODO: find out what 5000 is
  # 4800 code is returned when building an association. i.e Adding a product to a lead. Also this doesn't return a message
  raise(RuntimeError, "Zoho Error Code #{code.text}: #{REXML::XPath.first(x, '//message').text}.") unless code.nil? || %w(4422 5000 4800).index(code.text)

  return code.text unless code.nil?
  response.code
end
create_url(module_name, api_call) click to toggle source
# File lib/zoho_api.rb, line 83
def create_url(module_name, api_call)
  "https://crm.zoho.com/crm/private/xml/#{module_name}/#{api_call}"
end
delete_record(module_name, record_id) click to toggle source
# File lib/zoho_api.rb, line 87
def delete_record(module_name, record_id)
  post_action(module_name, record_id, 'deleteRecords')
end
extract_user_name_and_attribs(node) click to toggle source
# File lib/zoho_api.rb, line 203
def extract_user_name_and_attribs(node)
  record = {}
  record.merge!({:user_name => node.text})
  node.attributes.each_pair do |k, v|
    record.merge!({k.to_s.to_sym => v.to_string.match(/'(.*?)'/).to_s.gsub('\'', '')})
  end
  record
end
extract_users_from_xml_response(response) click to toggle source
# File lib/zoho_api.rb, line 191
def extract_users_from_xml_response(response)
  x = REXML::Document.new(response.body).elements.to_a('/users')
  result = []
  x.each do |e|
    e.elements.to_a.each do |node|
      record = extract_user_name_and_attribs(node)
      result << record
    end
  end
  result
end
first(module_name) click to toggle source
# File lib/zoho_api.rb, line 91
def first(module_name)
  some(module_name, 1, 1)
end
method_name?(n) click to toggle source
# File lib/zoho_api.rb, line 95
def method_name?(n)
  /[@$"]/ !~ n.inspect
end
post_action(module_name, record_id, action_type) click to toggle source
# File lib/zoho_api.rb, line 99
def post_action(module_name, record_id, action_type)
  r = self.class.post(create_url(module_name, action_type),
                      :query => {:newFormat => 1, :authtoken => @auth_token,
                                 :scope => 'crmapi', :id => record_id},
                      :headers => {'Content-length' => '0'})
  raise('Adding contact failed', RuntimeError, r.response.body.to_s) unless r.response.code == '200'
  check_for_errors(r)
end
primary_key(module_name) click to toggle source
# File lib/zoho_api.rb, line 108
def primary_key(module_name)
  activity_keys = {'Tasks' => :activityid, 'Events' => :activityid, 'Calls' => :activityid}
  return activity_keys[module_name] unless activity_keys[module_name].nil?
  (module_name.downcase.chop + 'id').to_sym
end
primary_key?(module_name, field_name) click to toggle source
# File lib/zoho_api.rb, line 114
def primary_key?(module_name, field_name)
  return nil if field_name.nil? || module_name.nil?
  fn = field_name.class == String ? field_name : field_name.to_s
  return true if fn == 'id'
  return true if %w[Calls Events Tasks].index(module_name) && fn.downcase == 'activityid'
  fn.downcase.gsub('id', '') == module_name.chop.downcase
end
some(module_name, index = 1, number_of_records = nil, sort_column = :id, sort_order = :asc) click to toggle source
# File lib/zoho_api.rb, line 138
def some(module_name, index = 1, number_of_records = nil, sort_column = :id, sort_order = :asc)
  r = self.class.get(create_url(module_name, 'getRecords'),
                     :query => {:newFormat => 2, :authtoken => @auth_token, :scope => 'crmapi',
                                :sortColumnString => sort_column, :sortOrderString => sort_order,
                                :fromIndex => index, :toIndex => index + (number_of_records || NUMBER_OF_RECORDS_TO_GET) - 1})
  return nil unless r.response.code == '200'
  check_for_errors(r)
  x = REXML::Document.new(r.body).elements.to_a("/response/result/#{module_name}/row")
  to_hash(x, module_name)
end
update_record(module_name, id, fields_values_hash) click to toggle source
# File lib/zoho_api.rb, line 166
def update_record(module_name, id, fields_values_hash)
  x = REXML::Document.new
  contacts = x.add_element module_name
  row = contacts.add_element 'row', {'no' => '1'}
  fields_values_hash.each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
  r = self.class.post(create_url(module_name, 'updateRecords'),
                      :query => {:newFormat => 1, :authtoken => @auth_token,
                                 :scope => 'crmapi', :id => id,
                                 :xmlData => x, :wfTrigger => 'true'},
                      :headers => {'Content-length' => '0'})
  check_for_errors(r)
  x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
  to_hash_with_id(x_r, module_name)[0]
end
users(user_type = 'AllUsers') click to toggle source
# File lib/zoho_api.rb, line 181
def users(user_type = 'AllUsers')
  return @@users unless @@users == [] || user_type == 'Refresh'
  r = self.class.get(create_url('Users', 'getUsers'),
                     :query => {:newFormat => 1, :authtoken => @auth_token, :scope => 'crmapi',
                                :type => 'AllUsers'})
  check_for_errors(r)
  result = extract_users_from_xml_response(r)
  @@users = result
end