Class: RelateIQ::APIResource

Inherits:
RiqObject show all
Defined in:
lib/relateiq/api_resource.rb

Direct Known Subclasses

List

Instance Attribute Summary

Attributes inherited from RiqObject

#api_key, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RiqObject

#add_accessors, construct_from, #initialize, #inspect, #metaclass, #method_missing, #refresh_from, #respond_to_missing?

Constructor Details

This class inherits a constructor from RelateIQ::RiqObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RelateIQ::RiqObject

Class Method Details

.all(params = {}, url = nil) ⇒ Object



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

def self.all(params = {}, url = nil)
  params = {} unless params.is_a? Hash
  plural = "#{name}s"
  path = url.nil? ? "#{plural}" : url
  response = RelateIQ.get(path, params)
  objects = response['objects'] || []
  list = Array.new
  objects.each do |v|
    if v.class == Hash && v['id'].nil?
      c = self.new(v['id'])
    else
      c = self.new
    end
    c.refresh_from(v)
    list.push(c)
  end
  return RiqList.new(list)
end

.delete(id, params = {}) ⇒ Object



69
70
71
72
73
# File 'lib/relateiq/api_resource.rb', line 69

def self.delete(id, params = {})
  plural = "#{name}s"
  path = "#{plural}/#{id}"
  RelateIQ.delete(path)
end

.find(id, params = {}, url = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/relateiq/api_resource.rb', line 15

def self.find(id, params = {}, url = nil)
  path = url.nil? ? "#{name}s/#{id}" : url
  instance = self.new(id)
  response = RelateIQ.get(path, params)
  instance.refresh_from(response)
  instance
end

.nameObject



3
4
5
6
7
# File 'lib/relateiq/api_resource.rb', line 3

def self.name
  n = self.to_s.split('::')[-1]
  # Hyphenize
  n.scan(/([A-Z][a-z]*)/).join('-').downcase
end

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



48
49
50
51
52
53
# File 'lib/relateiq/api_resource.rb', line 48

def self.update(id, params = {})
  plural = "#{name}s"
  path = "#{plural}/#{id}"
  p = { self.name => params }.to_json
  response = RelateIQ.put(path, p)
end

Instance Method Details

#create(params = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/relateiq/api_resource.rb', line 56

def create(params = {})
  plural = "#{name}s"
  path = "#{plural}/#{self.id}"
  p = { self.name => params }.to_json
  resonse = RelateIQ.post(path, p)
end

#delete(params = {}) ⇒ Object



63
64
65
66
67
# File 'lib/relateiq/api_resource.rb', line 63

def delete(params = {})
  plural = "#{name}s"
  path = "#{plural}/#{self.id}"
  RelateIQ.delete(path)
end

#nameObject



9
10
11
12
13
# File 'lib/relateiq/api_resource.rb', line 9

def name
  n = self.class.to_s.split('::')[-1]
  # Hyphenize
  n.scan(/([A-Z][a-z]*)/).join('-').downcase
end

#update(params = {}) ⇒ Object



42
43
44
45
46
# File 'lib/relateiq/api_resource.rb', line 42

def update(params = {})
  path = "#{self.name}s/#{self.id}"
  p = { self.name => params }.to_json
  response = RelateIQ.put(path, p)
end