Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/marcandre/aws-s3 into marca…
Browse files Browse the repository at this point in the history
…ndre/master
  • Loading branch information
marcel committed Apr 20, 2009
2 parents 7b76018 + 1191298 commit e2a13a6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 57 deletions.
63 changes: 9 additions & 54 deletions lib/aws/s3/connection.rb
Expand Up @@ -249,73 +249,28 @@ def default_connection
end

class Options < Hash #:nodoc:
class << self
def valid_options
[:access_key_id, :secret_access_key, :server, :port, :use_ssl, :persistent, :proxy]
end
end
VALID_OPTIONS = [:access_key_id, :secret_access_key, :server, :port, :use_ssl, :persistent, :proxy].freeze

attr_reader :options
def initialize(options = {})
super()
@options = options
validate!
extract_proxy_settings!
extract_persistent!
extract_server!
extract_port!
extract_remainder!
validate(options)
replace(:server => DEFAULT_HOST, :port => (options[:use_ssl] ? 443 : 80))
merge!(options)
end

def connecting_through_proxy?
!self[:proxy].nil?
end

def proxy_settings
proxy_setting_keys.map do |proxy_key|
self[:proxy][proxy_key]
end
self[:proxy].values_at(:host, :port, :user, :password)
end

private
def proxy_setting_keys
[:host, :port, :user, :password]
end

def missing_proxy_settings?
!self[:proxy].keys.include?(:host)
end

def extract_persistent!
self[:persistent] = options.has_key?(:persitent) ? options[:persitent] : false
end

def extract_proxy_settings!
self[:proxy] = options.delete(:proxy) if options.include?(:proxy)
validate_proxy_settings!
end

def extract_server!
self[:server] = options.delete(:server) || DEFAULT_HOST
end

def extract_port!
self[:port] = options.delete(:port) || (options[:use_ssl] ? 443 : 80)
end

def extract_remainder!
update(options)
end

def validate!
invalid_options = options.keys.select {|key| !self.class.valid_options.include?(key)}
def validate(options)
invalid_options = options.keys - VALID_OPTIONS
raise InvalidConnectionOption.new(invalid_options) unless invalid_options.empty?
end

def validate_proxy_settings!
if connecting_through_proxy? && missing_proxy_settings?
raise ArgumentError, "Missing proxy settings. Must specify at least :host."
end
raise ArgumentError, "Missing proxy settings. Must specify at least :host." if options[:proxy] && !options[:proxy][:host]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/aws/s3/exceptions.rb
Expand Up @@ -58,7 +58,7 @@ class NoConnectionEstablished < S3Exception
class InvalidConnectionOption < InvalidOption
def initialize(invalid_options)
message = "The following connection options are invalid: #{invalid_options.join(', ')}. " +
"The valid connection options are: #{Connection::Options.valid_options.join(', ')}."
"The valid connection options are: #{Connection::Options::VALID_OPTIONS.join(', ')}."
super(message)
end
end
Expand Down
3 changes: 1 addition & 2 deletions test/connection_test.rb
Expand Up @@ -3,7 +3,7 @@
class ConnectionTest < Test::Unit::TestCase
attr_reader :keys
def setup
@keys = {:access_key_id => '123', :secret_access_key => 'abc'}
@keys = {:access_key_id => '123', :secret_access_key => 'abc'}.freeze
end

def test_creating_a_connection
Expand Down Expand Up @@ -207,7 +207,6 @@ def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
private
def assert_key_transfered(key, value, options)
assert_equal value, options[key]
assert !options.instance_variable_get('@options').has_key?(key)
end

def generate_options(options = {})
Expand Down

0 comments on commit e2a13a6

Please sign in to comment.