Class: Resource

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/vivialconnect/resource.rb

Overview

This class implements the shared behavior of the other resource classes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(path = false) ⇒ Object

Returns an array containing all of the API resources



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vivialconnect/resource.rb', line 23

def self.all(path=false)
  final_array = []
  page = 1
  limit = 100
  while true
    path = path ||= path_builder(:all)
    uri = build_template_uri(path: path, start: page, batch_size: limit)
    current_iteration = VivialConnect::Client.instance.make_request('GET', uri)
    if current_iteration.class != Array
      return []
    end
    final_array = update_final_array(current_iteration, final_array)
    break final_array if current_iteration.count < limit
    page += 1
  end
end

.build_hash_root_and_add_user_hash(options) ⇒ Object

:nodoc:



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/vivialconnect/resource.rb', line 178

def self.build_hash_root_and_add_user_hash(options) # :nodoc:
  root = class_to_json_root
  data  = {}
  if root != "number"
    data[root] = options
    return data
  else 
    data['phone_number'] = options
    return data
  end
end

.build_template_uri(path: p, start: nil, batch_size: nil) ⇒ Object

:nodoc:



212
213
214
215
# File 'lib/vivialconnect/resource.rb', line 212

def self.build_template_uri(path: p, start: nil, batch_size: nil) # :nodoc:
  all_template = Addressable::Template.new("#{path}{?query*}")
  uri = all_template.expand(query: {page: start, limit: batch_size}).to_s
end

.class_to_json_rootObject

:nodoc:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/vivialconnect/resource.rb', line 197

def self.class_to_json_root # :nodoc:
  base =  self.to_s.split('::')[-1]
  base = base.split("")
  final = []
  final << base[0]
  base[1..-1].each do |letter|
    if letter == letter.upcase
      final <<  "_" + letter
    else
      final << letter
    end
  end
  final.join("").downcase
end

.class_to_pathObject

:nodoc:



190
191
192
193
194
195
# File 'lib/vivialconnect/resource.rb', line 190

def self.class_to_path # :nodoc:
  base = self.to_s.split('::')[-1].downcase
  base = pluralize(base)
  base = base.prepend('/') unless base == 'accounts'
  base
end

.countObject

Returns the amount of resources.



111
112
113
114
# File 'lib/vivialconnect/resource.rb', line 111

def self.count
  uri = path_builder(:count)
  VivialConnect::Client.instance.make_request('GET', uri)
end

.create(options = {}) ⇒ Object

Creates a Vivial Connect API resource, returns a ruby object containing the newly created resource. Options hash values are determined by the API for the resource in question.



9
10
11
12
13
14
15
16
17
18
# File 'lib/vivialconnect/resource.rb', line 9

def self.create(options = {})
  #TODO: delete this when more phone_number_types are possible
  if self == VivialConnect::Number && !options.keys.include?('phone_number_type')
    options.merge!(phone_number_type: 'local')
  end
  data = build_hash_root_and_add_user_hash(options)
  data = data.to_json
  uri = path_builder(:create)
  VivialConnect::Client.instance.make_request('POST', uri, data)
end

.delete(id) ⇒ Object

Deletes the record defined by the provided id. NOTE: not possible for all resource types



132
133
134
135
136
137
138
# File 'lib/vivialconnect/resource.rb', line 132

def self.delete(id)
  uri = path_builder(:delete, id)
  data = {}
  data['id'] = id 
  data = data.to_json
  VivialConnect::Client.instance.make_request('DELETE', uri, data)
end

.find(id) ⇒ Object

Returns the resource defined by the provided id.



103
104
105
106
# File 'lib/vivialconnect/resource.rb', line 103

def self.find(id)
  uri = path_builder(:find, id)
  VivialConnect::Client.instance.make_request('GET', uri)
end

.find_each(start: 1, finish: nil, batch_size: 150) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vivialconnect/resource.rb', line 51

def self.find_each(start: 1, finish: nil, batch_size: 150)
  return to_enum(:find_each, start: start, finish: finish, batch_size: batch_size) unless block_given? 
  final_array = []
  page = start

  while true
    path = path_builder(:all)
    uri = build_template_uri(path: path, start: page, batch_size: batch_size)
    current_batch = VivialConnect::Client.instance.make_request('GET', uri)
    final_array = update_final_array(current_batch, final_array)

    current_batch.each { |record| yield record }

    if current_batch.count < batch_size || page == finish 
      break final_array
    end
    page += 1
  end
end

.find_in_batches(start: 1, finish: nil, batch_size: 150) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vivialconnect/resource.rb', line 79

def self.find_in_batches(start: 1, finish: nil, batch_size: 150)
  return to_enum(:find_in_batches, start: start, finish: finish, batch_size: batch_size) unless block_given? 
  final_array = []
  page = start

  while true
    path = path_builder(:all)
    uri = build_template_uri(path: path, start: page, batch_size: batch_size)
    current_batch = VivialConnect::Client.instance.make_request('GET', uri)
    final_array = update_final_array(current_batch, final_array)

    yield current_batch
    
    if current_batch.count < batch_size || page == finish 
      break final_array
    end
    page += 1
  end

end

.path_builder(resource_method, id = nil) ⇒ Object

:nodoc:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vivialconnect/resource.rb', line 156

def self.path_builder(resource_method, id=nil) # :nodoc:
  json = '.json'
  base = class_to_path
  if resource_method == :create || resource_method == :all 
    url = base + json
  elsif resource_method == :find || resource_method == :delete || resource_method == :update
    url = base + '/' + id.to_s + json
  elsif resource_method == :count 
    if self == VivialConnect::Account
      url = '/' + base + '/' + 'count' + json
    else
      url = base + '/' + 'count' + json
    end
  end
  return url
end

.pluralize(string) ⇒ Object

:nodoc:



173
174
175
176
# File 'lib/vivialconnect/resource.rb', line 173

def self.pluralize(string) # :nodoc:
  # avoids importing a library but will need to be improved upon depending on future api resource names
  string + 's'
end

.redact(id) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/vivialconnect/resource.rb', line 141

def self.redact(id)
  raise VivialConnectClientError, "You can only redact message objects" unless self == VivialConnect::Message
  data = {}
  data['message'] = {id: id, body: ""}
  data = data.to_json
  VivialConnect::Client.instance.make_request('PUT',"/messages/#{id}.json", data) 
end

.update(id, options = {}) ⇒ Object

Updates the record defined by the provided id with data from the options hash. Options hash values are determined by the API for the resource in question



120
121
122
123
124
125
126
# File 'lib/vivialconnect/resource.rb', line 120

def self.update(id, options={})
  options = options.merge(id: id)
  data = build_hash_root_and_add_user_hash(options)
  data = data.to_json
  uri = path_builder(:update, id)
  VivialConnect::Client.instance.make_request('PUT', uri, data)
end

.update_final_array(current_iteration, final_array) ⇒ Object

:nodoc:



217
218
219
220
# File 'lib/vivialconnect/resource.rb', line 217

def self.update_final_array(current_iteration, final_array) # :nodoc:
  final_array << current_iteration
  final_array.flatten!
end

Instance Method Details

#add_methods(options = {}) ⇒ Object

:nodoc:



149
150
151
152
153
154
# File 'lib/vivialconnect/resource.rb', line 149

def add_methods(options={}) # :nodoc:
  options.each do |k,v|
    new_ostruct_member(k)
    send "#{k}=" , v
  end
end

#deleteObject

This instance method deletes the current object. NOTE: not all resouce types can be deleted



252
253
254
255
256
257
258
259
# File 'lib/vivialconnect/resource.rb', line 252

def delete
  options = self.to_h 
  uri = self.class.path_builder(:delete, self.id)
  data = {}
  data['id'] = self.id 
  data = data.to_json
  VivialConnect::Client.instance.make_request('DELETE', uri, data)
end

#saveObject

This instance method saves the current object if it does not have an id and updates the existing object if it does have an id. It is equivalent to calling .create or .update on the instance level.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/vivialconnect/resource.rb', line 227

def save
  if self.id.nil?
    data = self.class.build_hash_root_and_add_user_hash(self.to_h)
    data = data.to_json
    uri = self.class.path_builder(:create)
    response_object = VivialConnect::Client.instance.make_request('POST', uri, data)
    new_object_hash =response_object.marshal_dump
    self.add_methods(new_object_hash)
    self
  else
    options = self.to_h 
    data = self.class.build_hash_root_and_add_user_hash(self.to_h)
    data = data.to_json
    uri = self.class.path_builder(:update, id)
    response_object = VivialConnect::Client.instance.make_request('PUT', uri, data)
    new_object_hash =response_object.marshal_dump
    self.add_methods(new_object_hash)
    self
  end
end