class RapGenius::Song

Attributes

id[R]

Public Class Methods

find(id) click to toggle source
# File lib/rapgenius/song.rb, line 7
def self.find(id)
  self.new(id: id).tap { |song| song.document }
end
new(kwargs = {}) click to toggle source
# 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

Public Instance Methods

artist() click to toggle source
# 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() click to toggle source
# File lib/rapgenius/song.rb, line 54
def artists
  [artist] + featured_artists + producer_artists
end
concurrent_viewers() click to toggle source
# File lib/rapgenius/song.rb, line 91
def concurrent_viewers
  response["stats"]["concurrents"]
end
description() click to toggle source
# File lib/rapgenius/song.rb, line 62
def description
  @description ||= document["response"]["song"]["description"]["plain"]
end
hot?() click to toggle source
# File lib/rapgenius/song.rb, line 83
def hot?
  response["stats"]["hot"]
end
images() click to toggle source
# 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() click to toggle source
# File lib/rapgenius/song.rb, line 101
def lines
  @lines ||= response["lyrics"]["dom"]["children"].map do |node|
    parse_lines(node)
  end.flatten.compact
end
media() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/rapgenius/song.rb, line 79
def pyongs
  response["pyongs_count"]
end
response() click to toggle source
# File lib/rapgenius/song.rb, line 18
def response
  document["response"]["song"]
end
title() click to toggle source
# File lib/rapgenius/song.rb, line 58
def title
  @title ||= response["title"]
end
url() click to toggle source
# File lib/rapgenius/song.rb, line 40
def url
  response["url"]
end
views() click to toggle source
# File lib/rapgenius/song.rb, line 87
def views
  response["stats"]["pageviews"]
end

Private Instance Methods

keys_with_images() click to toggle source
# File lib/rapgenius/song.rb, line 130
def keys_with_images
  %w{featured_artists producer_artists primary_artist}
end
parse_lines(node) click to toggle source
# File lib/rapgenius/song.rb, line 109
def parse_lines(node)
  if node.is_a?(Array)
    node.map { |subnode| parse_lines(subnode) }
  elsif node.is_a?(String)
    Line.new(
      song: Song.new(id: @id),
      lyric: node
    )
  elsif node.is_a?(Hash) && node["tag"] == "p"
    parse_lines(node["children"])
  elsif node.is_a?(Hash) && node["tag"] == "a"
    Line.new(
      song: Song.new(id: @id),
      lyric: node["children"].select {|l| l.is_a? String }.join("\n"),
      id: node["data"]["id"]
    )
  else
    return
  end
end