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, Client::DOM_TEXT_FORMAT, Client::PLAIN_TEXT_FORMAT

Instance Attribute Summary collapse

Attributes included from Client

#text_format

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Client

#document, #fetch, #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



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

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

#descriptionObject



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

def description
  @description ||= document["response"]["song"]["description"]["plain"]
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)


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

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

#imagesObject



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

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



101
102
103
104
105
# File 'lib/rapgenius/song.rb', line 101

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

#mediaObject



95
96
97
98
99
# File 'lib/rapgenius/song.rb', line 95

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



79
80
81
# File 'lib/rapgenius/song.rb', line 79

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



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

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