ezclient 0.5.0 → 0.6.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: 36276a0601968419be332b235a0bacd0d8687a36b318d0d3351d4b431ef58f92
4
- data.tar.gz: 58e5d7f226a2c9792229212329bf0cea4f73b0712bf70d0a0ddb4df93f2cfb24
3
+ metadata.gz: '0218155bce003ba4d2c5002c78398553b3ae4097954f07a9f78d9f615a87bd28'
4
+ data.tar.gz: a3e75129937eadc69b9dd798b4726cc1735c777b11c838c250632a779f710005
5
5
  SHA512:
6
- metadata.gz: f9fddb90bf2bbf7fcfa337a9ca13f734e8330329345375679d3b2102012fd10f1d2abe1cc23498cdd3a66ba1ea85edbcdedb503c5c6753d9c18228ccaede9285
7
- data.tar.gz: 81cc96427b233facb25ca58542aa569ae304b6ba95e42b6f6a475b63013bb7ff032c26fa5b3ac6ed786ea9d4f314dd9ec80a16933b5bb259f7b14dd15a779f51
6
+ metadata.gz: 3f50a99a73be9d7608cd08d7b47ff8af15923a590967c1519d9f2df9ab0f03de1ba935cfc98bfeeede06cf963e0ec0eea2a24efc226750cfcfeb836682d26c56
7
+ data.tar.gz: d993d191ecd78bc05fa66f2f77c0cff899cba18f1589f6ea31c22cab87f554ee743883249bd129675b77d49db6aa0ce8c92139fa3a8ec39a6794a3d4ec3dc043
data/README.md CHANGED
@@ -8,6 +8,7 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
10
  gem "ezclient"
11
+ gem "http", github: "httprb/http"
11
12
  ```
12
13
 
13
14
  ## Contributing
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EzClient::Client
4
+ def initialize(options = {})
5
+ self.options = options
6
+ self.clients = {}
7
+ end
8
+
9
+ def request(verb, url, **options)
10
+ options = { **default_options, **options }
11
+
12
+ keep_alive_timeout = options.delete(:keep_alive)
13
+ api_auth = options.delete(:api_auth)
14
+
15
+ if keep_alive_timeout
16
+ client = persistent_client_for(url, timeout: keep_alive_timeout)
17
+ else
18
+ client = HTTP::Client.new
19
+ end
20
+
21
+ EzClient::Request.new(verb, url, client: client, **options).tap do |request|
22
+ request.api_auth!(*api_auth) if api_auth
23
+ end
24
+ end
25
+
26
+ def perform(*args)
27
+ request(*args).perform
28
+ end
29
+
30
+ def perform!(*args)
31
+ request(*args).perform!
32
+ end
33
+
34
+ private
35
+
36
+ attr_accessor :options, :clients
37
+
38
+ def persistent_client_for(url, timeout: 600)
39
+ uri = HTTP::URI.parse(url)
40
+ clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
41
+ end
42
+
43
+ def default_options
44
+ {
45
+ api_auth: options[:api_auth],
46
+ keep_alive: options[:keep_alive],
47
+ max_retries: options[:max_retries],
48
+ on_complete: options[:on_complete],
49
+ on_error: options[:on_error],
50
+ retry_exceptions: options[:retry_exceptions],
51
+ timeout: options[:default_timeout],
52
+ }
53
+ end
54
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EzClient
4
+ class ResponseStatusError < StandardError
5
+ attr_accessor :response
6
+
7
+ def initialize(response)
8
+ self.response = response
9
+ end
10
+ end
11
+ end
@@ -20,6 +20,16 @@ class EzClient::Request
20
20
  raise error
21
21
  end
22
22
 
23
+ def perform!
24
+ response = perform
25
+
26
+ if response.error?
27
+ raise EzClient::ResponseStatusError, response
28
+ else
29
+ response
30
+ end
31
+ end
32
+
23
33
  def api_auth!(*args)
24
34
  raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
25
35
  ApiAuth.sign!(http_request, *args)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class EzClient
4
- VERSION = "0.5.0"
3
+ module EzClient
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/ezclient.rb CHANGED
@@ -2,50 +2,13 @@
2
2
 
3
3
  require "http"
4
4
  require "ezclient/version"
5
+ require "ezclient/client"
5
6
  require "ezclient/request"
6
7
  require "ezclient/response"
8
+ require "ezclient/errors"
7
9
 
8
- class EzClient
9
- def initialize(options = {})
10
- self.options = options
11
- self.clients = {}
12
- end
13
-
14
- def request(verb, url, **options)
15
- options = { **default_options, **options }
16
-
17
- keep_alive_timeout = options.delete(:keep_alive)
18
- api_auth = options.delete(:api_auth)
19
-
20
- if keep_alive_timeout
21
- client = persistent_client_for(url, timeout: keep_alive_timeout)
22
- else
23
- client = HTTP::Client.new
24
- end
25
-
26
- Request.new(verb, url, client: client, **options).tap do |request|
27
- request.api_auth!(*api_auth) if api_auth
28
- end
29
- end
30
-
31
- private
32
-
33
- attr_accessor :options, :clients
34
-
35
- def persistent_client_for(url, timeout: 600)
36
- uri = HTTP::URI.parse(url)
37
- clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
38
- end
39
-
40
- def default_options
41
- {
42
- api_auth: options[:api_auth],
43
- keep_alive: options[:keep_alive],
44
- max_retries: options[:max_retries],
45
- on_complete: options[:on_complete],
46
- on_error: options[:on_error],
47
- retry_exceptions: options[:retry_exceptions],
48
- timeout: options[:default_timeout],
49
- }
10
+ module EzClient
11
+ def self.new(*args)
12
+ Client.new(*args)
50
13
  end
51
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov
@@ -153,6 +153,8 @@ files:
153
153
  - Rakefile
154
154
  - ezclient.gemspec
155
155
  - lib/ezclient.rb
156
+ - lib/ezclient/client.rb
157
+ - lib/ezclient/errors.rb
156
158
  - lib/ezclient/request.rb
157
159
  - lib/ezclient/response.rb
158
160
  - lib/ezclient/version.rb