Module: Allocine::WebCurb

Defined in:
lib/allocine/web/curb.rb

Instance Method Summary collapse

Instance Method Details

#decode_content(c) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/allocine/web/curb.rb', line 23

def decode_content(c)
  if c.header_str.match(/Content-Encoding: gzip/)
    begin
      gz =  Zlib::GzipReader.new(StringIO.new(c.body_str))
      xml = gz.read
      gz.close
    rescue Zlib::GzipFile::Error 
      # Maybe this is not gzipped?
      xml = c.body_str
    end
  elsif c.header_str.match(/Content-Encoding: deflate/)
    xml = Zlib::Inflate.inflate(c.body_str)
  else
    xml = c.body_str
  end
  xml
end

#download(urls) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/allocine/web/curb.rb', line 3

def download(urls)
  url_queue = [*urls]
  multi = Curl::Multi.new
  responses = {}
  url_queue.each do |url|
    easy = Curl::Easy.new(url) do |curl|
      curl.follow_location = true      
      curl.on_success do |c|
        responses[url] = decode_content(c)
      end
      curl.on_failure do |c, err|
        responses[url] = c.response_code
      end
    end
    multi.add(easy)
  end
  multi.perform
  urls.is_a?(String) ? responses.values.first : responses
end