Class: ZohoHub::BaseRecord
- Inherits:
-
Object
- Object
- ZohoHub::BaseRecord
show all
- Defined in:
- lib/zoho_hub/records/base_record.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.all(options = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/zoho_hub/records/base_record.rb', line 65
def all(options = {})
options[:page] ||= 1
options[:per_page] ||= 200
body = get(request_path, options)
response = build_response(body)
data = response.nil? ? [] : response.data
data.map { |info| new(info) }
end
|
.attribute_translation(translation = nil) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/zoho_hub/records/base_record.rb', line 24
def attribute_translation(translation = nil)
@attribute_translation ||= {}
return @attribute_translation unless translation
@attribute_translation = translation
end
|
.attributes(*attributes) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/zoho_hub/records/base_record.rb', line 14
def attributes(*attributes)
@attributes ||= []
return @attributes unless attributes
attr_accessor(*attributes)
@attributes += attributes
end
|
.build_response(body) ⇒ Object
97
98
99
100
101
102
103
104
|
# File 'lib/zoho_hub/records/base_record.rb', line 97
def build_response(body)
response = Response.new(body)
raise InvalidTokenError, response.msg if response.invalid_token?
raise RecordInvalid, response.msg if response.invalid_data?
response
end
|
.create(params) ⇒ Object
61
62
63
|
# File 'lib/zoho_hub/records/base_record.rb', line 61
def create(params)
new(params).save
end
|
.exists?(id) ⇒ Boolean
Also known as:
exist?
89
90
91
92
93
|
# File 'lib/zoho_hub/records/base_record.rb', line 89
def exists?(id)
!find(id).nil?
rescue RecordNotFound
false
end
|
.find(id) ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/zoho_hub/records/base_record.rb', line 36
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)
end
|
.find_by(params) ⇒ Object
56
57
58
59
|
# File 'lib/zoho_hub/records/base_record.rb', line 56
def find_by(params)
records = where(params)
records.first
end
|
.get(path, params = {}) ⇒ Object
77
78
79
|
# File 'lib/zoho_hub/records/base_record.rb', line 77
def get(path, params = {})
ZohoHub.connection.get(path, params)
end
|
.post(path, params = {}) ⇒ Object
81
82
83
|
# File 'lib/zoho_hub/records/base_record.rb', line 81
def post(path, params = {})
ZohoHub.connection.post(path, params.to_json)
end
|
.put(path, params = {}) ⇒ Object
85
86
87
|
# File 'lib/zoho_hub/records/base_record.rb', line 85
def put(path, params = {})
ZohoHub.connection.put(path, params.to_json)
end
|
.request_path(name = nil) ⇒ Object
8
9
10
11
12
|
# File 'lib/zoho_hub/records/base_record.rb', line 8
def request_path(name = nil)
@request_path = name if name
@request_path ||= to_s.demodulize.pluralize
@request_path
end
|
.where(params) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/zoho_hub/records/base_record.rb', line 47
def where(params)
path = File.join(request_path, 'search')
response = get(path, params)
data = response[:data]
data.map { |info| new(info) }
end
|
.zoho_key_translation ⇒ Object
32
33
34
|
# File 'lib/zoho_hub/records/base_record.rb', line 32
def zoho_key_translation
@attribute_translation.to_a.map(&:rotate).to_h
end
|
Instance Method Details
#attributes ⇒ Object
107
108
109
|
# File 'lib/zoho_hub/records/base_record.rb', line 107
def attributes
self.class.attributes
end
|
#build_response(body) ⇒ Object
152
153
154
|
# File 'lib/zoho_hub/records/base_record.rb', line 152
def build_response(body)
self.class.build_response(body)
end
|
#get(path, params = {}) ⇒ Object
111
112
113
|
# File 'lib/zoho_hub/records/base_record.rb', line 111
def get(path, params = {})
self.class.get(path, params)
end
|
#new_record? ⇒ Boolean
136
137
138
|
# File 'lib/zoho_hub/records/base_record.rb', line 136
def new_record?
!id.present?
end
|
#post(path, params = {}) ⇒ Object
115
116
117
|
# File 'lib/zoho_hub/records/base_record.rb', line 115
def post(path, params = {})
self.class.post(path, params)
end
|
#put(path, params = {}) ⇒ Object
119
120
121
|
# File 'lib/zoho_hub/records/base_record.rb', line 119
def put(path, params = {})
self.class.put(path, params)
end
|
#save ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/zoho_hub/records/base_record.rb', line 123
def save
body = if new_record? post(self.class.request_path, data: [to_params])
else path = File.join(self.class.request_path, id)
put(path, data: [to_params])
end
response = build_response(body)
response.data.dig(:details, :id)
end
|
#to_params ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/zoho_hub/records/base_record.rb', line 140
def to_params
params = {}
attributes.each do |attr|
key = attr_to_zoho_key(attr)
params[key] = send(attr)
end
params
end
|