]>
Commit | Line | Data |
---|---|---|
823e558b | 1 | module Lyricli |
f2ec7254 BB |
2 | |
3 | # This class gets the lyrics according to a given artist and song name. | |
823e558b BB |
4 | class LyricsEngine |
5 | ||
6 | include Util | |
7 | ||
f2ec7254 BB |
8 | # Starts a new instance of LyricsEngine |
9 | # | |
10 | # @param [String] artist the artist | |
11 | # @param [String] song the song to look for | |
823e558b BB |
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 | ||
f2ec7254 BB |
16 | # Asks Lyrics Wiki for the lyrics, also cleans up the output a little. |
17 | # | |
18 | # @return [String] the lyrics | |
823e558b BB |
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 | |
823e558b BB |
40 | end |
41 | end |