Class: Sqwiggle::Api::Resource
- Inherits:
-
Object
- Object
- Sqwiggle::Api::Resource
show all
- Defined in:
- lib/sqwiggle/api/resource.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.endpoint ⇒ Object
Returns the value of attribute endpoint.
16
17
18
|
# File 'lib/sqwiggle/api/resource.rb', line 16
def endpoint
@endpoint
end
|
Class Method Details
.all(client = Client.new) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/sqwiggle/api/resource.rb', line 18
def all(client=Client.new)
[].tap do |result|
JSON.parse(client.get(self.endpoint).body).each do |item|
result << self.new(item.merge client:client)
end
end
end
|
.create(params, client = Client.new) ⇒ Object
38
39
40
41
42
|
# File 'lib/sqwiggle/api/resource.rb', line 38
def create(params, client=Client.new)
create! params, client
rescue Errors::BadRequestError
false
end
|
.create!(params, client = Client.new) ⇒ Object
32
33
34
35
36
|
# File 'lib/sqwiggle/api/resource.rb', line 32
def create!(params, client=Client.new)
attrs = JSON.parse(client.post("#{endpoint}", params).body)
attrs.merge! client:client
self.new attrs
end
|
.default_endpoint ⇒ Object
48
49
50
|
# File 'lib/sqwiggle/api/resource.rb', line 48
def default_endpoint
"/#{self.to_s.downcase.split('::').last}s"
end
|
.find(id, client = Client.new) ⇒ Object
26
27
28
29
30
|
# File 'lib/sqwiggle/api/resource.rb', line 26
def find(id, client=Client.new)
attrs = JSON.parse client.get("#{endpoint}/#{id}").body
attrs.merge! client:client
self.new attrs
end
|
Instance Method Details
#default_client ⇒ Object
10
11
12
|
# File 'lib/sqwiggle/api/resource.rb', line 10
def default_client
Client.new
end
|
#delete ⇒ Object
83
84
85
86
|
# File 'lib/sqwiggle/api/resource.rb', line 83
def delete
res = client.delete("#{self.class.endpoint}/#{id}")
res.status == 204
end
|
#persisted? ⇒ Boolean
79
80
81
|
# File 'lib/sqwiggle/api/resource.rb', line 79
def persisted?
(id != nil)
end
|
#save ⇒ Object
73
74
75
76
77
|
# File 'lib/sqwiggle/api/resource.rb', line 73
def save
return update(self.attributes) if persisted?
self.attributes = self.class.create(attributes, client).attributes
self
end
|
#save! ⇒ Object
67
68
69
70
71
|
# File 'lib/sqwiggle/api/resource.rb', line 67
def save!
return update!(self.attributes) if persisted?
self.attributes = self.class.create!(attributes, client).attributes
self
end
|
#update(params) ⇒ Object
61
62
63
64
65
|
# File 'lib/sqwiggle/api/resource.rb', line 61
def update(params)
update! params
rescue Errors::BadRequestError
false
end
|
#update!(params) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/sqwiggle/api/resource.rb', line 54
def update!(params)
res = client.put("#{self.class.endpoint}/#{id}", params)
attrs = JSON.parse(res.body, :symbolize_names => true)
self.attributes = attrs
self
end
|