avrubykit 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +7 -0
- data/README.rdoc +0 -0
- data/Rakefile +14 -0
- data/Readme +4 -0
- data/avrubykit.gemspec +30 -0
- data/lib/aws/sqs/client.rb +136 -0
- data/lib/aws/sqs/queue.rb +81 -0
- data/lib/aws/sqs.rb +23 -0
- metadata +82 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('avrubykit', '0.1') do |p|
|
6
|
+
p.description = "Alta Vida Ruby utility libraries"
|
7
|
+
p.url = ""
|
8
|
+
p.author = "Shane Crawford"
|
9
|
+
p.email = "support@alta-vida.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/Readme
ADDED
data/avrubykit.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{avrubykit}
|
5
|
+
s.version = "0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Shane Crawford"]
|
9
|
+
s.date = %q{2011-01-16}
|
10
|
+
s.description = %q{Alta Vida Ruby utility libraries}
|
11
|
+
s.email = %q{support@alta-vida.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/aws/sqs.rb", "lib/aws/sqs/client.rb", "lib/aws/sqs/queue.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "Readme", "lib/aws/sqs.rb", "lib/aws/sqs/client.rb", "lib/aws/sqs/queue.rb", "Manifest", "avrubykit.gemspec"]
|
14
|
+
s.homepage = %q{}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Avrubykit", "--main", "Readme"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{avrubykit}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Alta Vida Ruby utility libraries}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# Copyright 2007 Amazon Technologies, Inc. Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
# you may not use this file except in compliance with the License. You may obtain a copy of the License at:
|
3
|
+
#
|
4
|
+
# http://aws.amazon.com/apache2.0
|
5
|
+
#
|
6
|
+
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
7
|
+
# See the License for the specific language governing permissions and limitations under the License.
|
8
|
+
|
9
|
+
module AWS
|
10
|
+
module SQS
|
11
|
+
class Client
|
12
|
+
require 'rubygems'
|
13
|
+
require 'net/http'
|
14
|
+
require 'openssl'
|
15
|
+
require 'base64'
|
16
|
+
require 'cgi'
|
17
|
+
require 'time'
|
18
|
+
require 'xmlsimple'
|
19
|
+
|
20
|
+
attr_reader :endpoint
|
21
|
+
|
22
|
+
# default options
|
23
|
+
DEFAULT_SQS_OPTIONS = { :endpoint => "http://queue.amazonaws.com" }
|
24
|
+
|
25
|
+
##############################################################################
|
26
|
+
# Use hardcoded endpoint (default)
|
27
|
+
# AWS::SQS::Client.new(XXXXXXXXXXXXXX,XXXXXXXXXXXXXXXXXXXXX)
|
28
|
+
# Specify endpoint
|
29
|
+
# AWS::SQS::Client.new(XXXXXXXXXXXXXX,XXXXXXXXXXXXXXXXXXXXX, :endpoint => 'http://queue.amazonaws.com')
|
30
|
+
##############################################################################
|
31
|
+
def initialize( aws_access_key_id, aws_secret_access_key, options = {} )
|
32
|
+
@aws_access_key_id, @aws_secret_access_key = aws_access_key_id, aws_secret_access_key
|
33
|
+
opts = DEFAULT_SQS_OPTIONS.merge(options)
|
34
|
+
@endpoint = opts[:endpoint]
|
35
|
+
end
|
36
|
+
|
37
|
+
##############################################################################
|
38
|
+
# Get an array of queues
|
39
|
+
##############################################################################
|
40
|
+
def list_queues()
|
41
|
+
result = make_request('ListQueues')
|
42
|
+
value = result['ListQueuesResult']
|
43
|
+
puts result.to_s
|
44
|
+
unless value.nil?
|
45
|
+
return value
|
46
|
+
else
|
47
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
48
|
+
"\n" + result['Error'][0]['Message'][0]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
##############################################################################
|
53
|
+
# Create a new queue
|
54
|
+
##############################################################################
|
55
|
+
def create_queue(name)
|
56
|
+
params = {}
|
57
|
+
params['QueueName'] = name
|
58
|
+
result = make_request('CreateQueue', queue = "", params)
|
59
|
+
unless result.include?('Error')
|
60
|
+
return AWS::SQS::Queue.new(name, self)
|
61
|
+
else
|
62
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
63
|
+
"\n" + result['Error'][0]['Message'][0]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
##############################################################################
|
68
|
+
# Delete the specified queue
|
69
|
+
#
|
70
|
+
# Note: this will delete ALL messages in your queue, so use this function with caution!
|
71
|
+
#
|
72
|
+
##############################################################################
|
73
|
+
def delete_queue(name)
|
74
|
+
result = make_request('DeleteQueue', queue = name)
|
75
|
+
unless result.include?('Error')
|
76
|
+
return true
|
77
|
+
else
|
78
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
79
|
+
"\n" + result['Error'][0]['Message'][0]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##############################################################################
|
84
|
+
# Send a query request and return a SimpleXML object
|
85
|
+
##############################################################################
|
86
|
+
def make_request(action, queue = "", params = {})
|
87
|
+
# Add Actions
|
88
|
+
params['Action'] = action
|
89
|
+
params['Version'] = '2008-01-01'
|
90
|
+
params['AWSAccessKeyId'] = @aws_access_key_id
|
91
|
+
params['Expires']= (Time.now + 10).gmtime.iso8601
|
92
|
+
params['SignatureVersion'] = '1'
|
93
|
+
|
94
|
+
# Sign the string
|
95
|
+
sorted_params = params.sort_by { |key,value| key.downcase }
|
96
|
+
joined_params = sorted_params.collect { |key, value| key + value}
|
97
|
+
string_to_sign = joined_params.to_s
|
98
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
99
|
+
hmac = OpenSSL::HMAC.digest(digest, @aws_secret_access_key, string_to_sign)
|
100
|
+
params['Signature'] = Base64.encode64(hmac).chomp
|
101
|
+
|
102
|
+
# Construct request
|
103
|
+
query = params.collect { |key, value| key + "=" + CGI.escape(value) }.join("&")
|
104
|
+
|
105
|
+
# Set our query, keeping in mind that most (not all) actions require a queue name in the URI
|
106
|
+
if query["Action"] == "ListQueues" || query["Action"] == "CreateQueue"
|
107
|
+
query = "?" + query
|
108
|
+
else
|
109
|
+
query = "/" + queue + "?" + query
|
110
|
+
end
|
111
|
+
|
112
|
+
# You should always retry a 5xx error, as some of these are expected
|
113
|
+
retry_count = 0
|
114
|
+
try_again = true
|
115
|
+
uri = URI.parse(self.endpoint)
|
116
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
117
|
+
request = Net::HTTP::Get.new(query)
|
118
|
+
while try_again do
|
119
|
+
# Send Amazon SQS query to endpoint
|
120
|
+
response = http.start { |http|
|
121
|
+
http.request(request)
|
122
|
+
}
|
123
|
+
# Check if we should retry this request
|
124
|
+
if response == Net::HTTPServerError && retry_count <= 5
|
125
|
+
retry_count ++
|
126
|
+
sleep(retry_count / 4 * retry_count)
|
127
|
+
else
|
128
|
+
try_again = false
|
129
|
+
xml = response.body.to_s
|
130
|
+
return XmlSimple.xml_in(xml)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end # Client
|
135
|
+
end # SQS
|
136
|
+
end # AWS
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Copyright 2007 Amazon Technologies, Inc. Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
# you may not use this file except in compliance with the License. You may obtain a copy of the License at:
|
3
|
+
#
|
4
|
+
# http://aws.amazon.com/apache2.0
|
5
|
+
#
|
6
|
+
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
7
|
+
# See the License for the specific language governing permissions and limitations under the License.
|
8
|
+
|
9
|
+
module AWS
|
10
|
+
module SQS
|
11
|
+
class Queue
|
12
|
+
attr_reader :name
|
13
|
+
|
14
|
+
def initialize(name, sqs_client)
|
15
|
+
@name, @sqs_client = name, sqs_client
|
16
|
+
end
|
17
|
+
|
18
|
+
##############################################################################
|
19
|
+
# Send a message to your queue
|
20
|
+
##############################################################################
|
21
|
+
def send_message(body)
|
22
|
+
params = {}
|
23
|
+
params['MessageBody'] = body
|
24
|
+
result = @sqs_client.make_request('SendMessage', self.name, params)
|
25
|
+
unless result.include?('Error')
|
26
|
+
return result['SendMessageResult'][0]['MessageId'][0].to_s
|
27
|
+
else
|
28
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
29
|
+
"\n" + result['Error'][0]['Message'][0]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
##############################################################################
|
34
|
+
# Get a message(s) from your queue
|
35
|
+
##############################################################################
|
36
|
+
def receive_messages(max_number_of_messages = -1, visibility_timeout = -1)
|
37
|
+
params = {}
|
38
|
+
params['MaxNumberOfMessages'] = max_number_of_messages if max_number_of_messages > -1
|
39
|
+
params['VisibilityTimeout'] = visibility_timeout if visibility_timeout > -1
|
40
|
+
result = @sqs_client.make_request('ReceiveMessage', self.name, params)
|
41
|
+
unless result.include?('Error')
|
42
|
+
return result['ReceiveMessageResult']
|
43
|
+
else
|
44
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
45
|
+
"\n" + result['Error'][0]['Message'][0]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
##############################################################################
|
50
|
+
# Delete a message
|
51
|
+
##############################################################################
|
52
|
+
def delete_message(receipt_handle)
|
53
|
+
params = {}
|
54
|
+
params['ReceiptHandle'] = receipt_handle
|
55
|
+
result = @sqs_client.make_request('DeleteMessage', self.name, params)
|
56
|
+
unless result.include?('Error')
|
57
|
+
return true
|
58
|
+
else
|
59
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
60
|
+
"\n" + result['Error'][0]['Message'][0]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
##############################################################################
|
65
|
+
# Get a queue attribute
|
66
|
+
##############################################################################
|
67
|
+
def get_queue_attributes(attribute)
|
68
|
+
params = {}
|
69
|
+
params['AttributeName'] = attribute
|
70
|
+
result = @sqs_client.make_request('GetQueueAttributes', self.name, params)
|
71
|
+
unless result.include?('Error')
|
72
|
+
return result['GetQueueAttributesResult'][0]['Attribute'][0]["Value"][0]
|
73
|
+
else
|
74
|
+
raise Exception, "Amazon SQS Error Code :" + result['Error'][0]['Code'][0] +
|
75
|
+
"\n" + result['Error'][0]['Message'][0]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/aws/sqs.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright 2007 Amazon Technologies, Inc. Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
# you may not use this file except in compliance with the License. You may obtain a copy of the License at:
|
3
|
+
#
|
4
|
+
# http://aws.amazon.com/apache2.0
|
5
|
+
#
|
6
|
+
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
7
|
+
# See the License for the specific language governing permissions and limitations under the License.
|
8
|
+
|
9
|
+
module AWS
|
10
|
+
module SQS
|
11
|
+
# strings are UTF-8 encoded
|
12
|
+
$KCODE = "u"
|
13
|
+
end
|
14
|
+
|
15
|
+
module VERSION #:nodoc:
|
16
|
+
MAJOR = 0
|
17
|
+
MINOR = 1
|
18
|
+
TINY = 3
|
19
|
+
|
20
|
+
STRING = [MAJOR, MINOR, TINY].join('.')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avrubykit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Shane Crawford
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-16 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Alta Vida Ruby utility libraries
|
22
|
+
email: support@alta-vida.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
- lib/aws/sqs.rb
|
30
|
+
- lib/aws/sqs/client.rb
|
31
|
+
- lib/aws/sqs/queue.rb
|
32
|
+
files:
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- Readme
|
36
|
+
- lib/aws/sqs.rb
|
37
|
+
- lib/aws/sqs/client.rb
|
38
|
+
- lib/aws/sqs/queue.rb
|
39
|
+
- Manifest
|
40
|
+
- avrubykit.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: ""
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --line-numbers
|
48
|
+
- --inline-source
|
49
|
+
- --title
|
50
|
+
- Avrubykit
|
51
|
+
- --main
|
52
|
+
- Readme
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 11
|
70
|
+
segments:
|
71
|
+
- 1
|
72
|
+
- 2
|
73
|
+
version: "1.2"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: avrubykit
|
77
|
+
rubygems_version: 1.3.7
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Alta Vida Ruby utility libraries
|
81
|
+
test_files: []
|
82
|
+
|