Class: OpenRecycling::Resource
- Inherits:
-
Object
- Object
- OpenRecycling::Resource
show all
- Defined in:
- lib/open_recycling/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(root_node_singular: nil, root_node_plural: nil, api_url: nil, jwt_token: nil) ⇒ Resource
Returns a new instance of Resource.
7
8
9
10
11
12
|
# File 'lib/open_recycling/resource.rb', line 7
def initialize(root_node_singular: nil, root_node_plural: nil, api_url: nil, jwt_token: nil)
@api_url = api_url
@jwt_token = jwt_token
@root_node_singular = root_node_singular
@root_node_plural = root_node_plural
end
|
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
5
6
7
|
# File 'lib/open_recycling/resource.rb', line 5
def api_url
@api_url
end
|
#jwt_token ⇒ Object
Returns the value of attribute jwt_token.
5
6
7
|
# File 'lib/open_recycling/resource.rb', line 5
def jwt_token
@jwt_token
end
|
#root_node_plural ⇒ Object
Returns the value of attribute root_node_plural.
5
6
7
|
# File 'lib/open_recycling/resource.rb', line 5
def root_node_plural
@root_node_plural
end
|
#root_node_singular ⇒ Object
Returns the value of attribute root_node_singular.
5
6
7
|
# File 'lib/open_recycling/resource.rb', line 5
def root_node_singular
@root_node_singular
end
|
Class Method Details
.only(*methods) ⇒ Object
85
86
87
|
# File 'lib/open_recycling/resource.rb', line 85
def self.only(*methods)
@only = methods
end
|
.supported_methods ⇒ Object
89
90
91
|
# File 'lib/open_recycling/resource.rb', line 89
def self.supported_methods
@only ||= [:all, :find, :create, :update, :destroy]
end
|
Instance Method Details
#all(options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/open_recycling/resource.rb', line 14
def all(options = {})
ensure_supported_method(:all)
uri = URI(build_uri_with_params(api_url, options))
response = execute_request(Net::HTTP::Get, uri)
if response.is_a?(Net::HTTPSuccess)
parse_resources(response.body)
else
handle_error(response)
end
end
|
#create(attributes) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/open_recycling/resource.rb', line 40
def create(attributes)
ensure_supported_method(:create)
uri = URI(api_url)
response = execute_request(Net::HTTP::Post, uri, { root_node_singular => attributes }.to_json)
if response.is_a?(Net::HTTPSuccess)
parse_resource(response.body)
else
handle_error(response)
end
end
|
#destroy(id) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/open_recycling/resource.rb', line 66
def destroy(id)
ensure_supported_method(:destroy)
uri = URI("#{api_url}/#{id}")
response = execute_request(Net::HTTP::Delete, uri)
if response.is_a?(Net::HTTPSuccess)
true
else
handle_error(response)
end
end
|
#ensure_supported_method(method) ⇒ Object
79
80
81
82
83
|
# File 'lib/open_recycling/resource.rb', line 79
def ensure_supported_method(method)
return if self.class.supported_methods.include?(method)
raise NotImplementedError.new("Method `#{method}` is not supported for this resource")
end
|
#find(id) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/open_recycling/resource.rb', line 27
def find(id)
ensure_supported_method(:find)
uri = URI("#{api_url}/#{id}")
response = execute_request(Net::HTTP::Get, uri)
if response.is_a?(Net::HTTPSuccess)
parse_resource(response.body)
else
handle_error(response)
end
end
|
#update(id, attributes) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/open_recycling/resource.rb', line 53
def update(id, attributes)
ensure_supported_method(:update)
uri = URI("#{api_url}/#{id}")
response = execute_request(Net::HTTP::Patch, uri, { root_node_singular => attributes }.to_json)
if response.is_a?(Net::HTTPSuccess)
parse_resource(response.body)
else
handle_error(response)
end
end
|