Class: Nokaya::Workers

Inherits:
Object
  • Object
show all
Defined in:
lib/nokaya/workers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Workers

Returns a new instance of Workers.



7
8
9
# File 'lib/nokaya/workers.rb', line 7

def initialize options
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/nokaya/workers.rb', line 5

def options
  @options
end

Instance Method Details

#check_args(args) ⇒ Object



72
73
74
75
# File 'lib/nokaya/workers.rb', line 72

def check_args args
  abort Status.no_url if args.empty?
  return args
end

#get_image(img_link) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/nokaya/workers.rb', line 49

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

#pathObject



59
60
61
62
63
64
65
# File 'lib/nokaya/workers.rb', line 59

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

#sanitize(str) ⇒ Object



67
68
69
70
# File 'lib/nokaya/workers.rb', line 67

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

#save(object) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/nokaya/workers.rb', line 11

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

#save_images(object) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/nokaya/workers.rb', line 19

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

#save_tuples(object) ⇒ Object

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



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nokaya/workers.rb', line 35

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

#timedObject



77
78
79
80
# File 'lib/nokaya/workers.rb', line 77

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