Class: Genius::Resource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/genius/resource.rb

Direct Known Subclasses

Account, Annotation, Artist, Referent, Song, WebPage

Instance Attribute Summary collapse

Class Method Summary collapse

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_responseObject (readonly)

Returns the value of attribute raw_response.



10
11
12
# File 'lib/genius/resource.rb', line 10

def raw_response
  @raw_response
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/genius/resource.rb', line 10

def resource
  @resource
end

Class Method Details

.default_headersObject



65
66
67
68
69
70
# 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_paramsObject



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)
  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) ⇒ 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: 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: 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: 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: 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

#reloadObject



27
28
29
# File 'lib/genius/resource.rb', line 27

def reload
  self.class.find(id, params: { text_format: text_format })
end