Class: ZohoHub::BaseRecord

Inherits:
Object
  • Object
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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zoho_hub/records/base_record.rb', line 66

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

Raises:



98
99
100
101
102
103
104
105
# File 'lib/zoho_hub/records/base_record.rb', line 98

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



62
63
64
# File 'lib/zoho_hub/records/base_record.rb', line 62

def create(params)
  new(params).save
end

.exists?(id) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/zoho_hub/records/base_record.rb', line 90

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



57
58
59
60
# File 'lib/zoho_hub/records/base_record.rb', line 57

def find_by(params)
  records = where(params)
  records.first
end

.get(path, params = {}, &block) ⇒ Object



78
79
80
# File 'lib/zoho_hub/records/base_record.rb', line 78

def get(path, params = {}, &block)
  ZohoHub.connection.get(path, params, &block)
end

.post(path, params = {}, &block) ⇒ Object



82
83
84
# File 'lib/zoho_hub/records/base_record.rb', line 82

def post(path, params = {}, &block)
  ZohoHub.connection.post(path, params.to_json, &block)
end

.put(path, params = {}, &block) ⇒ Object



86
87
88
# File 'lib/zoho_hub/records/base_record.rb', line 86

def put(path, params = {}, &block)
  ZohoHub.connection.put(path, params.to_json, &block)
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
55
# 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.nil? ? [] : response.fetch(:data, [])

  data.map { |info| new(info) }
end

.zoho_key_translationObject



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

#attributesObject



108
109
110
# File 'lib/zoho_hub/records/base_record.rb', line 108

def attributes
  self.class.attributes
end

#build_response(body) ⇒ Object



167
168
169
# File 'lib/zoho_hub/records/base_record.rb', line 167

def build_response(body)
  self.class.build_response(body)
end

#get(path, params = {}) ⇒ Object



112
113
114
# File 'lib/zoho_hub/records/base_record.rb', line 112

def get(path, params = {})
  self.class.get(path, params)
end

#new_record?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/zoho_hub/records/base_record.rb', line 151

def new_record?
  !id.present?
end

#post(path, params = {}) ⇒ Object



116
117
118
# File 'lib/zoho_hub/records/base_record.rb', line 116

def post(path, params = {})
  self.class.post(path, params)
end

#put(path, params = {}) ⇒ Object



120
121
122
# File 'lib/zoho_hub/records/base_record.rb', line 120

def put(path, params = {})
  self.class.put(path, params)
end

#save(trigger:) ⇒ Object

Save the record to Zoho trigger: [‘workflow’, ‘approval’, ‘blueprint’]



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zoho_hub/records/base_record.rb', line 126

def save(trigger:)
  body = if new_record? # create new record
           post(self.class.request_path, to_input(trigger: trigger))
         else # update existing record
           path = File.join(self.class.request_path, id)
           put(path, to_input(trigger: trigger))
         end

  response = build_response(body)

  id = response.data.dig(:details, :id)

  return id if id

  # Invalid errors
  response.data
end

#to_input(trigger:) ⇒ Object



144
145
146
147
148
149
# File 'lib/zoho_hub/records/base_record.rb', line 144

def to_input(trigger:)
  json = { data: [to_params] }
  json[:trigger] = Array(trigger) if trigger

  json
end

#to_paramsObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/zoho_hub/records/base_record.rb', line 155

def to_params
  params = {}

  attributes.each do |attr|
    key = attr_to_zoho_key(attr)

    params[key] = send(attr)
  end

  params
end