Class: ImageDownloader::Process

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

Constant Summary collapse

DEFAULT_USER_AGENT =
'Mozilla/5.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, path) ⇒ Process

Returns a new instance of Process.



31
32
33
34
35
36
# File 'lib/image_downloader.rb', line 31

def initialize(url, path)
  @argument = Arguments.new(url, path)
  @argument.check
  @argument.normalize
  @images = []
end

Instance Attribute Details

#argumentObject

Returns the value of attribute argument.



27
28
29
# File 'lib/image_downloader.rb', line 27

def argument
  @argument
end

#imagesObject

Returns the value of attribute images.



27
28
29
# File 'lib/image_downloader.rb', line 27

def images
  @images
end

Instance Method Details

#download(*args) ⇒ Object

:(parallel|consequentially) :(parallel|consequentially) => true :user_agent => ‘Mozilla/5.0’



67
68
69
70
71
72
73
74
75
76
# File 'lib/image_downloader.rb', line 67

def download(*args)
  user_agent = args_hash_and_contain(args, :user_agent) || DEFAULT_USER_AGENT
  if !args.first || args.first == :parallel || args_hash_and_contain(args, :parallel)
    Download.parallel(self.images, user_agent)
  elsif args.first == :consequentially || args_hash_and_contain(args, :consequentially)
    Download.consequentially(self.images, user_agent)
  else
    p "Not correct argument for download method"
  end
end

#parse(h = {:collect => {}, :ignore_without => {}}) ⇒ Object

:any_looks_like_image => true :regexp => /[^‘“]+.jpg/i :ignore_without => => true Nokogiri gem is required: :collect => => true, :(img_src|a_href|style_url|link_icon) => true :user_agent => ’Mozilla/5.0’



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/image_downloader.rb', line 44

def parse(h={:collect => {}, :ignore_without => {}})
  self.rebuild_collect_hash(h)

  parser = Parser.new(self.argument.url, h[:user_agent] || DEFAULT_USER_AGENT)
  if h[:any_looks_like_image]
    parser.get_content_raw
    parser.get_images_raw(self.argument.path, h[:collect])
  elsif h[:regexp]
    parser.get_content_raw
    parser.get_images_regexp(self.argument.path, h[:regexp])
  else
    parser.get_content
    parser.get_images(self.argument.path, h[:collect])
  end

  parser.ignore_file_without(h[:ignore_without])

  self.images = parser.images
end