mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-06 00:03:36 -04:00
enable hash-style access to special Options properties
Makes these equivalent: options[:request] == options.request proxy[:password] == proxy.password
This commit is contained in:
parent
cf2574b5e4
commit
9c7eb9c643
@ -98,6 +98,32 @@ module Faraday
|
||||
def self.attribute_options
|
||||
@attribute_options ||= {}
|
||||
end
|
||||
|
||||
def self.memoized(key)
|
||||
memoized_attributes[key.to_sym] = Proc.new
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{key}() self[:#{key}]; end
|
||||
RUBY
|
||||
end
|
||||
|
||||
def self.memoized_attributes
|
||||
@memoized_attributes ||= {}
|
||||
end
|
||||
|
||||
def [](key)
|
||||
key = key.to_sym
|
||||
if method = self.class.memoized_attributes[key]
|
||||
super(key) || (self[key] = instance_eval(&method))
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def self.inherited(subclass)
|
||||
super
|
||||
subclass.attribute_options.update(attribute_options)
|
||||
subclass.memoized_attributes.update(memoized_attributes)
|
||||
end
|
||||
end
|
||||
|
||||
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
|
||||
@ -141,13 +167,8 @@ module Faraday
|
||||
super(value)
|
||||
end
|
||||
|
||||
def user
|
||||
self[:user] ||= Utils.unescape(uri.user)
|
||||
end
|
||||
|
||||
def password
|
||||
self[:password] ||= Utils.unescape(uri.password)
|
||||
end
|
||||
memoized(:user) { uri.user && Utils.unescape(uri.user) }
|
||||
memoized(:password) { uri.password && Utils.unescape(uri.password) }
|
||||
end
|
||||
|
||||
class ConnectionOptions < Options.new(:request, :proxy, :ssl, :builder, :url,
|
||||
@ -155,17 +176,11 @@ module Faraday
|
||||
|
||||
options :request => RequestOptions, :ssl => SSLOptions
|
||||
|
||||
def request
|
||||
self[:request] ||= self.class.options_for(:request).new
|
||||
end
|
||||
memoized(:request) { self.class.options_for(:request).new }
|
||||
|
||||
def ssl
|
||||
self[:ssl] ||= self.class.options_for(:ssl).new
|
||||
end
|
||||
memoized(:ssl) { self.class.options_for(:ssl).new }
|
||||
|
||||
def builder_class
|
||||
self[:builder_class] ||= RackBuilder
|
||||
end
|
||||
memoized(:builder_class) { RackBuilder }
|
||||
|
||||
def new_builder(block)
|
||||
builder_class.new(&block)
|
||||
|
@ -33,6 +33,20 @@ class OptionsTest < Faraday::TestCase
|
||||
assert_equal 'http', options.scheme
|
||||
end
|
||||
|
||||
def test_proxy_options_hash_access
|
||||
proxy = Faraday::ProxyOptions.from 'http://a%40b:pw%20d@example.org'
|
||||
assert_equal 'a@b', proxy[:user]
|
||||
assert_equal 'a@b', proxy.user
|
||||
assert_equal 'pw d', proxy[:password]
|
||||
assert_equal 'pw d', proxy.password
|
||||
end
|
||||
|
||||
def test_proxy_options_no_auth
|
||||
proxy = Faraday::ProxyOptions.from 'http://example.org'
|
||||
assert_nil proxy.user
|
||||
assert_nil proxy.password
|
||||
end
|
||||
|
||||
def test_from_options
|
||||
options = Options.new 1
|
||||
|
||||
@ -67,6 +81,13 @@ class OptionsTest < Faraday::TestCase
|
||||
assert_equal 1, options.c.sub
|
||||
end
|
||||
|
||||
def test_inheritance
|
||||
subclass = Class.new(Options)
|
||||
options = subclass.from(:c => {:sub => 'hello'})
|
||||
assert_kind_of SubOptions, options.c
|
||||
assert_equal 'hello', options.c.sub
|
||||
end
|
||||
|
||||
def test_from_deep_hash
|
||||
hash = {:b => 1}
|
||||
options = Options.from :a => hash
|
||||
|
Loading…
x
Reference in New Issue
Block a user