faraday_persistent_excon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d37899aeab18fc6d2c7ddd0c2916084f16fd9a9
4
+ data.tar.gz: 1b7473207a1c333965cb2664209eec54cbcaa3f5
5
+ SHA512:
6
+ metadata.gz: 3780a0356610228664824d43a5b03eaa5bb61495e2df54d1cf3819fc59ef7d3520e71c7cf01d1cf448e9d06702c5e5128364a2422dd6da942f9862d3c4e7de3e
7
+ data.tar.gz: b0dd0f0ec8854b2d93fc8035e18707fa6892880b2de5e4bacd58261f2b7b024f0e660785dbc131cc13444e14129dac2d9b9bbf31abbb1c0a882b83af25d1d16e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday_persistent_excon.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryan Schlesinger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # FaradayPersistentExcon
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/faraday_persistent_excon`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'faraday_persistent_excon'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install faraday_persistent_excon
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 rspec` 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
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/faraday_persistent_excon.
36
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "faraday_persistent_excon"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday_persistent_excon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faraday_persistent_excon"
8
+ spec.version = FaradayPersistentExcon::VERSION
9
+ spec.authors = ["Ryan Schlesinger"]
10
+ spec.email = ["ryan@aceofsales.com"]
11
+
12
+ spec.summary = %q{Persistent connections with faraday and excon}
13
+ spec.description = %q{Adds configurable connection pools per host for persistent http connections}
14
+ spec.homepage = "https://github.com/aceofsales/faraday_persistent_excon"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday"
22
+ spec.add_dependency "excon"
23
+ spec.add_dependency "connection_pool"
24
+ spec.add_dependency "activesupport"
25
+ spec.add_dependency "thread_safe"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec"
30
+ end
@@ -0,0 +1,25 @@
1
+ require 'faraday'
2
+ require 'thread_safe'
3
+ require 'connection_pool'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/class/attribute'
6
+
7
+ require 'faraday_persistent_excon/version'
8
+ require 'faraday_persistent_excon/perform_request'
9
+ require 'faraday_persistent_excon/adapter'
10
+ require 'faraday_persistent_excon/request_options'
11
+ require 'faraday_persistent_excon/connection_pools'
12
+
13
+ module FaradayPersistentExcon
14
+ class << self
15
+ attr_accessor :excon_options
16
+ attr_accessor :perform_request_class
17
+ attr_accessor :connection_pools
18
+ end
19
+
20
+ self.excon_options = {}
21
+ self.perform_request_class = FaradayPersistentExcon::PerformRequest
22
+ self.connection_pools = []
23
+ end
24
+
25
+ Faraday::Adapter.register_middleware persistent_excon: ->{ FaradayPersistentExcon::Adapter }
@@ -0,0 +1,18 @@
1
+ module FaradayPersistentExcon
2
+ class Adapter < Faraday::Adapter
3
+ dependency 'excon'
4
+
5
+ def call(env)
6
+ super
7
+ perform_request env
8
+ @app.call env
9
+ end
10
+
11
+ protected
12
+
13
+ def perform_request(env)
14
+ response = FaradayPersistentExcon.perform_request_class.new.call env
15
+ save_response(env, response.status.to_i, response.body, response.headers)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ module FaradayPersistentExcon
2
+ class ConnectionPools
3
+ class << self
4
+ attr_accessor :__pools
5
+ end
6
+ self.__pools = ThreadSafe::Cache.new
7
+
8
+ class << self
9
+ def with_connection_for(url, &block)
10
+ pool = self.connection_pool_for(url)
11
+
12
+ if pool
13
+ begin
14
+ pool.with(&block)
15
+ rescue ::Excon::Errors::SocketError
16
+ pool.shutdown { |conn| conn.reset }
17
+ self.__pools.delete(url)
18
+ raise
19
+ rescue ::ConnectionPool::PoolShuttingDownError => err
20
+ raise Faraday::Error::ConnectionFailed, err
21
+ end
22
+ else
23
+ # No pool configured. Use normal connection
24
+ block.call(::Excon.new(url, persistent: false))
25
+ end
26
+ end
27
+
28
+ def connection_pool_for(url)
29
+ config = FaradayPersistentExcon.connection_pools.find do |hsh|
30
+ hsh[:url] == url
31
+ end
32
+
33
+ if config
34
+ self.__pools.fetch_or_store(url) do
35
+ ::ConnectionPool.new(size: config[:size]) do
36
+ ::Excon.new(config[:url], persistent: true, thread_safe_sockets: false)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ module FaradayPersistentExcon
2
+ class PerformRequest
3
+ attr_accessor :env
4
+
5
+ def call(env)
6
+ @env = env
7
+
8
+ resp = nil
9
+ ConnectionPools.with_connection_for(url_without_path.to_s) do |conn|
10
+ resp = conn.request(request_options)
11
+ end
12
+
13
+ rescue ::Excon::Errors::SocketError => err
14
+ if err.message =~ /\btimeout\b/
15
+ raise Faraday::Error::TimeoutError, err
16
+ elsif err.message =~ /\bcertificate\b/
17
+ raise Faraday::Error::SSLError, err
18
+ else
19
+ raise Faraday::Error::ConnectionFailed, err
20
+ end
21
+ rescue ::Excon::Errors::Timeout => err
22
+ raise Faraday::Error::TimeoutError, err
23
+ end
24
+
25
+ protected
26
+
27
+ def request_options
28
+ RequestOptions.new.call(env).merge(
29
+ method: env[:method].to_s.upcase,
30
+ path: env[:url].path,
31
+ query: env[:url].query,
32
+ headers: env[:request_headers],
33
+ body: read_body
34
+ )
35
+ end
36
+
37
+ # TODO: support streaming requests
38
+ def read_body
39
+ env[:body].respond_to?(:read) ? env[:body].read : env[:body]
40
+ end
41
+
42
+ def url_without_path
43
+ env[:url].dup.tap do |url|
44
+ url.path = ''
45
+ url.query = nil
46
+ url.fragment = nil
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ module FaradayPersistentExcon
2
+ class RequestOptions
3
+
4
+ def call(env)
5
+ opts = FaradayPersistentExcon.excon_options.dup
6
+
7
+ if env[:url].scheme == 'https' && ssl = env[:ssl]
8
+ opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
9
+ opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
10
+ opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
11
+ opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
12
+ opts[:client_key] = ssl[:client_key] if ssl[:client_key]
13
+ opts[:certificate] = ssl[:certificate] if ssl[:certificate]
14
+ opts[:private_key] = ssl[:private_key] if ssl[:private_key]
15
+ end
16
+
17
+ if ( req = env[:request] )
18
+ if req[:timeout]
19
+ opts[:read_timeout] = req[:timeout]
20
+ opts[:connect_timeout] = req[:timeout]
21
+ opts[:write_timeout] = req[:timeout]
22
+ end
23
+
24
+ if req[:open_timeout]
25
+ opts[:connect_timeout] = req[:open_timeout]
26
+ opts[:write_timeout] = req[:open_timeout]
27
+ end
28
+
29
+ if req[:proxy]
30
+ opts[:proxy] = {
31
+ host: req[:proxy][:uri].host,
32
+ port: req[:proxy][:uri].port,
33
+ scheme: req[:proxy][:uri].scheme,
34
+ user: req[:proxy][:user],
35
+ password: req[:proxy][:password]
36
+ }
37
+ end
38
+ end
39
+
40
+ opts
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module FaradayPersistentExcon
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_persistent_excon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Schlesinger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-22 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: excon
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: connection_pool
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thread_safe
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Adds configurable connection pools per host for persistent http connections
126
+ email:
127
+ - ryan@aceofsales.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.md
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - faraday_persistent_excon.gemspec
142
+ - lib/faraday_persistent_excon.rb
143
+ - lib/faraday_persistent_excon/adapter.rb
144
+ - lib/faraday_persistent_excon/connection_pools.rb
145
+ - lib/faraday_persistent_excon/perform_request.rb
146
+ - lib/faraday_persistent_excon/request_options.rb
147
+ - lib/faraday_persistent_excon/version.rb
148
+ homepage: https://github.com/aceofsales/faraday_persistent_excon
149
+ licenses: []
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.4.7
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Persistent connections with faraday and excon
171
+ test_files: []