Class: Wiki

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki/wiki.rb

Overview

Base class to fetch different lyrics sites

Direct Known Subclasses

AZLyrics, Genius

Instance Method Summary collapse

Instance Method Details

#fetch(uri_str, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



33
34
35
36
# 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) ⇒ Object



29
30
31
# File 'lib/wiki/wiki.rb', line 29

def set_lyrics(mp3, lyrics)
  mp3.tag2.USLT = lyrics
end