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.



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

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.



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

def id
  @id
end

Class Method Details

.find(id) ⇒ Object



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

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

Instance Method Details

#artistObject



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

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

#artistsObject



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

def artists
  [artist] + featured_artists + producer_artists
end

#concurrent_viewersObject



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

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

#descriptionObject



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

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


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

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)


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

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

#imagesObject



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

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



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

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

#mediaObject



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

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

#producer_artistsObject



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

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

#pyongsObject



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

def pyongs
  response["pyongs_count"]
end

#responseObject



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

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

#titleObject



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

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

#urlObject



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

def url
  response["url"]
end

#viewsObject



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

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