ezclient 1.2.0 → 1.3.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: '088411a531c19317ffbfe1807ef89e17b5db1607410c2f1797c1a673e65cbf17'
4
- data.tar.gz: eec6f54dcf8f648ae6685d5aacfcebeeb3e9861941a73c356d143e929be499a2
3
+ metadata.gz: 9bbefbed2ebd4c6b412246062c09bf178b39b10b54631963e559fbb6d6150023
4
+ data.tar.gz: 586fa332053ce325be892e05fb7eabfc7daecaf9c60a4aeae607d0fbb646366c
5
5
  SHA512:
6
- metadata.gz: 6014665fd4078412b21cd9c60dd0ca569dcff3e2649662dd04c32ece299c507f8565f7c3069b69b4bdf3551fa9f8bcb66f8a82d64ef192b537e808f33a3fb2c9
7
- data.tar.gz: '0430985d0af80a195f1e97b9a4d5f44c213dd3b60aeb4040d9ae256fd6ee7808b6b3c253a010d05cad367b7a8a5d68ee6039f211d28dad22794f27ca6dcd2634'
6
+ metadata.gz: d4e2142b3f825390f39dde75b2145da1485ba1211e2202639a0d88d5785373bbbf97e2dd4d8c12c0a4c7d4e0f57c927eec12300152ea757fd11ae6d33c019812
7
+ data.tar.gz: bc99bcb243ea9c4c70e4916e8252fac60cdefbdbd136193a6b6db0611b5bfbb6609233ba3d5c0fa21f6775ada8354e07aa6e4c0bff488eec9516bbeb14a62e3e
data/.rubocop.yml CHANGED
@@ -3,7 +3,15 @@ inherit_gem:
3
3
 
4
4
  AllCops:
5
5
  DisplayCopNames: true
6
- TargetRubyVersion: 2.5
6
+ TargetRubyVersion: 2.3
7
+ Include:
8
+ - bin/console
9
+ - Gemfile
10
+ - ezclient.gemspec
11
+ - Rakefile
12
+ - lib/**/*
13
+ - spec/**/*
14
+ - gemfile/**/*
7
15
  Exclude:
8
16
  - vendor/**/*
9
17
  - gemfiles/**/*
data/.travis.yml CHANGED
@@ -1,26 +1,18 @@
1
1
  language: ruby
2
-
3
2
  sudo: false
4
-
5
- rvm:
6
- - 2.3
7
- - 2.4
8
- - 2.5
9
- - ruby-head
10
-
3
+ cache: bundler
11
4
  before_install: gem install bundler
12
-
13
5
  env: SUITE="rspec"
14
-
15
6
  script: bundle exec $SUITE
16
-
17
7
  matrix:
18
8
  fast_finish: true
19
- # Only run RuboCop on the latest Ruby
20
9
  include:
10
+ - rvm: 2.3
11
+ - rvm: 2.4
21
12
  - rvm: 2.5
13
+ - rvm: 2.6
14
+ - rvm: ruby-head
15
+ - rvm: 2.6
22
16
  env: SUITE="rubocop"
23
17
  allow_failures:
24
18
  - rvm: ruby-head
25
-
26
- cache: bundler
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 Yuri Smirnov
3
+ Copyright (c) 2018-2019 Yuri Smirnov
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # EzClient   [![Gem Version](https://badge.fury.io/rb/ezclient.svg)](https://badge.fury.io/rb/ezclient) [![Build Status](https://travis-ci.org/umbrellio/ezclient.svg?branch=master)](https://travis-ci.org/umbrellio/ezclient) [![Coverage Status](https://coveralls.io/repos/github/umbrellio/ezclient/badge.svg?branch=master)](https://coveralls.io/github/umbrellio/ezclient?branch=master)
2
+
2
3
  EzClient is [HTTP gem](https://github.com/httprb/http) wrapper for easy persistent connections and more.
3
4
 
4
5
  ## Installation
6
+
5
7
  Add this line to your application's Gemfile:
6
8
 
7
9
  ```ruby
@@ -9,6 +11,7 @@ gem "ezclient"
9
11
  ```
10
12
 
11
13
  ## Usage
14
+
12
15
  ```ruby
13
16
  url = "http://example.com"
14
17
 
@@ -29,6 +32,7 @@ response = client.perform!(:get, url, request_options) # => EzClient::Response o
29
32
  ```
30
33
 
31
34
  Valid client options are:
35
+
32
36
  - `api_auth` – arguments for `ApiAuth.sign!` (see https://github.com/mgomes/api_auth)
33
37
  - `basic_auth` – arguments for basic authentication (either a hash with `:user` and `:pass` keys or a two-element array)
34
38
  - `headers` – a hash of headers for requests
@@ -40,10 +44,12 @@ Valid client options are:
40
44
  - `retry_exceptions` – an array of exception classes to retry
41
45
  - `ssl_context` – ssl context for requests (an `OpenSSL::SSL::SSLContext` instance)
42
46
  - `timeout` – timeout for requests in seconds
47
+ - `follow` - enable following redirects (`true` or hash with options – e.g. `{ max_hops: 1, strict: false}`)
43
48
 
44
49
  All these options are passed to each request made by this client but can be overriden on per-request basis.
45
50
 
46
51
  Extra per-request only options are:
52
+
47
53
  - `body` – raw request body
48
54
  - `form` – hash for urlencoded body
49
55
  - `json` – data for json (also adds `application/json` content-type header)
@@ -52,14 +58,32 @@ Extra per-request only options are:
52
58
  - `query` – hash for uri query
53
59
 
54
60
  ## Persistent connections
61
+
55
62
  If you provide `keep_alive` option to the client or particular request, the connection will be stored in the client and then
56
63
  reused for all following requests to the same origin within specified amount of time.
57
64
 
58
- Note that, as of now, EzClient will
65
+ Note that if you are using persistent connections, you shouldn't store your client in a variable that is accessable by different threads. See the example:
66
+
67
+ ```ruby
68
+ module MyApp
69
+ # Bad: multiple threads will use the same socket
70
+ def self.bad_client
71
+ @ezclient ||= EzClient.new(keep_alive: 100)
72
+ end
73
+
74
+ # Good: each thread has it's own socket
75
+ def self.good_client
76
+ Thread.current[:ezclient] ||= EzClient.new(keep_alive: 100)
77
+ end
78
+ end
79
+ ```
80
+
81
+ Alose note that, as of now, EzClient will
59
82
  automatically retry the request on any `HTTP::ConnectionError` exception in this case which may possibly result in two requests
60
83
  received by a server (see https://github.com/httprb/http/issues/459).
61
84
 
62
85
  ## Callbacks and retrying
86
+
63
87
  You can provide `on_complete`, `on_error` and `on_retry` callbacks like this:
64
88
 
65
89
  ```ruby
@@ -79,12 +103,14 @@ response = client.perform!(:get, url, metadata: :hello)
79
103
  ```
80
104
 
81
105
  The arguments passed into callbacks are:
106
+
82
107
  - `request` – an `EzClient::Request` instance
83
108
  - `response` – an `EzClient::Response` instance
84
109
  - `error` – an exception instance
85
110
  - `metadata` - the `metadata` option passed into a request
86
111
 
87
112
  ## Request object
113
+
88
114
  ```ruby
89
115
  request = client.request(:post, "http://example.com", json: { a: 1 }, timeout: 15)
90
116
 
@@ -95,6 +121,7 @@ request.headers # => { "Content-Type" => "application/json; charset=UTF-8", ...
95
121
  ```
96
122
 
97
123
  ## Response object
124
+
98
125
  ```ruby
99
126
  response = request.perform(...)
100
127
 
@@ -110,12 +137,15 @@ response.error? # Returns if request was 4xx or 5xx status
110
137
  ```
111
138
 
112
139
  ## Contributing
140
+
113
141
  Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/ezclient.
114
142
 
115
143
  ## License
144
+
116
145
  Released under MIT License.
117
146
 
118
147
  ## Authors
148
+
119
149
  Created by Yuri Smirnov.
120
150
 
121
151
  <a href="https://github.com/umbrellio/">
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ezclient"
6
+
7
+ require "pry"
8
+ Pry.start
data/ezclient.gemspec CHANGED
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "ezclient/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = "ezclient"
9
- spec.version = EzClient::VERSION
10
- spec.authors = ["Yuri Smirnov"]
11
- spec.email = ["tycooon@yandex.ru"]
8
+ spec.required_ruby_version = ">= 2.3.8"
12
9
 
13
- spec.summary = "An HTTP gem wrapper for easy persistent connections and more."
14
- spec.homepage = "https://github.com/umbrellio/ezclient"
15
- spec.license = "MIT"
10
+ spec.name = "ezclient"
11
+ spec.version = EzClient::VERSION
12
+ spec.authors = ["Yuri Smirnov"]
13
+ spec.email = ["tycooon@yandex.ru", "oss@umbrellio.biz"]
16
14
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
15
+ spec.summary = "An HTTP gem wrapper for easy persistent connections and more."
16
+ spec.homepage = "https://github.com/umbrellio/ezclient"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
18
20
  spec.require_paths = ["lib"]
19
21
 
20
22
  spec.add_runtime_dependency "http", ">= 4"
@@ -14,6 +14,7 @@ class EzClient::Client
14
14
  retry_exceptions
15
15
  ssl_context
16
16
  timeout
17
+ follow
17
18
  ].freeze
18
19
 
19
20
  def initialize(options = {})
@@ -101,13 +101,23 @@ class EzClient::Request
101
101
  end
102
102
 
103
103
  def perform_request
104
+ with_retry do
105
+ # Use original client so that connection can be reused
106
+ # client.perform(http_request, http_options)
107
+ res = client.perform(http_request, http_options)
108
+ return res unless follow
109
+
110
+ HTTP::Redirector.new(follow).perform(http_request, res) do |request|
111
+ client.perform(request, http_options)
112
+ end
113
+ end
114
+ end
115
+
116
+ def with_retry(&block)
104
117
  retries = 0
105
118
 
106
119
  begin
107
- retry_on_connection_error do
108
- # Use original client so that connection can be reused
109
- client.perform(http_request, http_options)
110
- end
120
+ retry_on_connection_error(&block)
111
121
  rescue *retried_exceptions => error
112
122
  if retries < max_retries.to_i
113
123
  retries += 1
@@ -152,6 +162,11 @@ class EzClient::Request
152
162
  options[:max_retries] || 1
153
163
  end
154
164
 
165
+ def follow
166
+ return unless options[:follow]
167
+ options[:follow].is_a?(Hash) ? options[:follow] : {}
168
+ end
169
+
155
170
  def prepare_headers(headers)
156
171
  headers = HTTP::Headers.coerce(headers)
157
172
  headers[:user_agent] ||= "ezclient/#{EzClient::VERSION}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EzClient
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  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: 1.2.0
4
+ version: 1.3.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-12-24 00:00:00.000000000 Z
11
+ date: 2019-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -139,6 +139,7 @@ dependencies:
139
139
  description:
140
140
  email:
141
141
  - tycooon@yandex.ru
142
+ - oss@umbrellio.biz
142
143
  executables: []
143
144
  extensions: []
144
145
  extra_rdoc_files: []
@@ -151,6 +152,7 @@ files:
151
152
  - LICENSE.txt
152
153
  - README.md
153
154
  - Rakefile
155
+ - bin/console
154
156
  - ezclient.gemspec
155
157
  - gemfiles/http3.gemfile
156
158
  - lib/ezclient.rb
@@ -172,15 +174,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
174
  requirements:
173
175
  - - ">="
174
176
  - !ruby/object:Gem::Version
175
- version: '0'
177
+ version: 2.3.8
176
178
  required_rubygems_version: !ruby/object:Gem::Requirement
177
179
  requirements:
178
180
  - - ">="
179
181
  - !ruby/object:Gem::Version
180
182
  version: '0'
181
183
  requirements: []
182
- rubyforge_project:
183
- rubygems_version: 2.7.6
184
+ rubygems_version: 3.0.3
184
185
  signing_key:
185
186
  specification_version: 4
186
187
  summary: An HTTP gem wrapper for easy persistent connections and more.