class Genius::Resource
Attributes
raw_response[R]
resource[R]
text_format[R]
Public Class Methods
default_headers()
click to toggle source
# File lib/genius/resource.rb, line 65 def self.default_headers { 'Authorization' => "Bearer #{Genius.access_token}", 'User-Agent' => "genius.rb v#{Genius::VERSION}" } end
default_params()
click to toggle source
# File lib/genius/resource.rb, line 59 def self.default_params { text_format: Genius.text_format } end
find(id, params: {}, headers: {})
click to toggle source
# File lib/genius/resource.rb, line 17 def self.find(id, params: {}, headers: {}) params = default_params.merge(params) headers = default_headers.merge(headers) new(http_get("/#{resource_name}s/#{id}", query: params, headers: headers), text_format: params[:text_format]) end
from_hash(attributes, text_format: Genius.text_format)
click to toggle source
# 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)
click to toggle source
# 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: {})
click to toggle source
# File lib/genius/resource.rb, line 46 def self.http_delete(path, headers: {}) response = delete(path, headers: headers) handle_response(response) end
http_get(path, query: {}, headers: {})
click to toggle source
# File lib/genius/resource.rb, line 31 def self.http_get(path, query: {}, headers: {}) response = get(path, query: query, headers: headers) handle_response(response) end
http_post(path, body: {}, headers: {})
click to toggle source
# File lib/genius/resource.rb, line 36 def self.http_post(path, body: {}, headers: {}) response = post(path, body: body, headers: headers) handle_response(response) end
http_put(path, body: {}, headers: {})
click to toggle source
# File lib/genius/resource.rb, line 41 def self.http_put(path, body: {}, headers: {}) response = put(path, body: body, headers: headers) handle_response(response) end
new(response, text_format: Genius.text_format, resource: nil)
click to toggle source
# 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
resource_name(resource_name = nil)
click to toggle source
# 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
Public Instance Methods
reload()
click to toggle source
# File lib/genius/resource.rb, line 27 def reload self.class.find(id, params: { text_format: text_format }) end
Private Instance Methods
formatted_attribute(attribute)
click to toggle source
Fetches an attribute that uses the API’s Text Formatting feature, which means that the attribute will be nested inside a key naming the format (plain/html/dom)
# File lib/genius/resource.rb, line 95 def formatted_attribute(attribute) return nil unless resource[attribute] resource[attribute][text_format] end
resource_name()
click to toggle source
# File lib/genius/resource.rb, line 89 def resource_name self.class.resource_name end