Class: Presss::HTTP
- Inherits:
-
Object
- Object
- Presss::HTTP
- Defined in:
- lib/presss.rb
Defined Under Namespace
Classes: RequestError, Response
Class Attribute Summary collapse
-
.port ⇒ Object
Returns the value of attribute port.
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#absolute_path(path) ⇒ Object
Returns the absolute path based on the key for the object.
-
#authorization ⇒ Object
Returns a Presss::Authorization instance for the configured AWS credentials.
-
#bucket_name ⇒ Object
Returns the configured bucket name.
-
#canonicalized_resource(absolute_path) ⇒ Object
Returns the canonicalized resource used in the authorization signature for an absolute path to an object.
- #domain ⇒ Object
-
#get(path) ⇒ Object
Get an object with a key.
-
#headers(date, message, content_type = nil) ⇒ Object
Returns the request headers for a date, message and content-type.
-
#host ⇒ Object
Returns the AWS hostname based on the configured bucket name.
-
#http ⇒ Object
Returns a Net::HTTP instance with the correct SSL configuration for a request.
-
#initialize(config) ⇒ HTTP
constructor
A new instance of HTTP.
-
#join(verb, body, content_type, date, headers, absolute_path) ⇒ Object
Joins a number of parameters for a valid request message used to compute the request signature.
-
#put(path, file, content_type = nil) ⇒ Object
Puts an object with a key using a file or string.
- #region ⇒ Object
Constructor Details
#initialize(config) ⇒ HTTP
Returns a new instance of HTTP.
55 56 57 |
# File 'lib/presss.rb', line 55 def initialize(config) @config = config end |
Class Attribute Details
.port ⇒ Object
Returns the value of attribute port.
49 50 51 |
# File 'lib/presss.rb', line 49 def port @port end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
53 54 55 |
# File 'lib/presss.rb', line 53 def config @config end |
Instance Method Details
#absolute_path(path) ⇒ Object
Returns the absolute path based on the key for the object.
83 84 85 |
# File 'lib/presss.rb', line 83 def absolute_path(path) path.start_with?('/') ? path : '/' + path end |
#authorization ⇒ Object
Returns a Presss::Authorization instance for the configured AWS credentials.
99 100 101 102 103 104 |
# File 'lib/presss.rb', line 99 def ||= Presss::Authorization.new( config[:access_key_id], config[:secret_access_key] ) end |
#bucket_name ⇒ Object
Returns the configured bucket name.
60 61 62 |
# File 'lib/presss.rb', line 60 def bucket_name config[:bucket_name] end |
#canonicalized_resource(absolute_path) ⇒ Object
Returns the canonicalized resource used in the authorization signature for an absolute path to an object.
89 90 91 92 93 94 95 |
# File 'lib/presss.rb', line 89 def canonicalized_resource(absolute_path) if bucket_name.nil? raise ArgumentError, "Please configure a bucket_name: Presss.config = { bucket_name: 'my-bucket-name }" else '/' + bucket_name + absolute_path end end |
#domain ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/presss.rb', line 68 def domain case region when 'us-east-1' 's3.amazonaws.com' else 's3-%s.amazonaws.com' % region end end |
#get(path) ⇒ Object
Get an object with a key.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/presss.rb', line 143 def get(path) path = absolute_path(path) date = Time.now.rfc2822 = join('GET', nil, nil, date, nil, path) request = Net::HTTP::Get.new(path, headers(date, )) begin response = http.start { |http| http.request(request) } Presss::HTTP::Response.new( response.code, response.instance_variable_get('@header'), response.body ) rescue EOFError => error raise Presss::HTTP::RequestError, error. end end |
#headers(date, message, content_type = nil) ⇒ Object
Returns the request headers for a date, message and content-type.
107 108 109 110 111 112 113 114 115 |
# File 'lib/presss.rb', line 107 def headers(date, , content_type=nil) headers = { 'Authorization' => .header(), 'Date' => date, 'User-Agent' => 'Press/0.9' } headers['Content-Type'] = content_type if content_type headers end |
#host ⇒ Object
Returns the AWS hostname based on the configured bucket name.
78 79 80 |
# File 'lib/presss.rb', line 78 def host bucket_name + '.' + domain end |
#http ⇒ Object
Returns a Net::HTTP instance with the correct SSL configuration for a request.
119 120 121 122 123 124 125 126 127 |
# File 'lib/presss.rb', line 119 def http @http ||= begin http = Net::HTTP.new(host, self.class.port) http.use_ssl = true http.ca_file = File.('../../support/cacert.pem', __FILE__) http.verify_mode = OpenSSL::SSL::VERIFY_PEER http end end |
#join(verb, body, content_type, date, headers, absolute_path) ⇒ Object
Joins a number of parameters for a valid request message used to compute the request signature.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/presss.rb', line 131 def join(verb, body, content_type, date, headers, absolute_path) [ verb.to_s.upcase, nil, content_type, date, # TODO: aws-x headers? canonicalized_resource(absolute_path) ].join("\n") end |
#put(path, file, content_type = nil) ⇒ Object
Puts an object with a key using a file or string. Optionally pass in the content-type if you want to set a specific one.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/presss.rb', line 162 def put(path, file, content_type=nil) path = absolute_path(path) body = file.respond_to?(:read) ? file.read : file.to_s date = Time.now.rfc2822 = join('PUT', body, content_type, date, nil, path) request = Net::HTTP::Put.new(path, headers(date, , content_type)) request.body = body begin response = http.start { |http| http.request(request) } Presss::HTTP::Response.new( response.code, response.instance_variable_get('@header'), response.body ) rescue EOFError => error raise Presss::HTTP::RequestError, error. end end |
#region ⇒ Object
64 65 66 |
# File 'lib/presss.rb', line 64 def region config[:region] || 'us-east-1' end |