Class: Gemstash::HTTPClient

Inherits:
Object
  • Object
show all
Extended by:
Env::Helper
Includes:
Logging
Defined in:
lib/gemstash/http_client.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_USER_AGENT =
"Gemstash/#{Gemstash::VERSION}".freeze

Constants included from Logging

Logging::LEVELS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_error, logger, reset, setup_logger

Constructor Details

#initialize(client = nil, user_agent: nil) ⇒ HTTPClient

Returns a new instance of HTTPClient.



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

def initialize(client = nil, user_agent: nil)
  @client = client
  @user_agent = user_agent || DEFAULT_USER_AGENT
end

Class Method Details

.for(upstream) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gemstash/http_client.rb', line 30

def self.for(upstream)
  client = Faraday.new(upstream.to_s) do |config|
    config.use FaradayMiddleware::FollowRedirects
    config.adapter :net_http
    config.options.timeout = gemstash_env.config[:fetch_timeout]
  end
  user_agent = "#{upstream.user_agent} " unless upstream.user_agent.to_s.empty?
  user_agent = user_agent.to_s + DEFAULT_USER_AGENT

  new(client, user_agent: user_agent)
end

Instance Method Details

#get(path) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gemstash/http_client.rb', line 47

def get(path)
  response = with_retries do
    @client.get(path) do |req|
      req.headers["User-Agent"] = @user_agent
      req.options.open_timeout = 2
    end
  end

  raise Gemstash::WebError.new(response.body, response.status) unless response.success?

  if block_given?
    yield(response.body, response.headers)
  else
    response.body
  end
end