Class: Gemstash::HTTPClient

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

Overview

:nodoc:

Constant Summary collapse

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

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.



40
41
42
43
# File 'lib/gemstash/http_client.rb', line 40

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

Class Method Details

.for(upstream) ⇒ Object



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

def self.for(upstream)
  client = Faraday.new(upstream.to_s) do |config|
    config.use FaradayMiddleware::FollowRedirects
    config.adapter :net_http
  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:



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

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