Sha256: e1855332ae5ee3ada68dbfce7e237d9f3eb8d570dc6b961291c58ac6125b1d81

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
module Nokaya
  class Workers

    attr_accessor :options

    def initialize options
      @options = options
    end

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

    def save_images object
      # if object.file_name.empty?
        # save_files(object)
      # else
        save_tuples(object)
      # end
    end

    # def save_files object
    #   object.image_url.each do |url|
    #     f = File.new("#{object.path}/#{file_name(object)}", "wb")
    #      f.puts(get_image(url))
    #     f.close
    #   end
    # end

    def save_tuples object
      begin
        tuples = object.image_url.zip(object.file_name)
        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('_')
    end

    def check_args args
      abort Status.no_url if args.empty?
      return args
    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

2 entries across 2 versions & 1 rubygems

Version Path
nokaya-0.1.2 lib/nokaya/workers.rb
nokaya-0.1.1 lib/nokaya/workers.rb