zoho_hub 0.1.25 → 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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +4 -0
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +5 -2
  7. data/README.md +91 -7
  8. data/bin/console +5 -6
  9. data/bin/read +44 -0
  10. data/bin/setup +0 -2
  11. data/bin/zoho_hub +62 -0
  12. data/lib/zoho_hub.rb +20 -19
  13. data/lib/zoho_hub/auth.rb +37 -47
  14. data/lib/zoho_hub/base_record.rb +122 -0
  15. data/lib/zoho_hub/configuration.rb +3 -4
  16. data/lib/zoho_hub/connection.rb +10 -17
  17. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/account.rb +0 -0
  18. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/campaign.rb +0 -0
  19. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/contact.rb +2 -4
  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 -7
  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 -7
  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/zoho_hub.gemspec +21 -17
  36. metadata +103 -78
  37. data/lib/zoho_hub/records/attachment.rb +0 -104
  38. data/lib/zoho_hub/records/base_record.rb +0 -189
  39. data/lib/zoho_hub/records/product.rb +0 -35
  40. data/lib/zoho_hub/records/quote.rb +0 -42
  41. data/lib/zoho_hub/records/vendor.rb +0 -34
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zoho_hub/records/base_record'
4
-
5
- module ZohoHub
6
- class Attachment < BaseRecord
7
- attributes :id, :parent_id, :file_name, :link_url, :type, :created_time
8
- attributes :parent, :attachment_url
9
-
10
- # The translation from attribute name to the JSON field on Zoho. The default behaviour will be
11
- # to Camel_Case the attribute so on this list we should only have exceptions to this rule.
12
- attribute_translation(
13
- id: :id,
14
- link_url: :'$link_url',
15
- type: :'$type'
16
- )
17
-
18
- def initialize(params)
19
- attributes.each do |attr|
20
- zoho_key = attr_to_zoho_key(attr)
21
-
22
- send("#{attr}=", params[zoho_key] || params[attr])
23
- end
24
-
25
- @parent_id = params.dig(:Parent_Id, :id)
26
- end
27
-
28
- class << self
29
- def exists?(id, parent:)
30
- !find(id, parent: parent).nil?
31
- rescue RecordNotFound
32
- false
33
- end
34
-
35
- def find(id, parent:)
36
- body = get(File.join(request_path, id.to_s), parent: parent)
37
- response = build_response(body)
38
-
39
- if response.empty?
40
- raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}"
41
- end
42
-
43
- new(response.data)
44
- end
45
-
46
- def get(path, parent:, **params)
47
- # remove /search added by where method
48
- path = path.sub('/search', '')
49
- ZohoHub.connection.get(parent_module_path(path, parent), params)
50
- end
51
-
52
- def post(path, parent:, **params)
53
- ZohoHub.connection.post(parent_module_path(path, parent), params) do |conn|
54
- conn.request :multipart
55
- conn.request :url_encoded
56
- end
57
- end
58
-
59
- def put(_path, _params = {})
60
- raise NotImplementedError
61
- end
62
-
63
- def parent_module_path(path, parent)
64
- File.join(
65
- parent.class.request_path,
66
- parent.id,
67
- path
68
- )
69
- end
70
- end
71
-
72
- def post(path, _params = {})
73
- self.class.post(path, parent: parent, **to_params)
74
- end
75
-
76
- def put(_path, _params = {})
77
- raise NotImplementedError
78
- end
79
-
80
- def save(*)
81
- super
82
- rescue => e
83
- if e.message.include?('Attachment link already exists')
84
- raise AttachmentLinkTakenError, e.message
85
- else
86
- raise e
87
- end
88
- end
89
-
90
- def to_params
91
- { attachmentUrl: attachment_url }
92
- end
93
-
94
- private
95
-
96
- def to_input(**)
97
- to_params
98
- end
99
-
100
- def parent_module_path(path)
101
- self.class.parent_module_path(path, parent)
102
- end
103
- end
104
- end
@@ -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,35 +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
- puts Rainbow(params).magenta
15
-
16
- attributes.each do |attr|
17
- zoho_key = attr_to_zoho_key(attr)
18
-
19
- send("#{attr}=", params[zoho_key] || params[attr])
20
- end
21
-
22
- @owner_id ||= params.dig(:Owner, :id)
23
- @vendor_id ||= params.dig(:Vendor_Name, :id)
24
- end
25
-
26
- def to_params
27
- params = super
28
-
29
- params[:Owner] = { id: @owner_id } if @owner_id
30
- params[:Vendor_Name] = { id: @vendor_id } if @vendor_id
31
-
32
- params
33
- end
34
- end
35
- end
@@ -1,42 +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, :extra_info
8
-
9
- attribute_translation(
10
- id: :id,
11
- stage: :Quote_Stage
12
- )
13
-
14
- def initialize(params)
15
- attributes.each do |attr|
16
- zoho_key = attr_to_zoho_key(attr)
17
- send("#{attr}=", params[zoho_key] || params[attr])
18
- end
19
-
20
- @potential_id ||= params.dig(:Deal_Name, :id)
21
- @account_id ||= params.dig(:Account_Name, :id)
22
- @owner_id ||= params.dig(:Owner, :id)
23
-
24
- # The Quote has an array of products but we only care about one
25
- if params.dig(:Product_Details)
26
- product = params.dig(:Product_Details).first
27
- @product_id = product.dig(:id)
28
- end
29
- end
30
-
31
- def to_params
32
- params = super
33
-
34
- params[:Deal_Name] = { id: @potential_id } if @potential_id
35
- params[:Account_Name] = { id: @account_id } if @account_id
36
- params[:Owner] = { id: @owner_id } if @owner_id
37
- params[:Product_Details] = [{ product: { id: @product_id }, quantity: 1 }] if @product_id
38
-
39
- params
40
- end
41
- end
42
- 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
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