Class: Gbv::SongFetcher

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

Class Method Summary collapse

Class Method Details

.fetch_song(title) ⇒ Object

This, ladies and gentlemen, is weird, programatic code at its worst



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/song_fetcher.rb', line 8

def self.fetch_song(title)
  begin
    response = HTTParty.get(song_url(title))

    if response.body.include?("No data found")
      search_response = HTTParty.get(search_url(title))
      song_node       = Nokogiri::HTML(search_response).css("td a").find_all {|a| a['href'].include? "all=true" }.first
      song = Song.new(Nokogiri::HTML(HTTParty.get(song_url(song_node.inner_text))))
    else
      song = Song.new(Nokogiri::HTML(response))
    end

    song
  rescue Exception => e
    return "Something went wrong. This thing is finicky about spelling. I'm not a scientist."
  end
end