Class: RapGenius::Song
- Inherits:
-
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
#id ⇒ Object
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
#artist ⇒ Object
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
|
#artists ⇒ Object
55
56
57
|
# File 'lib/rapgenius/song.rb', line 55
def artists
[artist] + featured_artists + producer_artists
end
|
#concurrent_viewers ⇒ Object
94
95
96
|
# File 'lib/rapgenius/song.rb', line 94
def concurrent_viewers
response["stats"]["concurrents"]
end
|
#description ⇒ Object
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
|
#featured_artists ⇒ Object
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
86
87
88
|
# File 'lib/rapgenius/song.rb', line 86
def hot?
response["stats"]["hot"]
end
|
#images ⇒ Object
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
|
#lines ⇒ Object
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
|
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_artists ⇒ Object
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
|
#pyongs ⇒ Object
82
83
84
|
# File 'lib/rapgenius/song.rb', line 82
def pyongs
response["pyongs_count"]
end
|
#response ⇒ Object
19
20
21
|
# File 'lib/rapgenius/song.rb', line 19
def response
document["response"]["song"]
end
|
#title ⇒ Object
59
60
61
|
# File 'lib/rapgenius/song.rb', line 59
def title
@title ||= response["title"]
end
|
#url ⇒ Object
41
42
43
|
# File 'lib/rapgenius/song.rb', line 41
def url
response["url"]
end
|
#views ⇒ Object
90
91
92
|
# File 'lib/rapgenius/song.rb', line 90
def views
response["stats"]["pageviews"]
end
|