faraday 0.5.6 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,7 +6,7 @@ This mess is gonna get raw, like sushi. So, haters to the left.
6
6
 
7
7
  ## Usage
8
8
 
9
- conn = Faraday::Connection.new(:url => 'http://sushi.com') do |builder|
9
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
10
10
  builder.use Faraday::Request::Yajl # convert body to json with Yajl lib
11
11
  builder.use Faraday::Adapter::Logger # log the request somewhere?
12
12
  builder.use Faraday::Adapter::Typhoeus # make http request with typhoeus
@@ -24,7 +24,8 @@ This mess is gonna get raw, like sushi. So, haters to the left.
24
24
  resp1 = conn.get '/nigiri/sake.json'
25
25
  resp2 = conn.post do |req|
26
26
  req.url "/nigiri.json", :page => 2
27
- req["Content-Type"] = 'application/json'
27
+ req.params['limit'] = 100 # &limit=100
28
+ req.headers["Content-Type"] = 'application/json'
28
29
  req.body = {:name => 'Unagi'}
29
30
  end
30
31
 
@@ -40,7 +41,7 @@ This mess is gonna get raw, like sushi. So, haters to the left.
40
41
 
41
42
  # You can pass stubbed request to the test adapter or define them in a block
42
43
  # or a combination of the two.
43
- test = Faraday::Connection.new do |builder|
44
+ test = Faraday.new do |builder|
44
45
  builder.adapter :test, stubs do |stub|
45
46
  stub.get('/ebi') {[ 200, {}, 'shrimp' ]}
46
47
  end
@@ -72,7 +73,7 @@ This mess is gonna get raw, like sushi. So, haters to the left.
72
73
  * Add curb, em-http, fast_http
73
74
 
74
75
  ## Note on Patches/Pull Requests
75
-
76
+
76
77
  * Fork the project.
77
78
  * Make your feature addition or bug fix.
78
79
  * Add tests for it. This is important so I don't break it in a
@@ -83,4 +84,4 @@ This mess is gonna get raw, like sushi. So, haters to the left.
83
84
 
84
85
  ## Copyright
85
86
 
86
- Copyright (c) 2009-2010 rick, hobson. See LICENSE for details.
87
+ Copyright (c) 2009-2011 rick, hobson. See LICENSE for details.
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  ## If your rubyforge_project name is different, then edit it and comment out
13
13
  ## the sub! line in the Rakefile
14
14
  s.name = 'faraday'
15
- s.version = '0.5.6'
16
- s.date = '2011-02-15'
15
+ s.version = '0.5.7'
16
+ s.date = '2011-02-27'
17
17
  s.rubyforge_project = 'faraday'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
@@ -52,6 +52,7 @@ Gem::Specification.new do |s|
52
52
  lib/faraday/adapter/action_dispatch.rb
53
53
  lib/faraday/adapter/em_synchrony.rb
54
54
  lib/faraday/adapter/excon.rb
55
+ lib/faraday/adapter/logger.rb
55
56
  lib/faraday/adapter/net_http.rb
56
57
  lib/faraday/adapter/patron.rb
57
58
  lib/faraday/adapter/test.rb
@@ -69,6 +70,7 @@ Gem::Specification.new do |s|
69
70
  lib/faraday/upload_io.rb
70
71
  lib/faraday/utils.rb
71
72
  test/adapters/live_test.rb
73
+ test/adapters/logger_test.rb
72
74
  test/adapters/test_middleware_test.rb
73
75
  test/adapters/typhoeus_test.rb
74
76
  test/connection_app_test.rb
@@ -1,10 +1,15 @@
1
1
  module Faraday
2
- VERSION = "0.5.6"
2
+ VERSION = "0.5.7"
3
3
 
4
4
  class << self
5
5
  attr_accessor :default_adapter
6
6
  attr_writer :default_connection
7
7
 
8
+ def new(url = nil, options = {})
9
+ block = block_given? ? Proc.new : nil
10
+ Faraday::Connection.new(url, options, &block)
11
+ end
12
+
8
13
  private
9
14
  def method_missing(name, *args, &block)
10
15
  default_connection.send(name, *args, &block)
@@ -13,7 +13,8 @@ module Faraday
13
13
  :EMSynchrony => 'em_synchrony',
14
14
  :Patron => 'patron',
15
15
  :Excon => 'excon',
16
- :Test => 'test'
16
+ :Test => 'test',
17
+ :Logger => 'logger'
17
18
 
18
19
  register_lookup_modules \
19
20
  :action_dispatch => :ActionDispatch,
@@ -22,7 +23,8 @@ module Faraday
22
23
  :typhoeus => :Typhoeus,
23
24
  :patron => :Patron,
24
25
  :em_synchrony => :EMSynchrony,
25
- :excon => :Excon
26
+ :excon => :Excon,
27
+ :logger => :Logger
26
28
 
27
29
  def call(env)
28
30
  process_body_for_request(env)
@@ -79,10 +81,11 @@ module Faraday
79
81
  Faraday::CompositeReadIO.new(*parts.map{|p| p.to_io })
80
82
  end
81
83
 
82
- def process_to_params(pieces, params, base = nil, &block)
84
+ def process_to_params(pieces, params, base = nil)
83
85
  params.to_a.each do |key, value|
84
86
  key_str = base ? "#{base}[#{key}]" : key
85
87
 
88
+ block = block_given? ? Proc.new : nil
86
89
  case value
87
90
  when Array
88
91
  values = value.inject([]) { |a,v| a << [nil, v] }
@@ -26,7 +26,8 @@ module Faraday
26
26
  process_body_for_request(env)
27
27
  request = EventMachine::HttpRequest.new(URI::parse(env[:url].to_s))
28
28
  options = {:head => env[:request_headers]}
29
-
29
+ options[:ssl] = env[:ssl] if env[:ssl]
30
+
30
31
  if env[:body]
31
32
  if env[:body].respond_to? :read
32
33
  options[:body] = env[:body].read
@@ -0,0 +1,32 @@
1
+ module Faraday
2
+ class Adapter
3
+ class Logger < Faraday::Adapter
4
+ def self.loaded?() false end
5
+
6
+ def initialize(app = nil, logger = nil)
7
+ super(app)
8
+ @logger = logger || begin
9
+ require 'logger'
10
+ ::Logger.new(STDOUT)
11
+ end
12
+ end
13
+
14
+ def call(env)
15
+ super
16
+ @logger.info "#{env[:method]} #{env[:url].to_s}"
17
+ @logger.debug("request") do
18
+ env[:request_headers].map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
19
+ end
20
+
21
+ env[:response].on_complete do |resp_env|
22
+ @logger.info("Status") { env[:status].to_s }
23
+ @logger.debug("response") do
24
+ resp_env[:response_headers].map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
25
+ end
26
+ end
27
+
28
+ @app.call(env)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -22,7 +22,6 @@ module Faraday
22
22
  :response_headers => resp.headers.
23
23
  inject({}) { |memo, (k, v)| memo.update(k.downcase => v) },
24
24
  :body => resp.body
25
- env[:response].finish(env)
26
25
 
27
26
  @app.call env
28
27
  rescue Errno::ECONNREFUSED
@@ -7,7 +7,7 @@ module Faraday
7
7
  # end
8
8
  # end
9
9
  # end
10
- #
10
+ #
11
11
  # resp = test.get '/nigiri/sake.json'
12
12
  # resp.body # => 'hi world'
13
13
  #
@@ -26,18 +26,19 @@ module Faraday
26
26
  req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
27
27
  req.connect_timeout = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
28
28
 
29
+ is_parallel = !!env[:parallel_manager]
29
30
  req.on_complete do |resp|
30
31
  env.update \
31
32
  :status => resp.code,
32
33
  :response_headers => parse_response_headers(resp.headers),
33
34
  :body => resp.body
34
- env[:response].finish(env)
35
+ env[:response].finish(env) if !is_parallel
35
36
  end
36
37
 
37
38
  hydra = env[:parallel_manager] || self.class.setup_parallel_manager
38
39
  hydra.queue req
39
40
 
40
- if !env[:parallel_manager]
41
+ if !is_parallel
41
42
  hydra.run
42
43
  end
43
44
 
@@ -9,7 +9,8 @@ module Faraday
9
9
  class Builder
10
10
  attr_accessor :handlers
11
11
 
12
- def self.create(&block)
12
+ def self.create
13
+ block = block_given? ? Proc.new : nil
13
14
  Builder.new(&block)
14
15
  end
15
16
 
@@ -19,17 +20,17 @@ module Faraday
19
20
  end
20
21
  end
21
22
 
22
- def initialize(handlers = [], &block)
23
+ def initialize(handlers = [])
23
24
  @handlers = handlers
24
- build(&block) if block_given?
25
+ build(Proc.new) if block_given?
25
26
  end
26
27
 
27
- def build(options = {}, &block)
28
+ def build(options = {})
28
29
  inner = @handlers.shift
29
30
  if !options[:keep]
30
31
  @handlers.clear
31
32
  end
32
- block.call(self)
33
+ yield self if block_given?
33
34
  run(inner || self.class.inner_app)
34
35
  end
35
36
 
@@ -51,23 +52,28 @@ module Faraday
51
52
  @handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) }
52
53
  end
53
54
 
54
- def use(klass, *args, &block)
55
+ def use(klass, *args)
56
+ block = block_given? ? Proc.new : nil
55
57
  run(lambda { |app| klass.new(app, *args, &block) })
56
58
  end
57
59
 
58
- def request(key, *args, &block)
60
+ def request(key, *args)
61
+ block = block_given? ? Proc.new : nil
59
62
  use_symbol(Faraday::Request, key, *args, &block)
60
63
  end
61
64
 
62
- def response(key, *args, &block)
65
+ def response(key, *args)
66
+ block = block_given? ? Proc.new : nil
63
67
  use_symbol(Faraday::Response, key, *args, &block)
64
68
  end
65
69
 
66
- def adapter(key, *args, &block)
70
+ def adapter(key, *args)
71
+ block = block_given? ? Proc.new : nil
67
72
  use_symbol(Faraday::Adapter, key, *args, &block)
68
73
  end
69
74
 
70
- def use_symbol(mod, key, *args, &block)
75
+ def use_symbol(mod, key, *args)
76
+ block = block_given? ? Proc.new : nil
71
77
  use(mod.lookup_module(key), *args, &block)
72
78
  end
73
79
 
@@ -17,7 +17,7 @@ module Faraday
17
17
  # :headers
18
18
  # :request
19
19
  # :ssl
20
- def initialize(url = nil, options = {}, &block)
20
+ def initialize(url = nil, options = {})
21
21
  if url.is_a?(Hash)
22
22
  options = url
23
23
  url = options[:url]
@@ -32,7 +32,8 @@ module Faraday
32
32
  merge_params @params, options[:params] if options[:params]
33
33
  merge_headers @headers, options[:headers] if options[:headers]
34
34
 
35
- if block
35
+ if block_given?
36
+ block = Proc.new
36
37
  @builder = Builder.new
37
38
  @builder.build { block.call(self) }
38
39
  else
@@ -60,23 +61,28 @@ module Faraday
60
61
  @builder.build(options, &block)
61
62
  end
62
63
 
63
- def get(url = nil, headers = nil, &block)
64
+ def get(url = nil, headers = nil)
65
+ block = block_given? ? Proc.new : nil
64
66
  run_request(:get, url, nil, headers, &block)
65
67
  end
66
68
 
67
- def post(url = nil, body = nil, headers = nil, &block)
69
+ def post(url = nil, body = nil, headers = nil)
70
+ block = block_given? ? Proc.new : nil
68
71
  run_request(:post, url, body, headers, &block)
69
72
  end
70
73
 
71
- def put(url = nil, body = nil, headers = nil, &block)
74
+ def put(url = nil, body = nil, headers = nil)
75
+ block = block_given? ? Proc.new : nil
72
76
  run_request(:put, url, body, headers, &block)
73
77
  end
74
78
 
75
- def head(url = nil, headers = nil, &block)
79
+ def head(url = nil, headers = nil)
80
+ block = block_given? ? Proc.new : nil
76
81
  run_request(:head, url, nil, headers, &block)
77
82
  end
78
83
 
79
- def delete(url = nil, headers = nil, &block)
84
+ def delete(url = nil, headers = nil)
85
+ block = block_given? ? Proc.new : nil
80
86
  run_request(:delete, url, nil, headers, &block)
81
87
  end
82
88
 
@@ -6,7 +6,7 @@ module Faraday
6
6
  end
7
7
 
8
8
  def message
9
- @inner_exception.respond_to?(:message) ?
9
+ @inner_exception.respond_to?(:message) ?
10
10
  @inner_exception.message :
11
11
  @inner_exception.to_s
12
12
  end
@@ -3,8 +3,9 @@ module Faraday
3
3
  #
4
4
  # @connection.post do |req|
5
5
  # req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
6
- # req.headers['b'] = '2' # header
7
- # req['b'] = '2' # header
6
+ # req.headers['b'] = '2' # Header
7
+ # req.params['c'] = '3' # GET Param
8
+ # req['b'] = '2' # also Header
8
9
  # req.body = 'abc'
9
10
  # end
10
11
  #
@@ -71,7 +72,7 @@ module Faraday
71
72
  { :method => request_method,
72
73
  :body => body,
73
74
  :url => connection.build_url(path, env_params),
74
- :request_headers => env_headers.update(headers),
75
+ :request_headers => env_headers,
75
76
  :parallel_manager => connection.parallel_manager,
76
77
  :response => Response.new,
77
78
  :request => connection.options.merge(:proxy => connection.proxy),
@@ -41,7 +41,9 @@ module Faraday
41
41
  end
42
42
 
43
43
  def finish(env)
44
- env[:body] ||= ''
44
+ return self if @status
45
+ env[:body] ||= ''
46
+ env[:response_headers] ||= {}
45
47
  @on_complete_callbacks.each { |c| c.call(env) }
46
48
  @status, @headers, @body = env[:status], env[:response_headers], env[:body]
47
49
  self
@@ -23,8 +23,8 @@ module Faraday
23
23
 
24
24
  def self.parse(body)
25
25
  ActiveSupport::JSON.decode(body)
26
- rescue Object => err
27
- raise Faraday::Error::ParsingError.new(err)
26
+ rescue Object
27
+ raise Faraday::Error::ParsingError, $!
28
28
  end
29
29
  end
30
30
  end
@@ -19,8 +19,8 @@ module Faraday
19
19
 
20
20
  def self.parse(body)
21
21
  Yajl::Parser.parse(body)
22
- rescue Object => err
23
- raise Faraday::Error::ParsingError.new(err)
22
+ rescue Object
23
+ raise Faraday::Error::ParsingError, $!
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
2
+ require 'stringio'
3
+ require 'logger'
4
+
5
+ module Adapters
6
+ class LoggerTest < Faraday::TestCase
7
+ def setup
8
+ @io = StringIO.new
9
+ @logger = Logger.new(@io)
10
+ @logger.level = Logger::DEBUG
11
+
12
+ @conn = Faraday.new do |b|
13
+ b.adapter :logger, @logger
14
+ b.adapter :test do |stubs|
15
+ stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
16
+ end
17
+ end
18
+ @resp = @conn.get '/hello', :accept => 'text/html'
19
+ end
20
+
21
+ def test_still_returns_output
22
+ assert_equal 'hello', @resp.body
23
+ end
24
+
25
+ def test_logs_method_and_url
26
+ assert_match "get /hello", @io.string
27
+ end
28
+
29
+ def test_logs_request_headers
30
+ assert_match %(Accept: "text/html), @io.string
31
+ end
32
+
33
+ def test_logs_response_headers
34
+ assert_match %(Content-Type: "text/html), @io.string
35
+ end
36
+ end
37
+ end
@@ -4,7 +4,7 @@ module Adapters
4
4
  class TestMiddleware < Faraday::TestCase
5
5
  def setup
6
6
  @stubs = Faraday::Adapter::Test::Stubs.new
7
- @conn = Faraday::Connection.new do |builder|
7
+ @conn = Faraday.new do |builder|
8
8
  builder.adapter :test, @stubs
9
9
  end
10
10
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
@@ -23,7 +23,7 @@ class TestConnectionApps < Faraday::TestCase
23
23
  end
24
24
 
25
25
  def setup
26
- @conn = Faraday::Connection.new do |b|
26
+ @conn = Faraday.new do |b|
27
27
  b.use TestMiddleWare
28
28
  b.use TestAdapter
29
29
  end
@@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
3
  class TestEnv < Faraday::TestCase
4
4
  def setup
5
- @conn = Faraday::Connection.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
5
+ @conn = Faraday.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
6
6
  @conn.options[:timeout] = 3
7
7
  @conn.options[:open_timeout] = 5
8
8
  @conn.ssl[:verify] = false
@@ -16,7 +16,7 @@ class RequestMiddlewareTest < Faraday::TestCase
16
16
 
17
17
  private
18
18
  def create_json_connection(encoder)
19
- Faraday::Connection.new do |b|
19
+ Faraday.new do |b|
20
20
  b.use encoder
21
21
  b.adapter :test do |stub|
22
22
  stub.post('echo_body') do |env|
@@ -25,13 +25,13 @@ class ResponseMiddlewareTest < Faraday::TestCase
25
25
 
26
26
  define_method "test_use_#{key}_to_raise_Faraday_Error_Parsing_with_no_json_content" do
27
27
  assert_raises Faraday::Error::ParsingError do
28
- response = create_json_connection(encoder).get('bad_json')
28
+ create_json_connection(encoder).get('bad_json')
29
29
  end
30
30
  end
31
31
  end
32
32
 
33
33
  def create_json_connection(encoder)
34
- Faraday::Connection.new do |b|
34
+ Faraday.new do |b|
35
35
  b.adapter :test do |stub|
36
36
  stub.get('json') { [200, {'Content-Type' => 'text/html'}, "[1,2,3]"] }
37
37
  stub.get('blank') { [200, {'Content-Type' => 'text/html'}, ''] }
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 5
9
- - 6
10
- version: 0.5.6
8
+ - 7
9
+ version: 0.5.7
11
10
  platform: ruby
12
11
  authors:
13
12
  - Rick Olson
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-02-15 00:00:00 -08:00
17
+ date: 2011-02-27 00:00:00 -08:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 27
30
28
  segments:
31
29
  - 0
32
30
  - 8
@@ -41,7 +39,6 @@ dependencies:
41
39
  requirements:
42
40
  - - ~>
43
41
  - !ruby/object:Gem::Version
44
- hash: 15
45
42
  segments:
46
43
  - 2
47
44
  - 2
@@ -57,7 +54,6 @@ dependencies:
57
54
  requirements:
58
55
  - - ~>
59
56
  - !ruby/object:Gem::Version
60
- hash: 19
61
57
  segments:
62
58
  - 1
63
59
  - 1
@@ -73,7 +69,6 @@ dependencies:
73
69
  requirements:
74
70
  - - ">="
75
71
  - !ruby/object:Gem::Version
76
- hash: 19
77
72
  segments:
78
73
  - 1
79
74
  - 1
@@ -81,7 +76,6 @@ dependencies:
81
76
  version: 1.1.0
82
77
  - - <
83
78
  - !ruby/object:Gem::Version
84
- hash: 7
85
79
  segments:
86
80
  - 2
87
81
  version: "2"
@@ -106,6 +100,7 @@ files:
106
100
  - lib/faraday/adapter/action_dispatch.rb
107
101
  - lib/faraday/adapter/em_synchrony.rb
108
102
  - lib/faraday/adapter/excon.rb
103
+ - lib/faraday/adapter/logger.rb
109
104
  - lib/faraday/adapter/net_http.rb
110
105
  - lib/faraday/adapter/patron.rb
111
106
  - lib/faraday/adapter/test.rb
@@ -123,6 +118,7 @@ files:
123
118
  - lib/faraday/upload_io.rb
124
119
  - lib/faraday/utils.rb
125
120
  - test/adapters/live_test.rb
121
+ - test/adapters/logger_test.rb
126
122
  - test/adapters/test_middleware_test.rb
127
123
  - test/adapters/typhoeus_test.rb
128
124
  - test/connection_app_test.rb
@@ -148,7 +144,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
144
  requirements:
149
145
  - - ">="
150
146
  - !ruby/object:Gem::Version
151
- hash: 3
152
147
  segments:
153
148
  - 0
154
149
  version: "0"
@@ -157,7 +152,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
152
  requirements:
158
153
  - - ">="
159
154
  - !ruby/object:Gem::Version
160
- hash: 17
161
155
  segments:
162
156
  - 1
163
157
  - 3
@@ -172,6 +166,7 @@ specification_version: 2
172
166
  summary: HTTP/REST API client library.
173
167
  test_files:
174
168
  - test/adapters/live_test.rb
169
+ - test/adapters/logger_test.rb
175
170
  - test/adapters/test_middleware_test.rb
176
171
  - test/adapters/typhoeus_test.rb
177
172
  - test/connection_app_test.rb