Sha256: 315251a781c9678f957190ab4df6da707400954951f2633cf111305ac59187bf

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8
module Nokaya
  class Workers

    attr_accessor :options

    def initialize options
      @options = options
    end

    def save object
      if object.urls.empty?
        abort(Status.no_can_do)
      else
        save_images(object)
      end
    end

    def save_images object
      save_tuples(object)
    end

    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

    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

    def path
      if options['output']
        options['output']
      else
        Dir.home + "/Downloads"
      end
    end

    def sanitize str
      reg = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/
      str.downcase.strip.gsub(reg, '_').split(' ').join('_').squeeze('_') unless str.nil? || str.empty?
    end

    def timed
      t = Time.now
      "#{t.year}#{'%02d' % t.month}#{'%02d' % t.day}#{'%02d' % t.hour}#{'%02d' % t.min}#{'%02d' % t.sec}"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nokaya-0.1.6 lib/nokaya/workers.rb
nokaya-0.1.5 lib/nokaya/workers.rb
nokaya-0.1.4 lib/nokaya/workers.rb
nokaya-0.1.3 lib/nokaya/workers.rb