swift-storage 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3246e84c36a57fc2892b66434fda76d6a0985c5
4
- data.tar.gz: 76f1ad2fa5f6b47e0d704ca2ec2095d3ac1ee7fc
3
+ metadata.gz: 72038776fffb8b39bab4365ba7a14e178d1f981a
4
+ data.tar.gz: 802bae9e36c40a8dc206a9da3501cf8b0318c9d9
5
5
  SHA512:
6
- metadata.gz: 9d0bee3254a69d404e1ef30e9ee454cc81d894dcb59a69dd7c2e5d4e8269ce5a38b402a5cfaf54527bbbada5f80190ddd16094eaf2703094fb28ff069c5f6a79
7
- data.tar.gz: 5035e8d1b0ac40516926bf47aab4e6d3a8597c23bc3acff638cce78018b1da0b18000281b9cb67b4f19878c6fe2ce8fc18bd74d37cc42343061f3eab28d17705
6
+ metadata.gz: c869406cf504fcea9315042c2dcff3735a273c3d96b87d55f53f066f65bba19e798ac9e82c6eb02001f213c500ac79217b5d45e84f7faebf72304dab1248e4b6
7
+ data.tar.gz: 74195392053ff11250f159069e69db38302e8e41a13347c3395aecd5a688fc969419c0b787cc0e130fd903ff2e96f2a68f82342251f04608f23888903fd380df
@@ -26,7 +26,7 @@ class SwiftStorage::ContainerCollection < SwiftStorage::Node
26
26
  # Container with given name
27
27
  #
28
28
  def [](name)
29
- SwiftStorage::Container.new(service, name)
29
+ SwiftStorage::Container.new(service, name) if name
30
30
  end
31
31
 
32
32
  end
@@ -30,9 +30,9 @@ class SwiftStorage::Node
30
30
  @service
31
31
  end
32
32
 
33
- def get_lines(path)
33
+ def get_lines(path, prefix: nil)
34
34
  headers = {'Accept' => 'text/plain'}
35
- response = request(path, :headers => headers)
35
+ response = request(path, :headers => headers, :params => {:prefix => prefix})
36
36
  body = response.body
37
37
  if body.nil? || body.empty?
38
38
  []
@@ -92,6 +92,10 @@ class SwiftStorage::Node
92
92
  request(relative_path, :method => :delete)
93
93
  end
94
94
 
95
+ def delete_if_exists
96
+ delete rescue SwiftStorage::NotFoundError
97
+ end
98
+
95
99
 
96
100
  private
97
101
 
@@ -44,6 +44,23 @@ class SwiftStorage::Object < SwiftStorage::Node
44
44
  end
45
45
  end
46
46
 
47
+ # Stream the object data to a file
48
+ #
49
+ # This will always make a request to the API server and will not use cache
50
+ #
51
+ # @param output_path [String]
52
+ # The path to the output file.
53
+ #
54
+ # @return [output_path]
55
+ # The passed path.
56
+ #
57
+ def stream_to_file(output_path)
58
+ open(output_path, 'wb') do |io|
59
+ read(io)
60
+ end
61
+ output_path
62
+ end
63
+
47
64
 
48
65
  # Write the object
49
66
  #
@@ -78,8 +95,10 @@ class SwiftStorage::Object < SwiftStorage::Node
78
95
  # always be served with the same max-age value. To have the resource expire
79
96
  # at point of time, use the expires header.
80
97
  #
81
- # @param expires [Time]
98
+ # @param expires [Symbol, Time]
82
99
  # Set the Expires header.
100
+ # Expires may also have the special value `:never` which override
101
+ # `cache_control` and set the expiration time in a long time.
83
102
  #
84
103
  # @param object_manifest [String]
85
104
  # When set, this object acts as a large object manifest. The value should be
@@ -105,6 +124,11 @@ class SwiftStorage::Object < SwiftStorage::Node
105
124
 
106
125
  object_manifest.nil? or input_stream.nil? or raise ArgumentError, 'Input must be nil on object manigest'
107
126
 
127
+ if expires == :never
128
+ expires = Time.at(4_000_000_000)
129
+ cache_control = "public, max_age=4000000000"
130
+ end
131
+
108
132
  h[H::CONTENT_DISPOSITION] = attachment ? 'attachment' : 'inline'
109
133
  h[H::OBJECT_MANIFEST] = object_manifest if object_manifest
110
134
  h[H::CONTENT_TYPE] = content_type if content_type
@@ -11,7 +11,7 @@ class SwiftStorage::ObjectCollection < SwiftStorage::Node
11
11
  # Objects in this collection
12
12
  #
13
13
  def all
14
- get_lines(container.name).map { |name| SwiftStorage::Object.new(container, name)}
14
+ get_objects
15
15
  end
16
16
 
17
17
  # Return a particular object
@@ -26,7 +26,16 @@ class SwiftStorage::ObjectCollection < SwiftStorage::Node
26
26
  # Object with given name
27
27
  #
28
28
  def [](name)
29
- SwiftStorage::Object.new(container, name)
29
+ SwiftStorage::Object.new(container, name) if name
30
+ end
31
+
32
+ def with_prefix(prefix)
33
+ get_objects(prefix)
34
+ end
35
+
36
+ private
37
+ def get_objects(prefix=nil)
38
+ get_lines(container.name, :prefix => prefix).map { |name| SwiftStorage::Object.new(container, name)}
30
39
  end
31
40
 
32
41
  end
@@ -112,6 +112,7 @@ class SwiftStorage::Service
112
112
  def request(path_or_url,
113
113
  method: :get,
114
114
  headers: nil,
115
+ params: nil,
115
116
  input_stream: nil,
116
117
  output_stream: nil)
117
118
  headers ||= {}
@@ -139,6 +140,11 @@ class SwiftStorage::Service
139
140
 
140
141
  case method
141
142
  when :get
143
+ if params.respond_to?(:to_hash)
144
+ params.reject!{|k,v| v.nil?}
145
+ path << '?'
146
+ path << URI.encode_www_form(params)
147
+ end
142
148
  req = Net::HTTP::Get.new(path, headers)
143
149
  when :delete
144
150
  req = Net::HTTP::Delete.new(path, headers)
@@ -188,13 +194,13 @@ class SwiftStorage::Service
188
194
  when /^2/
189
195
  return true
190
196
  when '401'
191
- raise AuthError
197
+ raise AuthError, response.body
192
198
  when '403'
193
- raise ForbiddenError
199
+ raise ForbiddenError, response.body
194
200
  when '404'
195
- raise NotFoundError
201
+ raise NotFoundError, response.body
196
202
  else
197
- raise ServerError
203
+ raise ServerError, response.body
198
204
  end
199
205
  end
200
206
 
@@ -1,3 +1,3 @@
1
1
  module SwiftStorage
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -56,7 +56,10 @@ module TestServerMixin
56
56
 
57
57
  end
58
58
 
59
- RSpec::Matchers.define :send_request do |method, path, headers, body|
59
+ RSpec::Matchers.define :send_request do |method, path, options={}|
60
+ headers = options[:headers]
61
+ body = options[:body]
62
+ params = options[:params]
60
63
  match do |actual|
61
64
  actual.call()
62
65
  env = server.app.last_env
@@ -82,7 +85,14 @@ RSpec::Matchers.define :send_request do |method, path, headers, body|
82
85
  @body_match = true
83
86
  @body_match = @actual_body == body if body
84
87
 
85
- @method_match && @path_match && @headers_match && @body_match
88
+ @params_match = true
89
+ if params
90
+ @actual_params = env['QUERY_STRING']
91
+ @params_string = URI.encode_www_form(params)
92
+ @params_match = @params_string == @actual_params
93
+ end
94
+
95
+ @method_match && @path_match && @headers_match && @body_match && @params_match
86
96
  end
87
97
 
88
98
  failure_message do
@@ -91,6 +101,7 @@ RSpec::Matchers.define :send_request do |method, path, headers, body|
91
101
  r << "Path should be #{path}, got #{@actual_path}" if !@path_match
92
102
  r << "Unmatched headers #{@unatched_header}" if !@headers_match
93
103
  r << "Body doesn't match, for #{@actual_body} expected #{body}" if !@body_match
104
+ r << "Params should be #{@params_string}, got #{@actual_params}" if !@params_match
94
105
  r.join("\n")
95
106
  end
96
107
 
@@ -9,7 +9,7 @@ RSpec.describe 'Auth' do
9
9
  h::STORAGE_TOKEN => 'storage_token'
10
10
  )
11
11
 
12
- expect{ subject.authenticate! }.to send_request(:get, '/auth/v1.0', {
12
+ expect{ subject.authenticate! }.to send_request(:get, '/auth/v1.0', :headers => {
13
13
  h::AUTH_USER => 'test:testuser',
14
14
  h::AUTH_KEY => 'testpassword'
15
15
  })
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SwiftStorage::Container do
4
+ subject { swift_service.containers['test'] }
5
+ it "list with prefix" do
6
+ expect{ subject.objects.with_prefix('foo/bar') }.to(
7
+ send_request(:get, '/v1/AUTH_test/test',
8
+ :params => {:prefix => 'foo/bar'})
9
+ )
10
+ end
11
+ end
12
+
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- RSpec.describe 'Object' do
3
+ RSpec.describe SwiftStorage::Object do
4
4
  subject { swift_service.containers['somecontainer'].objects['someobject'] }
5
5
  it "read" do
6
6
  headers(
@@ -32,14 +32,15 @@ RSpec.describe 'Object' do
32
32
  :attachment => true,
33
33
  :delete_at => Time.at(4000),
34
34
  :metadata => {:foo => :bar, :jon => :doe, 'WeiRd ^^!+Format With!! Spaces $$$' => 'weird'})
35
- }.to send_request(:put, '/v1/AUTH_test/somecontainer/someobject', {
35
+ }.to send_request(:put, '/v1/AUTH_test/somecontainer/someobject',
36
+ :headers => {
36
37
  'Content-Length' => b.length,
37
38
  'Content-Disposition' => 'attachment',
38
39
  'X-Delete-At' => 4000,
39
40
  'X-Object-Meta-Foo' => 'bar',
40
41
  'X-Object-Meta-Jon' => 'doe',
41
42
  'X-Object-Meta-Weird-Format-With-Spaces' => 'weird',
42
- }, b)
43
+ }, :body => b)
43
44
  expect(subject.metadata.jon_doe).to eq('a meta')
44
45
  end
45
46
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swift-storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Goy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-27 00:00:00.000000000 Z
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,6 +152,7 @@ files:
152
152
  - spec/spec_helper.rb
153
153
  - spec/support/local_server.rb
154
154
  - spec/swift/auth_spec.rb
155
+ - spec/swift/container_spec.rb
155
156
  - spec/swift/object_spec.rb
156
157
  - spec/swift/temp_url_spec.rb
157
158
  - swift-ruby.gemspec
@@ -183,6 +184,7 @@ test_files:
183
184
  - spec/spec_helper.rb
184
185
  - spec/support/local_server.rb
185
186
  - spec/swift/auth_spec.rb
187
+ - spec/swift/container_spec.rb
186
188
  - spec/swift/object_spec.rb
187
189
  - spec/swift/temp_url_spec.rb
188
190
  has_rdoc: