faraday 0.8.11 → 0.9.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +6 -0
- data/CONTRIBUTING.md +36 -0
- data/Gemfile +7 -6
- data/LICENSE.md +1 -1
- data/README.md +38 -51
- data/Rakefile +2 -18
- data/faraday.gemspec +34 -0
- data/lib/faraday.rb +181 -67
- data/lib/faraday/adapter.rb +19 -34
- data/lib/faraday/adapter/em_http.rb +24 -10
- data/lib/faraday/adapter/em_synchrony.rb +1 -15
- data/lib/faraday/adapter/excon.rb +6 -12
- data/lib/faraday/adapter/httpclient.rb +92 -0
- data/lib/faraday/adapter/net_http.rb +2 -3
- data/lib/faraday/adapter/net_http_persistent.rb +3 -15
- data/lib/faraday/adapter/patron.rb +13 -21
- data/lib/faraday/adapter/rack.rb +0 -2
- data/lib/faraday/adapter/test.rb +35 -36
- data/lib/faraday/adapter/typhoeus.rb +10 -12
- data/lib/faraday/autoload.rb +87 -0
- data/lib/faraday/connection.rb +196 -99
- data/lib/faraday/error.rb +33 -33
- data/lib/faraday/options.rb +215 -0
- data/lib/faraday/parameters.rb +193 -0
- data/lib/faraday/{builder.rb → rack_builder.rb} +78 -21
- data/lib/faraday/request.rb +12 -25
- data/lib/faraday/request/authorization.rb +3 -3
- data/lib/faraday/request/basic_authentication.rb +1 -1
- data/lib/faraday/request/instrumentation.rb +38 -0
- data/lib/faraday/request/multipart.rb +10 -9
- data/lib/faraday/request/retry.rb +70 -6
- data/lib/faraday/request/token_authentication.rb +2 -2
- data/lib/faraday/request/url_encoded.rb +7 -6
- data/lib/faraday/response.rb +17 -22
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +4 -5
- data/lib/faraday/utils.rb +54 -67
- data/script/console +7 -0
- data/script/release +6 -3
- data/script/server +3 -1
- data/script/test +7 -33
- data/test/adapters/em_http_test.rb +6 -1
- data/test/adapters/em_synchrony_test.rb +7 -1
- data/test/adapters/excon_test.rb +0 -7
- data/test/adapters/httpclient_test.rb +16 -0
- data/test/adapters/integration.rb +8 -39
- data/test/adapters/logger_test.rb +1 -1
- data/test/adapters/net_http_test.rb +0 -31
- data/test/adapters/patron_test.rb +1 -1
- data/test/adapters/rack_test.rb +0 -5
- data/test/adapters/test_middleware_test.rb +19 -4
- data/test/adapters/typhoeus_test.rb +20 -3
- data/test/authentication_middleware_test.rb +7 -7
- data/test/connection_test.rb +52 -75
- data/test/env_test.rb +33 -24
- data/test/helper.rb +15 -13
- data/test/live_server.rb +10 -4
- data/test/middleware/instrumentation_test.rb +75 -0
- data/test/middleware/retry_test.rb +44 -38
- data/test/middleware_stack_test.rb +12 -11
- data/test/options_test.rb +126 -0
- data/test/request_middleware_test.rb +17 -7
- data/test/response_middleware_test.rb +2 -4
- data/test/strawberry.rb +2 -0
- metadata +82 -28
- checksums.yaml +0 -7
- data/script/proxy-server +0 -41
- data/test/multibyte.txt +0 -1
- data/test/parameters_test.rb +0 -24
- data/test/utils_test.rb +0 -30
data/.document
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## Contributing
|
2
|
+
|
3
|
+
You can run the test suite against a live server by running `script/test`. It
|
4
|
+
automatically starts a test server in background. Only tests in
|
5
|
+
`test/adapters/*_test.rb` require a server, though.
|
6
|
+
|
7
|
+
``` sh
|
8
|
+
# run the whole suite
|
9
|
+
$ script/test
|
10
|
+
|
11
|
+
# run only specific files
|
12
|
+
$ script/test excon typhoeus
|
13
|
+
|
14
|
+
# run tests using SSL
|
15
|
+
$ SSL=yes script/test
|
16
|
+
```
|
17
|
+
|
18
|
+
We will accept middleware that:
|
19
|
+
|
20
|
+
1. is useful to a broader audience, but can be implemented relatively
|
21
|
+
simple; and
|
22
|
+
2. which isn't already present in [faraday_middleware][] project.
|
23
|
+
|
24
|
+
We will accept adapters that:
|
25
|
+
|
26
|
+
1. support SSL & streaming;
|
27
|
+
1. are proven and may have better performance than existing ones; or
|
28
|
+
2. if they have features not present in included adapters.
|
29
|
+
|
30
|
+
We are pushing towards a 1.0 release, when we will have to follow [Semantic
|
31
|
+
Versioning][semver]. If your patch includes changes to break compatiblitity,
|
32
|
+
note that so we can add it to the [Changelog][].
|
33
|
+
|
34
|
+
[semver]: http://semver.org/
|
35
|
+
[changelog]: https://github.com/technoweenie/faraday/wiki/Changelog
|
36
|
+
[faraday_middleware]: https://github.com/pengwynn/faraday_middleware/wiki
|
data/Gemfile
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'ffi-ncurses', '~> 0.3', :platforms => :jruby
|
4
|
-
gem 'jruby-openssl', '~> 0.
|
4
|
+
gem 'jruby-openssl', '~> 0.7', :platforms => :jruby
|
5
5
|
gem 'rake'
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
gem '
|
9
|
-
gem 'em-http-request', '>= 1.
|
8
|
+
gem 'coveralls', :require => false
|
9
|
+
gem 'em-http-request', '>= 1.0', :require => 'em-http'
|
10
10
|
gem 'em-synchrony', '>= 1.0', :require => ['em-synchrony', 'em-synchrony/em-http']
|
11
|
-
gem 'excon', '>= 0.
|
11
|
+
gem 'excon', '>= 0.16.1'
|
12
|
+
gem 'httpclient', '>= 2.2'
|
12
13
|
gem 'leftright', '>= 0.9', :require => false
|
13
|
-
gem '
|
14
|
+
gem 'minitest', '>= 4.3'
|
14
15
|
gem 'net-http-persistent', '>= 2.5', :require => false
|
15
16
|
gem 'patron', '>= 0.4.2', :platforms => :ruby
|
16
17
|
gem 'rack-test', '>= 0.6', :require => 'rack/test'
|
17
18
|
gem 'simplecov'
|
18
19
|
gem 'sinatra', '~> 1.3'
|
19
|
-
gem 'typhoeus', '~> 0.3.3', :platforms =>
|
20
|
+
gem 'typhoeus', '~> 0.3.3', :platforms => :ruby
|
20
21
|
end
|
21
22
|
|
22
23
|
gemspec
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -24,6 +24,17 @@ conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
|
|
24
24
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
25
25
|
end
|
26
26
|
|
27
|
+
# set proxy with string
|
28
|
+
conn.options.proxy = "http://user:password@example.org/"
|
29
|
+
|
30
|
+
# set proxy with hash
|
31
|
+
conn.options.proxy = { :uri => 'http://user:passwordexample.org' }
|
32
|
+
|
33
|
+
# specify proxy user/pass
|
34
|
+
conn.options.proxy = { :uri => 'http://user:pass.org',
|
35
|
+
:user => 'user',
|
36
|
+
:password => 'pass' }
|
37
|
+
|
27
38
|
## GET ##
|
28
39
|
|
29
40
|
response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
|
@@ -51,8 +62,19 @@ end
|
|
51
62
|
|
52
63
|
conn.get do |req|
|
53
64
|
req.url '/search'
|
54
|
-
req.options
|
55
|
-
req.options
|
65
|
+
req.options.timeout = 5 # open/read timeout in seconds
|
66
|
+
req.options.open_timeout = 2 # connection open timeout in seconds
|
67
|
+
|
68
|
+
# set proxy with string
|
69
|
+
req.options.proxy = "http://user:password@example.org/"
|
70
|
+
|
71
|
+
# set proxy with hash
|
72
|
+
req.options.proxy = { :uri => 'http://user:passwordexample.org' }
|
73
|
+
|
74
|
+
# specify proxy user/pass
|
75
|
+
req.options.proxy = { :uri => 'http://user:pass.org',
|
76
|
+
:user => 'user',
|
77
|
+
:password => 'pass' }
|
56
78
|
end
|
57
79
|
```
|
58
80
|
|
@@ -175,36 +197,6 @@ stubs.verify_stubbed_calls
|
|
175
197
|
* support streaming requests/responses
|
176
198
|
* better stubbing API
|
177
199
|
|
178
|
-
## Contributing
|
179
|
-
|
180
|
-
You can run the test suite against a live server by running `script/test`. It
|
181
|
-
automatically starts a test server in background. Only tests in
|
182
|
-
`test/adapters/*_test.rb` require a server, though.
|
183
|
-
|
184
|
-
``` sh
|
185
|
-
# run the whole suite
|
186
|
-
$ script/test
|
187
|
-
|
188
|
-
# run only specific files
|
189
|
-
$ script/test excon typhoeus
|
190
|
-
```
|
191
|
-
|
192
|
-
We will accept middleware that:
|
193
|
-
|
194
|
-
1. is useful to a broader audience, but can be implemented relatively
|
195
|
-
simple; and
|
196
|
-
2. which isn't already present in [faraday_middleware][] project.
|
197
|
-
|
198
|
-
We will accept adapters that:
|
199
|
-
|
200
|
-
1. support SSL & streaming;
|
201
|
-
1. are proven and may have better performance than existing ones; or
|
202
|
-
2. if they have features not present in included adapters.
|
203
|
-
|
204
|
-
We are pushing towards a 1.0 release, when we will have to follow [Semantic
|
205
|
-
Versioning][semver]. If your patch includes changes to break compatiblitity,
|
206
|
-
note that so we can add it to the [Changelog][].
|
207
|
-
|
208
200
|
## Supported Ruby versions
|
209
201
|
|
210
202
|
This library aims to support and is [tested against][travis] the following Ruby
|
@@ -213,11 +205,11 @@ implementations:
|
|
213
205
|
* MRI 1.8.7
|
214
206
|
* MRI 1.9.2
|
215
207
|
* MRI 1.9.3
|
208
|
+
* MRI 2.0.0
|
216
209
|
* [JRuby][]
|
217
210
|
* [Rubinius][]
|
218
211
|
|
219
|
-
If something doesn't work on one of these
|
220
|
-
a bug.
|
212
|
+
If something doesn't work on one of these Ruby versions, it's a bug.
|
221
213
|
|
222
214
|
This library may inadvertently work (or seem to work) on other Ruby
|
223
215
|
implementations, however support will only be provided for the versions listed
|
@@ -226,25 +218,20 @@ above.
|
|
226
218
|
If you would like this library to support another Ruby version, you may
|
227
219
|
volunteer to be a maintainer. Being a maintainer entails making sure all tests
|
228
220
|
run and pass on that implementation. When something breaks on your
|
229
|
-
implementation, you will be
|
230
|
-
|
231
|
-
|
221
|
+
implementation, you will be responsible for providing patches in a timely
|
222
|
+
fashion. If critical issues for a particular implementation exist at the time
|
223
|
+
of a major release, support for that Ruby version may be dropped.
|
232
224
|
|
233
225
|
## Copyright
|
234
226
|
|
235
|
-
Copyright (c) 2009-
|
227
|
+
Copyright (c) 2009-2013 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
|
236
228
|
See [LICENSE][] for details.
|
237
229
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
[typhoeus]: https://github.com/typhoeus/typhoeus#readme
|
247
|
-
[patron]: http://toland.github.com/patron/
|
248
|
-
|
249
|
-
[eventmachine]: https://github.com/igrigorik/em-http-request#readme
|
250
|
-
[faraday_middleware]: https://github.com/pengwynn/faraday_middleware/wiki
|
230
|
+
[travis]: http://travis-ci.org/lostisland/faraday
|
231
|
+
[excon]: https://github.com/geemus/excon#readme
|
232
|
+
[typhoeus]: https://github.com/typhoeus/typhoeus#readme
|
233
|
+
[patron]: http://toland.github.com/patron/
|
234
|
+
[eventmachine]: https://github.com/igrigorik/em-http-request#readme
|
235
|
+
[jruby]: http://jruby.org/
|
236
|
+
[rubinius]: http://rubini.us/
|
237
|
+
[license]: LICENSE.md
|
data/Rakefile
CHANGED
@@ -2,6 +2,8 @@ require 'date'
|
|
2
2
|
require 'fileutils'
|
3
3
|
require 'openssl'
|
4
4
|
require 'rake/testtask'
|
5
|
+
require 'bundler'
|
6
|
+
Bundler::GemHelper.install_tasks
|
5
7
|
|
6
8
|
task :default => :test
|
7
9
|
|
@@ -67,21 +69,3 @@ desc "Open an irb session preloaded with this library"
|
|
67
69
|
task :console do
|
68
70
|
sh "irb -rubygems -r ./lib/#{name}.rb"
|
69
71
|
end
|
70
|
-
|
71
|
-
## release management tasks
|
72
|
-
|
73
|
-
desc "Commit, create tag v#{version} and build and push #{gem_file} to Rubygems"
|
74
|
-
task :release => :build do
|
75
|
-
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
76
|
-
sh "git tag v#{version}"
|
77
|
-
sh "git push origin"
|
78
|
-
sh "git push origin v#{version}"
|
79
|
-
sh "gem push pkg/#{gem_file}"
|
80
|
-
end
|
81
|
-
|
82
|
-
desc "Build #{gem_file} into the pkg directory"
|
83
|
-
task :build do
|
84
|
-
FileUtils.mkdir_p 'pkg'
|
85
|
-
sh "gem build #{gemspec_file}"
|
86
|
-
sh "mv #{gem_file} pkg"
|
87
|
-
end
|
data/faraday.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = "faraday"
|
2
|
+
lib_file = File.expand_path("../lib/#{lib}.rb", __FILE__)
|
3
|
+
File.read(lib_file) =~ /\bVERSION\s*=\s*["'](.+?)["']/
|
4
|
+
version = $1
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.specification_version = 2 if spec.respond_to? :specification_version=
|
8
|
+
spec.required_rubygems_version = '>= 1.3.5'
|
9
|
+
|
10
|
+
spec.name = lib
|
11
|
+
spec.version = version
|
12
|
+
|
13
|
+
spec.summary = "HTTP/REST API client library."
|
14
|
+
|
15
|
+
spec.authors = ["Rick Olson"]
|
16
|
+
spec.email = 'technoweenie@gmail.com'
|
17
|
+
spec.homepage = 'https://github.com/lostisland/faraday'
|
18
|
+
spec.licenses = ['MIT']
|
19
|
+
|
20
|
+
spec.add_dependency 'multipart-post', '~> 1.1'
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
22
|
+
|
23
|
+
spec.files = %w(.document CONTRIBUTING.md Gemfile LICENSE.md README.md Rakefile)
|
24
|
+
spec.files << "#{lib}.gemspec"
|
25
|
+
spec.files += Dir.glob("lib/**/*.rb")
|
26
|
+
spec.files += Dir.glob("test/**/*.rb")
|
27
|
+
spec.files += Dir.glob("script/*")
|
28
|
+
|
29
|
+
dev_null = File.exist?('/dev/null') ? '/dev/null' : 'NUL'
|
30
|
+
git_files = `git ls-files -z 2>#{dev_null}`
|
31
|
+
spec.files &= git_files.split("\0") if $?.success?
|
32
|
+
|
33
|
+
spec.test_files = Dir.glob("test/**/*.rb")
|
34
|
+
end
|
data/lib/faraday.rb
CHANGED
@@ -1,25 +1,100 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'cgi'
|
3
|
+
require 'set'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
# Public: This is the main namespace for Faraday. You can either use it to
|
7
|
+
# create Faraday::Connection objects, or access it directly.
|
8
|
+
#
|
9
|
+
# Examples
|
10
|
+
#
|
11
|
+
# Faraday.get "http://faraday.com"
|
12
|
+
#
|
13
|
+
# conn = Faraday.new "http://faraday.com"
|
14
|
+
# conn.get '/'
|
15
|
+
#
|
1
16
|
module Faraday
|
2
|
-
VERSION = "0.
|
17
|
+
VERSION = "0.9.0.rc1"
|
3
18
|
|
4
19
|
class << self
|
5
|
-
|
20
|
+
# Public: Gets or sets the root path that Faraday is being loaded from.
|
21
|
+
# This is the root from where the libraries are auto-loaded from.
|
22
|
+
attr_accessor :root_path
|
23
|
+
|
24
|
+
# Public: Gets or sets the path that the Faraday libs are loaded from.
|
25
|
+
attr_accessor :lib_path
|
26
|
+
|
27
|
+
# Public: Gets or sets the Symbol key identifying a default Adapter to use
|
28
|
+
# for the default Faraday::Connection.
|
6
29
|
attr_accessor :default_adapter
|
7
|
-
attr_writer :default_connection
|
8
30
|
|
9
|
-
|
31
|
+
# Public: Sets the default Faraday::Connection for simple scripts that
|
32
|
+
# access the Faraday constant directly.
|
33
|
+
#
|
34
|
+
# Faraday.get "https://faraday.com"
|
35
|
+
attr_writer :default_connection
|
36
|
+
|
37
|
+
# Public: Sets the default options used when calling Faraday#new.
|
38
|
+
attr_writer :default_connection_options
|
39
|
+
|
40
|
+
# Public: Initializes a new Faraday::Connection.
|
41
|
+
#
|
42
|
+
# url - The optional String base URL to use as a prefix for all
|
43
|
+
# requests. Can also be the options Hash.
|
44
|
+
# options - The optional Hash used to configure this Faraday::Connection.
|
45
|
+
# Any of these values will be set on every request made, unless
|
46
|
+
# overridden for a specific request.
|
47
|
+
# :url - String base URL.
|
48
|
+
# :params - Hash of URI query unencoded key/value pairs.
|
49
|
+
# :headers - Hash of unencoded HTTP header key/value pairs.
|
50
|
+
# :request - Hash of request options.
|
51
|
+
# :ssl - Hash of SSL options.
|
52
|
+
# :proxy - Hash of Proxy options.
|
53
|
+
#
|
54
|
+
# Examples
|
55
|
+
#
|
56
|
+
# Faraday.new 'http://faraday.com'
|
57
|
+
#
|
58
|
+
# # http://faraday.com?page=1
|
59
|
+
# Faraday.new 'http://faraday.com', :params => {:page => 1}
|
60
|
+
#
|
61
|
+
# # same
|
62
|
+
#
|
63
|
+
# Faraday.new :url => 'http://faraday.com',
|
64
|
+
# :params => {:page => 1}
|
65
|
+
#
|
66
|
+
# Returns a Faraday::Connection.
|
67
|
+
def new(url = nil, options = nil)
|
10
68
|
block = block_given? ? Proc.new : nil
|
69
|
+
options = options ? default_connection_options.merge(options) : default_connection_options.dup
|
11
70
|
Faraday::Connection.new(url, options, &block)
|
12
71
|
end
|
13
72
|
|
73
|
+
# Internal: Requires internal Faraday libraries.
|
74
|
+
#
|
75
|
+
# *libs - One or more relative String names to Faraday classes.
|
76
|
+
#
|
77
|
+
# Returns nothing.
|
14
78
|
def require_libs(*libs)
|
15
79
|
libs.each do |lib|
|
16
80
|
require "#{lib_path}/#{lib}"
|
17
81
|
end
|
18
82
|
end
|
19
83
|
|
84
|
+
# Public: Updates default adapter while resetting
|
85
|
+
# #default_connection.
|
86
|
+
#
|
87
|
+
# Returns the new default_adapter.
|
88
|
+
def default_adapter=(adapter)
|
89
|
+
@default_connection = nil
|
90
|
+
@default_adapter = adapter
|
91
|
+
end
|
92
|
+
|
20
93
|
alias require_lib require_libs
|
21
94
|
|
22
95
|
private
|
96
|
+
# Internal: Proxies method calls on the Faraday constant to
|
97
|
+
# #default_connection.
|
23
98
|
def method_missing(name, *args, &block)
|
24
99
|
default_connection.send(name, *args, &block)
|
25
100
|
end
|
@@ -29,10 +104,20 @@ module Faraday
|
|
29
104
|
self.lib_path = File.expand_path "../faraday", __FILE__
|
30
105
|
self.default_adapter = :net_http
|
31
106
|
|
107
|
+
# Gets the default connection used for simple scripts.
|
108
|
+
#
|
109
|
+
# Returns a Faraday::Connection, configured with the #default_adapter.
|
32
110
|
def self.default_connection
|
33
111
|
@default_connection ||= Connection.new
|
34
112
|
end
|
35
113
|
|
114
|
+
# Gets the default connection options used when calling Faraday#new.
|
115
|
+
#
|
116
|
+
# Returns a Faraday::ConnectionOptions.
|
117
|
+
def self.default_connection_options
|
118
|
+
@default_connection_options ||= ConnectionOptions.new
|
119
|
+
end
|
120
|
+
|
36
121
|
if (!defined?(RUBY_ENGINE) || "ruby" == RUBY_ENGINE) && RUBY_VERSION < '1.9'
|
37
122
|
begin
|
38
123
|
require 'system_timer'
|
@@ -47,101 +132,130 @@ module Faraday
|
|
47
132
|
Timer = Timeout
|
48
133
|
end
|
49
134
|
|
135
|
+
# Public: Adds the ability for other modules to register and lookup
|
136
|
+
# middleware classes.
|
50
137
|
module MiddlewareRegistry
|
51
|
-
#
|
138
|
+
# Public: Register middleware class(es) on the current module.
|
139
|
+
#
|
140
|
+
# mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
|
141
|
+
# as fully qualified constant, or a Proc that will be lazily
|
142
|
+
# called to return the former.
|
143
|
+
#
|
144
|
+
# Examples
|
145
|
+
#
|
146
|
+
# module Faraday
|
147
|
+
# class Whatever
|
148
|
+
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
149
|
+
# register_middleware :foo => Foo
|
150
|
+
#
|
151
|
+
# # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
|
152
|
+
# register_middleware :bar => :Bar
|
52
153
|
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
|
56
|
-
|
154
|
+
# # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
|
155
|
+
# register_middleware :baz => [:Baz, 'baz']
|
156
|
+
# end
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# Returns nothing.
|
160
|
+
def register_middleware(autoload_path = nil, mapping = nil)
|
161
|
+
if mapping.nil?
|
162
|
+
mapping = autoload_path
|
163
|
+
autoload_path = nil
|
164
|
+
end
|
165
|
+
middleware_mutex do
|
166
|
+
@middleware_autoload_path = autoload_path if autoload_path
|
167
|
+
(@registered_middleware ||= {}).update(mapping)
|
168
|
+
end
|
57
169
|
end
|
58
170
|
|
59
|
-
#
|
171
|
+
# Public: Lookup middleware class with a registered Symbol shortcut.
|
172
|
+
#
|
173
|
+
# key - The Symbol key for the registered middleware.
|
174
|
+
#
|
175
|
+
# Examples
|
176
|
+
#
|
177
|
+
# module Faraday
|
178
|
+
# class Whatever
|
179
|
+
# register_middleware :foo => Foo
|
180
|
+
# end
|
181
|
+
# end
|
182
|
+
#
|
183
|
+
# Faraday::Whatever.lookup_middleware(:foo)
|
184
|
+
# # => Faraday::Whatever::Foo
|
60
185
|
#
|
61
186
|
# Returns a middleware Class.
|
62
187
|
def lookup_middleware(key)
|
63
|
-
|
64
|
-
raise
|
65
|
-
end
|
66
|
-
found = @registered_middleware[key] = found.call if found.is_a? Proc
|
67
|
-
found.is_a?(Module) ? found : const_get(found)
|
188
|
+
load_middleware(key) ||
|
189
|
+
raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
|
68
190
|
end
|
69
|
-
end
|
70
191
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
76
|
-
options.each do |const_name, path|
|
77
|
-
autoload const_name, File.join(prefix, path)
|
192
|
+
def middleware_mutex(&block)
|
193
|
+
@middleware_mutex ||= begin
|
194
|
+
require 'monitor'
|
195
|
+
Monitor.new
|
78
196
|
end
|
197
|
+
@middleware_mutex.synchronize(&block)
|
79
198
|
end
|
80
199
|
|
81
|
-
|
82
|
-
|
83
|
-
def load_autoloaded_constants
|
84
|
-
constants.each do |const|
|
85
|
-
const_get(const) if autoload?(const)
|
86
|
-
end
|
200
|
+
def fetch_middleware(key)
|
201
|
+
defined?(@registered_middleware) && @registered_middleware[key]
|
87
202
|
end
|
88
203
|
|
89
|
-
def
|
90
|
-
|
91
|
-
|
204
|
+
def load_middleware(key)
|
205
|
+
value = fetch_middleware(key)
|
206
|
+
case value
|
207
|
+
when Module then value
|
208
|
+
when Symbol, String
|
209
|
+
middleware_mutex do
|
210
|
+
@registered_middleware[key] = const_get(value)
|
211
|
+
end
|
212
|
+
when Proc
|
213
|
+
middleware_mutex do
|
214
|
+
@registered_middleware[key] = value.call
|
215
|
+
end
|
216
|
+
when Array
|
217
|
+
middleware_mutex do
|
218
|
+
const, path = value
|
219
|
+
if root = @middleware_autoload_path
|
220
|
+
path = "#{root}/#{path}"
|
221
|
+
end
|
222
|
+
require(path)
|
223
|
+
@registered_middleware[key] = const
|
224
|
+
end
|
225
|
+
load_middleware(key)
|
226
|
+
end
|
92
227
|
end
|
93
228
|
end
|
94
229
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
# to return the former.
|
103
|
-
#
|
104
|
-
# Examples
|
105
|
-
#
|
106
|
-
# Faraday.register_middleware :aloha => MyModule::Aloha
|
107
|
-
# Faraday.register_middleware :response, :boom => MyModule::Boom
|
108
|
-
#
|
109
|
-
# # shortcuts are now available in Builder:
|
110
|
-
# builder.use :aloha
|
111
|
-
# builder.response :boom
|
112
|
-
#
|
113
|
-
# Returns nothing.
|
114
|
-
def self.register_middleware type, mapping = nil
|
115
|
-
type, mapping = :middleware, type if mapping.nil?
|
116
|
-
component = self.const_get(type.to_s.capitalize)
|
117
|
-
component.register_middleware(mapping)
|
230
|
+
def self.const_missing(name)
|
231
|
+
if name.to_sym == :Builder
|
232
|
+
warn "Faraday::Builder is now Faraday::RackBuilder."
|
233
|
+
const_set name, RackBuilder
|
234
|
+
else
|
235
|
+
super
|
236
|
+
end
|
118
237
|
end
|
119
238
|
|
120
|
-
|
121
|
-
|
122
|
-
:Builder => 'builder',
|
123
|
-
:Request => 'request',
|
124
|
-
:Response => 'response',
|
125
|
-
:CompositeReadIO => 'upload_io',
|
126
|
-
:UploadIO => 'upload_io',
|
127
|
-
:Parts => 'upload_io'
|
128
|
-
|
129
|
-
require_libs "utils", "connection", "adapter", "error"
|
239
|
+
require_libs "utils", "options", "connection", "rack_builder", "parameters",
|
240
|
+
"middleware", "adapter", "request", "response", "upload_io", "error"
|
130
241
|
end
|
131
242
|
|
132
|
-
|
133
243
|
# not pulling in active-support JUST for this method. And I love this method.
|
134
244
|
class Object
|
135
|
-
# Yields <code>x</code> to the block, and then returns <code>x</code>.
|
136
245
|
# The primary purpose of this method is to "tap into" a method chain,
|
137
246
|
# in order to perform operations on intermediate results within the chain.
|
138
247
|
#
|
248
|
+
# Examples
|
249
|
+
#
|
139
250
|
# (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
|
140
251
|
# tap { |x| puts "array: #{x.inspect}" }.
|
141
252
|
# select { |x| x%2 == 0 }.
|
142
253
|
# tap { |x| puts "evens: #{x.inspect}" }.
|
143
254
|
# map { |x| x*x }.
|
144
255
|
# tap { |x| puts "squares: #{x.inspect}" }
|
256
|
+
#
|
257
|
+
# Yields self.
|
258
|
+
# Returns self.
|
145
259
|
def tap
|
146
260
|
yield self
|
147
261
|
self
|