shin-faraday 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +84 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/faraday.gemspec +87 -0
- data/lib/faraday.rb +94 -0
- data/lib/faraday/adapter/action_dispatch.rb +33 -0
- data/lib/faraday/adapter/net_http.rb +59 -0
- data/lib/faraday/adapter/patron.rb +33 -0
- data/lib/faraday/adapter/test.rb +117 -0
- data/lib/faraday/adapter/typhoeus.rb +65 -0
- data/lib/faraday/builder.rb +82 -0
- data/lib/faraday/connection.rb +263 -0
- data/lib/faraday/error.rb +8 -0
- data/lib/faraday/middleware.rb +54 -0
- data/lib/faraday/request.rb +87 -0
- data/lib/faraday/request/active_support_json.rb +21 -0
- data/lib/faraday/request/yajl.rb +18 -0
- data/lib/faraday/response.rb +56 -0
- data/lib/faraday/response/active_support_json.rb +31 -0
- data/lib/faraday/response/nokogiri.rb +21 -0
- data/lib/faraday/response/yajl.rb +26 -0
- data/test/adapters/live_test.rb +141 -0
- data/test/adapters/test_middleware_test.rb +26 -0
- data/test/adapters/typhoeus_test.rb +26 -0
- data/test/connection_app_test.rb +60 -0
- data/test/connection_test.rb +284 -0
- data/test/env_test.rb +56 -0
- data/test/helper.rb +32 -0
- data/test/live_server.rb +34 -0
- data/test/request_middleware_test.rb +24 -0
- data/test/response_middleware_test.rb +44 -0
- metadata +142 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-* rick olson, zack hobson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= faraday
|
2
|
+
|
3
|
+
Modular HTTP client library using middleware heavily inspired by Rack.
|
4
|
+
|
5
|
+
This mess is gonna get raw, like sushi. So, haters to the left.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
conn = Faraday::Connection.new(:url => 'http://sushi.com') do |builder|
|
10
|
+
builder.use Faraday::Request::Yajl # convert body to json with Yajl lib
|
11
|
+
builder.use Faraday::Adapter::Logger # log the request somewhere?
|
12
|
+
builder.use Faraday::Adapter::Typhoeus # make http request with typhoeus
|
13
|
+
builder.use Faraday::Response::Yajl # # parse body with yajl
|
14
|
+
|
15
|
+
# or use shortcuts
|
16
|
+
builder.request :yajl # Faraday::Request::Yajl
|
17
|
+
builder.adapter :logger # Faraday::Adapter::Logger
|
18
|
+
builder.adapter :typhoeus # Faraday::Adapter::Typhoeus
|
19
|
+
builder.response :yajl # Faraday::Response::Yajl
|
20
|
+
end
|
21
|
+
|
22
|
+
resp1 = conn.get '/nigiri/sake.json'
|
23
|
+
resp2 = conn.post do |req|
|
24
|
+
req.url "/nigiri.json", :page => 2
|
25
|
+
req[:content_type] = 'application/json'
|
26
|
+
req.body = {:name => 'Unagi'}
|
27
|
+
end
|
28
|
+
|
29
|
+
# If you're ready to roll with just the bare minimum (net/http):
|
30
|
+
resp1 = Faraday.get 'http://sushi.com/nigiri/sake.json'
|
31
|
+
|
32
|
+
== Testing
|
33
|
+
|
34
|
+
# It's possible to define stubbed request outside a test adapter block.
|
35
|
+
stubs = Faraday::Test::Stubs.new do |stub|
|
36
|
+
stub.get('/tamago') { [200, {}, 'egg'] }
|
37
|
+
end
|
38
|
+
|
39
|
+
# You can pass stubbed request to the test adapter or define them in a block
|
40
|
+
# or a combination of the two.
|
41
|
+
test = Faraday::Connection.new do |builder|
|
42
|
+
builder.adapter :test, stubs do |stub|
|
43
|
+
stub.get('/ebi') {[ 200, {}, 'shrimp' ]}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# It's also possible to stub additional requests after the connection has
|
48
|
+
# been initialized. This is useful for testing.
|
49
|
+
stubs.get('/uni') {[ 200, {}, 'urchin' ]}
|
50
|
+
|
51
|
+
resp = test.get '/tamago'
|
52
|
+
resp.body # => 'egg'
|
53
|
+
resp = test.get '/ebi'
|
54
|
+
resp.body # => 'shrimp'
|
55
|
+
resp = test.get '/uni'
|
56
|
+
resp.body # => 'urchin'
|
57
|
+
resp = test.get '/else' #=> raises "no such stub" error
|
58
|
+
|
59
|
+
# If you like, you can treat your stubs as mocks by verifying that all of
|
60
|
+
# the stubbed calls were made. NOTE that this feature is still fairly
|
61
|
+
# experimental: It will not verify the order or count of any stub, only that
|
62
|
+
# it was called once during the course of the test.
|
63
|
+
stubs.verify_stubbed_calls
|
64
|
+
|
65
|
+
== TODO
|
66
|
+
|
67
|
+
* support streaming requests/responses
|
68
|
+
* better stubbing API
|
69
|
+
* Support timeouts
|
70
|
+
* Add curb, em-http, fast_http
|
71
|
+
|
72
|
+
== Note on Patches/Pull Requests
|
73
|
+
|
74
|
+
* Fork the project.
|
75
|
+
* Make your feature addition or bug fix.
|
76
|
+
* Add tests for it. This is important so I don't break it in a
|
77
|
+
future version unintentionally.
|
78
|
+
* Commit, do not mess with rakefile, version, or history.
|
79
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
80
|
+
* Send me a pull request. Bonus points for topic branches.
|
81
|
+
|
82
|
+
== Copyright
|
83
|
+
|
84
|
+
Copyright (c) 2009-2010 rick, hobson. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "shin-faraday"
|
8
|
+
gem.summary = "HTTP/REST API client library"
|
9
|
+
gem.description = "HTTP/REST API client library with pluggable components"
|
10
|
+
gem.email = "technoweenie@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/shingara/faraday"
|
12
|
+
gem.authors = ["rick", "Cyril Mougel"]
|
13
|
+
gem.add_dependency "rack", ">= 1.0.1"
|
14
|
+
gem.add_dependency "addressable", ">= 2.1.1"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
|
18
|
+
task :test => :check_dependencies
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/**/*_test.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rcov/rcovtask'
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << 'test'
|
34
|
+
test.pattern = 'test/**/*_test.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "faraday #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.7
|
data/faraday.gemspec
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{faraday}
|
8
|
+
s.version = "0.4.6"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["rick"]
|
12
|
+
s.date = %q{2010-05-28}
|
13
|
+
s.description = %q{HTTP/REST API client library with pluggable components}
|
14
|
+
s.email = %q{technoweenie@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"faraday.gemspec",
|
27
|
+
"lib/faraday.rb",
|
28
|
+
"lib/faraday/adapter/net_http.rb",
|
29
|
+
"lib/faraday/adapter/patron.rb",
|
30
|
+
"lib/faraday/adapter/test.rb",
|
31
|
+
"lib/faraday/adapter/typhoeus.rb",
|
32
|
+
"lib/faraday/builder.rb",
|
33
|
+
"lib/faraday/connection.rb",
|
34
|
+
"lib/faraday/error.rb",
|
35
|
+
"lib/faraday/middleware.rb",
|
36
|
+
"lib/faraday/request.rb",
|
37
|
+
"lib/faraday/request/active_support_json.rb",
|
38
|
+
"lib/faraday/request/yajl.rb",
|
39
|
+
"lib/faraday/response.rb",
|
40
|
+
"lib/faraday/response/active_support_json.rb",
|
41
|
+
"lib/faraday/response/yajl.rb",
|
42
|
+
"test/adapters/live_test.rb",
|
43
|
+
"test/adapters/test_middleware_test.rb",
|
44
|
+
"test/adapters/typhoeus_test.rb",
|
45
|
+
"test/connection_app_test.rb",
|
46
|
+
"test/connection_test.rb",
|
47
|
+
"test/env_test.rb",
|
48
|
+
"test/helper.rb",
|
49
|
+
"test/live_server.rb",
|
50
|
+
"test/request_middleware_test.rb",
|
51
|
+
"test/response_middleware_test.rb"
|
52
|
+
]
|
53
|
+
s.homepage = %q{http://github.com/technoweenie/faraday}
|
54
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
55
|
+
s.require_paths = ["lib"]
|
56
|
+
s.rubygems_version = %q{1.3.6}
|
57
|
+
s.summary = %q{HTTP/REST API client library}
|
58
|
+
s.test_files = [
|
59
|
+
"test/adapters/live_test.rb",
|
60
|
+
"test/adapters/test_middleware_test.rb",
|
61
|
+
"test/adapters/typhoeus_test.rb",
|
62
|
+
"test/connection_app_test.rb",
|
63
|
+
"test/connection_test.rb",
|
64
|
+
"test/env_test.rb",
|
65
|
+
"test/helper.rb",
|
66
|
+
"test/live_server.rb",
|
67
|
+
"test/request_middleware_test.rb",
|
68
|
+
"test/response_middleware_test.rb"
|
69
|
+
]
|
70
|
+
|
71
|
+
if s.respond_to? :specification_version then
|
72
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
73
|
+
s.specification_version = 3
|
74
|
+
|
75
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
76
|
+
s.add_runtime_dependency(%q<rack>, [">= 1.0.1"])
|
77
|
+
s.add_runtime_dependency(%q<addressable>, [">= 2.1.1"])
|
78
|
+
else
|
79
|
+
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
80
|
+
s.add_dependency(%q<addressable>, [">= 2.1.1"])
|
81
|
+
end
|
82
|
+
else
|
83
|
+
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
84
|
+
s.add_dependency(%q<addressable>, [">= 2.1.1"])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
data/lib/faraday.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class << self
|
5
|
+
attr_accessor :default_adapter
|
6
|
+
attr_writer :default_connection
|
7
|
+
|
8
|
+
private
|
9
|
+
def method_missing(name, *args, &block)
|
10
|
+
default_connection.send(name, *args, &block)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
self.default_adapter = :net_http
|
15
|
+
|
16
|
+
def self.default_connection
|
17
|
+
@default_connection ||= Connection.new
|
18
|
+
end
|
19
|
+
|
20
|
+
module AutoloadHelper
|
21
|
+
def register_lookup_modules(mods)
|
22
|
+
(@lookup_module_index ||= {}).update(mods)
|
23
|
+
end
|
24
|
+
|
25
|
+
def lookup_module(key)
|
26
|
+
return if !@lookup_module_index
|
27
|
+
const_get @lookup_module_index[key] || key
|
28
|
+
end
|
29
|
+
|
30
|
+
def autoload_all(prefix, options)
|
31
|
+
options.each do |const_name, path|
|
32
|
+
autoload const_name, File.join(prefix, path)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Loads each autoloaded constant. If thread safety is a concern, wrap
|
37
|
+
# this in a Mutex.
|
38
|
+
def load_autoloaded_constants
|
39
|
+
constants.each do |const|
|
40
|
+
const_get(const) if autoload?(const)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def all_loaded_constants
|
45
|
+
constants.map { |c| const_get(c) }.select { |a| a.loaded? }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
extend AutoloadHelper
|
50
|
+
|
51
|
+
autoload_all 'faraday',
|
52
|
+
:Connection => 'connection',
|
53
|
+
:Middleware => 'middleware',
|
54
|
+
:Builder => 'builder',
|
55
|
+
:Request => 'request',
|
56
|
+
:Response => 'response',
|
57
|
+
:Error => 'error'
|
58
|
+
|
59
|
+
module Adapter
|
60
|
+
extend AutoloadHelper
|
61
|
+
autoload_all 'faraday/adapter',
|
62
|
+
:ActionDispatch => 'action_dispatch',
|
63
|
+
:NetHttp => 'net_http',
|
64
|
+
:Typhoeus => 'typhoeus',
|
65
|
+
:Patron => 'patron',
|
66
|
+
:Test => 'test'
|
67
|
+
|
68
|
+
register_lookup_modules \
|
69
|
+
:action_dispatch => :ActionDispatch,
|
70
|
+
:test => :Test,
|
71
|
+
:net_http => :NetHttp,
|
72
|
+
:typhoeus => :Typhoeus,
|
73
|
+
:patron => :Patron,
|
74
|
+
:net_http => :NetHttp
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# not pulling in active-support JUST for this method.
|
79
|
+
class Object
|
80
|
+
# Yields <code>x</code> to the block, and then returns <code>x</code>.
|
81
|
+
# The primary purpose of this method is to "tap into" a method chain,
|
82
|
+
# in order to perform operations on intermediate results within the chain.
|
83
|
+
#
|
84
|
+
# (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
|
85
|
+
# tap { |x| puts "array: #{x.inspect}" }.
|
86
|
+
# select { |x| x%2 == 0 }.
|
87
|
+
# tap { |x| puts "evens: #{x.inspect}" }.
|
88
|
+
# map { |x| x*x }.
|
89
|
+
# tap { |x| puts "squares: #{x.inspect}" }
|
90
|
+
def tap
|
91
|
+
yield self
|
92
|
+
self
|
93
|
+
end unless Object.respond_to?(:tap)
|
94
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Faraday
|
2
|
+
module Adapter
|
3
|
+
class ActionDispatch < Middleware
|
4
|
+
attr_reader :session
|
5
|
+
|
6
|
+
# Initializes a new middleware instance for each request. Instead of
|
7
|
+
# initiating an HTTP request with a web server, this adapter calls
|
8
|
+
# a Rails 3 app using integration tests.
|
9
|
+
#
|
10
|
+
# app - The current Faraday request.
|
11
|
+
# session - An ActionDispatch::Integration::Session instance.
|
12
|
+
#
|
13
|
+
# Returns nothing.
|
14
|
+
def initialize(app, session)
|
15
|
+
super(app)
|
16
|
+
@session = session
|
17
|
+
@session.reset!
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(env)
|
21
|
+
process_body_for_request(env)
|
22
|
+
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
|
23
|
+
@session.__send__(env[:method], full_path, env[:body], env[:request_headers])
|
24
|
+
resp = @session.response
|
25
|
+
env.update \
|
26
|
+
:status => resp.status,
|
27
|
+
:response_headers => resp.headers,
|
28
|
+
:body => resp.body
|
29
|
+
@app.call env
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
begin
|
2
|
+
require 'net/https'
|
3
|
+
rescue LoadError
|
4
|
+
puts "no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
|
5
|
+
require 'net/http'
|
6
|
+
end
|
7
|
+
|
8
|
+
module Faraday
|
9
|
+
module Adapter
|
10
|
+
class NetHttp < Middleware
|
11
|
+
def call(env)
|
12
|
+
process_body_for_request(env)
|
13
|
+
|
14
|
+
is_ssl = env[:url].scheme == 'https'
|
15
|
+
|
16
|
+
http = net_http_class(env).new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
|
17
|
+
if http.use_ssl = is_ssl
|
18
|
+
ssl = env[:ssl]
|
19
|
+
if ssl[:verify] == false
|
20
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
21
|
+
else
|
22
|
+
http.verify_mode = ssl[:verify]
|
23
|
+
end
|
24
|
+
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
25
|
+
http.key = ssl[:client_key] if ssl[:client_key]
|
26
|
+
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
27
|
+
end
|
28
|
+
req = env[:request]
|
29
|
+
http.read_timeout = net.open_timeout = req[:timeout] if req[:timeout]
|
30
|
+
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
31
|
+
|
32
|
+
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
|
33
|
+
http_resp = http.send_request(env[:method].to_s.upcase, full_path, env[:body], env[:request_headers])
|
34
|
+
|
35
|
+
resp_headers = {}
|
36
|
+
http_resp.each_header do |key, value|
|
37
|
+
resp_headers[key] = value
|
38
|
+
end
|
39
|
+
|
40
|
+
env.update \
|
41
|
+
:status => http_resp.code.to_i,
|
42
|
+
:response_headers => resp_headers,
|
43
|
+
:body => http_resp.body
|
44
|
+
|
45
|
+
@app.call env
|
46
|
+
rescue Errno::ECONNREFUSED
|
47
|
+
raise Error::ConnectionFailed, "connection refused"
|
48
|
+
end
|
49
|
+
|
50
|
+
def net_http_class(env)
|
51
|
+
if proxy = env[:request][:proxy]
|
52
|
+
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
|
53
|
+
else
|
54
|
+
Net::HTTP
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|