raws 0.0.7

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.
data/CHANGELOG ADDED
File without changes
data/COPYING ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2009 Jun Kikuchi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,108 @@
1
+ = RAWS
2
+
3
+ RAWS is a Ruby library for Amazon Web Service (AWS).
4
+
5
+ == SQS (Amazon Simple Queue Service)
6
+
7
+ require 'rubygems'
8
+ require 'raws'
9
+
10
+ RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
11
+ RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_
12
+
13
+ RAWS::SQS.create_queue('test_queue')
14
+ sleep 60
15
+
16
+ RAWS::SQS['test_queue'].send('hello1')
17
+ RAWS::SQS['test_queue'].send('hello2')
18
+ RAWS::SQS['test_queue'].send('hello3')
19
+
20
+ RAWS::SQS['test_queue'].receive(:limit => 10).each do |msg|
21
+ p msg.body
22
+ msg.delete
23
+ end
24
+
25
+ RAWS::SQS['test_queue'].delete_queue
26
+
27
+ == SDB (Amazon SimpleDB)
28
+
29
+ プリミティブな使い方
30
+
31
+ require 'rubygems'
32
+ require 'raws'
33
+
34
+ RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
35
+ RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_
36
+
37
+ RAWS::SDB.create_domain('test_domain')
38
+ sleep 60
39
+
40
+ RAWS::SDB['test_domain'].put('1', 'a' => '10')
41
+ RAWS::SDB['test_domain'].put('2', 'b' => '10')
42
+ RAWS::SDB['test_domain'].put('3', 'c' => '10')
43
+
44
+ p RAWS::SDB['test_domain'].get('1')
45
+ p RAWS::SDB['test_domain'].get('2')
46
+ p RAWS::SDB['test_domain'].get('3')
47
+
48
+ RAWS::SDB['test_domain'].all.each do |a|
49
+ key, data = a
50
+ p [key, data]
51
+ end
52
+
53
+ RAWS::SDB['test_domain'].all.filter('a = ?', 10).each do |a|
54
+ key, data = a
55
+ p [key, data]
56
+ end
57
+
58
+ RAWS::SDB['test_domain'].delete('1')
59
+ RAWS::SDB['test_domain'].delete('2')
60
+ RAWS::SDB['test_domain'].delete('3')
61
+
62
+ RAWS::SDB['test_domain'].delete_domain
63
+
64
+ クラスとして使う場合
65
+
66
+ require 'rubygems'
67
+ require 'raws'
68
+
69
+ RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
70
+ RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_
71
+
72
+ class Foo
73
+ include RAWS::SDB::Model
74
+ self.domain_name = 'test_domain'
75
+
76
+ sdb_accessor 'a', 'b'
77
+ end
78
+
79
+ Foo.create_domain
80
+ sleep 60
81
+
82
+ foo1 = Foo.new('a' => '10', 'b' => '100', 'c' => ['1000a', '1000b'])
83
+ foo1.save
84
+
85
+ foo2 = Foo.new
86
+ foo2['a'] = '20'
87
+ foo2['b'] = ['200a', '200b']
88
+ foo2['c'] = '2000'
89
+ foo2.save
90
+
91
+ foo3 = Foo.new
92
+ foo3.a = ['30a', '30b']
93
+ foo3.b = '300'
94
+ foo3['c'] = '3000'
95
+ foo3.save
96
+
97
+ Foo.all.each do |a|
98
+ p [a.a, a['b'], a['c']]
99
+ end
100
+
101
+ Foo.all.filter('a = ?', '10').each do |a|
102
+ p [a.a, a.b, a['c']]
103
+ a.delete
104
+ end
105
+
106
+ Foo.delete_domain
107
+
108
+ == S3 (Amazon Simple Storage Service)
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'spec/rake/spectask'
3
+
4
+ NAME = 'raws'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |s|
9
+ s.name = NAME
10
+ s.platform = Gem::Platform::RUBY
11
+ s.summary = NAME
12
+ s.description = NAME
13
+ s.author = "Jun Kikuchi"
14
+ s.email = "kikuchi@bonnou.com"
15
+ s.homepage = "http://github.com/JunKikuchi/raws"
16
+ s.files = %w(
17
+ COPYING CHANGELOG README.rdoc Rakefile VERSION
18
+ ) + Dir.glob("{bin,doc,lib}/**/*")
19
+ s.require_path = "lib"
20
+ s.has_rdoc = true
21
+ s.add_dependency('pauldix-typhoeus', '>=0.1.2')
22
+ s.add_dependency('nokogiri', '>=1.3.3')
23
+ s.add_dependency('uuidtools', '>=2.0.0')
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new do |t|
30
+ #t.spec_opts = ['-c --format specdoc']
31
+ t.spec_opts = ['-c']
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.7
@@ -0,0 +1,123 @@
1
+ require 'digest/md5'
2
+
3
+ class RAWS::S3::Adapter
4
+ module Adapter20060301
5
+ URI_PARAMS = {
6
+ :scheme => 'https',
7
+ :host => 's3.amazonaws.com',
8
+ :path => '/',
9
+ :query => {}
10
+ }
11
+
12
+ def build_uri(params={})
13
+ "#{params[:scheme]}://#{
14
+ if params[:bucket]
15
+ "#{params[:bucket]}.#{params[:host]}"
16
+ else
17
+ params[:host]
18
+ end
19
+ }#{params[:path]}#{
20
+ if params[:query].empty?
21
+ ''
22
+ else
23
+ '?' << a[:query].map do |key, val|
24
+ val ? "#{RAWS.escape(key)}=#{RAWS.escape(val)}" : RAWS.escape(key)
25
+ end.sort.join(';')
26
+ end
27
+ }"
28
+ end
29
+
30
+ def sign(http_verb, content, type, date, path)
31
+ "#{RAWS.aws_access_key_id}:#{
32
+ [
33
+ ::OpenSSL::HMAC.digest(
34
+ ::OpenSSL::Digest::SHA1.new,
35
+ RAWS.aws_secret_access_key,
36
+ [
37
+ http_verb,
38
+ content ? Digest::MD5.hexdigest(content) : '',
39
+ type,
40
+ date,
41
+ path
42
+ ].join("\n")
43
+ )
44
+ ].pack('m').strip
45
+ }"
46
+ end
47
+
48
+ def fetch(http_verb, _uri={}, options={}, content=nil, type='')
49
+ date = Time.now.httpdate
50
+ uri = URI_PARAMS.merge(_uri)
51
+ r = RAWS.__send__(
52
+ http_verb.downcase.to_sym,
53
+ build_uri(uri),
54
+ :headers => {
55
+ 'Date' => date,
56
+ 'Authorization' => "AWS #{
57
+ sign(http_verb, content, type, date, uri[:path])
58
+ }"
59
+ }
60
+ )
61
+ data = RAWS.parse(Nokogiri::XML.parse(r.body), options)
62
+ if 200 <= r.code && r.code <= 299
63
+ data
64
+ else
65
+ raise RAWS::Error.new(r, data)
66
+ end
67
+ end
68
+
69
+ def get_service
70
+ fetch('GET', {}, :multiple => 'Bucket')
71
+ end
72
+
73
+ def put_bucket(bucket_name)
74
+ fetch('PUT', :path => "/#{bucket_name}")
75
+ end
76
+
77
+ def put_request_payment(bucket_name)
78
+ fetch(
79
+ 'PUT',
80
+ {
81
+ :bucket => bucket_name,
82
+ :query => {'requestPayment' => nil}
83
+ }
84
+ )
85
+ end
86
+
87
+ def get_bucket(bucket_name, params={})
88
+ fetch(
89
+ 'GET',
90
+ {
91
+ :bucket => bucket_name,
92
+ :query => params
93
+ }
94
+ )
95
+ end
96
+
97
+ def get_request_payment(bucket_name)
98
+ fetch(
99
+ 'GET',
100
+ {
101
+ :bucket => bucket_name,
102
+ :query => {'requestPayment' => nil}
103
+ }
104
+ )
105
+ end
106
+
107
+ def get_bucket_location(bucket_name)
108
+ fetch(
109
+ 'GET',
110
+ {
111
+ :bucket => bucket_name,
112
+ :query => {'location' => nil}
113
+ }
114
+ )
115
+ end
116
+
117
+ def delete_bucket(bucket_name)
118
+ fetch('DELETE', :path => "/#{bucket_name}")
119
+ end
120
+ end
121
+
122
+ extend Adapter20060301
123
+ end
data/lib/raws/s3.rb ADDED
@@ -0,0 +1,21 @@
1
+ class RAWS::S3
2
+ autoload :Adapter, 'raws/s3/adapter'
3
+
4
+ class << self
5
+ def create_bucket(bucket_name)
6
+ end
7
+
8
+ def delete_bucket(bucket_name)
9
+ end
10
+
11
+ def list
12
+ Adapter.get_service
13
+ end
14
+
15
+ def each(&block)
16
+ end
17
+
18
+ def [](bucket_name)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,139 @@
1
+ class RAWS::SDB::Adapter
2
+ module Adapter20090415
3
+ URI = 'https://sdb.amazonaws.com/'
4
+ PARAMS = {'Version' => '2009-04-15'}
5
+ KEYWORDS = %w'or and not from where select like null is order by asc desc in between intersection limit every'
6
+ REXP_NAME = /^[a-zA-Z_$]/
7
+
8
+ def create_domain(domain_name)
9
+ params = {
10
+ 'Action' => 'CreateDomain',
11
+ 'DomainName' => domain_name
12
+ }
13
+
14
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
15
+ end
16
+
17
+ def delete_domain(domain_name)
18
+ params = {
19
+ 'Action' => 'DeleteDomain',
20
+ 'DomainName' => domain_name
21
+ }
22
+
23
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
24
+ end
25
+
26
+ def domain_metadata(domain_name)
27
+ params = {
28
+ 'Action' => 'DomainMetadata',
29
+ 'DomainName' => domain_name
30
+ }
31
+
32
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
33
+ end
34
+
35
+ def list_domains(next_token=nil, max_num=nil, &block)
36
+ params = {'Action' => 'ListDomains'}
37
+ params['NextToken'] = next_token if next_token
38
+ params['MaxNumberOfDomains'] = max_num if max_num
39
+
40
+ RAWS.fetch('GET', URI, PARAMS.merge(params), :multiple => %w'DomainName')
41
+ end
42
+
43
+ def get_attributes(domain_name, item_name, *attrs)
44
+ params = {
45
+ 'Action' => 'GetAttributes',
46
+ 'DomainName' => domain_name,
47
+ 'ItemName' => item_name
48
+ }
49
+
50
+ i = 0
51
+ attrs.each do |name|
52
+ params["AttributeName.#{i}"] = name
53
+ i += 1
54
+ end
55
+
56
+ RAWS.fetch(
57
+ 'GET',
58
+ URI,
59
+ PARAMS.merge(params),
60
+ :multiple => %w'Attribute',
61
+ :unpack => %w'Attribute'
62
+ )
63
+ end
64
+
65
+ def put_attributes(domain_name, item_name, attrs={}, *replaces)
66
+ params = {
67
+ 'Action' => 'PutAttributes',
68
+ 'DomainName' => domain_name,
69
+ 'ItemName' => item_name
70
+ }
71
+ params.merge!(RAWS.pack_attrs(attrs, replaces))
72
+
73
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
74
+ end
75
+
76
+ def batch_put_attributes(domain_name, items={}, replaces={})
77
+ params = {
78
+ 'Action' => 'BatchPutAttributes',
79
+ 'DomainName' => domain_name
80
+ }
81
+
82
+ i = 0
83
+ items.each do |key, attrs|
84
+ params["Item.#{i}.ItemName"] = key
85
+ params.merge!(RAWS.pack_attrs(attrs, replaces[key], "Item.#{i}."))
86
+ i += 1
87
+ end
88
+
89
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
90
+ end
91
+
92
+ def delete_attributes(domain_name, item_name, attrs={})
93
+ params = {
94
+ 'Action' => 'DeleteAttributes',
95
+ 'DomainName' => domain_name,
96
+ 'ItemName' => item_name
97
+ }
98
+ params.merge!(RAWS.pack_attrs(attrs))
99
+
100
+ RAWS.fetch('GET', URI, PARAMS.merge(params))
101
+ end
102
+
103
+ def quote(val)
104
+ if !REXP_NAME.match(val) || KEYWORDS.include?(val)
105
+ "'#{val}'"
106
+ else
107
+ val
108
+ end
109
+ end
110
+
111
+ def query_expr(expr, *params)
112
+ expr.gsub(/(\\)?(\?)/) do
113
+ if $1
114
+ "?"
115
+ else
116
+ "'#{params.shift.to_s.gsub(/(['])/, '\1\1')}'"
117
+ end
118
+ end
119
+ end
120
+
121
+ def select(expr, expr_params=[], next_token=nil)
122
+ params = {
123
+ 'Action' => 'Select',
124
+ 'SelectExpression' => query_expr(expr, *expr_params)
125
+ }
126
+ params['NextToken'] = next_token if next_token
127
+
128
+ RAWS.fetch(
129
+ 'GET',
130
+ URI,
131
+ PARAMS.merge(params),
132
+ :multiple => %w'Item Attribute',
133
+ :unpack => %w'Attribute'
134
+ )
135
+ end
136
+ end
137
+
138
+ extend Adapter20090415
139
+ end
@@ -0,0 +1,121 @@
1
+ require 'uuidtools'
2
+
3
+ module RAWS::SDB::Model
4
+ class Select < RAWS::SDB::Select
5
+ def initialize(model)
6
+ super()
7
+ @model = model
8
+ end
9
+
10
+ def attr_filter(val)
11
+ @model.new(val.last, val.first)
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ attr_accessor :domain_name
17
+
18
+ def create_domain
19
+ RAWS::SDB[domain_name].create_domain
20
+ end
21
+
22
+ def delete_domain
23
+ RAWS::SDB[domain_name].delete_domain
24
+ end
25
+
26
+ def select(&block)
27
+ Select.new(self).from(domain_name, &block)
28
+ end
29
+ alias :all :select
30
+
31
+ def find(id)
32
+ if attrs = RAWS::SDB[domain_name].get(id)
33
+ self.new(attrs, id)
34
+ end
35
+ end
36
+
37
+ def generate_id
38
+ UUIDTools::UUID.random_create
39
+ end
40
+
41
+ def sdb_reader(*names)
42
+ names.each do |name|
43
+ module_eval %Q{
44
+ def #{name}
45
+ self['#{name}']
46
+ end
47
+ }
48
+ end
49
+ end
50
+
51
+ def sdb_writer(*names)
52
+ names.each do |name|
53
+ module_eval %Q{
54
+ def #{name}=(val)
55
+ self['#{name}'] = val
56
+ end
57
+ }
58
+ end
59
+ end
60
+
61
+ def sdb_accessor(*names)
62
+ sdb_reader(*names)
63
+ sdb_writer(*names)
64
+ end
65
+ end
66
+
67
+ module InstanceMethods
68
+ attr_reader :id, :values
69
+
70
+ def initialize(values={}, id=nil)
71
+ @id, @values = id, values
72
+ after_initialize
73
+ end
74
+
75
+ def [](key)
76
+ values[key]
77
+ end
78
+
79
+ def []=(key, val)
80
+ values[key] = val
81
+ end
82
+
83
+ def delete
84
+ before_delete
85
+ RAWS::SDB[self.class.domain_name].delete(id) if id
86
+ after_delete
87
+ end
88
+
89
+ def save
90
+ before_save
91
+ if id
92
+ before_update
93
+ RAWS::SDB[self.class.domain_name].put(id, values)
94
+ after_update
95
+ else
96
+ before_insert
97
+ @id = self.class.generate_id
98
+ RAWS::SDB[self.class.domain_name].put(id, values)
99
+ after_insert
100
+ end
101
+ after_save
102
+ end
103
+
104
+ def after_initialize; end
105
+ def before_delete; end
106
+ def after_delete; end
107
+ def before_save; end
108
+ def after_save; end
109
+ def before_update; end
110
+ def after_update; end
111
+ def before_insert; end
112
+ def after_insert; end
113
+ end
114
+
115
+ def self.included(mod)
116
+ mod.class_eval do
117
+ include InstanceMethods
118
+ extend ClassMethods
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,78 @@
1
+ class RAWS::SDB::Select
2
+ RESERVED_KEYWORD = %w'or and not from where select like null is order by asc desc in between intersection limit every'
3
+
4
+ def initialize
5
+ @output_list = '*'
6
+ @domain = nil
7
+ @condition = nil
8
+ @values = nil
9
+ @sort = nil
10
+ @limit = nil
11
+ end
12
+
13
+ def attr_filter(val)
14
+ val
15
+ end
16
+
17
+ def each(&block)
18
+ RAWS::SDB.select(*to_sql) do |val|
19
+ block.call(attr_filter(val))
20
+ end
21
+ end
22
+ alias :fetch :each
23
+
24
+ def get
25
+ ret = nil
26
+
27
+ _limit = @limit
28
+ limit(1)
29
+ each do |val|
30
+ ret = val
31
+ end
32
+ @limit = _limit
33
+
34
+ ret
35
+ end
36
+
37
+ def columns(*val)
38
+ @output_list = val.join(',')
39
+ self
40
+ end
41
+
42
+ def from(val)
43
+ @domain = val
44
+ self
45
+ end
46
+
47
+ def where(condition, *values)
48
+ @condition = condition
49
+ @values = values
50
+ self
51
+ end
52
+ alias :filter :where
53
+
54
+ def order(val)
55
+ @sort = val
56
+ self
57
+ end
58
+
59
+ def limit(val)
60
+ @limit = val
61
+ self
62
+ end
63
+
64
+ def to_sql
65
+ s = [
66
+ 'select',
67
+ @output_list,
68
+ 'from',
69
+ ::RAWS::SDB::Adapter.quote(@domain)
70
+ ]
71
+
72
+ s.push('where', @condition) if @condition
73
+ s.push('order by', @sort ) if @sort
74
+ s.push('limit', @limit ) if @limit
75
+
76
+ [s.join(' '), @values]
77
+ end
78
+ end