Module: Percy::Capybara::HttpFetcher

Defined in:
lib/percy/capybara/httpfetcher.rb

Defined Under Namespace

Classes: Response

Class Method Summary collapse

Class Method Details

.fetch(url) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/percy/capybara/httpfetcher.rb', line 9

def self.fetch(url)
  tempfile = Tempfile.new('percy-capybara-fetch')
  temppath = tempfile.path

  # Close and delete the tempfile, we just wanted the name. Also, we use the existence of the
  # file as a signal below.
  tempfile.close
  tempfile.unlink

  # Use curl as a magical subprocess weapon which escapes this Ruby sandbox and is not
  # influenced by any HTTP middleware/restrictions. This helps us avoid causing lots of
  # problems for people using gems like VCR/WebMock. We also disable certificate checking
  # because, as odd as that is, it's the default state for Selenium Firefox and others.
  output = `curl --insecure -v -o #{temppath} "#{url.shellescape}" 2>&1`
  content_type = output.match(/< Content-Type:(.*)/i)
  content_type = content_type[1].strip if content_type

  if File.exist?(temppath)
    response = Percy::Capybara::HttpFetcher::Response.new(File.read(temppath), content_type)
    # We've broken the tempfile so it won't get deleted when garbage collected. Delete!
    File.delete(temppath)
    return if response.body == ''
    response
  end
end