faraday 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/faraday.gemspec +2 -2
- data/lib/faraday.rb +25 -12
- data/lib/faraday/adapter/mock_request.rb +7 -4
- data/lib/faraday/adapter/net_http.rb +2 -2
- data/lib/faraday/connection.rb +55 -18
- data/lib/faraday/response.rb +3 -1
- data/test/adapter_test.rb +10 -0
- data/test/connection_test.rb +15 -0
- data/test/helper.rb +6 -0
- data/test/live_server.rb +8 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/faraday.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{faraday}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["rick"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-01-02}
|
13
13
|
s.description = %q{HTTP/REST API client library with pluggable components}
|
14
14
|
s.email = %q{technoweenie@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/faraday.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
1
|
module Faraday
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
module AutoloadHelper
|
3
|
+
def autoload_all(prefix, options)
|
4
|
+
options.each do |const_name, path|
|
5
|
+
autoload const_name, File.join(prefix, path)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Loads each autoloaded constant. If thread safety is a concern, wrap
|
10
|
+
# this in a Mutex.
|
11
|
+
def load
|
12
|
+
constants.each do |const|
|
13
|
+
const_get(const) if autoload?(const)
|
14
|
+
end
|
7
15
|
end
|
8
16
|
end
|
9
17
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
18
|
+
extend AutoloadHelper
|
19
|
+
|
20
|
+
autoload_all 'faraday',
|
21
|
+
:Connection => 'connection',
|
22
|
+
:TestConnection => 'test_connection',
|
23
|
+
:Response => 'response',
|
24
|
+
:Error => 'error'
|
14
25
|
|
15
26
|
module Adapter
|
16
|
-
|
17
|
-
|
18
|
-
|
27
|
+
extend AutoloadHelper
|
28
|
+
autoload_all 'faraday/adapter',
|
29
|
+
:NetHttp => 'net_http',
|
30
|
+
:Typhoeus => 'typhoeus',
|
31
|
+
:MockRequest => 'mock_request'
|
19
32
|
|
20
33
|
# Names of available adapters. Should not actually load them.
|
21
34
|
def self.adapters
|
@@ -35,16 +35,19 @@ module Faraday
|
|
35
35
|
class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
|
36
36
|
def matches?(request_path, headers)
|
37
37
|
return false if request_path != path
|
38
|
-
return true if request_headers.empty?
|
39
38
|
request_headers.each do |key, value|
|
40
|
-
return
|
41
|
-
end
|
42
|
-
|
39
|
+
return false if headers[key] != value
|
40
|
+
end
|
41
|
+
true
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
45
|
def initialize &block
|
47
46
|
super nil
|
47
|
+
configure(&block) if block
|
48
|
+
end
|
49
|
+
|
50
|
+
def configure
|
48
51
|
yield stubs
|
49
52
|
end
|
50
53
|
|
@@ -5,9 +5,9 @@ module Faraday
|
|
5
5
|
extend Faraday::Connection::Options
|
6
6
|
|
7
7
|
def _get(uri, request_headers)
|
8
|
-
http
|
8
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
9
9
|
response_class.new do |resp|
|
10
|
-
http_resp = http.get(uri
|
10
|
+
http_resp = http.get(path_for(uri), request_headers) do |chunk|
|
11
11
|
resp.process(chunk)
|
12
12
|
end
|
13
13
|
http_resp.each_header do |key, value|
|
data/lib/faraday/connection.rb
CHANGED
@@ -12,18 +12,30 @@ module Faraday
|
|
12
12
|
|
13
13
|
include Addressable
|
14
14
|
|
15
|
-
attr_accessor :host, :port, :scheme
|
15
|
+
attr_accessor :host, :port, :scheme, :params, :headers
|
16
16
|
attr_reader :path_prefix
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
# :url
|
19
|
+
# :params
|
20
|
+
# :headers
|
21
|
+
# :response
|
22
|
+
def initialize(url = nil, options = {})
|
23
|
+
if url.is_a?(Hash)
|
24
|
+
options = url
|
25
|
+
url = options[:url]
|
26
26
|
end
|
27
|
+
@response_class = options[:response]
|
28
|
+
@params = options[:params] || {}
|
29
|
+
@headers = options[:headers] || {}
|
30
|
+
self.url_prefix = url if url
|
31
|
+
end
|
32
|
+
|
33
|
+
def url_prefix=(url)
|
34
|
+
uri = URI.parse(url)
|
35
|
+
self.scheme = uri.scheme
|
36
|
+
self.host = uri.host
|
37
|
+
self.port = uri.port
|
38
|
+
self.path_prefix = uri.path
|
27
39
|
end
|
28
40
|
|
29
41
|
# Override in a subclass, or include an adapter
|
@@ -31,8 +43,9 @@ module Faraday
|
|
31
43
|
# def _get(uri, headers)
|
32
44
|
# end
|
33
45
|
#
|
34
|
-
def get(url, params =
|
35
|
-
|
46
|
+
def get(url, params = nil, headers = nil)
|
47
|
+
uri = build_uri(url, build_params(params))
|
48
|
+
_get(uri, build_headers(headers))
|
36
49
|
end
|
37
50
|
|
38
51
|
def response_class
|
@@ -49,16 +62,16 @@ module Faraday
|
|
49
62
|
def in_parallel?
|
50
63
|
!!@parallel_manager
|
51
64
|
end
|
52
|
-
|
65
|
+
|
53
66
|
def in_parallel(options = {})
|
54
67
|
@parallel_manager = true
|
55
68
|
yield
|
56
69
|
@parallel_manager = false
|
57
70
|
end
|
58
|
-
|
71
|
+
|
59
72
|
def setup_parallel_manager(options = {})
|
60
73
|
end
|
61
|
-
|
74
|
+
|
62
75
|
def run_parallel_requests
|
63
76
|
end
|
64
77
|
|
@@ -70,7 +83,7 @@ module Faraday
|
|
70
83
|
@path_prefix = value
|
71
84
|
end
|
72
85
|
|
73
|
-
def build_uri(url, params =
|
86
|
+
def build_uri(url, params = nil)
|
74
87
|
uri = URI.parse(url)
|
75
88
|
uri.scheme ||= @scheme
|
76
89
|
uri.host ||= @host
|
@@ -78,11 +91,35 @@ module Faraday
|
|
78
91
|
if @path_prefix && uri.path !~ /^\//
|
79
92
|
uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
|
80
93
|
end
|
81
|
-
|
82
|
-
|
94
|
+
if params && !params.empty?
|
95
|
+
uri.query = params_to_query(params)
|
96
|
+
end
|
83
97
|
uri
|
84
98
|
end
|
85
99
|
|
100
|
+
def path_for(uri)
|
101
|
+
uri.path.tap do |s|
|
102
|
+
s << "?#{uri.query}" if uri.query
|
103
|
+
s << "##{uri.fragment}" if uri.fragment
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def build_params(existing)
|
108
|
+
build_hash :params, existing
|
109
|
+
end
|
110
|
+
|
111
|
+
def build_headers(existing)
|
112
|
+
build_hash(:headers, existing).tap do |headers|
|
113
|
+
headers.keys.each do |key|
|
114
|
+
headers[key] = headers.delete(key).to_s
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def build_hash(method, existing)
|
120
|
+
existing ? send(method).merge(existing) : send(method)
|
121
|
+
end
|
122
|
+
|
86
123
|
def params_to_query(params)
|
87
124
|
params.inject([]) do |memo, (key, val)|
|
88
125
|
memo << "#{escape_for_querystring(key)}=#{escape_for_querystring(val)}"
|
@@ -92,7 +129,7 @@ module Faraday
|
|
92
129
|
# Some servers convert +'s in URL query params to spaces.
|
93
130
|
# Go ahead and encode it.
|
94
131
|
def escape_for_querystring(s)
|
95
|
-
URI.encode_component(s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
|
132
|
+
URI.encode_component(s.to_s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
|
96
133
|
escaped.gsub! /\+/, "%2B"
|
97
134
|
end
|
98
135
|
end
|
data/lib/faraday/response.rb
CHANGED
@@ -7,7 +7,9 @@ module Faraday
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
|
10
|
+
extend AutoloadHelper
|
11
|
+
autoload_all 'faraday/response',
|
12
|
+
:YajlResponse => 'yajl_response'
|
11
13
|
|
12
14
|
def initialize(headers = nil, body = nil)
|
13
15
|
super(headers || {}, body)
|
data/test/adapter_test.rb
CHANGED
@@ -11,6 +11,16 @@ class AdapterTest < Faraday::TestCase
|
|
11
11
|
@connection.extend adapter
|
12
12
|
end
|
13
13
|
|
14
|
+
it "passes params" do
|
15
|
+
@connection.params = {:a => 1}
|
16
|
+
assert_equal "params[:a] == 1", @connection.get('params').body
|
17
|
+
end
|
18
|
+
|
19
|
+
it "passes headers" do
|
20
|
+
@connection.headers = {"X-Test" => 1}
|
21
|
+
assert_equal "env[HTTP_X_TEST] == 1", @connection.get('headers').body
|
22
|
+
end
|
23
|
+
|
14
24
|
it "retrieves the response body" do
|
15
25
|
assert_equal 'hello world', @connection.get('hello_world').body
|
16
26
|
end
|
data/test/connection_test.rb
CHANGED
@@ -31,6 +31,21 @@ class ConnectionTest < Faraday::TestCase
|
|
31
31
|
conn = Faraday::Connection.new "http://sushi.com/fish"
|
32
32
|
assert_equal '/fish', conn.path_prefix
|
33
33
|
end
|
34
|
+
|
35
|
+
it "parses @path_prefix out of given url option" do
|
36
|
+
conn = Faraday::Connection.new :url => "http://sushi.com/fish"
|
37
|
+
assert_equal '/fish', conn.path_prefix
|
38
|
+
end
|
39
|
+
|
40
|
+
it "stores default params from options" do
|
41
|
+
conn = Faraday::Connection.new :params => {:a => 1}
|
42
|
+
assert_equal 1, conn.params[:a]
|
43
|
+
end
|
44
|
+
|
45
|
+
it "stores default headers from options" do
|
46
|
+
conn = Faraday::Connection.new :headers => {:a => 1}
|
47
|
+
assert_equal 1, conn.headers[:a]
|
48
|
+
end
|
34
49
|
end
|
35
50
|
|
36
51
|
describe "#build_uri" do
|
data/test/helper.rb
CHANGED
@@ -8,6 +8,12 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
8
8
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
9
|
require 'faraday'
|
10
10
|
|
11
|
+
begin
|
12
|
+
require 'ruby-debug'
|
13
|
+
Debugger.start
|
14
|
+
rescue LoadError
|
15
|
+
end
|
16
|
+
|
11
17
|
module Faraday
|
12
18
|
class TestCase < Test::Unit::TestCase
|
13
19
|
LIVE_SERVER = 'http://localhost:4567'
|
data/test/live_server.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rick
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-02 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|