swirl 1.5.3 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/swirl/ec2.rb +28 -21
  2. data/lib/swirl/ec2/em.rb +21 -0
  3. 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
- code, data = call!(action, expand(query))
61
-
62
- case code
63
- when 200
64
- compact(data)
65
- when 400...500
66
- messages = Array(data["Response"]["Errors"]).map {|_, e| e["Message"] }
67
- raise InvalidRequest, messages.join(",")
68
- else
69
- msg = "unexpected response #{code} -> #{data.inspect}"
70
- raise InvalidRequest, msg
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
- response = post(body)
94
+ post(body) do |code, xml|
95
+ if ENV["SWIRL_LOG"]
96
+ puts response.body
97
+ end
89
98
 
90
- if ENV["SWIRL_LOG"]
91
- puts response.body
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
@@ -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
- - 5
8
- - 3
9
- version: 1.5.3
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: 2010-01-26 00:00:00 -08:00
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