em-net-http 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +2 -0
  2. data/em-net-http.gemspec +1 -1
  3. data/lib/em-net-http.rb +34 -26
  4. metadata +17 -17
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://secure.travis-ci.org/jfairbairn/em-net-http.png)](http://travis-ci.org/jfairbairn/em-net-http])
2
+
1
3
  Most Ruby web API libraries use <tt>Net::HTTP</tt> (because it's ubiquitous),
2
4
  but I want to use them in my non-blocking EventMachine-based applications, and
3
5
  I don't want Net::HTTP to block. I therefore wrote this.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{em-net-http}
5
- s.version = "0.3.6"
5
+ s.version = "0.3.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["James Fairbairn"]
@@ -9,7 +9,7 @@ module EventMachine
9
9
  class Response
10
10
  attr_reader :code, :body, :header, :message, :http_version
11
11
  alias_method :msg, :message
12
-
12
+
13
13
  def initialize(response_header)
14
14
  @code = response_header.http_status.to_s
15
15
  @message = response_header.http_reason
@@ -21,23 +21,23 @@ module EventMachine
21
21
  @already_buffered = true
22
22
  @body = body
23
23
  end
24
-
24
+
25
25
  def content_type
26
26
  self['content-type']
27
27
  end
28
-
28
+
29
29
  def [](k)
30
30
  @header[key(k)]
31
31
  end
32
-
32
+
33
33
  def key?(k)
34
34
  @header.key? key(k)
35
35
  end
36
-
36
+
37
37
  def read_body(dest=nil,&block)
38
38
  @body
39
39
  end
40
-
40
+
41
41
  def to_hash
42
42
  h={}
43
43
  @header.each do |k, v|
@@ -45,32 +45,32 @@ module EventMachine
45
45
  end
46
46
  h
47
47
  end
48
-
48
+
49
49
  private
50
50
  def key(k)
51
51
  k.upcase.tr('-','_')
52
52
  end
53
-
53
+
54
54
  def fromkey(k)
55
55
  k.tr('_', '-').split('-').map{|i|i.capitalize}.join('-')
56
56
  end
57
-
57
+
58
58
  include Enumerable
59
59
  def each(&blk)
60
60
  @header.each(&blk)
61
61
  end
62
-
62
+
63
63
  end
64
64
  end
65
65
  end
66
-
66
+
67
67
 
68
68
  module Net
69
69
  class HTTPResponse
70
70
  class << self
71
71
  public :response_class
72
72
  end
73
-
73
+
74
74
  alias_method :orig_net_http_read_body, :read_body
75
75
 
76
76
  def read_body(dest=nil, &block)
@@ -98,19 +98,19 @@ module Net
98
98
  end
99
99
  end
100
100
  end
101
-
101
+
102
102
  class HTTP
103
103
  alias_method :orig_net_http_request, :request
104
-
104
+
105
105
  alias_method :orig_net_http_do_start, :do_start
106
-
106
+
107
107
  def do_start
108
108
 
109
109
  return orig_net_http_do_start unless ::EM.reactor_running?
110
110
 
111
111
  @started = true
112
112
  end
113
-
113
+
114
114
  def request(req, body = nil, &block)
115
115
 
116
116
  return orig_net_http_request(req, body, &block) unless ::EM.reactor_running?
@@ -133,7 +133,7 @@ module Net
133
133
  end
134
134
 
135
135
  headers['content-type'] ||= "application/x-www-form-urlencoded"
136
-
136
+
137
137
  t0 = Time.now
138
138
  httpreq = EM::HttpRequest.new(uri).send(req.class::METHOD.downcase.to_sym, opts)
139
139
 
@@ -152,7 +152,7 @@ module Net
152
152
  f.resume nhres
153
153
  end
154
154
 
155
-
155
+
156
156
  if block_given?
157
157
  httpreq.headers { |headers|
158
158
 
@@ -164,8 +164,9 @@ module Net
164
164
  end
165
165
  f.resume nhres
166
166
  }
167
+ httpreq.errback {|err|f.resume(:error)}
167
168
 
168
- nhres = Fiber.yield
169
+ nhres = yield_with_error_check
169
170
  nhres.instance_variable_set :@httpreq, httpreq
170
171
 
171
172
  yield nhres
@@ -173,17 +174,24 @@ module Net
173
174
  else
174
175
  httpreq.callback &convert_em_http_response
175
176
  httpreq.errback {|err|f.resume(:error)}
176
- res = Fiber.yield
177
177
 
178
- if res == :error
179
- raise 'EM::HttpRequest error - request timed out' if Time.now - self.read_timeout > t0
180
- raise 'EM::HttpRequest error - unknown error'
181
- end
178
+ yield_with_error_check
179
+ end
180
+ end
181
+
182
+ private
182
183
 
183
- res
184
+ def yield_with_error_check
185
+ res = Fiber.yield
186
+
187
+ if res == :error
188
+ raise 'EM::HttpRequest error - request timed out' if Time.now - self.read_timeout > t0
189
+ raise 'EM::HttpRequest error - unknown error'
184
190
  end
191
+
192
+ res
185
193
  end
186
-
194
+
187
195
  end
188
196
  end
189
197
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-net-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-12 00:00:00.000000000Z
12
+ date: 2011-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70106516929060 !ruby/object:Gem::Requirement
16
+ requirement: &70245767732660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70106516929060
24
+ version_requirements: *70245767732660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: eventmachine
27
- requirement: &70106516928320 !ruby/object:Gem::Requirement
27
+ requirement: &70245767732080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.12.10
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70106516928320
35
+ version_requirements: *70245767732080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: addressable
38
- requirement: &70106516927800 !ruby/object:Gem::Requirement
38
+ requirement: &70245767731500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70106516927800
46
+ version_requirements: *70245767731500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: em-http-request
49
- requirement: &70106516927300 !ruby/object:Gem::Requirement
49
+ requirement: &70245767730920 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.2.10
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70106516927300
57
+ version_requirements: *70245767730920
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70106516926680 !ruby/object:Gem::Requirement
60
+ requirement: &70245767730220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '2.5'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70106516926680
68
+ version_requirements: *70245767730220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mimic
71
- requirement: &70106516926100 !ruby/object:Gem::Requirement
71
+ requirement: &70245767727780 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.3.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70106516926100
79
+ version_requirements: *70245767727780
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: weary
82
- requirement: &70106516925520 !ruby/object:Gem::Requirement
82
+ requirement: &70245767727120 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70106516925520
90
+ version_requirements: *70245767727120
91
91
  description: Monkeypatching Net::HTTP to use em-http-request under the hood.
92
92
  email: james@netlagoon.com
93
93
  executables: []
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  requirements: []
133
133
  rubyforge_project:
134
- rubygems_version: 1.8.10
134
+ rubygems_version: 1.8.15
135
135
  signing_key:
136
136
  specification_version: 3
137
137
  summary: Non-blocking replacement for Net::HTTP, for use in EventMachine