Class: Gemstash::HTTPClient
- Inherits:
-
Object
- Object
- Gemstash::HTTPClient
- Includes:
- Logging
- Defined in:
- lib/gemstash/http_client.rb
Overview
:nodoc:
Constant Summary collapse
Constants included from Logging
Class Method Summary collapse
Instance Method Summary collapse
- #get(path) ⇒ Object
-
#initialize(client = nil, user_agent: nil) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
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
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..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 |