ezclient 0.3.0 → 0.4.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: a67e136a6d0f0ca48210c21a25ee05389b62cf8eb2e2c4de8fcbd16b3c93f30b
4
- data.tar.gz: 34c747b1a393ce7d639a7357a369e581047679ed7ee8bf650f5f2284e396362a
3
+ metadata.gz: 8095e9d89e1ba040935437e755e022ff458f09f16a99967a9def2059406a3448
4
+ data.tar.gz: 23ba78d9d72e9ce7dbbda36a66be93cb6a4d5f9948bc71d42f7cdbb0f52c36d3
5
5
  SHA512:
6
- metadata.gz: f78f96a599a6dc7fdf92bf1d03ebf701f4b29d7204f74a23630b93b1ebcc60de723f557a50ae534d2e2c13125c2895d3abb5fbbb33bf6730a2d128b787b4f7ea
7
- data.tar.gz: bae71e4f2b4e84d36d1cf9e92936aa7722c18387dca44beaa6a9ac702e3260ac078d662c59ada4fbf75f9db9b19ae59daf9223f9dd2f4325d1c1df1d51419b3d
6
+ metadata.gz: 5eeec168072569e4bf30602b58315111bad499b91b242b5ac66ea28f59ed2e8214b2209d78e0eace3d36ede29676656a1a652a70fb2b4a03536a2a17001c69d8
7
+ data.tar.gz: 5407977b45b49797bc7eaf75f2b8458ec84be9c9cfca3d3c313dc71f6a0916c3908b76988b4e9d894bf2987dd2b24088d59fe092e0c3b4d1b411f316048bf4f2
data/.travis.yml CHANGED
@@ -6,6 +6,7 @@ rvm:
6
6
  - 2.5.0
7
7
  before_install: gem install bundler -v 1.16.1
8
8
  env: SUITE="rspec"
9
+ script: bundle exec $SUITE
9
10
  matrix:
10
11
  fast_finish: true
11
12
  # Only run RuboCop on the latest Ruby
data/README.md CHANGED
@@ -1,35 +1,15 @@
1
1
  # EzClient
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ezclient`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ EzClient is [HTTP gem](https://github.com/httprb/http) wrapper for easy persistent connections and more.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem 'ezclient'
10
+ gem "ezclient"
13
11
  ```
14
12
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install ezclient
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
13
  ## Contributing
34
14
 
35
15
  Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/ezclient.
data/ezclient.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["tycooon@yandex.ru"]
12
12
 
13
13
  spec.summary = "An HTTP gem wrapper for easy persistent connections and more."
14
- spec.homepage = "https://github.org/umbrellio/ezclient"
14
+ spec.homepage = "https://github.com/umbrellio/ezclient"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
@@ -10,7 +10,7 @@ class EzClient::Request
10
10
  end
11
11
 
12
12
  def perform
13
- http_response = http_client.perform(http_request, http_client.default_options)
13
+ http_response = perform_request
14
14
 
15
15
  EzClient::Response.new(http_response).tap do |response|
16
16
  on_complete.call(self, response, options[:metadata])
@@ -21,7 +21,7 @@ class EzClient::Request
21
21
  end
22
22
 
23
23
  def api_auth!(*args)
24
- # raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
24
+ raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
25
25
  ApiAuth.sign!(http_request, *args)
26
26
  self
27
27
  end
@@ -41,8 +41,9 @@ class EzClient::Request
41
41
 
42
42
  def http_client
43
43
  @http_client ||= begin
44
- client = options.fetch(:client)
45
- client.timeout(timeout) if timeout
44
+ client = options.fetch(:client) # TODO: dup?
45
+ client = client.timeout(timeout) if timeout
46
+ client = client.basic_auth(basic_auth) if basic_auth
46
47
  client
47
48
  end
48
49
  end
@@ -56,6 +57,31 @@ class EzClient::Request
56
57
  options.select { |key| [:params, :form, :json, :body, :headers].include?(key) }
57
58
  end
58
59
 
60
+ def perform_request
61
+ retries = 0
62
+
63
+ begin
64
+ retry_on_connection_error do
65
+ http_client.perform(http_request, http_client.default_options)
66
+ end
67
+ rescue *retried_exceptions
68
+ if retries < max_retries.to_i
69
+ retries += 1
70
+ retry
71
+ else
72
+ raise
73
+ end
74
+ end
75
+ end
76
+
77
+ def retry_on_connection_error
78
+ # This may result in 2 requests reaching the server so I hope HTTP fixes it
79
+ # https://github.com/httprb/http/issues/459
80
+ yield
81
+ rescue HTTP::ConnectionError
82
+ yield
83
+ end
84
+
59
85
  def timeout
60
86
  options[:timeout]&.to_f
61
87
  end
@@ -67,4 +93,24 @@ class EzClient::Request
67
93
  def on_error
68
94
  options[:on_error] || proc {}
69
95
  end
96
+
97
+ def retried_exceptions
98
+ Array(options[:retry_exceptions])
99
+ end
100
+
101
+ def max_retries
102
+ options[:max_retries] || 1
103
+ end
104
+
105
+ def basic_auth
106
+ @basic_auth ||= begin
107
+ case options[:basic_auth]
108
+ when Array
109
+ user, password = options[:basic_auth]
110
+ { user: user, pass: password }
111
+ when Hash
112
+ options[:basic_auth]
113
+ end
114
+ end
115
+ end
70
116
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class EzClient
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/ezclient.rb CHANGED
@@ -32,11 +32,13 @@ class EzClient
32
32
  clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
33
33
  end
34
34
 
35
- def default_options()
35
+ def default_options
36
36
  {
37
37
  on_complete: options[:on_complete],
38
38
  on_error: options[:on_error],
39
39
  timeout: options[:default_timeout],
40
+ retry_exceptions: options[:retry_exceptions],
41
+ max_retries: options[:max_retries],
40
42
  }
41
43
  end
42
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
11
+ date: 2018-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -158,7 +158,7 @@ files:
158
158
  - lib/ezclient/request.rb
159
159
  - lib/ezclient/response.rb
160
160
  - lib/ezclient/version.rb
161
- homepage: https://github.org/umbrellio/ezclient
161
+ homepage: https://github.com/umbrellio/ezclient
162
162
  licenses:
163
163
  - MIT
164
164
  metadata: {}