module Lyricfy
  class MetroLyrics < Lyricfy::LyricProvider
    include URIHelper

    def initialize(parameters)
      super(parameters)
      self.base_url = "http://m.metrolyrics.com/"
      self.url = URI.escape(self.base_url + format_parameters)
    end

    def search
      begin
        html = Nokogiri::HTML(open(url))
        lyricbox = html.css('p.lyricsbody').first

        # Removing ads
        if ads = lyricbox.css('.lyrics-ringtone')
          ads.each do |matcher|
            matcher.remove
          end
        end

        if credits = lyricbox.css('span')
          credits.each do |span|
            span.remove
          end
        end

        lyricbox.children.to_html
      rescue OpenURI::HTTPError
        nil
      end
    end

    private
    def prepare_parameter(parameter)
      parameter.downcase.split(' ').map { |w| w.gsub(/\W/, '') }.join('-')
    end

    def format_parameters
      artist_name = prepare_parameter(self.parameters[:artist_name])
      song_name = prepare_parameter(self.parameters[:song_name])
      "#{song_name}-lyrics-#{artist_name}"
    end
  end
end
module Lyricfy
  class MetroLyrics < Lyricfy::LyricProvider
    include URIHelper

    def initialize(parameters)
      super(parameters)
      self.base_url = "http://m.metrolyrics.com/"
      self.url = URI.escape(self.base_url + format_parameters)
    end

    def search
      if data =  super
        html = Nokogiri::HTML(data)
        container = html.css('p.gnlyricsbody').first
        elements = container.children.to_a
        paragraphs = elements.select { |ele| ele.text? }
        paragraphs.map! { |paragraph| paragraph.text.strip.chomp if paragraph.text != "\n" }.reject! { |ele| ele.empty? }
      end
    end

    private
    def prepare_parameter(parameter)
      parameter.downcase.split(' ').map { |w| w.gsub(/\W/, '') }.join('-')
    end

    def format_parameters
      artist_name = prepare_parameter(self.parameters[:artist_name])
      song_name = prepare_parameter(self.parameters[:song_name])
      "#{song_name}-lyrics-#{artist_name}"
    end
  end
end
Change Lyricfy::MetroLyrics to pass tests
Change Lyricfy::MetroLyrics to pass tests
