raws 0.0.8 → 0.0.9

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/lib/raws.rb CHANGED
@@ -1,149 +1,48 @@
1
1
  require 'uri'
2
2
  require 'time'
3
+ require 'logger'
3
4
  require 'openssl'
4
-
5
5
  require 'rubygems'
6
- require 'typhoeus'
7
- require 'nokogiri'
8
6
 
9
7
  module RAWS
10
- include Typhoeus
11
-
12
- class Error < StandardError
13
- attr_reader :response
14
- attr_reader :data
15
-
16
- def initialize(response, data)
17
- super()
18
- @response, @data = response, data
19
- end
20
- end
21
-
22
8
  class << self
23
9
  attr_accessor :aws_access_key_id
24
10
  attr_accessor :aws_secret_access_key
11
+ attr_accessor :http
12
+ attr_accessor :xml
13
+ end
25
14
 
26
- def escape(val)
27
- URI.escape(val.to_s, /([^a-zA-Z0-9\-_.~]+)/n)
28
- end
29
-
30
- def sign(http_verb, base_uri, params)
31
- path = {
32
- 'AWSAccessKeyId' => aws_access_key_id,
33
- 'SignatureMethod' => 'HmacSHA256',
34
- 'SignatureVersion' => '2',
35
- 'Timestamp' => Time.now.utc.iso8601
36
- }.merge(params).map do |key, val|
37
- "#{escape(key)}=#{escape(val)}"
38
- end.sort.join('&')
39
-
40
- uri = URI.parse(base_uri)
41
- "#{path}&Signature=" + escape(
42
- [
43
- ::OpenSSL::HMAC.digest(
44
- ::OpenSSL::Digest::SHA256.new,
45
- aws_secret_access_key,
46
- "#{http_verb.upcase}\n#{uri.host.downcase}\n#{uri.path}\n#{path}"
47
- )
48
- ].pack('m').strip
49
- )
50
- end
51
-
52
- def pack_attrs(attrs, replaces=nil, prefix=nil)
53
- params = {}
15
+ def self.escape(val)
16
+ URI.escape(val.to_s, /([^a-zA-Z0-9\-_.~]+)/n)
17
+ end
54
18
 
55
- i = 1
56
- attrs.each do |key, val|
57
- if !replaces.nil? && replaces.include?(key)
58
- params["#{prefix}Attribute.#{i}.Replace"] = 'true'
59
- end
19
+ def self.unescape(val)
20
+ URI.unescape(val.to_s)
21
+ end
60
22
 
61
- if val.is_a? Array
62
- val.each do |v|
63
- params["#{prefix}Attribute.#{i}.Name"] = key
64
- params["#{prefix}Attribute.#{i}.Value"] = v
65
- i += 1
66
- end
67
- else
68
- params["#{prefix}Attribute.#{i}.Name"] = key
69
- params["#{prefix}Attribute.#{i}.Value"] = val
70
- i += 1
71
- end
23
+ def self.logger
24
+ @logger ||= begin
25
+ logger = Logger.new(STDERR)
26
+ logger.progname = self.name
27
+ logger.level = Logger::INFO
28
+ def logger.debug(val)
29
+ require 'yaml'
30
+ super(val.to_yaml)
72
31
  end
73
-
74
- params
32
+ logger
75
33
  end
34
+ end
76
35
 
77
- def unpack_attrs(attrs)
78
- ret = {}
79
-
80
- if attrs.is_a? Array
81
- attrs
82
- else
83
- [attrs]
84
- end.map do |val|
85
- name, value = val['Name'], val['Value']
86
-
87
- if ret.key? name
88
- ret[name] = [ret[name]] unless ret[name].is_a? Array
89
- ret[name] << value
90
- else
91
- ret[name] = value
92
- end
93
- end if attrs
94
-
95
- ret
96
- end
97
-
98
- def parse(doc, params={}, ret={})
99
- multiple = params[:multiple] || []
100
- unpack = params[:unpack] || []
101
-
102
- name = nil
103
- doc.children.each do |tag|
104
- name = tag.name #.gsub(/([A-Z])/, '_\1').gsub(/^_/, '').downcase.to_sym
105
-
106
- unless ret[name].is_a? Array
107
- if ret.key?(name)
108
- ret[name] = [ret[name]]
109
- elsif multiple.include? name
110
- ret[name] = []
111
- end
112
- end
36
+ autoload :HTTP, 'raws/http'
37
+ autoload :XML, 'raws/xml'
113
38
 
114
- if tag.child.is_a? Nokogiri::XML::Text
115
- if ret.key? name
116
- ret[name] << tag.content
117
- else
118
- ret[name] = tag.content
119
- end
120
- else
121
- if ret.key? name
122
- ret[name] << {}
123
- parse(tag, params, ret[name].last)
124
- else
125
- ret[name] = {}
126
- parse(tag, params, ret[name])
127
- end
128
- end
129
- end
130
- ret[name] = unpack_attrs(ret[name]) if unpack.include?(name)
39
+ autoload :SDB, 'raws/sdb'
40
+ autoload :SQS, 'raws/sqs'
41
+ autoload :S3, 'raws/s3'
42
+ end
131
43
 
132
- ret
133
- end
44
+ RAWS.http = RAWS::HTTP::Typhoeus
45
+ RAWS.xml = RAWS::XML::Nokogiri
134
46
 
135
- def fetch(http_verb, base_uri, params, options={})
136
- r = get("#{base_uri}?#{sign(http_verb, base_uri, params)}")
137
- data = parse(Nokogiri::XML.parse(r.body), options)
138
- if 200 <= r.code && r.code <= 299
139
- data
140
- else
141
- raise Error.new(r, data)
142
- end
143
- end
144
- end
145
-
146
- autoload :SDB, 'raws/sdb'
147
- autoload :SQS, 'raws/sqs'
148
- autoload :S3, 'raws/s3'
149
- end
47
+ # 今のところ S3 Typhoeus を使うのは無理っぽい
48
+ RAWS::S3.http = RAWS::HTTP::HT2P
data/spec/raws/s3_spec.rb CHANGED
@@ -1,28 +1,219 @@
1
- require 'spec/spec_config'
2
-
3
- describe RAWS::S3::Adapter do
4
- describe 'class' do
5
- it 'get_service' do
6
- begin
7
- p RAWS::S3::Adapter.get_service
8
- rescue RAWS::Error => e
9
- p e.response.code
10
- p e.data
11
- rescue => e
12
- p e
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ RAWS::S3.http = RAWS::HTTP::HT2P
4
+
5
+ RAWS_S3_BUCKETS.each do |bucket_name, location, acl|
6
+ location_label = location ? location : 'US'
7
+
8
+ describe RAWS::S3 do
9
+ describe 'class' do
10
+ before(:all) do
11
+ response = RAWS::S3.create_bucket(bucket_name, location)
12
+ response.should be_kind_of(RAWS::HTTP::Response)
13
+
14
+ RAWS::S3.put(
15
+ bucket_name,
16
+ 'aaa',
17
+ 'content-length' => 3
18
+ ) do |io|
19
+ io.write 'AAA'
20
+ end
21
+
22
+ RAWS::S3.put(
23
+ bucket_name,
24
+ 'bbb',
25
+ 'content-length' => 3
26
+ ) do |io|
27
+ io.write 'BBB'
28
+ end
29
+
30
+ RAWS::S3.put(
31
+ bucket_name,
32
+ 'ccc',
33
+ 'content-length' => 3
34
+ ) do |io|
35
+ io.write 'CCC'
36
+ end
37
+ end
38
+
39
+ after(:all) do
40
+ response = RAWS::S3.delete_bucket(bucket_name, :force)
41
+ response.should be_kind_of(RAWS::HTTP::Response)
42
+
43
+ sleep 30
44
+ end
45
+
46
+ it "owner should return a owner information of the bucket" do
47
+ RAWS::S3.owner.should be_instance_of(Hash)
48
+ RAWS::S3.owner['DisplayName'].should be_instance_of(String)
49
+ RAWS::S3.owner['ID'].should be_instance_of(String)
50
+ end
51
+
52
+ it "buckets should return an array of RAWS::S3" do
53
+ RAWS::S3.buckets.should be_instance_of(Array)
54
+ RAWS::S3.buckets.each do |bucket|
55
+ bucket.should be_instance_of(RAWS::S3)
56
+ end
57
+ RAWS::S3.buckets.should include(RAWS::S3[bucket_name])
58
+ end
59
+
60
+ it "self['#{bucket_name}'] should be instance of RAWS::S3" do
61
+ RAWS::S3[bucket_name].should be_instance_of(RAWS::S3)
62
+ RAWS::S3[bucket_name].bucket_name.should == bucket_name
63
+ RAWS::S3[bucket_name].name.should == bucket_name
64
+ end
65
+
66
+ it "location('#{bucket_name}') should return location of the bucket" do
67
+ begin
68
+ RAWS::S3.location(bucket_name).should == location_label
69
+ rescue => e
70
+ d e.response.doc
71
+ end
72
+ end
73
+
74
+ it "filter('#{bucket_name}') should return an array of RAWS::HTTP::Response" do
75
+ RAWS::S3.filter(bucket_name).should be_instance_of(Array)
76
+ RAWS::S3.filter(bucket_name).each do |object|
77
+ object.should be_instance_of(Hash)
78
+ end
79
+ end
80
+
81
+ it 'put, get and delete method should put, get and delete the object' do
82
+ response = RAWS::S3.put(bucket_name, 'a', 'content-length' => 1) do |io|
83
+ io.write 'A'
84
+ end
85
+ response.should be_kind_of(RAWS::HTTP::Response)
86
+
87
+ response = RAWS::S3.get(bucket_name, 'a') do |io|
88
+ io.read
89
+ end
90
+ response.should be_kind_of(RAWS::HTTP::Response)
91
+
92
+ response = RAWS::S3.delete(bucket_name, 'a')
93
+ response.should be_kind_of(RAWS::HTTP::Response)
94
+
95
+ lambda do
96
+ RAWS::S3.get(bucket_name, 'a')
97
+ end.should raise_error(RAWS::HTTP::Error)
98
+ end
99
+
100
+ it "copy method should copy the object" do
101
+ response = RAWS::S3.copy(bucket_name, 'aaa', bucket_name, 'AAA')
102
+ response.should be_kind_of(RAWS::HTTP::Response)
103
+
104
+ src = nil
105
+ RAWS::S3.get(bucket_name, 'aaa') do |io|
106
+ src = io.read
107
+ end
108
+
109
+ dest = nil
110
+ RAWS::S3.get(bucket_name, 'AAA') do |io|
111
+ dest = io.read
112
+ end
113
+
114
+ dest.should == src
115
+
116
+ response = RAWS::S3.delete(bucket_name, 'AAA')
117
+ response.should be_kind_of(RAWS::HTTP::Response)
118
+ end
119
+
120
+ it "head method should return header information of the object" do
121
+ response = RAWS::S3.head(bucket_name, 'aaa')
122
+ response.should be_kind_of(RAWS::HTTP::Response)
13
123
  end
14
124
  end
15
125
 
16
- it 'put_bucket' do
17
- begin
18
- # p RAWS::S3::Adapter.delete_bucket('kikuchitestbucket')
19
- #p RAWS::S3::Adapter.put_bucket('kikuchitestbucket')
20
- rescue RAWS::Error => e
21
- p e.response.code
22
- p e.data
23
- rescue => e
24
- p e
126
+ describe 'object' do
127
+ before(:all) do
128
+ @bucket = RAWS::S3[bucket_name]
129
+ @bucket.create_bucket.should be_kind_of(RAWS::HTTP::Response)
130
+
131
+ @bucket.put('aaa', 'content-length' => 3) do |io| io.write 'AAA' end
132
+ @bucket.put('bbb', 'content-length' => 3) do |io| io.write 'BBB' end
133
+ @bucket.put('ccc', 'content-length' => 3) do |io| io.write 'CCC' end
134
+ end
135
+
136
+ after(:all) do
137
+ response = @bucket.delete_bucket(:force)
138
+ response.should be_kind_of(RAWS::HTTP::Response)
139
+
140
+ sleep 30
141
+ end
142
+
143
+ it "location should return location of the bucket" do
144
+ @bucket.location.should == location_label
145
+ end
146
+
147
+ it "filter should return an array of RAWS::HTTP::Response" do
148
+ @bucket.filter.should be_instance_of(Array)
149
+ @bucket.filter.each do |object|
150
+ object.should be_instance_of(Hash)
151
+ end
25
152
  end
153
+
154
+ it 'put, get and delete method should put, get and delete the object' do
155
+ @bucket.put('a', 'content-length' => 1) do |io|
156
+ io.write 'A'
157
+ end.should be_kind_of(RAWS::HTTP::Response)
158
+
159
+ @bucket.get('a') do |io|
160
+ io.read
161
+ end.should be_kind_of(RAWS::HTTP::Response)
162
+
163
+ @bucket.delete('a').should be_kind_of(RAWS::HTTP::Response)
164
+
165
+ lambda do
166
+ @bucket.get('a')
167
+ end.should raise_error(RAWS::HTTP::Error)
168
+ end
169
+
170
+ it "copy method should copy the object" do
171
+ response = @bucket.copy('aaa', bucket_name, 'AAA')
172
+ response.should be_kind_of(RAWS::HTTP::Response)
173
+
174
+ src = nil
175
+ @bucket.get('aaa') do |io|
176
+ src = io.read
177
+ end
178
+
179
+ dest = nil
180
+ @bucket.get('AAA') do |io|
181
+ dest = io.read
182
+ end
183
+
184
+ dest.should == src
185
+
186
+ @bucket.delete('AAA').should be_kind_of(RAWS::HTTP::Response)
187
+ end
188
+
189
+ it "head method should return header information of the object" do
190
+ @bucket.head('aaa').should be_kind_of(RAWS::HTTP::Response)
191
+ end
192
+ end
193
+ end
194
+ =begin
195
+ describe RAWS::S3::S3Object do
196
+ class S3 < RAWS::S3::S3Object
197
+ self.bucket_name = bucket_name
198
+ end
199
+
200
+ describe 'class' do
201
+ before(:all) do
202
+ # S3.create_bucket
203
+ end
204
+
205
+ after(:all) do
206
+ # S3.delete_bucket
207
+ # sleep 60
208
+ end
209
+
210
+ it "location should return location of the bucket"
211
+ it "filter should return an array of RAWS::S3::Object"
212
+ it "create method should put the object"
213
+ end
214
+
215
+ describe 'object' do
26
216
  end
27
217
  end
218
+ =end
28
219
  end
@@ -1,4 +1,4 @@
1
- require 'spec/spec_config'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe RAWS::SDB do
4
4
  before :all do
@@ -1,16 +1,25 @@
1
1
  require 'spec/spec_config'
2
2
 
3
3
  describe RAWS::SQS do
4
+ =begin
4
5
  before :all do
5
- RAWS::SQS.create_queue(RAWS_SQS_QUEUE)
6
- puts '[waiting 60 secs]'
7
- sleep 60
6
+ begin
7
+ RAWS::SQS.create_queue(RAWS_SQS_QUEUE)
8
+ puts '[waiting 61 secs]'
9
+ sleep 61
10
+ rescue => e
11
+ d e
12
+ end
8
13
  end
9
14
 
10
15
  after :all do
11
- RAWS::SQS[RAWS_SQS_QUEUE].delete_queue
12
- puts '[waiting 60 secs]'
13
- sleep 60
16
+ begin
17
+ RAWS::SQS[RAWS_SQS_QUEUE].delete_queue
18
+ puts '[waiting 61 secs]'
19
+ sleep 61
20
+ rescue => e
21
+ d e
22
+ end
14
23
  end
15
24
 
16
25
  describe 'class' do
@@ -47,7 +56,7 @@ describe RAWS::SQS do
47
56
  RAWS::SQS[RAWS_SQS_QUEUE].should be_kind_of RAWS::SQS
48
57
  end
49
58
  end
50
-
59
+ =end
51
60
  describe 'object' do
52
61
  before do
53
62
  @queue = RAWS::SQS[RAWS_SQS_QUEUE]
data/spec/raws_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'spec/spec_config'
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe RAWS do
4
4
  describe 'class' do
@@ -7,8 +7,8 @@ describe RAWS do
7
7
  aws_access_key_id
8
8
  aws_secret_access_key
9
9
  escape
10
- sign
11
- fetch
10
+ unescape
11
+ logger
12
12
  '.each do |val|
13
13
  RAWS.should respond_to val.to_sym
14
14
  end
@@ -19,25 +19,17 @@ describe RAWS do
19
19
  <?xml version="1.0"?>
20
20
  <GetQueueAttributesResponse xmlns="http://queue.amazonaws.com/doc/2009-02-01/"><GetQueueAttributesResult><Attribute><Name>VisibilityTimeout</Name><Value>60</Value></Attribute></GetQueueAttributesResult><ResponseMetadata><RequestId>6f950716-2579-4c55-92e8-ff0cdec28e6d</RequestId></ResponseMetadata></GetQueueAttributesResponse>
21
21
  END
22
- data = RAWS.parse(
23
- Nokogiri::XML.parse(xml)
24
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
22
+ data = RAWS.xml.parse(xml)['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
25
23
  data.should be_kind_of(Hash)
26
24
 
27
- data = RAWS.parse(
28
- Nokogiri::XML.parse(xml),
29
- :multiple => ['Attribute']
30
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
25
+ data = RAWS.xml.parse(xml, :multiple => ['Attribute'])['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
31
26
  data.should be_kind_of(Array)
32
27
 
33
- data = RAWS.parse(
34
- Nokogiri::XML.parse(xml),
35
- :unpack => ['Attribute']
36
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
28
+ data = RAWS.xml.parse(xml, :unpack => ['Attribute'])['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
37
29
  data.should be_kind_of(Hash)
38
30
 
39
- data = RAWS.parse(
40
- Nokogiri::XML.parse(xml),
31
+ data = RAWS.xml.parse(
32
+ xml,
41
33
  :multiple => ['Attribute'],
42
34
  :unpack => ['Attribute']
43
35
  )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
@@ -47,25 +39,17 @@ describe RAWS do
47
39
  <?xml version="1.0"?>
48
40
  <GetQueueAttributesResponse xmlns="http://queue.amazonaws.com/doc/2009-02-01/"><GetQueueAttributesResult><Attribute><Name>VisibilityTimeout</Name><Value>60</Value></Attribute><Attribute><Name>ApproximateNumberOfMessages</Name><Value>7</Value></Attribute><Attribute><Name>CreatedTimestamp</Name><Value>1248498270</Value></Attribute><Attribute><Name>LastModifiedTimestamp</Name><Value>1248501553</Value></Attribute></GetQueueAttributesResult><ResponseMetadata><RequestId>6f950716-2579-4c55-92e8-ff0cdec28e6d</RequestId></ResponseMetadata></GetQueueAttributesResponse>
49
41
  END
50
- data = RAWS.parse(
51
- Nokogiri::XML.parse(xml)
52
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
42
+ data = RAWS.xml.parse(xml)['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
53
43
  data.should be_kind_of(Array)
54
44
 
55
- data = RAWS.parse(
56
- Nokogiri::XML.parse(xml),
57
- :multiple => ['Attribute']
58
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
45
+ data = RAWS.xml.parse(xml, :multiple => ['Attribute'])['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
59
46
  data.should be_kind_of(Array)
60
47
 
61
- data = RAWS.parse(
62
- Nokogiri::XML.parse(xml),
63
- :unpack => ['Attribute']
64
- )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
48
+ data = RAWS.xml.parse(xml, :unpack => ['Attribute'])['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
65
49
  data.should be_kind_of(Hash)
66
50
 
67
- data = RAWS.parse(
68
- Nokogiri::XML.parse(xml),
51
+ data = RAWS.xml.parse(
52
+ xml,
69
53
  :multiple => ['Attribute'],
70
54
  :unpack => ['Attribute']
71
55
  )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'raws'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'spec/spec_config'
7
+
8
+ require 'yaml'
9
+ def d(val)
10
+ puts val.to_yaml
11
+ end
12
+
13
+ Spec::Runner.configure do |config|
14
+
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jun Kikuchi
@@ -9,18 +9,28 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-11 00:00:00 +09:00
12
+ date: 2009-11-19 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: pauldix-typhoeus
16
+ name: typhoeus
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.1.2
23
+ version: 0.1.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ht2p
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.5
24
34
  version:
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: nokogiri
@@ -57,14 +67,21 @@ files:
57
67
  - Rakefile
58
68
  - VERSION
59
69
  - lib/raws.rb
70
+ - lib/raws/http.rb
71
+ - lib/raws/http/ht2p.rb
72
+ - lib/raws/http/typhoeus.rb
60
73
  - lib/raws/s3.rb
61
74
  - lib/raws/s3/adapter.rb
75
+ - lib/raws/s3/model.rb
76
+ - lib/raws/s3/s3object.rb
62
77
  - lib/raws/sdb.rb
63
78
  - lib/raws/sdb/adapter.rb
64
79
  - lib/raws/sdb/model.rb
65
80
  - lib/raws/sdb/select.rb
66
81
  - lib/raws/sqs.rb
67
82
  - lib/raws/sqs/adapter.rb
83
+ - lib/raws/xml.rb
84
+ - lib/raws/xml/nokogiri.rb
68
85
  has_rdoc: true
69
86
  homepage: http://github.com/JunKikuchi/raws
70
87
  licenses: []
@@ -99,3 +116,4 @@ test_files:
99
116
  - spec/raws/sdb_spec.rb
100
117
  - spec/raws/sqs_spec.rb
101
118
  - spec/raws_spec.rb
119
+ - spec/spec_helper.rb