Class: Closeio::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Closeio::Base
show all
- Extended by:
- Forwardable
- Includes:
- HTTParty
- Defined in:
- lib/closeio/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
16
17
18
19
20
21
22
|
# File 'lib/closeio/base.rb', line 16
def initialize attrs={}
if attrs['data']
super attrs['data']
else
super attrs
end
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
14
15
16
|
# File 'lib/closeio/base.rb', line 14
def data
@data
end
|
Class Method Details
.all(response = nil, opts = {}) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/closeio/base.rb', line 32
def all response = nil, opts={}
res = response || get(resource_path, opts)
if res.success?
res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
else
bad_response res
end
end
|
.bad_response(response) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/closeio/base.rb', line 25
def bad_response response
if response.class == HTTParty::Response
raise HTTParty::ResponseError, response
end
raise StandardError, 'Unknown error'
end
|
.create(opts = {}) ⇒ Object
Closeio::Lead.create name: “Bluth Company”, contacts: [“Buster Bluth”, emails: [{email: “[email protected]”]}]
43
44
45
46
|
# File 'lib/closeio/base.rb', line 43
def create opts={}
res = post resource_path, body: opts.to_json
res.success? ? new(res) : bad_response(res)
end
|
.destroy ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/closeio/base.rb', line 52
def destroy
if res['data'].is_a? Array
raise "Yo I'm an array"
else
delete "#{resource_path}#{id}"
end
end
|
.find(id) ⇒ Object
61
62
63
64
|
# File 'lib/closeio/base.rb', line 61
def find id
res = get "#{resource_path}#{id}"
res.ok? ? new(res) : bad_response(res)
end
|
.resource_path ⇒ Object
66
67
68
69
|
# File 'lib/closeio/base.rb', line 66
def resource_path
klass = name.split('::').last.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
return "/#{klass}/"
end
|
.update(id, opts = {}) ⇒ Object
48
49
50
|
# File 'lib/closeio/base.rb', line 48
def update id, opts={}
put "#{resource_path}#{id}", body: opts.to_json
end
|