rubyzoho 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  {<img src="https://gemnasium.com/amalc/rubyzoho.png" alt="Dependency Status" />}[https://gemnasium.com/amalc/rubyzoho]
4
4
 
5
+
5
6
  = rubyzoho
6
7
 
7
8
  Abstracting Zoho's API into a set of Ruby classes, with reflection of Zoho's fields using a more familiar
@@ -194,8 +195,20 @@ either the related \module's record id, which is stored with the Task. But no, c
194
195
  == Bugs and Enhancements
195
196
  Please open an issue on GitHub. Or better yet, send in a pull request with the fix or enhancement!
196
197
 
197
- === Known Bugs
198
+ === Known Bugs or Issues
198
199
  1. Caching is causing a problem with Quotes.
200
+ 2. If you're having trouble with updating custom fields, be sure to check the permission of the user that created the custom field.
201
+
202
+ === Roadmap (Ranked)
203
+ 1. Get related records using AR style syntax, e.g.
204
+ pp a.contacts
205
+ to get contacts associated with an account.
206
+ 2. Support for multiple find fields.
207
+ 3. AR style master/detail updates e.g. where +a+ is an account.
208
+ a << RubyZoho::Crm::Contact.new(
209
+ :last_name => 'Last Name',
210
+ :first_name => 'First Name'
211
+ )
199
212
 
200
213
  == Contributing to rubyzoho
201
214
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/ruby_zoho.rb CHANGED
@@ -103,16 +103,22 @@ module RubyZoho
103
103
  end
104
104
  end
105
105
 
106
- def create(object_attribute_hash)
107
- initialize(object_attribute_hash)
108
- save
106
+ def self.method_missing(meth, *args, &block)
107
+ if meth.to_s =~ /^find_by_(.+)$/
108
+ run_find_by_method($1, *args, &block)
109
+ else
110
+ super
111
+ end
109
112
  end
110
113
 
111
- def save
112
- h = {}
113
- @fields.each { |f| h.merge!({ f => eval("self.#{f.to_s}") }) }
114
- h.delete_if { |k, v| v.nil? }
115
- RubyZoho.configuration.api.add_record(Crm.module_name, h)
114
+ def self.run_find_by_method(attrs, *args, &block)
115
+ attrs = attrs.split('_and_')
116
+ conditions = Array.new(args.size, '=')
117
+ h = RubyZoho.configuration.api.find_records(
118
+ Crm.module_name, ApiUtils.string_to_symbol(attrs[0]), conditions[0], args[0]
119
+ )
120
+ return h.collect { |r| new(r) } unless h.nil?
121
+ nil
116
122
  end
117
123
 
118
124
  def self.all #TODO Refactor into low level API
@@ -126,26 +132,21 @@ module RubyZoho
126
132
  result.collect { |r| new(r) }
127
133
  end
128
134
 
129
- def self.delete(id)
130
- RubyZoho.configuration.api.delete_record(Crm.module_name, id)
135
+ def create(object_attribute_hash)
136
+ initialize(object_attribute_hash)
137
+ save
131
138
  end
132
139
 
133
- def self.method_missing(meth, *args, &block)
134
- if meth.to_s =~ /^find_by_(.+)$/
135
- run_find_by_method($1, *args, &block)
136
- else
137
- super
138
- end
140
+ def self.delete(id)
141
+ RubyZoho.configuration.api.delete_record(Crm.module_name, id)
139
142
  end
140
143
 
141
- def self.run_find_by_method(attrs, *args, &block)
142
- attrs = attrs.split('_and_')
143
- conditions = Array.new(args.size, '=')
144
- h = RubyZoho.configuration.api.find_records(
145
- Crm.module_name, ApiUtils.string_to_symbol(attrs[0]), conditions[0], args[0]
146
- )
147
- return h.collect { |r| new(r) } unless h.nil?
148
- nil
144
+ def save
145
+ h = {}
146
+ @fields.each { |f| h.merge!({ f => eval("self.#{f.to_s}") }) }
147
+ h.delete_if { |k, v| v.nil? }
148
+ r = RubyZoho.configuration.api.add_record(Crm.module_name, h)
149
+ up_date(r)
149
150
  end
150
151
 
151
152
  def self.update(object_attribute_hash)
@@ -155,6 +156,25 @@ module RubyZoho
155
156
  RubyZoho.configuration.api.update_record(Crm.module_name, id, object_attribute_hash)
156
157
  end
157
158
 
159
+ def up_date(object_attribute_hash)
160
+ retry_counter = object_attribute_hash.length
161
+ begin
162
+ object_attribute_hash.map { |(k, v)| public_send("#{k}=", v) }
163
+ rescue NoMethodError => e
164
+ m = e.message.slice(/`(.*?)=/)
165
+ unless m.nil?
166
+ m.gsub!('`', '')
167
+ m.gsub!('(', '')
168
+ m.gsub!(')', '')
169
+ RubyZoho::Crm.create_accessor(self.class, [m.chop])
170
+ end
171
+ retry_counter -= 1
172
+ retry if retry_counter > 0
173
+ end
174
+ self
175
+ end
176
+
177
+
158
178
  def self.setup_classes
159
179
  RubyZoho.configuration.crm_modules.each do |module_name|
160
180
  klass_name = module_name.chop
data/lib/zoho_api.rb CHANGED
@@ -2,6 +2,7 @@ $:.unshift File.join('..', File.dirname(__FILE__), 'lib')
2
2
 
3
3
  require 'httmultiparty'
4
4
  require 'rexml/document'
5
+ require 'net/https'
5
6
  require 'net/http/post/multipart'
6
7
  require 'mime/types'
7
8
  require 'ruby_zoho'
@@ -35,30 +36,35 @@ module ZohoApi
35
36
  element = x.add_element module_name
36
37
  row = element.add_element 'row', { 'no' => '1'}
37
38
  fields_values_hash.each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
38
- #pp x.to_s
39
39
  r = self.class.post(create_url(module_name, 'insertRecords'),
40
40
  :query => { :newFormat => 1, :authtoken => @auth_token,
41
41
  :scope => 'crmapi', :xmlData => x },
42
42
  :headers => { 'Content-length' => '0' })
43
43
  check_for_errors(r)
44
+ x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
45
+ to_hash_with_id(x_r, module_name)[0]
44
46
  end
45
47
 
46
48
  def add_field(row, field, value)
47
49
  r = (REXML::Element.new 'FL')
48
50
  r.attributes['val'] = field
49
- r.add_text(value)
51
+ r.add_text(value.to_s)
50
52
  row.elements << r
51
53
  row
52
54
  end
53
55
 
54
56
  def attach_file(module_name, record_id, file_path)
55
- r = self.class.post(create_url(module_name, 'uploadFile'),
56
- :query => { :newFormat => 1, :authtoken => @auth_token,
57
- :scope => 'crmapi',
58
- :id => record_id, :content => File.open(file_path) },
59
- :headers => { 'Content-length' => '0' })
60
- raise(RuntimeError, 'Attaching file failed.', r.body.to_s) unless r.response.code == '200'
61
- r.code
57
+ mime_type = (MIME::Types.type_for(file_path)[0] || MIME::Types["application/octet-stream"][0])
58
+ url = URI.parse(create_url(module_name, 'uploadFile') + "?authtoken=#{@auth_token}&scope=crmapi&id=#{record_id}")
59
+ File.open(file_path) do |file|
60
+ req = Net::HTTP::Post::Multipart.new url.path,
61
+ "file" => UploadIO.new(file, mime_type, File.basename(file_path))
62
+ http = Net::HTTP.new(url.host, url.port)
63
+ http.use_ssl = url.port == 443 if url.scheme == "https"
64
+ res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
65
+ end
66
+ pp res
67
+ pp res.code
62
68
  end
63
69
 
64
70
  def add_file(module_name, record_id, file_path)
@@ -242,6 +248,16 @@ module ZohoApi
242
248
  r
243
249
  end
244
250
 
251
+ def to_hash_with_id(xml_results, module_name)
252
+ h = to_hash(xml_results)
253
+ primary_key = module_name.chop.downcase + 'id'
254
+ h.each do |e|
255
+ e.merge!({ primary_key.to_sym => e[:id] }) if e[primary_key.to_sym].nil? && !e[:id].nil?
256
+ e.merge!({ e[:id] => primary_key.to_sym }) if e[:id].nil? && !e[primary_key.to_sym].nil?
257
+ end
258
+ h
259
+ end
260
+
245
261
  def update_module_fields(mod_name, module_name, r)
246
262
  @@module_fields[mod_name] = []
247
263
  x = REXML::Document.new(r.body)
data/rubyzoho.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rubyzoho"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["amalc"]
@@ -14,7 +14,7 @@ describe RubyZoho::Crm do
14
14
  #config.api_key = 'e194b2951fb238e26bc096de9d0cf5f8'
15
15
  config.api_key = '62cedfe9427caef8afb9ea3b5bf68154'
16
16
  config.crm_modules = %w(Quotes)
17
- config.cache_fields = false
17
+ config.cache_fields = true
18
18
  end
19
19
  #r = RubyZoho::Crm::Contact.find_by_last_name('Smithereens')
20
20
  #r.each { |m| RubyZoho::Crm::Contact.delete(m.contactid) } unless r.nil?
@@ -173,6 +173,19 @@ describe RubyZoho::Crm do
173
173
  r.each { |c| RubyZoho::Crm::Lead.delete(c.leadid) }
174
174
  end
175
175
 
176
+ it 'should save and retrieve an account record with a custom field' do
177
+ accounts = RubyZoho::Crm::Account.all
178
+ a = accounts.first
179
+ if defined?(a.par_ltd)
180
+ RubyZoho::Crm::Lead.update(
181
+ :id => a.accountid,
182
+ :test_custom => '$1,000,000'
183
+ )
184
+ a2 = RubyZoho::Crm::Account.find_by_accountid(a.accountid)
185
+ a2.first.test_custom.should eq('$1,000,000')
186
+ end
187
+ end
188
+
176
189
  it 'should save and retrieve a potential record' do
177
190
  accounts = RubyZoho::Crm::Account.all
178
191
  h = {
@@ -85,7 +85,7 @@ describe ZohoApi do
85
85
  pending
86
86
  @zoho.add_record('Contacts', @h_smith)
87
87
  contacts = @zoho.find_records('Contacts', :email, '=', @h_smith[:email])
88
- @zoho.add_file('Contacts', contacts[0][:contactid], @sample_pdf)
88
+ @zoho.attach_file('Contacts', contacts[0][:contactid], @sample_pdf)
89
89
  #@zoho.delete_record('Contacts', contacts[0][:contactid])
90
90
  end
91
91
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyzoho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: