Class: RapGenius::Song

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/rapgenius/song.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 = {}) ⇒ Song

Returns a new instance of Song.



11
12
13
14
15
16
# File 'lib/rapgenius/song.rb', line 11

def initialize(kwargs = {})
  @id = kwargs.delete(:id)
  @artist = kwargs.delete(:artist)
  @title  = kwargs.delete(:title)
  self.url = "songs/#{@id}"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/rapgenius/song.rb', line 5

def id
  @id
end

Class Method Details

.find(id) ⇒ Object



7
8
9
# File 'lib/rapgenius/song.rb', line 7

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

Instance Method Details

#artistObject



22
23
24
25
26
27
28
# File 'lib/rapgenius/song.rb', line 22

def artist
  @artist ||= Artist.new(
    name: response["primary_artist"]["name"],
    id: response["primary_artist"]["id"],
    type: :primary
  )
end

#artistsObject



54
55
56
# File 'lib/rapgenius/song.rb', line 54

def artists
  [artist] + featured_artists + producer_artists
end

#concurrent_viewersObject



93
94
95
# File 'lib/rapgenius/song.rb', line 93

def concurrent_viewers
  response["stats"]["concurrents"]
end

#descriptionObject



62
63
64
65
66
# File 'lib/rapgenius/song.rb', line 62

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


30
31
32
33
34
35
36
37
38
# File 'lib/rapgenius/song.rb', line 30

def featured_artists
  @featured_artists ||= response["featured_artists"].map do |artist|
    Artist.new(
      name: artist["name"],
      id: artist["id"],
      type: :featured
    )
  end
end

#hot?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/rapgenius/song.rb', line 85

def hot?
  response["stats"]["hot"]
end

#imagesObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rapgenius/song.rb', line 68

def images
  @images ||= keys_with_images.map do |key|
    node = response[key]
    if node.is_a? Array
      node.map { |subnode| subnode["image_url"] }
    elsif node.is_a? Hash
      node["image_url"]
    else
      return
    end
  end.flatten
end

#linesObject



103
104
105
106
107
# File 'lib/rapgenius/song.rb', line 103

def lines
  @lines ||= response["lyrics"]["dom"]["children"].map do |node|
    parse_lines(node)
  end.flatten.compact
end

#mediaObject



97
98
99
100
101
# File 'lib/rapgenius/song.rb', line 97

def media
  response["media"].map do |m| 
    Media.new(type: m["type"], provider: m["provider"], url: m["url"])
  end
end

#producer_artistsObject



44
45
46
47
48
49
50
51
52
# File 'lib/rapgenius/song.rb', line 44

def producer_artists
  @producer_artists ||= response["producer_artists"].map do |artist|
    Artist.new(
      name: artist["name"],
      id: artist["id"],
      type: :producer
    )
  end
end

#pyongsObject



81
82
83
# File 'lib/rapgenius/song.rb', line 81

def pyongs
  response["pyongs_count"]
end

#responseObject



18
19
20
# File 'lib/rapgenius/song.rb', line 18

def response
  document["response"]["song"]
end

#titleObject



58
59
60
# File 'lib/rapgenius/song.rb', line 58

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

#urlObject



40
41
42
# File 'lib/rapgenius/song.rb', line 40

def url
  response["url"]
end

#viewsObject



89
90
91
# File 'lib/rapgenius/song.rb', line 89

def views
  response["stats"]["pageviews"]
end