faraday_middleware-retry_proxy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8989b8252fcf80b2204dce3a4415c83e8d539352
4
+ data.tar.gz: 8d11ed61efdb5bcd381f0f6914690121be12058f
5
+ SHA512:
6
+ metadata.gz: 302d321ec1b376606498b2b364f5153d85d28a2d4738c248f9a6e1aa4bcb92b74abefaf322b9ff31c6e0d2c77c332e8def76e943cec32936ad6dee903aca11fe
7
+ data.tar.gz: bdd1d179b9b38c5f9140c3a90d0429ef09b1c8ad40b1bf26ac160690e218668b495e566e744db1a565674aaadb06c05fad50108e0616ae98de770fecc5d3e03a
@@ -0,0 +1,33 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+ /log/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalisation:
25
+ /.bundle/
26
+ /lib/bundler/man/
27
+
28
+ ## Misc
29
+ Gemfile.lock
30
+ gemfiles/*.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+ .rvmrc
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ env:
8
+ -
9
+ - FARADAY_VERSION=0.9.0
10
+ - FARADAY_VERSION=0.8.9
11
+
12
+ script: "bundle exec rake spec"
13
+
14
+ branches:
15
+ only:
16
+ - master
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'faraday', "~> #{ENV['FARADAY_VERSION']}" if ENV['FARADAY_VERSION'].to_s != ''
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Daisuke Taniwaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # faraday_middleware-retry_proxy
2
+
3
+ [![Gem Version][gem-image]][gem-link]
4
+ [![Dependency Status][deps-image]][deps-link]
5
+ [![Build Status][build-image]][build-link]
6
+ [![Coverage Status][cov-image]][cov-link]
7
+ [![Code Climate][gpa-image]][gpa-link]
8
+
9
+ Retry with proxy in Faraday.
10
+
11
+ ## Installation
12
+
13
+ Add the faraday_middleware-retry_proxy gem to your Gemfile.
14
+
15
+ ```ruby
16
+ gem "faraday_middleware-retry_proxy"
17
+ ```
18
+
19
+ And run `bundle install`.
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new [Pull Request](../../pull/new/master)
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2014 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
32
+
33
+
34
+
35
+
36
+ [gem-image]: https://badge.fury.io/rb/faraday_middleware-retry_proxy.svg
37
+ [gem-link]: http://badge.fury.io/rb/faraday_middleware-retry_proxy
38
+ [build-image]: https://secure.travis-ci.org/dtaniwaki/faraday_middleware-retry_proxy.png
39
+ [build-link]: http://travis-ci.org/dtaniwaki/faraday_middleware-retry_proxy
40
+ [deps-image]: https://gemnasium.com/dtaniwaki/faraday_middleware-retry_proxy.svg
41
+ [deps-link]: https://gemnasium.com/dtaniwaki/faraday_middleware-retry_proxy
42
+ [cov-image]: https://coveralls.io/repos/dtaniwaki/faraday_middleware-retry_proxy/badge.png
43
+ [cov-link]: https://coveralls.io/r/dtaniwaki/faraday_middleware-retry_proxy
44
+ [gpa-image]: https://codeclimate.com/github/dtaniwaki/faraday_middleware-retry_proxy.png
45
+ [gpa-link]: https://codeclimate.com/github/dtaniwaki/faraday_middleware-retry_proxy
46
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "faraday_middleware-retry_proxy"
3
+ gem.version = ::File.read(::File.expand_path('../VERSION', __FILE__)).to_s.strip
4
+ gem.platform = Gem::Platform::RUBY
5
+ gem.authors = ["Daisuke Taniwaki"]
6
+ gem.email = ["daisuketaniwaki@gmail.com"]
7
+ gem.homepage = "https://github.com/dtaniwaki/faraday_middleware-retry_proxy"
8
+ gem.summary = "Retry with proxy in Faraday"
9
+ gem.description = "Retry with proxy in Faraday"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.require_paths = ['lib']
16
+
17
+ gem.add_dependency "faraday", ">= 0.8.0"
18
+
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec", ">= 3.0"
21
+ gem.add_development_dependency "coveralls"
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware/retry_proxy'
@@ -0,0 +1,11 @@
1
+ if Faraday::VERSION =~ /^0\.8\.\d$/
2
+ load File.expand_path('../retry_proxy_0_8.rb', __FILE__)
3
+ Faraday.register_middleware retry_proxy: FaradayMiddleware::RetryProxy
4
+ else
5
+ load File.expand_path('../retry_proxy_0_8.rb', __FILE__)
6
+ Faraday::Request.register_middleware retry_proxy: FaradayMiddleware::RetryProxy
7
+ end
8
+
9
+ FaradayMiddleware::RetryProxy.class_eval do
10
+ VERSION = ::File.read(::File.expand_path('../../../VERSION', __FILE__)).to_s.strip
11
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'retry_proxy_core'
2
+
3
+ module FaradayMiddleware
4
+ class RetryProxy < RetryProxyCore
5
+ def initialize(app, options = {})
6
+ super(app)
7
+ @options = options
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'retry_proxy_core'
2
+
3
+ module FaradayMiddleware
4
+ class RetryProxy < RetryProxyCore
5
+ class Options < Faraday::Options.new(:interval, :interval_randomness)
6
+ def self.from(value)
7
+ if Fixnum === value
8
+ new(value)
9
+ else
10
+ super(value)
11
+ end
12
+ end
13
+
14
+ def interval
15
+ (self[:interval] ||= 0).to_f
16
+ end
17
+
18
+ def interval_randomness
19
+ (self[:interval_randomness] ||= 0).to_f
20
+ end
21
+ end
22
+
23
+ def initialize(app, options = nil)
24
+ super(app)
25
+ @options = Options.from(options)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ module FaradayMiddleware
2
+ class RetryProxyCore < Faraday::Middleware
3
+ TARGET_ERRORS = [Errno::ETIMEDOUT, Timeout::Error, Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed]
4
+
5
+ def call(env)
6
+ env[:request] ||= {}
7
+ proxies = self.normalize_proxies(env[:request][:proxy])
8
+ request_body = env[:body]
9
+ begin
10
+ proxy = proxies.pop
11
+ env[:request][:proxy] = proxy ? {uri: URI.parse(proxy)} : nil
12
+ env[:body] = request_body
13
+ @app.call(env)
14
+ rescue *TARGET_ERRORS => exception
15
+ if proxies.size > 0
16
+ sleep (1 + SecureRandom.random_number * @options[:interval_randomness].to_f) * @options[:interval].to_f
17
+ retry
18
+ end
19
+ raise
20
+ end
21
+ end
22
+
23
+ def normalize_proxies(proxy)
24
+ if proxy.respond_to?(:call)
25
+ proxy = proxy.call(self)
26
+ end
27
+ proxy ? Array(proxy) : []
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FaradayMiddleware::RetryProxy do
4
+ let(:subject_klass) { FaradayMiddleware::RetryProxy }
5
+ let(:app) { double }
6
+ let(:request) { {} }
7
+ let(:env) { {request: request, body: nil} }
8
+ before do
9
+ allow(app).to receive(:call)
10
+ end
11
+ subject { subject_klass.new app }
12
+
13
+ describe "#call" do
14
+ let(:request) { {proxy: ['x', 'y', 'z']} }
15
+ let(:error) { Faraday::Error::TimeoutError.new }
16
+ it "retries with proxy" do
17
+ expect(app).to receive(:call).with(env).exactly(3).times.and_raise error
18
+ expect {
19
+ subject.call(env)
20
+ }.to raise_error(error)
21
+ end
22
+ context "request succeeds" do
23
+ it "does not retry with proxy" do
24
+ expect(app).to receive(:call).with(env).exactly(1).times.and_return nil
25
+ expect {
26
+ subject.call(env)
27
+ }.not_to raise_error
28
+ end
29
+ end
30
+ context "no proxy" do
31
+ let(:request) { {} }
32
+ it "does not retry" do
33
+ expect(app).to receive(:call) do |env|
34
+ expect(env[:request][:proxy]).to eq nil
35
+ end
36
+ subject.call(env)
37
+ end
38
+ end
39
+ context "interval option" do
40
+ subject { subject_klass.new app, interval: 0.5 }
41
+ it "retries with interval" do
42
+ expect(app).to receive(:call).with(env).and_raise error
43
+ expect(subject).to receive(:sleep).with(0.5).and_return nil
44
+ subject.call(env)
45
+ end
46
+ context "interval_randomness option" do
47
+ subject { subject_klass.new app, interval: 1, interval_randomness: 0.5 }
48
+ it "retries with interval" do
49
+ expect(app).to receive(:call).with(env).and_raise error
50
+ expect(subject).to receive(:sleep) do |time|
51
+ expect(time).to be_within(0.5).of(1)
52
+ end
53
+ subject.call(env)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ describe "#normalize_proxies" do
59
+ it "returns a proxy as an array" do
60
+ proxies = subject.normalize_proxies('a')
61
+ expect(proxies).to eq ['a']
62
+ end
63
+ it "returns an array of proxies" do
64
+ proxies = subject.normalize_proxies(['a', 'b'])
65
+ expect(proxies).to eq ['a', 'b']
66
+ end
67
+ context "nil" do
68
+ it "returns empty array" do
69
+ proxies = subject.normalize_proxies(nil)
70
+ expect(proxies).to eq []
71
+ end
72
+ end
73
+ context "empty string" do
74
+ it "returns empty array" do
75
+ proxies = subject.normalize_proxies(nil)
76
+ expect(proxies).to eq []
77
+ end
78
+ end
79
+ context "proc" do
80
+ it "returns a proxy from the proc as an array" do
81
+ proxies = subject.normalize_proxies(lambda{ |env| 'a' })
82
+ expect(proxies).to eq ['a']
83
+ end
84
+ it "returns an array of proxies from the proc as an array" do
85
+ proxies = subject.normalize_proxies(lambda{ |env| ['a', 'b'] })
86
+ expect(proxies).to eq ['a', 'b']
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ require 'faraday_middleware-retry_proxy'
6
+
7
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
8
+
9
+ RSpec.configure do |config|
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware-retry_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daisuke Taniwaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Retry with proxy in Faraday
70
+ email:
71
+ - daisuketaniwaki@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - VERSION
83
+ - faraday_middleware-retry_proxy.gemspec
84
+ - lib/faraday_middleware-retry_proxy.rb
85
+ - lib/faraday_middleware/retry_proxy.rb
86
+ - lib/faraday_middleware/retry_proxy_0_8.rb
87
+ - lib/faraday_middleware/retry_proxy_0_9.rb
88
+ - lib/faraday_middleware/retry_proxy_core.rb
89
+ - spec/faraday_middleware/retry_proxy_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/dtaniwaki/faraday_middleware-retry_proxy
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Retry with proxy in Faraday
115
+ test_files:
116
+ - spec/faraday_middleware/retry_proxy_spec.rb
117
+ - spec/spec_helper.rb