class Gemstash::HTTPClient

Constants

DEFAULT_USER_AGENT

Attributes

client[R]

Public Class Methods

for(upstream) click to toggle source
# File lib/gemstash/http_client.rb, line 32
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

  client.basic_auth(upstream.user, upstream.password) if upstream.auth?

  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
new(client = nil, user_agent: nil) click to toggle source
# File lib/gemstash/http_client.rb, line 49
def initialize(client = nil, user_agent: nil)
  @client = client
  @user_agent = user_agent || DEFAULT_USER_AGENT
end

Public Instance Methods

get(path) { |body, headers| ... } click to toggle source
# File lib/gemstash/http_client.rb, line 54
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

Private Instance Methods

with_retries(times: 3) { || ... } click to toggle source
# File lib/gemstash/http_client.rb, line 73
def with_retries(times: 3)
  loop do
    times -= 1
    begin
      return yield
    rescue Faraday::ConnectionFailed => e
      log_error("Connection failure", e)
      raise(ConnectionError, e.message) unless times > 0

      log.info "retrying... #{times} more times"
    end
  end
end