class Nokaya::Workers

Attributes

options[RW]

Public Class Methods

new(options) click to toggle source
# File lib/nokaya/workers.rb, line 7
def initialize options
  @options = options
end

Public Instance Methods

get_image(img_link) click to toggle source
# File lib/nokaya/workers.rb, line 37
def get_image img_link
  begin
    open(img_link).read
  rescue SocketError, SystemCallError
    abort(Status.no_cnx)
  rescue Exception
    abort(Status.no_can_do)
  end
end
path() click to toggle source
# File lib/nokaya/workers.rb, line 47
def path
  if options['output']
    options['output']
  else
    Dir.home + "/Downloads"
  end
end
sanitize(str) click to toggle source
# File lib/nokaya/workers.rb, line 55
def sanitize str
  reg = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/
  str.downcase.strip.gsub(reg, '_').split(' ').join('_').squeeze('_') unless str.nil? || str.empty?
end
save(object) click to toggle source
# File lib/nokaya/workers.rb, line 11
def save object
  if object.urls.empty?
    abort(Status.no_can_do)
  else
    save_images(object)
  end
end
save_images(object) click to toggle source
# File lib/nokaya/workers.rb, line 19
def save_images object
  save_tuples(object)
end
save_tuples(object) click to toggle source
# File lib/nokaya/workers.rb, line 23
def save_tuples object
  begin
    tuples = object.urls.zip(object.filenames)
    Dir.mkdir(object.path) unless Dir.exist?(object.path)
    tuples.each do |url, name|
      f = File.new("#{object.path}/#{name}", "wb")
       f.puts(get_image(url))
      f.close
    end
  rescue Errno::EACCES
    abort(Status.no_access)
  end
end
timed() click to toggle source
# File lib/nokaya/workers.rb, line 60
def timed
  t = Time.now
  "#{t.year}#{'%02d' % t.month}#{'%02d' % t.day}#{'%02d' % t.hour}#{'%02d' % t.min}#{'%02d' % t.sec}"
end