Class: RapGenius::Artist
- Inherits:
-
Object
show all
- Includes:
- Client
- Defined in:
- lib/rapgenius/artist.rb
Constant Summary
Constants included
from Client
Client::BASE_URL
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Client
#document, #fetch, #parse_description, #url=
Constructor Details
#initialize(kwargs = {}) ⇒ Artist
Returns a new instance of Artist.
10
11
12
13
14
15
|
# File 'lib/rapgenius/artist.rb', line 10
def initialize(kwargs = {})
@id = kwargs.delete(:id)
@name = kwargs.delete(:name)
@type = kwargs.delete(:type)
self.url = "artists/#{@id}"
end
|
Class Method Details
.find(id) ⇒ Object
6
7
8
|
# File 'lib/rapgenius/artist.rb', line 6
def self.find(id)
self.new(id: id).tap { |artist| artist.document }
end
|
Instance Method Details
#description ⇒ Object
33
34
35
36
37
|
# File 'lib/rapgenius/artist.rb', line 33
def description
@description ||= response["description"]["dom"]["children"].map do |node|
parse_description(node)
end.flatten.join("")
end
|
#image ⇒ Object
25
26
27
|
# File 'lib/rapgenius/artist.rb', line 25
def image
@image ||= response["image_url"]
end
|
#name ⇒ Object
21
22
23
|
# File 'lib/rapgenius/artist.rb', line 21
def name
@name ||= response["name"]
end
|
#response ⇒ Object
17
18
19
|
# File 'lib/rapgenius/artist.rb', line 17
def response
document["response"]["artist"]
end
|
#songs(options = {page: 1}) ⇒ Object
You seem to be able to load 25 songs at a time for an artist. I haven’t found a way to vary the number you get back from the query, but you can paginate through in blocks of 25 songs.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/rapgenius/artist.rb', line 42
def songs(options = {page: 1})
songs_url = "/artists/#{@id}/songs/?page=#{options[:page]}"
fetch(songs_url)["response"]["songs"].map do |song|
Song.new(
artist: Artist.new(
name: song["primary_artist"]["name"],
id: song["primary_artist"]["id"],
type: :primary
),
title: song["title"],
id: song["id"]
)
end
end
|
#url ⇒ Object
29
30
31
|
# File 'lib/rapgenius/artist.rb', line 29
def url
response["url"]
end
|