Class: Lyricli::LyricsEngine

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/lyricli/lyrics_engine.rb

Overview

This class gets the lyrics according to a given artist and song name.

Instance Method Summary (collapse)

Methods included from Util

#camelize, #parse_class, #sanitize_param

Constructor Details

- (LyricsEngine) initialize(artist, song)

Starts a new instance of LyricsEngine

Parameters:

  • artist (String)

    the artist

  • song (String)

    the song to look for



12
13
14
# File 'lib/lyricli/lyrics_engine.rb', line 12

def initialize(artist, song)
  @provider = URI("http://lyrics.wikia.com/api.php?artist=#{sanitize_param artist}&song=#{sanitize_param song}&fmt=realjson")
end

Instance Method Details

- (String) get_lyrics

Asks Lyrics Wiki for the lyrics, also cleans up the output a little.

Returns:

  • (String)

    the lyrics



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lyricli/lyrics_engine.rb', line 19

def get_lyrics
  begin
    response = Net::HTTP.get(@provider)
    response = MultiJson.decode(response)

    doc = Nokogiri::HTML(open(response['url']))
    node = doc.search(".lyricbox").first
  rescue
    raise Lyricli::LyricsNotFoundException
  end

  node.search(".rtMatcher").each do |n|
    n.remove
  end

  node.search("br").each do |br|
    br.replace "\n"
  end

  node.inner_text
end