Module: FaviconParty::HTTPClient

Extended by:
HTTPClient
Includes:
Utils
Included in:
HTTPClient
Defined in:
lib/favicon_party/http_client.rb

Overview

For now, wrap command-line curl rather than using net/http or open-uri because of easier/more reliable SSL handling

Constant Summary collapse

TIMEOUT =
5

Instance Method Summary collapse

Methods included from Utils

#encode_utf8, #get_mime_type, #prefix_url, #with_temp_data_file

Instance Method Details

#bin_get(url) ⇒ Object

Get binary data from url and ignore errors



34
35
36
# File 'lib/favicon_party/http_client.rb', line 34

def bin_get(url)
  `#{curl_get_cmd(url)} 2>/dev/null`
end

#build_curl_cmd(url, flags = "") ⇒ Object



43
44
45
# File 'lib/favicon_party/http_client.rb', line 43

def build_curl_cmd(url, flags = "")
  "curl #{curl_shared_flags} #{flags} '#{prefix_url(url, :downcase => false)}'"
end

#curl_get_cmd(url) ⇒ Object



47
48
49
# File 'lib/favicon_party/http_client.rb', line 47

def curl_get_cmd(url)
  build_curl_cmd url, "--compressed --fail --show-error"
end

#curl_head_cmd(url) ⇒ Object



51
52
53
# File 'lib/favicon_party/http_client.rb', line 51

def curl_head_cmd(url)
  build_curl_cmd url, "-I -1"
end

#get(url) ⇒ Object

Encodes output as utf8 - Not for binary http responses



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/favicon_party/http_client.rb', line 16

def get(url)
  stdin, stdout, stderr, t = Open3.popen3(curl_get_cmd(url))
  output = encode_utf8(stdout.read).strip
  error = encode_utf8(stderr.read).strip
  if !error.nil? && !error.empty?
    if error.include? "SSL"
      raise FaviconParty::Curl::SSLError.new(error)
    elsif error.include? "Couldn't resolve host"
      raise FaviconParty::Curl::DNSError.new(error)
    else
      raise FaviconParty::CurlError.new(error)
    end
  end
  output
end

#head(url) ⇒ Object



38
39
40
41
# File 'lib/favicon_party/http_client.rb', line 38

def head(url)
  response_headers = `#{curl_head_cmd(url)}`
  encode_utf8 response_headers
end