Class: RemoteImageFetch

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_image_fetch.rb,
lib/remote_image_fetch/version.rb,
lib/remote_image_fetch/curl_wrap.rb,
lib/remote_image_fetch/curl_error.rb,
lib/remote_image_fetch/uri_restrictions.rb

Overview

RemoteImageFetch

Defined Under Namespace

Classes: CurlError, CurlWrap, UriRestrictions

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RemoteImageFetch

Returns a new instance of RemoteImageFetch.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/remote_image_fetch.rb', line 13

def initialize(options = {})
  @max_parallel  = options[:max_parallel]  || 16
  @max_redirects = options[:max_redirects] || 4

  @uri_restrictions = RemoteImageFetch::UriRestrictions.new(options)

  @change_flagged = false
  @core = Curl::Multi.new
  @downloads = []

  @success  = 0
  @failures = 0
end

Instance Attribute Details

#failuresObject (readonly)

Returns the value of attribute failures.



11
12
13
# File 'lib/remote_image_fetch.rb', line 11

def failures
  @failures
end

#successObject (readonly)

Returns the value of attribute success.



11
12
13
# File 'lib/remote_image_fetch.rb', line 11

def success
  @success
end

Instance Method Details

#check_and_redirect(curl) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/remote_image_fetch.rb', line 56

def check_and_redirect(curl)
  raise "Too many redirects for #{curl.url}" if curl.redirects > max_redirects
  check_url(curl.redirect_url)
  curl.url = curl.redirect_url
  core.add(curl)
rescue => e
  report_failed(curl, e)
end

#download(url, *args, &callback) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/remote_image_fetch.rb', line 27

def download(url, *args, &callback)
  curl = CurlWrap.new(url)
  curl.setup self
  curl.args = args
  curl.callback = callback
  downloads.push(curl)
  curl
end

#report_done(curl, *_args) ⇒ Object



41
42
43
44
45
46
# File 'lib/remote_image_fetch.rb', line 41

def report_done(curl, *_args)
  @change_flagged = true
  @success += 1
  curl.report(DownloadResult::OK.new(curl))
  auto_queue!
end

#report_failed(curl, error = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/remote_image_fetch.rb', line 48

def report_failed(curl, error = nil)
  @change_flagged = true
  @failures += 1
  error ||= CurlError.new(curl)
  curl.report(DownloadResult::Fail.new(curl, error))
  auto_queue!
end

#runObject



36
37
38
39
# File 'lib/remote_image_fetch.rb', line 36

def run
  auto_queue!
  core.perform
end