class Wiki

Base class to fetch different lyrics sites

Constants

USLT

Public Instance Methods

fetch(uri_str, limit = 10) click to toggle source
# File lib/wiki/wiki.rb, line 9
def fetch(uri_str, limit = 10)
  raise ArgumentError, 'The wiki site is redirecting too much, aborting...' if limit.zero?

  uri = prepare_url(uri_str)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    req = Net::HTTP::Get.new(uri.path)
    begin
      http.request(req)
    rescue EOFError
      fetch(uri_str, limit - 1)
    end
  end
  case response
  when Net::HTTPRedirection
    fetch(response['location'], limit - 1)
  else
    response
  end
end
prepare_url(uri_str) click to toggle source
# File lib/wiki/wiki.rb, line 33
def prepare_url(uri_str)
  bom_fixed_uri = uri_str.gsub('%EF%BB%BF', '') # fix BOM
  URI.parse(bom_fixed_uri)
end
set_lyrics(mp3, lyrics) click to toggle source
# File lib/wiki/wiki.rb, line 29
def set_lyrics(mp3, lyrics)
  mp3.tag2.USLT = lyrics
end