]> git.r.bdr.sh - rbdr/lyricli.rb/blame_incremental - lib/lyricli/lyrics_engine.rb
Skeletons for the exceptions
[rbdr/lyricli.rb] / lib / lyricli / lyrics_engine.rb
... / ...
CommitLineData
1module Lyricli
2
3 # This class gets the lyrics according to a given artist and song name.
4 class LyricsEngine
5
6 include Util
7
8 # Starts a new instance of LyricsEngine
9 #
10 # @param [String] artist the artist
11 # @param [String] song the song to look for
12 def initialize(artist, song)
13 @provider = URI("http://lyrics.wikia.com/api.php?artist=#{sanitize_param artist}&song=#{sanitize_param song}&fmt=realjson")
14 end
15
16 # Asks Lyrics Wiki for the lyrics, also cleans up the output a little.
17 #
18 # @return [String] the lyrics
19 def get_lyrics
20 begin
21 response = Net::HTTP.get(@provider)
22 response = MultiJson.decode(response)
23
24 doc = Nokogiri::HTML(open(response['url']))
25 node = doc.search(".lyricbox").first
26 rescue
27 raise Lyricli::LyricsNotFoundException
28 end
29
30 node.search(".rtMatcher").each do |n|
31 n.remove
32 end
33
34 node.search("br").each do |br|
35 br.replace "\n"
36 end
37
38 node.inner_text
39 end
40 end
41end