Permalink
Browse files

dont create unnecessary procs

  • Loading branch information...
1 parent 98883a4 commit dddc8697e0399abaffd1592e0a2cc6f9b19b7446 @technoweenie technoweenie committed Feb 27, 2011
Showing with 31 additions and 18 deletions.
  1. +2 −1 lib/faraday/adapter.rb
  2. +16 −10 lib/faraday/builder.rb
  3. +13 −7 lib/faraday/connection.rb
@@ -81,10 +81,11 @@ def create_multipart(env, params, boundary = nil)
Faraday::CompositeReadIO.new(*parts.map{|p| p.to_io })
end
- def process_to_params(pieces, params, base = nil, &block)
+ def process_to_params(pieces, params, base = nil)
params.to_a.each do |key, value|
key_str = base ? "#{base}[#{key}]" : key
+ block = block_given? ? Proc.new : nil
case value
when Array
values = value.inject([]) { |a,v| a << [nil, v] }
@@ -9,7 +9,8 @@ module Faraday
class Builder
attr_accessor :handlers
- def self.create(&block)
+ def self.create
+ block = block_given? ? Proc.new : nil
Builder.new(&block)
end
@@ -19,17 +20,17 @@ def self.inner_app
end
end
- def initialize(handlers = [], &block)
+ def initialize(handlers = [])
@handlers = handlers
- build(&block) if block_given?
+ build(Proc.new) if block_given?
end
- def build(options = {}, &block)
+ def build(options = {})
inner = @handlers.shift
if !options[:keep]
@handlers.clear
end
- block.call(self)
+ yield self if block_given?
run(inner || self.class.inner_app)
end
@@ -51,23 +52,28 @@ def to_app
@handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) }
end
- def use(klass, *args, &block)
+ def use(klass, *args)
+ block = block_given? ? Proc.new : nil
run(lambda { |app| klass.new(app, *args, &block) })
end
- def request(key, *args, &block)
+ def request(key, *args)
+ block = block_given? ? Proc.new : nil
use_symbol(Faraday::Request, key, *args, &block)
end
- def response(key, *args, &block)
+ def response(key, *args)
+ block = block_given? ? Proc.new : nil
use_symbol(Faraday::Response, key, *args, &block)
end
- def adapter(key, *args, &block)
+ def adapter(key, *args)
+ block = block_given? ? Proc.new : nil
use_symbol(Faraday::Adapter, key, *args, &block)
end
- def use_symbol(mod, key, *args, &block)
+ def use_symbol(mod, key, *args)
+ block = block_given? ? Proc.new : nil
use(mod.lookup_module(key), *args, &block)
end
@@ -17,7 +17,7 @@ class Connection
# :headers
# :request
# :ssl
- def initialize(url = nil, options = {}, &block)
+ def initialize(url = nil, options = {})
if url.is_a?(Hash)
options = url
url = options[:url]
@@ -32,7 +32,8 @@ def initialize(url = nil, options = {}, &block)
merge_params @params, options[:params] if options[:params]
merge_headers @headers, options[:headers] if options[:headers]
- if block
+ if block_given?
+ block = Proc.new
@builder = Builder.new
@builder.build { block.call(self) }
else
@@ -60,23 +61,28 @@ def build(options = {}, &block)
@builder.build(options, &block)
end
- def get(url = nil, headers = nil, &block)
+ def get(url = nil, headers = nil)
+ block = block_given? ? Proc.new : nil
run_request(:get, url, nil, headers, &block)
end
- def post(url = nil, body = nil, headers = nil, &block)
+ def post(url = nil, body = nil, headers = nil)
+ block = block_given? ? Proc.new : nil
run_request(:post, url, body, headers, &block)
end
- def put(url = nil, body = nil, headers = nil, &block)
+ def put(url = nil, body = nil, headers = nil)
+ block = block_given? ? Proc.new : nil
run_request(:put, url, body, headers, &block)
end
- def head(url = nil, headers = nil, &block)
+ def head(url = nil, headers = nil)
+ block = block_given? ? Proc.new : nil
run_request(:head, url, nil, headers, &block)
end
- def delete(url = nil, headers = nil, &block)
+ def delete(url = nil, headers = nil)
+ block = block_given? ? Proc.new : nil
run_request(:delete, url, nil, headers, &block)
end

0 comments on commit dddc869

Please sign in to comment.