require 'nokogiri'
require 'open-uri'
require 'openssl'
require 'timeout'
# The main GrabMoot Class
class GrabMoot
  # GrabMoot will be able to download all images from any thread passed in
  #
  # Example:
  #   >> GrabMoot.down_thread('http://boards.4chan.org/b/res/404803253')
  #   => image...
  #
  # Arguments:
  #   url: (String)
  def self.down_thread(url, timeout_var = 120)
    timeout_var = nil if timeout_var == 0
    timeout(timeout_var) do
      doc = Nokogiri::HTML(open(url,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))
      doc.xpath("//a[@class='fileThumb']").each do |link|
        img_url = "http:#{link.attributes['href'].to_s}"
        open(img_url) do |image|
          unless File.exists? File.basename(img_url)
            print '+'
            File.open(File.basename(img_url),"wb") {|file| file.write image.read}
          else
            print '.'
          end
        end
      end
    end
  end
end