Class: RapGenius::Line

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/rapgenius/line.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) ⇒ Line

Returns a new instance of Line.



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

def initialize(kwargs)
  @id = kwargs.delete(:id)
  @song = kwargs.delete(:song)
  @lyric = kwargs.delete(:lyric)
  self.url = "referents/#{@id}" if @id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.find(id) ⇒ Object



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

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

Instance Method Details

#annotated?Boolean Also known as: explained?

Returns:

  • (Boolean)


31
32
33
# File 'lib/rapgenius/line.rb', line 31

def annotated?
  !!@id
end

#explanationsObject Also known as: annotations

A line can have multiple annotations, usually if it has a community one and a verified one. Ideally, these would be encapsulated into an Annotation class, but I don’t have time for now.



40
41
42
43
44
45
46
47
# File 'lib/rapgenius/line.rb', line 40

def explanations
  return nil unless @id
  @explanation ||= response["annotations"].map do |annotation|
    annotation["body"]["dom"]["children"].map do |node|
      parse_description(node)
    end.join("")
  end.flatten
end

#lyricObject



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

def lyric
  if @id
    @lyric ||= response["fragment"]
  else
    @lyric
  end
end

#responseObject



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

def response
  return nil unless @id
  document["response"]["referent"]
end

#songObject



51
52
53
54
55
56
57
# File 'lib/rapgenius/line.rb', line 51

def song
  if @id
    @song ||= Song.find(response['song_id'])
  else
    @song
  end
end