X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli.rb/blobdiff_plain/823e558b5cd2ec219d0fc7226c54f2ee7ad807d2..b434f05e58de158e651fbc83cead322f6a864d36:/lib/lyricli/lyrics_engine.rb?ds=sidebyside diff --git a/lib/lyricli/lyrics_engine.rb b/lib/lyricli/lyrics_engine.rb index 65e36db..1291d41 100644 --- a/lib/lyricli/lyrics_engine.rb +++ b/lib/lyricli/lyrics_engine.rb @@ -1,12 +1,21 @@ module Lyricli + + # This class gets the lyrics according to a given artist and song name. class LyricsEngine include Util + # Starts a new instance of LyricsEngine + # + # @param [String] artist the artist + # @param [String] song the song to look for def initialize(artist, song) @provider = URI("http://lyrics.wikia.com/api.php?artist=#{sanitize_param artist}&song=#{sanitize_param song}&fmt=realjson") end + # Asks Lyrics Wiki for the lyrics, also cleans up the output a little. + # + # @return [String] the lyrics def get_lyrics begin response = Net::HTTP.get(@provider) @@ -15,19 +24,22 @@ module Lyricli doc = Nokogiri::HTML(open(response['url'])) node = doc.search(".lyricbox").first rescue - raise Lyricli::LyricsNotFoundException + raise Exceptions::LyricsNotFoundError end node.search(".rtMatcher").each do |n| n.remove end + node.search("script").each do |n| + n.remove + end + node.search("br").each do |br| br.replace "\n" end - node.inner_text + node.inner_text.gsub(/\s+$/, "") end - end end