Class: RapGenius::Artist

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/rapgenius/artist.rb

Constant Summary

Constants included from Client

Client::BASE_URL

Instance Attribute Summary collapse

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.



12
13
14
15
16
17
# File 'lib/rapgenius/artist.rb', line 12

def initialize(kwargs = {})
  @id = kwargs.delete(:id)
  @name = kwargs.delete(:name)
  @type = kwargs.delete(:type)
  self.url = "artists/#{@id}"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/rapgenius/artist.rb', line 6

def id
  @id
end

Class Method Details

.find(id) ⇒ Object



8
9
10
# File 'lib/rapgenius/artist.rb', line 8

def self.find(id)
  self.new(id: id).tap { |artist| artist.document }
end

Instance Method Details

#descriptionObject



35
36
37
38
39
# File 'lib/rapgenius/artist.rb', line 35

def description
  @description ||= response["description"]["dom"]["children"].map do |node|
    parse_description(node)
  end.flatten.join("")
end

#imageObject



27
28
29
# File 'lib/rapgenius/artist.rb', line 27

def image
  @image ||= response["image_url"]
end

#nameObject



23
24
25
# File 'lib/rapgenius/artist.rb', line 23

def name
  @name ||= response["name"]
end

#responseObject



19
20
21
# File 'lib/rapgenius/artist.rb', line 19

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rapgenius/artist.rb', line 44

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

#urlObject



31
32
33
# File 'lib/rapgenius/artist.rb', line 31

def url
  response["url"]
end