Class: RapGenius::Song
- Inherits:
-
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
#id ⇒ Object
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
#artist ⇒ Object
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
|
#artists ⇒ Object
54
55
56
|
# File 'lib/rapgenius/song.rb', line 54
def artists
[artist] + featured_artists + producer_artists
end
|
#concurrent_viewers ⇒ Object
91
92
93
|
# File 'lib/rapgenius/song.rb', line 91
def concurrent_viewers
response["stats"]["concurrents"]
end
|
#description ⇒ Object
62
63
64
|
# File 'lib/rapgenius/song.rb', line 62
def description
@description ||= document["response"]["song"]["description"]["plain"]
end
|
#featured_artists ⇒ Object
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
83
84
85
|
# File 'lib/rapgenius/song.rb', line 83
def hot?
response["stats"]["hot"]
end
|
#images ⇒ Object
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
|
#lines ⇒ Object
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
|
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_artists ⇒ Object
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
|
#pyongs ⇒ Object
79
80
81
|
# File 'lib/rapgenius/song.rb', line 79
def pyongs
response["pyongs_count"]
end
|
#response ⇒ Object
18
19
20
|
# File 'lib/rapgenius/song.rb', line 18
def response
document["response"]["song"]
end
|
#title ⇒ Object
58
59
60
|
# File 'lib/rapgenius/song.rb', line 58
def title
@title ||= response["title"]
end
|
#url ⇒ Object
40
41
42
|
# File 'lib/rapgenius/song.rb', line 40
def url
response["url"]
end
|
#views ⇒ Object
87
88
89
|
# File 'lib/rapgenius/song.rb', line 87
def views
response["stats"]["pageviews"]
end
|