Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring

  • Loading branch information...
commit 56ac81141689135b9f1b4b5059eeb9af91eda4ff 1 parent daf3b59
Jakub Kuźma authored
2  LICENSE
View
@@ -1,4 +1,4 @@
-Copyright (c) 2009 Jakub Kuźma
+Copyright (c) 2009 Jakub Kuźma, Mirosław Boruta
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
10 lib/s3.rb
View
@@ -3,14 +3,14 @@
require "net/http"
require "net/https"
require "base64"
-require "forwardable"
+# require "forwardable"
require "digest/md5"
require "xmlsimple"
-require "s3/base"
-require "s3/signature"
-require "s3/service"
require "s3/bucket"
-require "s3/object"
+require "s3/connection"
require "s3/exceptions"
+require "s3/object"
+require "s3/service"
+require "s3/signature"
4 lib/s3/bucket.rb
View
@@ -1,5 +1,5 @@
module S3
- class Bucket < Base
+ class Bucket
attr_accessor :name
def initialize(options)
@@ -15,7 +15,7 @@ def self.parse_name(bucket_name, host)
host = "#{bucket_name}.#{host}"
else
# PATH BASED
- path_prefix = "/#{bucket_name}"
+ path_prefix = "/#{bucket_name}" unless bucket_name.empty?
end
[host, path_prefix]
end
99 lib/s3/base.rb → lib/s3/connection.rb
View
@@ -1,22 +1,15 @@
module S3
- class Base
+ class Connection
HOST = "s3.amazonaws.com"
attr_accessor :access_key_id, :secret_access_key, :host, :use_ssl, :path_prefix
alias :use_ssl? :use_ssl
- protected
-
def initialize(options)
@access_key_id = options[:access_key_id]
@secret_access_key = options[:secret_access_key]
@use_ssl = options[:use_ssl] || false
- @host = options[:host] || HOST
- @path_prefix = options[:path_prefix] || ""
- end
-
- def port
- use_ssl? ? 443 : 80
+ @host, @path_prefix = self.class.parse_host(options[:bucket_name] || "", options[:host] || HOST)
end
def get(path, params = {})
@@ -39,42 +32,10 @@ def delete(path)
send_request(request)
end
- def self.parse_params(params)
- interesting_keys = [:max_keys, :prefix, :marker, :delimiter,
- :location]
-
- result = []
- params.each do |key, value|
- if interesting_keys.include?(key)
- parsed_key = key.to_s.gsub("_", "-")
- case value
- when nil
- result << parsed_key
- else
- result << "#{parsed_key}=#{value}"
- end
- end
- end
- result.join("&")
- end
+ private
- def self.parse_headers(headers)
- interesting_keys = [:content_type, :x_amz_acl, :range,
- :if_modified_since, :if_unmodified_since,
- :if_match, :if_none_match]
- parsed_headers = {}
- headers.each do |key, value|
- if interesting_keys.include?(key)
- parsed_key = key.to_s.gsub("_", "-")
- parsed_value = value
- case value
- when Range
- parsed_value = "#{value.first}-#{value.last}"
- end
- parsed_headers[parsed_key] = parsed_value
- end
- end
- parsed_headers
+ def port
+ use_ssl? ? 443 : 80
end
def prepare_request(verb, path, body = nil, params = {}, headers = {})
@@ -137,5 +98,55 @@ def send_request(request)
response
end
+
+ def self.parse_host(bucket_name, host)
+ path_prefix = ""
+ if "#{bucket_name}.#{host}" =~ /\A#{URI::REGEXP::PATTERN::HOSTNAME}\Z/
+ # VHOST
+ host = "#{bucket_name}.#{host}"
+ else
+ # PATH BASED
+ path_prefix = "/#{bucket_name}" unless bucket_name.empty?
+ end
+ [host, path_prefix]
+ end
+
+ def self.parse_params(params)
+ interesting_keys = [:max_keys, :prefix, :marker, :delimiter,
+ :location]
+
+ result = []
+ params.each do |key, value|
+ if interesting_keys.include?(key)
+ parsed_key = key.to_s.gsub("_", "-")
+ case value
+ when nil
+ result << parsed_key
+ else
+ result << "#{parsed_key}=#{value}"
+ end
+ end
+ end
+ result.join("&")
+ end
+
+ def self.parse_headers(headers)
+ interesting_keys = [:content_type, :x_amz_acl, :range,
+ :if_modified_since, :if_unmodified_since,
+ :if_match, :if_none_match]
+ parsed_headers = {}
+ headers.each do |key, value|
+ if interesting_keys.include?(key)
+ parsed_key = key.to_s.gsub("_", "-")
+ parsed_value = value
+ case value
+ when Range
+ parsed_value = "#{value.first}-#{value.last}"
+ end
+ parsed_headers[parsed_key] = parsed_value
+ end
+ end
+ parsed_headers
+ end
end
end
4 lib/s3/service.rb
View
@@ -1,7 +1,7 @@
module S3
- class Service < Base
+ class Service
def initialize(options)
- super
+
end
def buckets
8 test/bucket_test.rb
View
@@ -1,13 +1,19 @@
require 'test_helper'
class BucketTest < Test::Unit::TestCase
+ def test_parse_name_without_bucket
+ host, prefix = S3::Bucket.parse_name("", "s3.amazonaws.com")
+ assert_equal "s3.amazonaws.com", host
+ assert_equal "", prefix
+ end
+
def test_parse_name_with_vhost_name
host, prefix = S3::Bucket.parse_name("data.synergypeople.net", "s3.amazonaws.com")
assert_equal "data.synergypeople.net.s3.amazonaws.com", host
assert_equal "", prefix
end
- def test_parse_name_with_prefix_based_name
+ def test_parse_name_with_path_based_name
host, prefix = S3::Bucket.parse_name("synergypeople_net", "s3.amazonaws.com")
assert_equal "s3.amazonaws.com", host
assert_equal "/synergypeople_net", prefix
Please sign in to comment.
Something went wrong with that request. Please try again.