Class: Gemirro::Http

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

Overview

The Http class is responsible for executing GET request to a specific url and return an response as an HTTP::Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#clientHTTPClient

Returns:

  • (HTTPClient)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gemirro/http.rb', line 11

class Http
  attr_accessor :client

  ##
  # Requests the given HTTP resource.
  #
  # @param [String] url
  # @return [HTTP::Message]
  #
  def self.get(url)
    response = client.get(url, follow_redirect: true)

    raise HTTPClient::BadResponseError, response.reason unless HTTP::Status.successful?(response.status)

    response
  end

  ##
  # @return [HTTPClient]
  #
  def self.client
    client ||= HTTPClient.new
    config = Utils.configuration
    if defined?(config.upstream_user)
      user = config.upstream_user
      password = config.upstream_password
      domain = config.upstream_domain
      client.set_auth(domain, user, password)
    end

    if defined?(config.proxy)
      proxy = config.proxy
      client.proxy=(proxy)
    end

    # Use my own ca file for self signed cert
    if defined?(config.rootca)
        abort "The configuration file #{config.rootca} does not exist" unless File.file?(config.rootca)
        client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_PEER
        client.ssl_config.set_trust_ca(config.rootca)
    elsif defined?(config.verify_mode)
      client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless config.verify_mode
    end

    # Enforce base auth
    if defined?(config.basic_auth)
      client.force_basic_auth=(true) if config.basic_auth
    end
    @client = client
  end
end

Class Method Details

.clientHTTPClient

Returns:

  • (HTTPClient)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gemirro/http.rb', line 31

def self.client
  client ||= HTTPClient.new
  config = Utils.configuration
  if defined?(config.upstream_user)
    user = config.upstream_user
    password = config.upstream_password
    domain = config.upstream_domain
    client.set_auth(domain, user, password)
  end

  if defined?(config.proxy)
    proxy = config.proxy
    client.proxy=(proxy)
  end

  # Use my own ca file for self signed cert
  if defined?(config.rootca)
      abort "The configuration file #{config.rootca} does not exist" unless File.file?(config.rootca)
      client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_PEER
      client.ssl_config.set_trust_ca(config.rootca)
  elsif defined?(config.verify_mode)
    client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless config.verify_mode
  end

  # Enforce base auth
  if defined?(config.basic_auth)
    client.force_basic_auth=(true) if config.basic_auth
  end
  @client = client
end

.get(url) ⇒ HTTP::Message

Requests the given HTTP resource.

Parameters:

  • url (String)

Returns:

  • (HTTP::Message)

Raises:

  • (HTTPClient::BadResponseError)


20
21
22
23
24
25
26
# File 'lib/gemirro/http.rb', line 20

def self.get(url)
  response = client.get(url, follow_redirect: true)

  raise HTTPClient::BadResponseError, response.reason unless HTTP::Status.successful?(response.status)

  response
end