Class: Net::Hippie::Client
- Inherits:
-
Object
- Object
- Net::Hippie::Client
- Includes:
- TlsParser
- Defined in:
- lib/net/hippie/client.rb
Overview
HTTP client with connection pooling, DNS caching, and retry logic.
Constant Summary collapse
- DEFAULT_HEADERS =
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "net/hippie #{VERSION}" }.freeze
- JITTER =
Random.new.freeze
Instance Attribute Summary collapse
-
#follow_redirects ⇒ Object
readonly
Returns the value of attribute follow_redirects.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#mapper ⇒ Object
readonly
Returns the value of attribute mapper.
Instance Method Summary collapse
- #close_all ⇒ Object
- #execute(uri, request, limit: follow_redirects, stream: false, &block) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #with_retry(retries: 3) ⇒ Object
Methods included from TlsParser
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 27 28 29 30 |
# File 'lib/net/hippie/client.rb', line 22 def initialize( = {}) @options = @mapper = .fetch(:mapper, ContentTypeMapper.new) @logger = .fetch(:logger, Net::Hippie.logger) @follow_redirects = .fetch(:follow_redirects, 0) @default_headers = .fetch(:headers, DEFAULT_HEADERS).freeze configure_pool() configure_tls() end |
Instance Attribute Details
#follow_redirects ⇒ Object (readonly)
Returns the value of attribute follow_redirects.
20 21 22 |
# File 'lib/net/hippie/client.rb', line 20 def follow_redirects @follow_redirects end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
20 21 22 |
# File 'lib/net/hippie/client.rb', line 20 def logger @logger end |
#mapper ⇒ Object (readonly)
Returns the value of attribute mapper.
20 21 22 |
# File 'lib/net/hippie/client.rb', line 20 def mapper @mapper end |
Instance Method Details
#close_all ⇒ Object
65 66 67 68 |
# File 'lib/net/hippie/client.rb', line 65 def close_all @pool.close_all @dns_cache.clear end |
#execute(uri, request, limit: follow_redirects, stream: false, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/net/hippie/client.rb', line 44 def execute(uri, request, limit: follow_redirects, stream: false, &block) conn = connection_for(uri) return conn.run(request) { |res| res.read_body(&block) } if stream && block return execute_with_block(conn, request, &block) if block response = conn.run(request) limit.positive? && response.is_a?(Net::HTTPRedirection) ? follow_redirect(uri, response, limit) : response end |
#with_retry(retries: 3) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/net/hippie/client.rb', line 54 def with_retry(retries: 3) retries = [retries.to_i, 0].max 0.upto(retries) do |attempt| return yield self rescue *CONNECTION_ERRORS => error raise if attempt == retries sleep_with_backoff(attempt, retries, error) end end |