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}"

Constants included from Logging

Logging::LEVELS

Instance Attribute Summary collapse

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.



49
50
51
52
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



47
48
49
# File 'lib/gemstash/http_client.rb', line 47

def client
  @client
end

Class Method Details

.for(upstream) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Method Details

#get(path) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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