Class: Net::Hippie::Client
- Inherits:
-
Object
- Object
- Net::Hippie::Client
- Defined in:
- lib/net/hippie/client.rb
Overview
A simple client for connecting with http resources.
Constant Summary collapse
- DEFAULT_HEADERS =
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "net/hippie #{Net::Hippie::VERSION}" }.freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#mapper ⇒ Object
Returns the value of attribute mapper.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
Instance Method Summary collapse
- #delete(uri, headers: {}, body: {}, &block) ⇒ Object
- #execute(uri, request) ⇒ Object
- #get(uri, headers: {}, body: {}, &block) ⇒ Object
-
#initialize(certificate: nil, headers: DEFAULT_HEADERS, key: nil, passphrase: nil, verify_mode: Net::Hippie.verify_mode) ⇒ Client
constructor
A new instance of Client.
- #post(uri, headers: {}, body: {}, &block) ⇒ Object
- #put(uri, headers: {}, body: {}, &block) ⇒ Object
-
#with_retry(retries: 3) ⇒ Object
attempt 1 -> delay 0.1 second attempt 2 -> delay 0.2 second attempt 3 -> delay 0.4 second attempt 4 -> delay 0.8 second attempt 5 -> delay 1.6 second attempt 6 -> delay 3.2 second attempt 7 -> delay 6.4 second attempt 8 -> delay 12.8 second.
Constructor Details
#initialize(certificate: nil, headers: DEFAULT_HEADERS, key: nil, passphrase: nil, verify_mode: Net::Hippie.verify_mode) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/net/hippie/client.rb', line 15 def initialize( certificate: nil, headers: DEFAULT_HEADERS, key: nil, passphrase: nil, verify_mode: Net::Hippie.verify_mode ) @certificate = certificate @default_headers = headers @key = key @mapper = ContentTypeMapper.new @passphrase = passphrase @read_timeout = 30 @verify_mode = verify_mode @logger = Net::Hippie.logger end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/net/hippie/client.rb', line 13 def logger @logger end |
#mapper ⇒ Object
Returns the value of attribute mapper.
13 14 15 |
# File 'lib/net/hippie/client.rb', line 13 def mapper @mapper end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
13 14 15 |
# File 'lib/net/hippie/client.rb', line 13 def open_timeout @open_timeout end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
13 14 15 |
# File 'lib/net/hippie/client.rb', line 13 def read_timeout @read_timeout end |
Instance Method Details
#delete(uri, headers: {}, body: {}, &block) ⇒ Object
57 58 59 60 61 |
# File 'lib/net/hippie/client.rb', line 57 def delete(uri, headers: {}, body: {}, &block) type = Net::HTTP::Delete request = request_for(type, uri, headers: headers, body: body) execute(uri, request, &block) end |
#execute(uri, request) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/net/hippie/client.rb', line 32 def execute(uri, request) response = http_for(normalize_uri(uri)).request(request) if block_given? yield request, response else response end end |
#get(uri, headers: {}, body: {}, &block) ⇒ Object
41 42 43 44 |
# File 'lib/net/hippie/client.rb', line 41 def get(uri, headers: {}, body: {}, &block) request = request_for(Net::HTTP::Get, uri, headers: headers, body: body) execute(uri, request, &block) end |
#post(uri, headers: {}, body: {}, &block) ⇒ Object
46 47 48 49 50 |
# File 'lib/net/hippie/client.rb', line 46 def post(uri, headers: {}, body: {}, &block) type = Net::HTTP::Post request = request_for(type, uri, headers: headers, body: body) execute(uri, request, &block) end |
#put(uri, headers: {}, body: {}, &block) ⇒ Object
52 53 54 55 |
# File 'lib/net/hippie/client.rb', line 52 def put(uri, headers: {}, body: {}, &block) request = request_for(Net::HTTP::Put, uri, headers: headers, body: body) execute(uri, request, &block) end |
#with_retry(retries: 3) ⇒ Object
attempt 1 -> delay 0.1 second attempt 2 -> delay 0.2 second attempt 3 -> delay 0.4 second attempt 4 -> delay 0.8 second attempt 5 -> delay 1.6 second attempt 6 -> delay 3.2 second attempt 7 -> delay 6.4 second attempt 8 -> delay 12.8 second
71 72 73 74 75 76 77 78 79 |
# File 'lib/net/hippie/client.rb', line 71 def with_retry(retries: 3) retries = 0 if retries.nil? || retries.negative? 0.upto(retries) do |n| attempt(n, retries) do return yield self end end end |