Sha256: 0b43baa16908eae5e2f0744bbeabee597fd7864f782a7edb16a025d63eea017b

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Gemirro
  ##
  # The Http class is responsible for executing GET request
  # to a specific url and return an response as an HTTP::Message
  #
  # @!attribute [r] client
  #  @return [HTTPClient]
  #
  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) && config.basic_auth
        client.www_auth.basic_auth.force_auth = (true)
      end
      @client = client
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemirro-1.5.0 lib/gemirro/http.rb