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

#get_image(img_link) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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

#pathObject



47
48
49
50
51
52
53
# File 'lib/nokaya/workers.rb', line 47

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

#sanitize(str) ⇒ Object



55
56
57
58
# 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) ⇒ Object



11
12
13
14
15
16
17
# 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) ⇒ Object



19
20
21
# File 'lib/nokaya/workers.rb', line 19

def save_images object
  save_tuples(object)
end

#save_tuples(object) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

#timedObject



60
61
62
63
# 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