Class: ZohoHub::BaseRecord
Constant Summary
collapse
- DEFAULT_RECORDS_PER_PAGE =
Default number of records when fetching all.
200
- DEFAULT_PAGE =
Default page number when fetching all.
1
- MIN_RECORDS =
Minimum number of records to fetch when fetching all.
2
Class Method Summary
collapse
-
.add_note(id:, title: '', content: '') ⇒ Object
-
.add_related(parent_module:, parent_id:, related_id:) ⇒ Object
-
.all(params = {}) ⇒ Object
-
.all_related(parent_module:, parent_id:) ⇒ Object
-
.blueprint_transition(id, transition_id, data = {}) ⇒ Object
-
.blueprint_transitions(id) ⇒ Object
-
.build_response(body) ⇒ Object
-
.create(params) ⇒ Object
-
.download_attachment(parent_id:, attachment_id:) ⇒ Object
-
.exists?(id) ⇒ Boolean
(also: exist?)
-
.find(id) ⇒ Object
-
.find_by(params) ⇒ Object
-
.related_attachments(parent_id:) ⇒ Object
-
.remove_related(parent_module:, parent_id:, related_id:) ⇒ Object
-
.request_path(name = nil) ⇒ Object
-
.update(id, params) ⇒ Object
-
.update_all(records) ⇒ Object
-
.update_related(parent_module:, parent_id:, related_id:, data:) ⇒ Object
-
.where(params) ⇒ Object
Instance Method Summary
collapse
#add_error, #errors, included, #validate!, #validate_field!
#assign_attributes, #attributes, included
#delete, #get, included, #post, #put
Constructor Details
#initialize(params = {}) ⇒ BaseRecord
Returns a new instance of BaseRecord.
168
169
170
171
172
173
174
175
|
# File 'lib/zoho_hub/base_record.rb', line 168
def initialize(params = {})
attributes.each do |attr|
zoho_key = attr_to_zoho_key(attr)
value = params[zoho_key].nil? ? params[attr] : params[zoho_key]
send("#{attr}=", value)
end
end
|
Class Method Details
.add_note(id:, title: '', content: '') ⇒ Object
89
90
91
92
|
# File 'lib/zoho_hub/base_record.rb', line 89
def add_note(id:, title: '', content: '')
path = File.join(request_path, id, 'Notes')
post(path, data: [{ Note_Title: title, Note_Content: content }])
end
|
111
112
113
114
115
|
# File 'lib/zoho_hub/base_record.rb', line 111
def add_related(parent_module:, parent_id:, related_id:)
update_related(
parent_module: parent_module, parent_id: parent_id, related_id: related_id, data: [{}]
)
end
|
.all(params = {}) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/zoho_hub/base_record.rb', line 132
def all(params = {})
params[:page] ||= DEFAULT_PAGE
params[:per_page] ||= DEFAULT_RECORDS_PER_PAGE
params[:per_page] = MIN_RECORDS if params[:per_page] < MIN_RECORDS
body = get(request_path, params)
response = build_response(body)
data = response.nil? ? [] : response.data
data.map { |json| new(json) }
end
|
94
95
96
97
98
99
100
101
|
# File 'lib/zoho_hub/base_record.rb', line 94
def all_related(parent_module:, parent_id:)
body = get(File.join(parent_module.constantize.request_path, parent_id, request_path))
response = build_response(body)
data = response.nil? ? [] : response.data
data.map { |json| new(json) }
end
|
.blueprint_transition(id, transition_id, data = {}) ⇒ Object
81
82
83
|
# File 'lib/zoho_hub/base_record.rb', line 81
def blueprint_transition(id, transition_id, data = {})
new(id: id).blueprint_transition(transition_id, data)
end
|
.blueprint_transitions(id) ⇒ Object
85
86
87
|
# File 'lib/zoho_hub/base_record.rb', line 85
def blueprint_transitions(id)
new(id: id).blueprint_transitions
end
|
.build_response(body) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/zoho_hub/base_record.rb', line 153
def build_response(body)
response = Response.new(body)
raise InvalidTokenError, response.msg if response.invalid_token?
raise InternalError, response.msg if response.internal_error?
raise RecordInvalid, response.msg if response.invalid_data?
raise InvalidModule, response.msg if response.invalid_module?
raise NoPermission, response.msg if response.no_permission?
raise MandatoryNotFound, response.msg if response.mandatory_not_found?
raise RecordInBlueprint, response.msg if response.record_in_blueprint?
response
end
|
.create(params) ⇒ Object
73
74
75
|
# File 'lib/zoho_hub/base_record.rb', line 73
def create(params)
new(params).save
end
|
.download_attachment(parent_id:, attachment_id:) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/zoho_hub/modules/attachment.rb', line 17
def download_attachment(parent_id:, attachment_id:)
attachment = related_attachments(parent_id: parent_id).find { |a| a.id == attachment_id }
uri = File.join(request_path, parent_id, 'Attachments', attachment_id)
res = ZohoHub.connection.adapter.get(uri)
attachment.content_type = res.['content-type']
extension = File.extname(attachment.file_name)
basename = File.basename(attachment.file_name, extension)
file = Tempfile.new([basename, extension])
file.binmode
file.write(res.body)
file.rewind
attachment.file = file
attachment
end
|
.exists?(id) ⇒ Boolean
Also known as:
exist?
145
146
147
148
149
|
# File 'lib/zoho_hub/base_record.rb', line 145
def exists?(id)
!find(id).nil?
rescue RecordNotFound
false
end
|
.find(id) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/zoho_hub/base_record.rb', line 31
def find(id)
body = get(File.join(request_path, id.to_s))
response = build_response(body)
if response.empty?
raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}"
end
new(response.data.first)
end
|
.find_by(params) ⇒ Object
68
69
70
71
|
# File 'lib/zoho_hub/base_record.rb', line 68
def find_by(params)
records = where(params)
records.first
end
|
8
9
10
11
12
13
14
15
|
# File 'lib/zoho_hub/modules/attachment.rb', line 8
def related_attachments(parent_id:)
body = get(File.join(request_path, parent_id, 'Attachments'))
response = build_response(body)
data = response.nil? ? [] : response.data
data.map { |json| Attachment.new(json) }
end
|
117
118
119
120
121
122
|
# File 'lib/zoho_hub/base_record.rb', line 117
def remove_related(parent_module:, parent_id:, related_id:)
body = delete(
File.join(parent_module.constantize.request_path, parent_id, request_path, related_id)
)
build_response(body)
end
|
.request_path(name = nil) ⇒ Object
25
26
27
28
29
|
# File 'lib/zoho_hub/base_record.rb', line 25
def request_path(name = nil)
@request_path = name if name
@request_path ||= StringUtils.pluralize(StringUtils.demodulize(to_s))
@request_path
end
|
.update(id, params) ⇒ Object
77
78
79
|
# File 'lib/zoho_hub/base_record.rb', line 77
def update(id, params)
new(id: id).update(params)
end
|
.update_all(records) ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/zoho_hub/base_record.rb', line 124
def update_all(records)
zoho_params = records.map { |record| record.transform_keys { |key| attr_to_zoho_key(key) } }
body = put(File.join(request_path), data: zoho_params)
build_response(body)
end
|
103
104
105
106
107
108
109
|
# File 'lib/zoho_hub/base_record.rb', line 103
def update_related(parent_module:, parent_id:, related_id:, data:)
path = File.join(
parent_module.constantize.request_path, parent_id, request_path, related_id
)
body = put(path, data: data)
build_response(body)
end
|
.where(params) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/zoho_hub/base_record.rb', line 42
def where(params)
path = File.join(request_path, 'search')
if params.size == 1
params = case params.keys.first
when :criteria, :email, :phone, :word
params
else
key = attr_to_zoho_key(params.keys.first)
{
criteria: "#{key}:equals:#{params.values.first}"
}
end
end
body = get(path, params)
response = build_response(body)
data = response.nil? ? [] : response.data
data.map { |json| new(json) }
end
|
Instance Method Details
#blueprint_transition(transition_id, data = {}) ⇒ Object
196
197
198
199
200
201
|
# File 'lib/zoho_hub/base_record.rb', line 196
def blueprint_transition(transition_id, data = {})
body = put(File.join(self.class.request_path, id, 'actions/blueprint'),
blueprint: [{ transition_id: transition_id, data: data }])
build_response(body)
end
|
#blueprint_transitions ⇒ Object
203
204
205
206
|
# File 'lib/zoho_hub/base_record.rb', line 203
def blueprint_transitions
body = get(File.join(self.class.request_path, id, 'actions/blueprint'))
build_response(body)
end
|
#build_response(body) ⇒ Object
224
225
226
|
# File 'lib/zoho_hub/base_record.rb', line 224
def build_response(body)
self.class.build_response(body)
end
|
#new_record? ⇒ Boolean
208
209
210
|
# File 'lib/zoho_hub/base_record.rb', line 208
def new_record?
!id
end
|
#save ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/zoho_hub/base_record.rb', line 177
def save
body = if new_record? post(self.class.request_path, data: [to_params])
else put(File.join(self.class.request_path, id), data: [to_params])
end
response = build_response(body)
response.data.first.dig(:details, :id)
end
|
#to_params ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/zoho_hub/base_record.rb', line 212
def to_params
params = {}
attributes.each do |attr|
key = attr_to_zoho_key(attr)
params[key] = send(attr)
end
params
end
|
#update(params) ⇒ Object
189
190
191
192
193
194
|
# File 'lib/zoho_hub/base_record.rb', line 189
def update(params)
zoho_params = params.transform_keys { |key| attr_to_zoho_key(key) }
body = put(File.join(self.class.request_path, id), data: [zoho_params])
build_response(body)
end
|