Class: CurdBee::Base
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- CurdBee::Base
show all
- Includes:
- HTTParty
- Defined in:
- lib/curdbee/base.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.element ⇒ Object
Returns the value of attribute element.
8
9
10
|
# File 'lib/curdbee/base.rb', line 8
def element
@element
end
|
.resource ⇒ Object
Returns the value of attribute resource.
8
9
10
|
# File 'lib/curdbee/base.rb', line 8
def resource
@resource
end
|
Class Method Details
.list(opts = {}) ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/curdbee/base.rb', line 10
def self.list(opts = {})
limit = opts[:limit] || 20
page = opts[:page] || 1
response = send_request(:get, "/#{self.resource}.json", :query => {'per_page' => limit, 'page' => page })
response.map {|c| self.new c["#{self.element}"]}
end
|
.send_request(method, path, opts = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/curdbee/base.rb', line 44
def self.send_request(method, path, opts={})
api_key = CurdBee::Config.api_key
subdomain = CurdBee::Config.subdomain
use_https = CurdBee::Config.use_https
scheme = use_https ? "https" : "http"
self.base_uri "#{scheme}://#{subdomain}.curdbee.com"
opts[:query] = opts.has_key?(:query) ? opts[:query].merge({'api_token' => api_key}) : {'api_token' => api_key}
begin
response = self.send(method, path, opts)
rescue
raise(CurdBee::Error::ConnectionFailed.new, "Failed to connect to server.")
end
case response.code.to_i
when 401
raise(CurdBee::Error::AccessDenied.new(response), "Access Denied.")
when 403
raise(CurdBee::Error::Forbidden.new(response), "Your action was forbidden.")
when 422
raise(CurdBee::Error::BadRequest.new(response), (!response.body.empty? ? JSON.parse(response.body).join(" ") : "") )
when 404
raise(CurdBee::Error::NotFound.new(response), "Resource not found.")
when 500
raise(CurdBee::Error::ServerError.new(response), "Error occured in server.")
end
response
end
|
.show(id) ⇒ Object
18
19
20
21
22
|
# File 'lib/curdbee/base.rb', line 18
def self.show(id)
response = send_request(:get, "/#{self.resource}/#{id}.json")
self.new(response["#{self.element}"])
end
|
Instance Method Details
#create ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/curdbee/base.rb', line 24
def create
body = {"#{self.class.element}" => self}.to_json
response = self.class.send_request(:post, "/#{self.class.resource}", :body => body)
self.id = response["#{self.class.element}"]["id"]
return true if (response.code.to_i == 201)
end
|
#delete ⇒ Object
39
40
41
42
|
# File 'lib/curdbee/base.rb', line 39
def delete
response = self.class.send_request(:delete, "/#{self.class.resource}/#{self[:id]}")
return true if response.code.to_i == 200
end
|
#update(attrs = nil) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/curdbee/base.rb', line 32
def update(attrs=nil)
body = {"#{self.class.element}" => (attrs || self)}.to_json
response = self.class.send_request(:put, "/#{self.class.resource}/#{self[:id]}", :body => body)
return true if (response.code.to_i == 200)
end
|