mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-09 00:04:39 -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
|
def self.attribute_options
|
||||||
@attribute_options ||= {}
|
@attribute_options ||= {}
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
|
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
|
||||||
@ -141,13 +167,8 @@ module Faraday
|
|||||||
super(value)
|
super(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def user
|
memoized(:user) { uri.user && Utils.unescape(uri.user) }
|
||||||
self[:user] ||= Utils.unescape(uri.user)
|
memoized(:password) { uri.password && Utils.unescape(uri.password) }
|
||||||
end
|
|
||||||
|
|
||||||
def password
|
|
||||||
self[:password] ||= Utils.unescape(uri.password)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class ConnectionOptions < Options.new(:request, :proxy, :ssl, :builder, :url,
|
class ConnectionOptions < Options.new(:request, :proxy, :ssl, :builder, :url,
|
||||||
@ -155,17 +176,11 @@ module Faraday
|
|||||||
|
|
||||||
options :request => RequestOptions, :ssl => SSLOptions
|
options :request => RequestOptions, :ssl => SSLOptions
|
||||||
|
|
||||||
def request
|
memoized(:request) { self.class.options_for(:request).new }
|
||||||
self[:request] ||= self.class.options_for(:request).new
|
|
||||||
end
|
|
||||||
|
|
||||||
def ssl
|
memoized(:ssl) { self.class.options_for(:ssl).new }
|
||||||
self[:ssl] ||= self.class.options_for(:ssl).new
|
|
||||||
end
|
|
||||||
|
|
||||||
def builder_class
|
memoized(:builder_class) { RackBuilder }
|
||||||
self[:builder_class] ||= RackBuilder
|
|
||||||
end
|
|
||||||
|
|
||||||
def new_builder(block)
|
def new_builder(block)
|
||||||
builder_class.new(&block)
|
builder_class.new(&block)
|
||||||
|
@ -33,6 +33,20 @@ class OptionsTest < Faraday::TestCase
|
|||||||
assert_equal 'http', options.scheme
|
assert_equal 'http', options.scheme
|
||||||
end
|
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
|
def test_from_options
|
||||||
options = Options.new 1
|
options = Options.new 1
|
||||||
|
|
||||||
@ -67,6 +81,13 @@ class OptionsTest < Faraday::TestCase
|
|||||||
assert_equal 1, options.c.sub
|
assert_equal 1, options.c.sub
|
||||||
end
|
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
|
def test_from_deep_hash
|
||||||
hash = {:b => 1}
|
hash = {:b => 1}
|
||||||
options = Options.from :a => hash
|
options = Options.from :a => hash
|
||||||
|
Loading…
x
Reference in New Issue
Block a user