aws-sdb 0.1.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ Spec::Rake::SpecTask.new
7
7
  gem_spec = Gem::Specification.new do |s|
8
8
  s.name = "aws-sdb"
9
9
  s.rubyforge_project = s.name
10
- s.version = "0.1.4"
10
+ s.version = "0.3.0"
11
11
  s.platform = Gem::Platform::RUBY
12
12
  s.has_rdoc = true
13
13
  s.extra_rdoc_files = ["README", "LICENSE"]
@@ -16,11 +16,16 @@ gem_spec = Gem::Specification.new do |s|
16
16
  s.author = "Tim Dysinger"
17
17
  s.email = "tim@dysinger.net"
18
18
  s.homepage = "http://aws-sdb.rubyforge.org"
19
- s.add_dependency "needle"
19
+ s.add_dependency "uuidtools"
20
20
  s.require_path = 'lib'
21
21
  s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
22
22
  end
23
23
 
24
+ desc "Open an irb session preloaded with this library"
25
+ task :console do
26
+ sh "irb -rubygems -I lib -r aws_sdb.rb"
27
+ end
28
+
24
29
  Rake::GemPackageTask.new(gem_spec) do |pkg|
25
30
  pkg.gem_spec = gem_spec
26
31
  end
data/lib/aws_sdb/error.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  module AwsSdb
2
2
 
3
3
  class Error < RuntimeError ; end
4
-
4
+
5
5
  class RequestError < Error
6
6
  attr_reader :request_id
7
-
7
+
8
8
  def initialize(message, request_id=nil)
9
9
  super(message)
10
10
  @request_id = request_id
11
11
  end
12
12
  end
13
-
13
+
14
14
  class InvalidDomainNameError < RequestError ; end
15
15
  class InvalidParameterValueError < RequestError ; end
16
16
  class InvalidNextTokenError < RequestError ; end
@@ -24,12 +24,12 @@ module AwsSdb
24
24
  class NumberDomainBytesExceededError < RequestError ; end
25
25
  class NumberItemAttributesExceededError < RequestError ; end
26
26
  class RequestTimeoutError < RequestError ; end
27
-
27
+
28
28
  class FeatureDeprecatedError < RequestError ; end
29
-
29
+
30
30
  class ConnectionError < Error
31
31
  attr_reader :response
32
-
32
+
33
33
  def initialize(response)
34
34
  super(
35
35
  "#{response.code} \
@@ -38,5 +38,5 @@ module AwsSdb
38
38
  @response = response
39
39
  end
40
40
  end
41
-
41
+
42
42
  end
@@ -1,82 +1,76 @@
1
+ require 'logger'
1
2
  require 'time'
2
3
  require 'cgi'
3
4
  require 'uri'
4
5
  require 'net/http'
5
-
6
6
  require 'base64'
7
7
  require 'openssl'
8
-
9
8
  require 'rexml/document'
10
9
  require 'rexml/xpath'
11
10
 
12
11
  module AwsSdb
13
-
12
+
14
13
  class Service
15
- def initialize(
16
- logger,
17
- access_key_id,
18
- secret_access_key,
19
- url = "http://sds.amazonaws.com"
20
- )
21
- @logger = logger
22
- @access_key_id = access_key_id
23
- @secret_access_key = secret_access_key
24
- @base_url = url
14
+ def initialize(options={})
15
+ @access_key_id = options[:access_key_id] || ENV['AMAZON_ACCESS_KEY_ID']
16
+ @secret_access_key = options[:secret_access_key] || ENV['AMAZON_SECRET_ACCESS_KEY']
17
+ @base_url = options[:url] || 'http://sdb.amazonaws.com'
18
+ @logger = options[:logger] || Logger.new("aws_sdb.log")
25
19
  end
26
-
20
+
27
21
  def list_domains(max = nil, token = nil)
28
22
  params = { 'Action' => 'ListDomains' }
29
- params['NextToken'] =
23
+ params['NextToken'] =
30
24
  token unless token.nil? || token.empty?
31
- params['MaxNumberOfDomains'] =
25
+ params['MaxNumberOfDomains'] =
32
26
  max.to_s unless max.nil? || max.to_i == 0
33
27
  doc = call(:get, params)
34
28
  results = []
35
- REXML::XPath.each(doc, '//DomainName/text()') do |domain|
29
+ REXML::XPath.each(doc, '//DomainName/text()') do |domain|
36
30
  results << domain.to_s
37
31
  end
38
- return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
32
+ return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
39
33
  end
40
-
34
+
41
35
  def create_domain(domain)
42
36
  call(:post, { 'Action' => 'CreateDomain', 'DomainName'=> domain.to_s })
43
37
  nil
44
38
  end
45
-
39
+
46
40
  def delete_domain(domain)
47
41
  call(
48
- :delete,
42
+ :delete,
49
43
  { 'Action' => 'DeleteDomain', 'DomainName' => domain.to_s }
50
44
  )
51
45
  nil
52
- end
53
-
46
+ end
47
+
54
48
  def query(domain, query, max = nil, token = nil)
55
- params = {
56
- 'Action' => 'Query',
49
+ params = {
50
+ 'Action' => 'Query',
57
51
  'QueryExpression' => query,
58
- 'DomainName' => domain.to_s
52
+ 'DomainName' => domain.to_s
59
53
  }
60
- params['NextToken'] =
54
+ params['NextToken'] =
61
55
  token unless token.nil? || token.empty?
62
- params['MaxNumberOfItems'] =
56
+ params['MaxNumberOfItems'] =
63
57
  max.to_s unless max.nil? || max.to_i == 0
64
58
  doc = call(:get, params)
65
59
  results = []
66
- REXML::XPath.each(doc, '//ItemName/text()') do |item|
60
+ REXML::XPath.each(doc, '//ItemName/text()') do |item|
67
61
  results << item.to_s
68
62
  end
69
- return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
63
+ return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
70
64
  end
71
-
65
+
72
66
  def put_attributes(domain, item, attributes, replace = true)
73
- params = {
74
- 'Action' => 'PutAttributes',
67
+ params = {
68
+ 'Action' => 'PutAttributes',
75
69
  'DomainName' => domain.to_s,
76
70
  'ItemName' => item.to_s
77
71
  }
78
72
  count = 0
79
- attributes.each do | key, values |
73
+ attributes.each do | key, values |
80
74
  ([]<<values).flatten.each do |value|
81
75
  params["Attribute.#{count}.Name"] = key.to_s
82
76
  params["Attribute.#{count}.Value"] = value.to_s
@@ -87,15 +81,15 @@ module AwsSdb
87
81
  call(:put, params)
88
82
  nil
89
83
  end
90
-
84
+
91
85
  def get_attributes(domain, item)
92
- doc = call(
93
- :get,
94
- {
95
- 'Action' => 'GetAttributes',
86
+ doc = call(
87
+ :get,
88
+ {
89
+ 'Action' => 'GetAttributes',
96
90
  'DomainName' => domain.to_s,
97
- 'ItemName' => item.to_s
98
- }
91
+ 'ItemName' => item.to_s
92
+ }
99
93
  )
100
94
  attributes = {}
101
95
  REXML::XPath.each(doc, "//Attribute") do |attr|
@@ -107,26 +101,26 @@ module AwsSdb
107
101
  end
108
102
 
109
103
  def delete_attributes(domain, item)
110
- call(
111
- :delete,
112
- {
113
- 'Action' => 'DeleteAttributes',
104
+ call(
105
+ :delete,
106
+ {
107
+ 'Action' => 'DeleteAttributes',
114
108
  'DomainName' => domain.to_s,
115
- 'ItemName' => item.to_s
116
- }
109
+ 'ItemName' => item.to_s
110
+ }
117
111
  )
118
112
  nil
119
113
  end
120
114
 
121
115
  protected
122
-
123
- def call(method, params)
124
- params.merge!( {
116
+
117
+ def call(method, params)
118
+ params.merge!( {
125
119
  'Version' => '2007-11-07',
126
120
  'SignatureVersion' => '1',
127
121
  'AWSAccessKeyId' => @access_key_id,
128
122
  'Timestamp' => Time.now.gmtime.iso8601
129
- }
123
+ }
130
124
  )
131
125
  data = ''
132
126
  query = []
@@ -142,7 +136,7 @@ module AwsSdb
142
136
  url = "#{@base_url}?#{query}"
143
137
  uri = URI.parse(url)
144
138
  @logger.debug("#{url}") if @logger
145
- response =
139
+ response =
146
140
  Net::HTTP.new(uri.host, uri.port).send_request(method, url)
147
141
  @logger.debug("#{response.code}\n#{response.body}") if @logger
148
142
  raise(ConnectionError.new(response)) unless (200..400).include?(
@@ -156,7 +150,7 @@ module AwsSdb
156
150
  ).new(
157
151
  error.get_elements('Message')[0].text,
158
152
  doc.get_elements('*/RequestID')[0].text
159
- )
153
+ )
160
154
  ) unless error.nil?
161
155
  doc
162
156
  end
data/lib/aws_sdb.rb CHANGED
@@ -1,23 +1,3 @@
1
- # require needle
2
- require 'rubygems'
3
- require 'needle'
4
-
5
- # require modules
6
1
  require 'aws_sdb/error'
7
2
  require 'aws_sdb/service'
8
3
 
9
- module AwsSdb
10
-
11
- def self.container
12
- @@container ||= Needle::Registry.new(
13
- :logs => { :filename => "aws_sdb.log" }
14
- ).namespace!(:aws_sdb) do
15
-
16
- service(:model => :multiton) do | c, p, access, secret |
17
- Service.new(c.log_for(p), access, secret)
18
- end
19
-
20
- end
21
- end
22
-
23
- end
@@ -11,11 +11,7 @@ include AwsSdb
11
11
 
12
12
  describe Service, "when creating a new domain" do
13
13
  before(:all) do
14
- @logger = AwsSdb.container.logs.get(:rspec)
15
- @service = AwsSdb.container.service(
16
- ENV['AMAZON_ACCESS_KEY_ID'],
17
- ENV['AMAZON_SECRET_ACCESS_KEY']
18
- )
14
+ @service = AwsSdb::Service.new
19
15
  @domain = "test-#{UUID.random_create.to_s}"
20
16
  # domains = @service.list_domains[0]
21
17
  # domains.each do |d|
@@ -66,14 +62,14 @@ describe Service, "when creating a new domain" do
66
62
  http.stub!(:send_request).and_return(resp)
67
63
  Net::HTTP.stub!(:new).and_return(http)
68
64
  end
69
-
65
+
70
66
  it "should not raise an error if a valid new domain name is given" do
71
67
  stub_success
72
68
  lambda {
73
69
  @service.create_domain("test-#{UUID.random_create.to_s}")
74
70
  }.should_not raise_error
75
71
  end
76
-
72
+
77
73
  it "should not raise an error if the domain name already exists" do
78
74
  stub_success
79
75
  domain = "test-#{UUID.random_create.to_s}"
@@ -82,55 +78,55 @@ describe Service, "when creating a new domain" do
82
78
  @service.create_domain(domain)
83
79
  }.should_not raise_error
84
80
  end
85
-
81
+
86
82
  it "should raise an error if an a nil or '' domain name is given" do
87
83
  stub_error(
88
- 400,
89
- :InvalidParameterValue,
84
+ 400,
85
+ :InvalidParameterValue,
90
86
  "Value () for parameter DomainName is invalid."
91
87
  )
92
- lambda {
93
- @service.create_domain('')
88
+ lambda {
89
+ @service.create_domain('')
94
90
  }.should raise_error(InvalidParameterValueError)
95
- lambda {
91
+ lambda {
96
92
  @service.create_domain(nil)
97
93
  }.should raise_error(InvalidParameterValueError)
98
94
  stub_error(
99
- 400,
100
- :InvalidParameterValue,
95
+ 400,
96
+ :InvalidParameterValue,
101
97
  "Value ( ) for parameter DomainName is invalid."
102
98
  )
103
- lambda {
99
+ lambda {
104
100
  @service.create_domain(' ')
105
101
  }.should raise_error(InvalidParameterValueError)
106
102
  end
107
103
 
108
104
  it "should raise an error if the domain name length is < 3 or > 255" do
109
105
  stub_error(
110
- 400,
111
- :InvalidParameterValue,
106
+ 400,
107
+ :InvalidParameterValue,
112
108
  "Value (xx) for parameter DomainName is invalid."
113
109
  )
114
- lambda {
110
+ lambda {
115
111
  @service.create_domain('xx')
116
112
  }.should raise_error(InvalidParameterValueError)
117
113
  stub_error(
118
- 400,
119
- :InvalidParameterValue,
114
+ 400,
115
+ :InvalidParameterValue,
120
116
  "Value (#{:x.to_s*256}) for parameter DomainName is invalid."
121
117
  )
122
- lambda {
118
+ lambda {
123
119
  @service.create_domain('x'*256)
124
120
  }.should raise_error(InvalidParameterValueError)
125
121
  end
126
122
 
127
123
  it "should only accept domain names with a-z, A-Z, 0-9, '_', '-', and '.' " do
128
124
  stub_error(
129
- 400,
130
- :InvalidParameterValue,
125
+ 400,
126
+ :InvalidParameterValue,
131
127
  "Value (@$^*()) for parameter DomainName is invalid."
132
128
  )
133
- lambda {
129
+ lambda {
134
130
  @service.create_domain('@$^*()')
135
131
  }.should raise_error(InvalidParameterValueError)
136
132
  end
@@ -146,10 +142,7 @@ end
146
142
 
147
143
  describe Service, "when listing domains" do
148
144
  before(:all) do
149
- @service = AwsSdb.container.service(
150
- ENV['AMAZON_ACCESS_KEY_ID'],
151
- ENV['AMAZON_SECRET_ACCESS_KEY']
152
- )
145
+ @service = AwsSdb::Service.new
153
146
  @domain = "test-#{UUID.random_create.to_s}"
154
147
  # @service.list_domains[0].each do |d|
155
148
  # @service.delete_domain(d) if d =~ /^test/
@@ -179,10 +172,10 @@ describe Service, "when listing domains" do
179
172
  http = mock(Net::HTTP)
180
173
  http.stub!(:send_request).and_return(resp)
181
174
  Net::HTTP.stub!(:new).and_return(http)
182
-
175
+
183
176
  result = nil
184
- lambda { result = @service.list_domains[0] }.should_not raise_error
185
- result.should_not be_nil
177
+ lambda { result = @service.list_domains[0] }.should_not raise_error
178
+ result.should_not be_nil
186
179
  result.should_not be_empty
187
180
  result.size.should == 1
188
181
  result.should_not be_nil
@@ -192,21 +185,18 @@ end
192
185
 
193
186
  describe Service, "when deleting domains" do
194
187
  before(:all) do
195
- @service = AwsSdb.container.service(
196
- ENV['AMAZON_ACCESS_KEY_ID'],
197
- ENV['AMAZON_SECRET_ACCESS_KEY']
198
- )
188
+ @service = AwsSdb::Service.new
199
189
  @domain = "test-#{UUID.random_create.to_s}"
200
190
  # @service.list_domains[0].each do |d|
201
191
  # @service.delete_domain(d) if d =~ /^test/
202
192
  # end
203
193
  # @service.create_domain(@domain)
204
194
  end
205
-
195
+
206
196
  after do
207
197
  # @service.delete_domain(@domain)
208
198
  end
209
-
199
+
210
200
  def stub_success
211
201
  resp = mock(Net::HTTPResponse)
212
202
  resp.stub!(:code).and_return("200")
@@ -230,21 +220,18 @@ describe Service, "when deleting domains" do
230
220
  stub_success
231
221
  lambda { @service.delete_domain(@domain) }.should_not raise_error
232
222
  end
233
-
223
+
234
224
  it "should not raise an error trying to delete a non-existing domain" do
235
225
  stub_success
236
- lambda {
237
- @service.delete_domain(UUID.random_create.to_s)
226
+ lambda {
227
+ @service.delete_domain(UUID.random_create.to_s)
238
228
  }.should_not raise_error
239
229
  end
240
230
  end
241
231
 
242
232
  describe Service, "when managing items" do
243
233
  before(:all) do
244
- @service = AwsSdb.container.service(
245
- ENV['AMAZON_ACCESS_KEY_ID'],
246
- ENV['AMAZON_SECRET_ACCESS_KEY']
247
- )
234
+ @service = AwsSdb::Service.new
248
235
  @domain = "test-#{UUID.random_create.to_s}"
249
236
  # @service.list_domains[0].each do |d|
250
237
  # @service.delete_domain(d) if d =~ /^test/
@@ -256,11 +243,11 @@ describe Service, "when managing items" do
256
243
  :answer => [ true, 'testing123', 4.2, 42, 420 ]
257
244
  }
258
245
  end
259
-
246
+
260
247
  after(:all) do
261
248
  # @service.delete_domain(@domain)
262
249
  end
263
-
250
+
264
251
  def stub_put
265
252
  resp = mock(Net::HTTPResponse)
266
253
  resp.stub!(:code).and_return("200")
@@ -276,9 +263,9 @@ describe Service, "when managing items" do
276
263
  )
277
264
  http = mock(Net::HTTP)
278
265
  http.stub!(:send_request).and_return(resp)
279
- Net::HTTP.stub!(:new).and_return(http)
266
+ Net::HTTP.stub!(:new).and_return(http)
280
267
  end
281
-
268
+
282
269
  def stub_get
283
270
  resp = mock(Net::HTTPResponse)
284
271
  resp.stub!(:code).and_return("200")
@@ -319,9 +306,9 @@ describe Service, "when managing items" do
319
306
  )
320
307
  http = mock(Net::HTTP)
321
308
  http.stub!(:send_request).and_return(resp)
322
- Net::HTTP.stub!(:new).and_return(http)
309
+ Net::HTTP.stub!(:new).and_return(http)
323
310
  end
324
-
311
+
325
312
  def stub_query
326
313
  resp = mock(Net::HTTPResponse)
327
314
  resp.stub!(:code).and_return("200")
@@ -341,7 +328,7 @@ describe Service, "when managing items" do
341
328
  http.stub!(:send_request).and_return(resp)
342
329
  Net::HTTP.stub!(:new).and_return(http)
343
330
  end
344
-
331
+
345
332
  def stub_delete
346
333
  resp = mock(Net::HTTPResponse)
347
334
  resp.stub!(:code).and_return("200")
@@ -360,7 +347,7 @@ describe Service, "when managing items" do
360
347
  http.stub!(:send_request).and_return(resp)
361
348
  Net::HTTP.stub!(:new).and_return(http)
362
349
  end
363
-
350
+
364
351
  it "should be able to put attributes" do
365
352
  stub_put
366
353
  lambda {
@@ -368,7 +355,7 @@ describe Service, "when managing items" do
368
355
  }.should_not raise_error
369
356
  end
370
357
 
371
- it "should be able to get attributes" do
358
+ it "should be able to get attributes" do
372
359
  stub_get
373
360
  result = nil
374
361
  lambda {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Dysinger
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-19 00:00:00 -10:00
12
+ date: 2008-08-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: needle
16
+ name: uuidtools
17
17
  version_requirement:
18
18
  version_requirements: !ruby/object:Gem::Requirement
19
19
  requirements:
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  requirements: []
64
64
 
65
65
  rubyforge_project: aws-sdb
66
- rubygems_version: 1.0.1
66
+ rubygems_version: 1.1.1
67
67
  signing_key:
68
68
  specification_version: 2
69
69
  summary: Amazon SDB API