zoho_hub 0.1.56 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -2
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +5 -2
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +5 -2
  7. data/Gemfile +0 -10
  8. data/README.md +91 -7
  9. data/bin/console +6 -7
  10. data/bin/read +44 -0
  11. data/bin/setup +0 -2
  12. data/bin/zoho_hub +62 -0
  13. data/cache/.git_keep +0 -0
  14. data/lib/zoho_hub/auth.rb +38 -47
  15. data/lib/zoho_hub/base_record.rb +122 -0
  16. data/lib/zoho_hub/configuration.rb +3 -4
  17. data/lib/zoho_hub/connection.rb +12 -17
  18. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/account.rb +1 -1
  19. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/contact.rb +3 -9
  20. data/lib/zoho_hub/deprecated_and_only_for_reference_records/funder.rb +9 -0
  21. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/potential.rb +1 -25
  22. data/lib/zoho_hub/deprecated_and_only_for_reference_records/product.rb +9 -0
  23. data/lib/zoho_hub/deprecated_and_only_for_reference_records/quote.rb +34 -0
  24. data/lib/zoho_hub/errors.rb +0 -3
  25. data/lib/zoho_hub/module_builder.rb +61 -0
  26. data/lib/zoho_hub/oauth_callback_server.rb +26 -0
  27. data/lib/zoho_hub/response.rb +1 -4
  28. data/lib/zoho_hub/settings/field.rb +33 -0
  29. data/lib/zoho_hub/settings/module.rb +49 -0
  30. data/lib/zoho_hub/string_utils.rb +34 -0
  31. data/lib/zoho_hub/version.rb +1 -1
  32. data/lib/zoho_hub/views/variables.erb +72 -0
  33. data/lib/zoho_hub/with_attributes.rb +74 -0
  34. data/lib/zoho_hub/with_connection.rb +36 -0
  35. data/lib/zoho_hub.rb +21 -23
  36. data/zoho_hub.gemspec +21 -8
  37. metadata +180 -45
  38. data/lib/zoho_hub/records/adverse_criteria.rb +0 -43
  39. data/lib/zoho_hub/records/attachment.rb +0 -106
  40. data/lib/zoho_hub/records/base_record.rb +0 -189
  41. data/lib/zoho_hub/records/credit_score.rb +0 -36
  42. data/lib/zoho_hub/records/product.rb +0 -33
  43. data/lib/zoho_hub/records/quote.rb +0 -44
  44. data/lib/zoho_hub/records/sms_message.rb +0 -32
  45. data/lib/zoho_hub/records/vendor.rb +0 -34
  46. /data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/campaign.rb +0 -0
@@ -1,189 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zoho_hub/response'
4
-
5
- module ZohoHub
6
- class BaseRecord
7
- class << self
8
- def request_path(name = nil)
9
- @request_path = name if name
10
- @request_path ||= to_s.demodulize.pluralize
11
- @request_path
12
- end
13
-
14
- def attributes(*attributes)
15
- @attributes ||= []
16
-
17
- return @attributes unless attributes
18
-
19
- attr_accessor(*attributes)
20
-
21
- @attributes += attributes
22
- end
23
-
24
- def attribute_translation(translation = nil)
25
- @attribute_translation ||= {}
26
-
27
- return @attribute_translation unless translation
28
-
29
- @attribute_translation = translation
30
- end
31
-
32
- def zoho_key_translation
33
- @attribute_translation.to_a.map(&:rotate).to_h
34
- end
35
-
36
- def find(id)
37
- body = get(File.join(request_path, id.to_s))
38
- response = build_response(body)
39
-
40
- if response.empty?
41
- raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}"
42
- end
43
-
44
- new(response.data)
45
- end
46
-
47
- def where(params)
48
- path = File.join(request_path, 'search')
49
-
50
- response = get(path, params)
51
-
52
- data = response.nil? ? [] : response.fetch(:data, [])
53
-
54
- data.map { |info| new(info) }
55
- end
56
-
57
- def find_by(params)
58
- records = where(params)
59
- records.first
60
- end
61
-
62
- def create(params)
63
- new(params).save
64
- end
65
-
66
- def all(options = {})
67
- options[:page] ||= 1
68
- options[:per_page] ||= 200
69
-
70
- body = get(request_path, options)
71
- response = build_response(body)
72
-
73
- data = response.nil? ? [] : response.data
74
-
75
- data.map { |info| new(info) }
76
- end
77
-
78
- def get(path, params = {}, &block)
79
- ZohoHub.connection.get(path, params, &block)
80
- end
81
-
82
- def post(path, params = {}, &block)
83
- ZohoHub.connection.post(path, params.to_json, &block)
84
- end
85
-
86
- def put(path, params = {}, &block)
87
- ZohoHub.connection.put(path, params.to_json, &block)
88
- end
89
-
90
- def exists?(id)
91
- !find(id).nil?
92
- rescue RecordNotFound
93
- false
94
- end
95
-
96
- alias exist? exists?
97
-
98
- def build_response(body)
99
- response = Response.new(body)
100
-
101
- raise InvalidTokenError, response.msg if response.invalid_token?
102
- raise RecordInvalid, response.msg if response.invalid_data?
103
-
104
- response
105
- end
106
- end
107
-
108
- def attributes
109
- self.class.attributes
110
- end
111
-
112
- def get(path, params = {})
113
- self.class.get(path, params)
114
- end
115
-
116
- def post(path, params = {})
117
- self.class.post(path, params)
118
- end
119
-
120
- def put(path, params = {})
121
- self.class.put(path, params)
122
- end
123
-
124
- # Save the record to Zoho
125
- # trigger: ['workflow', 'approval', 'blueprint']
126
- def save(trigger:)
127
- body = if new_record? # create new record
128
- post(self.class.request_path, to_input(trigger: trigger))
129
- else # update existing record
130
- path = File.join(self.class.request_path, id)
131
- put(path, to_input(trigger: trigger))
132
- end
133
-
134
- response = build_response(body)
135
-
136
- id = response.data.dig(:details, :id)
137
-
138
- return id if id
139
-
140
- # Invalid errors
141
- response.data
142
- end
143
-
144
- def to_input(trigger:)
145
- json = { data: [to_params] }
146
- json[:trigger] = Array(trigger) if trigger
147
-
148
- json
149
- end
150
-
151
- def new_record?
152
- !id.present?
153
- end
154
-
155
- def to_params
156
- params = {}
157
-
158
- attributes.each do |attr|
159
- key = attr_to_zoho_key(attr)
160
-
161
- params[key] = send(attr)
162
- end
163
-
164
- params
165
- end
166
-
167
- def build_response(body)
168
- self.class.build_response(body)
169
- end
170
-
171
- private
172
-
173
- def attr_to_zoho_key(attr_name)
174
- translations = self.class.attribute_translation
175
-
176
- return translations[attr_name.to_sym] if translations.key?(attr_name.to_sym)
177
-
178
- attr_name.to_s.split('_').map(&:capitalize).join('_').to_sym
179
- end
180
-
181
- def zoho_key_to_attr(zoho_key)
182
- translations = self.class.zoho_key_translation
183
-
184
- return translations[zoho_key.to_sym] if translations.key?(zoho_key.to_sym)
185
-
186
- zoho_key.to_sym
187
- end
188
- end
189
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zoho_hub/records/base_record'
4
-
5
- module ZohoHub
6
- class CreditScore < BaseRecord
7
- request_path 'Credit_Scores'
8
-
9
- attributes :id, :account_id, :credit_data_source, :credit_score_band
10
- attributes :score_description, :credit_score_number, :credit_limit, :currency
11
-
12
- attribute_translation(
13
- id: :id,
14
- credit_score_number: :Name
15
- )
16
-
17
- def initialize(params)
18
- attributes.each do |attr|
19
- zoho_key = attr_to_zoho_key(attr)
20
-
21
- send("#{attr}=", params[zoho_key] || params[attr])
22
- end
23
-
24
- # Setup values as they come from the Zoho API if needed
25
- @account_id ||= params.dig(:Account_Name, :id)
26
- end
27
-
28
- def to_params
29
- params = super
30
-
31
- params[:Account_Name] = { id: @account_id } if @account_id
32
-
33
- params
34
- end
35
- end
36
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ZohoHub
4
- class Product < BaseRecord
5
- attributes :id, :description, :vendor_id, :owner_id, :active, :name
6
-
7
- attribute_translation(
8
- id: :id,
9
- active: :Product_Active,
10
- name: :Product_Name
11
- )
12
-
13
- def initialize(params)
14
- attributes.each do |attr|
15
- zoho_key = attr_to_zoho_key(attr)
16
-
17
- send("#{attr}=", params[zoho_key] || params[attr])
18
- end
19
-
20
- @owner_id ||= params.dig(:Owner, :id)
21
- @vendor_id ||= params.dig(:Vendor_Name, :id)
22
- end
23
-
24
- def to_params
25
- params = super
26
-
27
- params[:Owner] = { id: @owner_id } if @owner_id
28
- params[:Vendor_Name] = { id: @vendor_id } if @vendor_id
29
-
30
- params
31
- end
32
- end
33
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zoho_hub/records/base_record'
4
-
5
- module ZohoHub
6
- class Quote < BaseRecord
7
- attributes :id, :stage, :subject, :potential_id, :owner_id, :product_id, :account_id
8
- attributes :funding_amount, :financed_on, :extra_info
9
-
10
- attribute_translation(
11
- id: :id,
12
- stage: :Quote_Stage,
13
- financed_on: :Financed_on
14
- )
15
-
16
- def initialize(params)
17
- attributes.each do |attr|
18
- zoho_key = attr_to_zoho_key(attr)
19
- send("#{attr}=", params[zoho_key] || params[attr])
20
- end
21
-
22
- @potential_id ||= params.dig(:Deal_Name, :id)
23
- @account_id ||= params.dig(:Account_Name, :id)
24
- @owner_id ||= params.dig(:Owner, :id)
25
-
26
- # The Quote has an array of products but we only care about one
27
- if params.dig(:Product_Details)
28
- product = params.dig(:Product_Details).first
29
- @product_id = product[:id]
30
- end
31
- end
32
-
33
- def to_params
34
- params = super
35
-
36
- params[:Deal_Name] = { id: @potential_id } if @potential_id
37
- params[:Account_Name] = { id: @account_id } if @account_id
38
- params[:Owner] = { id: @owner_id } if @owner_id
39
- params[:Product_Details] = [{ product: { id: @product_id }, quantity: 1 }] if @product_id
40
-
41
- params
42
- end
43
- end
44
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zoho_hub/records/base_record'
4
-
5
- module ZohoHub
6
- class SMSMessage < BaseRecord
7
- attributes :id, :template, :state, :text, :contact_id, :potential_id, :name, :sent_by
8
-
9
- attribute_translation id: :id
10
-
11
- def initialize(params)
12
- attributes.each do |attr|
13
- zoho_key = attr_to_zoho_key(attr)
14
-
15
- send("#{attr}=", params[zoho_key] || params[attr])
16
- end
17
-
18
- # Setup values as they come from the Zoho API if needed
19
- @contact_id ||= params.dig(:Contact, :id)
20
- @potential_id ||= params.dig(:Potential, :id)
21
- end
22
-
23
- def to_params
24
- params = super
25
-
26
- params[:Contact] = { id: @contact_id } if @contact_id
27
- params[:Potential] = { id: @potential_id } if @potential_id
28
-
29
- params
30
- end
31
- end
32
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ZohoHub
4
- class Vendor < BaseRecord
5
- attributes :id, :email, :description, :vendor_name, :website, :owner_id, :phone, :currency
6
- attributes :company_reg_no, :created_time, :modified_time, :lender_status
7
-
8
- attribute_translation(
9
- id: :id
10
- )
11
-
12
- DEFAULTS = {
13
- currency: 'GBP'
14
- }.freeze
15
-
16
- def initialize(params)
17
- attributes.each do |attr|
18
- zoho_key = attr_to_zoho_key(attr)
19
-
20
- send("#{attr}=", params[zoho_key] || params[attr] || DEFAULTS[attr])
21
- end
22
-
23
- @owner_id ||= params.dig(:Owner, :id)
24
- end
25
-
26
- def to_params
27
- params = super
28
-
29
- params[:Owner] = { id: @owner_id } if @owner_id
30
-
31
- params
32
- end
33
- end
34
- end