swirl 1.5.3 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/swirl/ec2.rb +28 -21
- data/lib/swirl/ec2/em.rb +21 -0
- metadata +5 -4
data/lib/swirl/ec2.rb
CHANGED
@@ -56,22 +56,28 @@ module Swirl
|
|
56
56
|
# ec2.call("DescribeInstances")
|
57
57
|
# ec2.call("TerminateInstances", "InstanceId" => ["i-1234", "i-993j"]
|
58
58
|
#
|
59
|
-
def call(action, query={})
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
59
|
+
def call(action, query={}, &blk)
|
60
|
+
call!(action, expand(query)) do |code, data|
|
61
|
+
case code
|
62
|
+
when 200
|
63
|
+
response = compact(data)
|
64
|
+
when 400...500
|
65
|
+
messages = Array(data["Response"]["Errors"]).map {|_, e| e["Message"] }
|
66
|
+
raise InvalidRequest, messages.join(",")
|
67
|
+
else
|
68
|
+
msg = "unexpected response #{code} -> #{data.inspect}"
|
69
|
+
raise InvalidRequest, msg
|
70
|
+
end
|
71
|
+
|
72
|
+
if blk
|
73
|
+
blk.call(response)
|
74
|
+
else
|
75
|
+
response
|
76
|
+
end
|
71
77
|
end
|
72
78
|
end
|
73
79
|
|
74
|
-
def call!(action, query={})
|
80
|
+
def call!(action, query={}, &blk)
|
75
81
|
# Hard coding this here until otherwise needed
|
76
82
|
method = "POST"
|
77
83
|
|
@@ -85,17 +91,17 @@ module Swirl
|
|
85
91
|
body = compile_sorted_form_data(query)
|
86
92
|
body += "&" + ["Signature", compile_signature(method, body)].join("=")
|
87
93
|
|
88
|
-
|
94
|
+
post(body) do |code, xml|
|
95
|
+
if ENV["SWIRL_LOG"]
|
96
|
+
puts response.body
|
97
|
+
end
|
89
98
|
|
90
|
-
|
91
|
-
|
99
|
+
data = Crack::XML.parse(xml)
|
100
|
+
blk.call([code, data])
|
92
101
|
end
|
93
|
-
|
94
|
-
data = Crack::XML.parse(response.body)
|
95
|
-
[response.code.to_i, data]
|
96
102
|
end
|
97
103
|
|
98
|
-
def post(body)
|
104
|
+
def post(body, &blk)
|
99
105
|
headers = { "Content-Type" => "application/x-www-form-urlencoded" }
|
100
106
|
|
101
107
|
http = Net::HTTP.new(@url.host, @url.port)
|
@@ -105,7 +111,8 @@ module Swirl
|
|
105
111
|
request = Net::HTTP::Post.new(@url.request_uri, headers)
|
106
112
|
request.body = body
|
107
113
|
|
108
|
-
http.request(request)
|
114
|
+
response = http.request(request)
|
115
|
+
blk.call(response.code.to_i, response.body)
|
109
116
|
end
|
110
117
|
|
111
118
|
def inspect
|
data/lib/swirl/ec2/em.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'swirl/ec2'
|
2
|
+
require 'em-http'
|
3
|
+
|
4
|
+
module Swirl
|
5
|
+
class EC2
|
6
|
+
def post(body, &blk)
|
7
|
+
headers = { "Content-Type" => "application/x-www-form-urlencoded" }
|
8
|
+
|
9
|
+
http = EM::HttpRequest.new(@url.to_s)
|
10
|
+
req = http.post(:head => headers, :body => body)
|
11
|
+
|
12
|
+
req.callback do
|
13
|
+
blk.call(req.response_header.status, req.response)
|
14
|
+
end
|
15
|
+
|
16
|
+
req.errback do
|
17
|
+
raise "Invalid HTTP Request: #{@url}\n"+req.response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
7
|
+
- 6
|
8
|
+
- 0
|
9
|
+
version: 1.6.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Blake Mizerany
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-02-09 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- LICENSE
|
73
73
|
- README.md
|
74
74
|
- list-types
|
75
|
+
- lib/swirl/ec2/em.rb
|
75
76
|
- lib/swirl/ec2.rb
|
76
77
|
- lib/swirl/helpers.rb
|
77
78
|
- lib/swirl.rb
|