class Generator
Attributes
album_title[R]
artist_name[R]
artwork_file[R]
song_names[R]
Public Class Methods
generate(song_count = 5)
click to toggle source
# File lib/album_generator/generator.rb, line 8 def self.generate(song_count = 5) new(song_count).tap(&:generate) end
new(song_count)
click to toggle source
# File lib/album_generator/generator.rb, line 16 def initialize(song_count) @title_retry = 0 @name_retry = 0 @artwork_retry = 0 @song_retry = 0 @song_count = song_count @song_names = [] end
Public Instance Methods
generate()
click to toggle source
# File lib/album_generator/generator.rb, line 25 def generate get_album_title get_artist_name get_album_artwork get_song_names end
Private Instance Methods
fetch_url(url)
click to toggle source
# File lib/album_generator/generator.rb, line 77 def fetch_url(url) Nokogiri::HTML(open(url)) end
get_album_artwork()
click to toggle source
# File lib/album_generator/generator.rb, line 60 def get_album_artwork html = fetch_url("https://www.flickr.com/explore/interesting/7days") user_url = "https://www.flickr.com" + html.css(".Photo")[2].children.xpath("a").last.values[1] + "sizes/o" html2 = fetch_url(user_url) artwork_url = html2.css("#allsizes-photo").last.children.css("img").attribute("src").value image = MiniMagick::Image.open(artwork_url) image.combine_options do |b| b.resize "1600x1600!" end @artwork_file = "/tmp/artwork.jpg" image.write(artwork_file) rescue => e handle_exception(e, @artwork_retry += 1, @@artwork_retry_limit, :get_album_artwork) end
get_album_title()
click to toggle source
# File lib/album_generator/generator.rb, line 42 def get_album_title @album_title = random_quote rescue => e handle_exception(e, @title_retry += 1, @@title_retry_limit, :get_album_title) end
get_artist_name()
click to toggle source
# File lib/album_generator/generator.rb, line 52 def get_artist_name html = fetch_url("https://en.wikipedia.org/wiki/Special:Random") @artist_name = html.css("#firstHeading").last.text rescue => e handle_exception(e, @name_retry += 1, @@name_retry_limit, :get_artist_name) end
get_song_names()
click to toggle source
# File lib/album_generator/generator.rb, line 48 def get_song_names (1..@song_count).each {|i| @song_names << random_quote } end
handle_exception(error, retry_count, retry_limit, callback)
click to toggle source
# File lib/album_generator/generator.rb, line 81 def handle_exception(error, retry_count, retry_limit, callback) if retry_count > retry_limit raise GeneratorException.new "unable to perform #{callback}: #{error.message}" else send(callback) end end
random_quote()
click to toggle source
# File lib/album_generator/generator.rb, line 36 def random_quote html = fetch_url("http://www.quotationspage.com/random.php3") q = html.css(".quote").last.text.split("\s") q[(q.length - 5)..-1].map(&:capitalize).join(" ") end