Permalink
Browse files

added objects for Lead and Contact

  • Loading branch information...
1 parent 7099858 commit 993d416cb97a44390b0aea7c9eab5b0f2a4d0162 @niciliketo committed May 4, 2012
Showing with 174 additions and 44 deletions.
  1. +4 −4 .gitignore
  2. +4 −1 lib/zohoho.rb
  3. +26 −21 lib/zohoho/connection.rb
  4. +25 −0 lib/zohoho/contact.rb
  5. +34 −18 lib/zohoho/crm.rb
  6. +23 −0 lib/zohoho/lead.rb
  7. +35 −0 lib/zohoho/lead.rb.old
  8. +23 −0 lib/zohoho/zoho_object.rb
View
@@ -14,7 +14,7 @@ doc
# jeweler generated
pkg
-# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
# * Create a file at ~/.gitignore
# * Include files you want ignored
@@ -33,18 +33,18 @@ pkg
#tmtags
# For emacs:
-#*~
+*~
#\#*
#.\#*
# For vim:
-#*.swp
+*.swp
# For redcar:
#.redcar
# For rubinius:
-#*.rbc
+#*.rbc
# For RVM
.rvmrc
View
@@ -1,3 +1,6 @@
require 'zohoho/connection'
require 'zohoho/crm'
-require 'zohoho/authentication'
+require 'zohoho/authentication'
+require 'zohoho/zoho_object'
+require 'zohoho/lead'
+require 'zohoho/contact'
View
@@ -1,59 +1,64 @@
-module Zohoho
+module Zohoho
require 'httparty'
require 'json'
-
+
class Connection
include HTTParty
attr_reader :auth_token
-
+
def initialize(service_name, auth_token)
@service_name, @auth_token = service_name, auth_token
- end
-
+ end
+
def zoho_uri
zoho_uri = "https://#{@service_name.downcase}.zoho.com/#{@service_name.downcase}/private/json"
end
-
+
def call(entry, api_method, query = {}, http_method = :get)
query.merge!({ :authtoken => self.auth_token, :scope => "#{@service_name.downcase}api" })
url = [zoho_uri, entry, api_method].join('/')
-
+
case http_method
when :get
raw = JSON.parse(self.class.get(url, :query => query).parsed_response)
- parse_raw_get(raw, entry)
+ parse_raw_get(raw, entry)
when :post
raw = JSON.parse(self.class.post(url, :body => query).parsed_response)
+
parse_raw_post(raw)
else
raise "#{http_method} is not a recognized http method"
- end
- end
-
+ end
+ end
+
private
-
+
def parse_raw_get(raw, entry)
return [] if raw['response']['result'].nil?
- rows = raw['response']['result'][entry]['row']
+ rows = raw['response']['result'][entry]['row']
rows = [rows] unless rows.class == Array
rows.map {|i|
raw_to_hash i['FL']
}
end
-
+
def parse_raw_post(raw)
- return [] if raw['response']['result'].nil?
- record = raw['response']['result']['recorddetail']
- raw_to_hash record['FL']
+ puts raw
+ if raw['response']['result'].nil?
+ return Hash[raw]['response']['error']
+ else
+ record = raw['response']['result']['recorddetail']
+ raw_to_hash record['FL']
+ end
end
-
+
def raw_to_hash(raw)
raw.map! {|r| [r['val'], r['content']]}
Hash[raw]
end
-
- end
-end
+
+ end
+end
View
@@ -0,0 +1,25 @@
+module Zohoho
+ class Contact < ZohoObject
+ @@myFields = [:contact_owner, :lead_source, :first_name, :last_name, :email,:title, :department, :phone, :home_phone, :fax, :mobile, :date_of_birth, :assistant, :asst_phone, :reports_to, :mailing_street, :other_street, :mailing_city, :other_city, :mailing_state, :other_state, :mailing_zip, :other_zip, :mailing_country, :description, :skype_ID, :salutation, :email3, :lead_source_detail, :registration_tatus, :email_opt_out, :secondary_email]
+
+ def initialize(h)
+ @@myFields.each do |f|
+ self[f] = h[f] ||'123'
+ end
+ end
+
+ protected
+ def zohoizeField(sym)
+ if sym == :date_of_birth
+ r = "Date of Birth"
+ elsif sym == :skype_id
+ r = "Skype ID"
+ elsif sym == :email_opt_out
+ r = "e-Mail Opt out"
+ else
+ r = super
+ end
+ r
+ end
+ end
+end
View
@@ -1,36 +1,49 @@
-module Zohoho
+module Zohoho
require 'httparty'
require 'json'
require 'xmlsimple'
require 'date'
-
+
class Crm
include HTTParty
-
+
def initialize(auth_token)
@conn = Zohoho::Connection.new 'CRM', auth_token
end
-
+
def contact(name)
first_name, last_name = parse_name(name)
contacts = find_contacts_by_last_name(last_name)
contacts.select! {|c|
if c['First Name'].nil? then c['First Name'] = '' end
first_name.match(c['First Name'])
- }
+ }
contacts.first
end
+ def lead(name)
+ first_name, last_name = parse_name(name)
+ leads = find_leads_by_last_name(last_name)
+ leads.select! {|l|
+ if l['First Name'].nil? then l['First Name'] = '' end
+ first_name.match(l['First Name'])
+ }
+ leads.first
+ end
+ def add_object(zObject)
+ record = @conn.call(zObject.data_name, 'insertRecords', {:xmlData => zObject.xmlData, :newFormat => 1}, :post)
+ record['Id']
+ end
def add_contact(name)
first_name, last_name = parse_name(name)
- xmlData = parse_data({'First Name' => first_name, 'Last Name' => last_name}, 'Contacts')
- record = @conn.call('Contacts', 'insertRecords', {:xmlData => xmlData, :newFormat => 1}, :post)
- record['Id']
+ xmlData = parse_data({'First Name' => first_name, 'Last Name' => last_name}, 'Contacts')
+ record = @conn.call('Contacts', 'insertRecords', {:xmlData => xmlData, :newFormat => 1}, :post)
+ record['Id']
end
def post_note(entity_id, note_title, note_content)
xmlData = parse_data({'entityId' => entity_id, 'Note Title' => note_title, 'Note Content' => note_content}, 'Notes')
- record = @conn.call('Notes', 'insertRecords', {:xmlData => xmlData, :newFormat => 1}, :post)
+ record = @conn.call('Notes', 'insertRecords', {:xmlData => xmlData, :newFormat => 1}, :post)
record['Id']
end
@@ -53,13 +66,13 @@ def tasks_by_due_date(due_date)
query = "(Due Date|is|#{date_str})"
@conn.call('Tasks', 'getSearchRecords', :searchCondition => query, :selectColumns => 'All')
end
-
+
def call(*params)
@conn.call(*params)
end
-
- private
-
+
+ private
+
def parse_name(name)
match_data = name.match(/\s(\S*)$/)
match_data.nil? ? last_name = name : last_name = match_data[1]
@@ -68,19 +81,22 @@ def parse_name(name)
match_data.nil? ? first_name = '' : first_name = match_data[1]
return first_name, last_name
- end
+ end
def parse_data(data, entry)
fl = data.map {|e| Hash['val', e[0], 'content', e[1]]}
row = Hash['no', '1', 'FL', fl]
data = Hash['row', row]
- XmlSimple.xml_out(data, :RootName => entry)
+ XmlSimple.xml_out(data, :RootName => entry)
end
def find_contacts_by_last_name(last_name)
search_condition = "(Contact Name|ends with|#{last_name})"
@conn.call('Contacts', 'getSearchRecords', :searchCondition => search_condition, :selectColumns => 'All')
end
-
- end
-end
+ def find_leads_by_last_name(last_name)
+ search_condition = "(Lead Name|ends with|#{last_name})"
+ @conn.call('Leads', 'getSearchRecords', :searchCondition => search_condition, :selectColumns => 'All')
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+module Zohoho
+ class Lead < ZohoObject
+ @@myFields = [:company, :first_name, :last_name, :designation, :email, :phone, :fax, :mobile, :website, :lead_source, :lead_status, :industry, :no_of_employees, :annual_revenue, :email_opt_out, :skype_id, :salutation, :street, :city, :state, :zip_code, :country, :description, :annual_revenue, :rating]
+ attr_accessor *@@myFields
+
+ def initialize(h)
+ @@myFields.each do |f|
+ self[f] = h[f] ||''
+ end
+ end
+ protected
+ def zohoizeField(sym)
+ if sym == :no_of_employees
+ r = "No of Employees"
+ elsif sym == :skype_id
+ r = "Skype ID"
+ else
+ r = super
+ end
+ r
+ end
+ end
+end
View
@@ -0,0 +1,35 @@
+module Zohoho
+ class Lead
+ @@myFields = [:company, :first_name, :last_name, :first_name, :last_name, :designation, :email, :phone, :fax, :mobile, :website, :lead_source, :lead_status, :industry, :no_of_employee, :annual_revenue, :email_opt_out, :skype_id, :salutation, :street, :city, :state, :zip_code, :country, :description]
+ attr_accessor *@@myFields
+
+
+ def xmlData
+
+ parse_data({'Company' => self.company, 'First Name' => self.first_name, 'Last Name' => self.last_name}, data_name)
+ end
+ #def serialize
+
+ def inspect
+ r = Hash.new
+ @@myFields.each do |f|
+ r[f] = self.send(f) || ''
+ end
+ return r
+ end
+
+ def data_name
+ self.class.to_s.split('::').last.pluralize || ''
+ end
+ private
+ def parse_data(data, entry)
+ data = self.inspect
+ fl = data.map {|e| Hash['val', e[0].to_s.gsub('_',' ').titleize, 'content', e[1]]}
+ row = Hash['no', '1', 'FL', fl]
+ data = Hash['row', row]
+ XmlSimple.xml_out(data, :RootName => entry)
+ #XmlSimple.xml_out(self.inspect, :RootName => entry)
+ end
+ end
+
+end
View
@@ -0,0 +1,23 @@
+module Zohoho
+ class ZohoObject < Hash
+ def xmlData
+ parse_data
+ end
+
+ def data_name
+ self.class.to_s.split('::').last.pluralize || ''
+ end
+
+ protected
+ def parse_data
+ #data = self.inspect
+ fl = self.map {|e| Hash['val', zohoizeField(e[0]).titleize, 'content', e[1]]}
+ row = Hash['no', '1', 'FL', fl]
+ data = Hash['row', row]
+ XmlSimple.xml_out(data, :RootName => data_name)
+ end
+ def zohoizeField(sym)
+ sym.to_s.gsub('_',' ').titleize
+ end
+ end
+end

0 comments on commit 993d416

Please sign in to comment.