net-hippie 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0898b54f65bde3d92208c3131caa0ac10a38912e4c0d28c8722a87f69c128725'
4
- data.tar.gz: 3aad4dde1f3106dba73b8faefc2ecbaa9738bab8ab54050dffc961c741f3b859
3
+ metadata.gz: 7f64cd42e98caffcf56a025383d07b7defe8991f191597859bfa1b12613127ea
4
+ data.tar.gz: ca7ac7d7d44f13e6241f87ff2e31bcf76c1f89051a6c562843ec419ddec0ecc2
5
5
  SHA512:
6
- metadata.gz: d5f8c7bf17364bbd2622a27593d658fad71520b2d488a0df23dd74ce6b6e92a5556d5c5ed2f894f6c71b9f695ad22c69f28b8480cfaee60bdcfc44327da62416
7
- data.tar.gz: 7a728cc75297a8fb01f9e3f434cf41567ae2b0197968641b5d5bf5a8e62c0b0dc17dbdb6e04cbeb6a54dda0175bcd32d203ca5244b0044440f75b6529b957fb2
6
+ metadata.gz: e4f12a3384fd90f92f0470231c89ba1c2980701b5e3f75ea73751281750276b266c8dbc4f6ad9ab10088e073cf2f3b638c283e06045f6a3984b8e5c2d3e7eeea
7
+ data.tar.gz: 26cfa4495ca4ed7edac3f9533d257d199fd24a37e85c55f10c3017159230308799f10acceb9a94d3fb3c64c11fe6ce4495f824fb15d1ac76809590962a614c6d
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Net::Hippie
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/net-hippie.svg)](https://rubygems.org/gems/net-hippie)
4
+ [![Build Status](https://travis-ci.org/mokhan/net-hippie.svg?branch=master)](https://travis-ci.org/mokhan/net-hippie)
5
+
3
6
  Net::Hippie is a light weight wrapper around `net/http` that defaults to
4
7
  sending JSON messages.
5
8
 
@@ -40,7 +43,7 @@ puts JSON.parse(response.body)
40
43
  ```ruby
41
44
  client = Net::Hippie::Client.new
42
45
  body = { user: { name: 'hippie' } }
43
- response = client.post(URI.parse('https://example.com'), body: body)
46
+ response = client.post(URI.parse('https://example.org'), body: body)
44
47
  puts JSON.parse(response.body)
45
48
  ```
46
49
 
@@ -63,6 +66,22 @@ client = Net::Hippie::Client.new(
63
66
  )
64
67
  ```
65
68
 
69
+ ### Basic Auth
70
+
71
+ ```ruby
72
+ client = Net::Hippie::Client.new
73
+ headers = { 'Authorization' => Net::Hippie.basic_auth('username', 'password') }
74
+ client.get('https://www.example.org', headers: headers)
75
+ ```
76
+
77
+ ### Bearer Auth
78
+
79
+ ```ruby
80
+ client = Net::Hippie::Client.new
81
+ headers = { 'Authorization' => Net::Hippie.bearer_auth('token') }
82
+ client.get('https://www.example.org', headers: headers)
83
+ ```
84
+
66
85
  ## Development
67
86
 
68
87
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/bin/cibuild CHANGED
@@ -15,6 +15,7 @@ export RUBY_GC_HEAP_INIT_SLOTS=800000
15
15
  export RUBY_HEAP_FREE_MIN=100000
16
16
  export RUBY_HEAP_SLOTS_INCREMENT=400000
17
17
  export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
18
+ export CIBUILD=1
18
19
 
19
20
  ruby -v
20
21
  gem install bundler --no-ri --no-rdoc --conservative
data/lib/net/hippie.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'base64'
3
4
  require 'json'
4
5
  require 'logger'
5
6
  require 'net/http'
@@ -13,6 +14,14 @@ require 'net/hippie/api'
13
14
  module Net
14
15
  # net/http for hippies.
15
16
  module Hippie
17
+ CONNECTION_ERRORS = [
18
+ EOFError,
19
+ Errno::ECONNRESET,
20
+ Errno::EINVAL,
21
+ Net::ProtocolError,
22
+ Timeout::Error
23
+ ].freeze
24
+
16
25
  def self.logger
17
26
  @logger ||= Logger.new(STDOUT)
18
27
  end
@@ -20,5 +29,13 @@ module Net
20
29
  def self.logger=(logger)
21
30
  @logger = logger
22
31
  end
32
+
33
+ def self.basic_auth(username, password)
34
+ "Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
35
+ end
36
+
37
+ def self.bearer_auth(token)
38
+ "Bearer #{token}"
39
+ end
23
40
  end
24
41
  end
@@ -12,6 +12,7 @@ module Net
12
12
 
13
13
  attr_accessor :mapper
14
14
  attr_accessor :read_timeout
15
+ attr_accessor :logger
15
16
 
16
17
  def initialize(
17
18
  certificate: nil,
@@ -27,6 +28,7 @@ module Net
27
28
  @passphrase = passphrase
28
29
  @read_timeout = 30
29
30
  @verify_mode = verify_mode
31
+ @logger = Net::Hippie.logger
30
32
  end
31
33
 
32
34
  def execute(uri, request)
@@ -60,6 +62,27 @@ module Net
60
62
  execute(uri, request, &block)
61
63
  end
62
64
 
65
+ # attempt 1 -> delay 1 second
66
+ # attempt 2 -> delay 2 second
67
+ # attempt 3 -> delay 4 second
68
+ # attempt 4 -> delay 8 second
69
+ # attempt 5 -> delay 16 second
70
+ # attempt 6 -> delay 32 second
71
+ # attempt 7 -> delay 64 second
72
+ # attempt 8 -> delay 128 second
73
+ def with_retry(retries: 3)
74
+ retries = 3 if retries <= 0
75
+ 0.upto(retries) do |n|
76
+ return yield self
77
+ rescue *::Net::Hippie::CONNECTION_ERRORS => error
78
+ raise error if n >= retries
79
+
80
+ delay = (2**n) + rand(0.5) # delay + jitter
81
+ warn("`#{error.message}` Retry: #{n + 1}/#{retries} Delay: #{delay}s")
82
+ sleep delay
83
+ end
84
+ end
85
+
63
86
  private
64
87
 
65
88
  attr_reader :default_headers
@@ -71,7 +94,7 @@ module Net
71
94
  http.read_timeout = read_timeout
72
95
  http.use_ssl = uri.scheme == 'https'
73
96
  http.verify_mode = verify_mode
74
- http.set_debug_output(Net::Hippie.logger)
97
+ http.set_debug_output(logger)
75
98
  apply_client_tls_to(http)
76
99
  http
77
100
  end
@@ -97,6 +120,10 @@ module Net
97
120
  http.cert = OpenSSL::X509::Certificate.new(certificate)
98
121
  http.key = private_key
99
122
  end
123
+
124
+ def warn(message)
125
+ logger.warn(message)
126
+ end
100
127
  end
101
128
  end
102
129
  end
@@ -7,6 +7,7 @@ module Net
7
7
  def map_from(headers, body)
8
8
  content_type = headers['Content-Type'] || ''
9
9
  return JSON.generate(body) if content_type.include?('json')
10
+
10
11
  body
11
12
  end
12
13
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Net
4
4
  module Hippie
5
- VERSION = '0.1.9'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-hippie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-14 00:00:00.000000000 Z
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  requirements: []
145
145
  rubyforge_project:
146
- rubygems_version: 2.7.7
146
+ rubygems_version: 2.7.6
147
147
  signing_key:
148
148
  specification_version: 4
149
149
  summary: net/http for hippies.