Class: Genius::Resource
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/genius/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.default_headers ⇒ Object
-
.default_params ⇒ Object
-
.find(id, params: {}, headers: {}) ⇒ Object
-
.from_hash(attributes, text_format: Genius.text_format) ⇒ Object
-
.handle_response(response) ⇒ Object
-
.http_delete(path, headers: {}) ⇒ Object
-
.http_get(path, query: {}, headers: {}) ⇒ Object
-
.http_post(path, body: {}, headers: {}) ⇒ Object
-
.http_put(path, body: {}, headers: {}) ⇒ Object
-
.resource_name(resource_name = nil) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(response, text_format: Genius.text_format, resource: nil) ⇒ Resource
Returns a new instance of Resource.
72
73
74
75
76
77
78
|
# File 'lib/genius/resource.rb', line 72
def initialize(response, text_format: Genius.text_format, resource: nil)
@raw_response = response
@resource = resource || response.parsed_response["response"][resource_name]
@text_format = text_format
parse_resource!
end
|
Instance Attribute Details
#raw_response ⇒ Object
Returns the value of attribute raw_response.
10
11
12
|
# File 'lib/genius/resource.rb', line 10
def raw_response
@raw_response
end
|
#resource ⇒ Object
Returns the value of attribute resource.
10
11
12
|
# File 'lib/genius/resource.rb', line 10
def resource
@resource
end
|
Class Method Details
65
66
67
68
69
70
|
# File 'lib/genius/resource.rb', line 65
def self.
{
'Authorization' => "Bearer #{Genius.access_token}",
'User-Agent' => "genius.rb v#{Genius::VERSION}"
}
end
|
.default_params ⇒ Object
59
60
61
62
63
|
# File 'lib/genius/resource.rb', line 59
def self.default_params
{
text_format: Genius.text_format
}
end
|
.find(id, params: {}, headers: {}) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/genius/resource.rb', line 17
def self.find(id, params: {}, headers: {})
params = default_params.merge(params)
= .merge()
new(http_get("/#{resource_name}s/#{id}",
query: params,
headers: ),
text_format: params[:text_format])
end
|
.from_hash(attributes, text_format: Genius.text_format) ⇒ Object
80
81
82
83
|
# File 'lib/genius/resource.rb', line 80
def self.from_hash(attributes, text_format: Genius.text_format)
return nil unless attributes
new(nil, text_format: text_format, resource: attributes)
end
|
.handle_response(response) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/genius/resource.rb', line 51
def self.handle_response(response)
case response.code
when 404 then raise NotFoundError
when 401, 403 then raise AuthenticationError
else response
end
end
|
.http_delete(path, headers: {}) ⇒ Object
46
47
48
49
|
# File 'lib/genius/resource.rb', line 46
def self.http_delete(path, headers: {})
response = delete(path, headers: )
handle_response(response)
end
|
.http_get(path, query: {}, headers: {}) ⇒ Object
31
32
33
34
|
# File 'lib/genius/resource.rb', line 31
def self.http_get(path, query: {}, headers: {})
response = get(path, query: query, headers: )
handle_response(response)
end
|
.http_post(path, body: {}, headers: {}) ⇒ Object
36
37
38
39
|
# File 'lib/genius/resource.rb', line 36
def self.http_post(path, body: {}, headers: {})
response = post(path, body: body, headers: )
handle_response(response)
end
|
.http_put(path, body: {}, headers: {}) ⇒ Object
41
42
43
44
|
# File 'lib/genius/resource.rb', line 41
def self.http_put(path, body: {}, headers: {})
response = put(path, body: body, headers: )
handle_response(response)
end
|
.resource_name(resource_name = nil) ⇒ Object
12
13
14
15
|
# File 'lib/genius/resource.rb', line 12
def self.resource_name(resource_name = nil)
@resource_name = resource_name if resource_name
@resource_name || name.downcase.split("::").last
end
|
Instance Method Details
#reload ⇒ Object
27
28
29
|
# File 'lib/genius/resource.rb', line 27
def reload
self.class.find(id, params: { text_format: text_format })
end
|