mingle_events 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mingle_events/http.rb +18 -8
- data/lib/mingle_events/http_error.rb +8 -8
- data/test/mingle_events/http_test.rb +48 -0
- data/test/test_helper.rb +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d698d7bfba52d210386fcfcc08932d91c83e1eee
|
4
|
+
data.tar.gz: ecbc8067509a2d0c09d752f0f29e3b7c769b2a92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dd16e77733ee7dbd5703bc82ca8201c5b52d89d7fdc81d7e08959b4e137f72ac52d4b5c1761c7b7a792bf0bd6aea3f1ff45036864359ba65e2a7ab5c11c40af
|
7
|
+
data.tar.gz: 64b001acbcd89fd8f45e11e43dfdb18069a22e41b4f4a14396e256ed57e65c3fb776be245e965b6fecb61289b92ebac50b8d7d08c42f9a3af25cc80e0e184a1c
|
data/lib/mingle_events/http.rb
CHANGED
@@ -5,8 +5,22 @@ module MingleEvents
|
|
5
5
|
MAX_RETRY_TIMES = 5
|
6
6
|
|
7
7
|
# get response body for a url, a block can be passed in for request pre-processing
|
8
|
-
def get(url, retry_count=0, &block)
|
9
|
-
rsp =
|
8
|
+
def get(url, retry_count=0, max_retry_times=MAX_RETRY_TIMES, &block)
|
9
|
+
rsp = nil
|
10
|
+
retrying = lambda do
|
11
|
+
raise HttpError.new(rsp, url) if retry_count >= max_retry_times
|
12
|
+
cooldown = retry_count * 2
|
13
|
+
MingleEvents.log.info "Getting service error when get page at #{url}, retry after #{cooldown}s..."
|
14
|
+
sleep cooldown
|
15
|
+
get(url, retry_count + 1, max_retry_times, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
rsp = fetch_page_response(url, &block)
|
20
|
+
rescue EOFError => e
|
21
|
+
return retrying.call
|
22
|
+
end
|
23
|
+
|
10
24
|
case rsp
|
11
25
|
when Net::HTTPSuccess
|
12
26
|
rsp.body
|
@@ -15,12 +29,8 @@ module MingleEvents
|
|
15
29
|
If you think you are passing correct credentials, please check
|
16
30
|
that you have enabled Mingle for basic authentication.
|
17
31
|
See <http://www.thoughtworks-studios.com/mingle/3.3/help/configuring_mingle_authentication.html>.})
|
18
|
-
when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
|
19
|
-
|
20
|
-
cooldown = retry_count * 2
|
21
|
-
MingleEvents.log.info "Getting service error when get page at #{url}, retry after #{cooldown}s..."
|
22
|
-
sleep cooldown
|
23
|
-
get(url, retry_count + 1, &block)
|
32
|
+
when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
|
33
|
+
retrying.call
|
24
34
|
else
|
25
35
|
raise HttpError.new(rsp, url)
|
26
36
|
end
|
@@ -1,26 +1,26 @@
|
|
1
1
|
module MingleEvents
|
2
|
-
|
2
|
+
|
3
3
|
class HttpError < StandardError
|
4
|
-
|
4
|
+
|
5
5
|
attr_reader :response, :requested_location, :additional_context
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(response, requested_location, additional_context = nil)
|
8
8
|
super(%{
|
9
9
|
Unable to retrieve 200 response from URI: <#{requested_location}>!
|
10
|
-
HTTP Code: #{response.code}
|
11
|
-
Body: #{response.body}
|
10
|
+
HTTP Code: #{response && response.code}
|
11
|
+
Body: #{response && response.body}
|
12
12
|
#{additional_context.nil? ? "" : additional_context}
|
13
13
|
})
|
14
14
|
@response = response
|
15
15
|
@requested_location = requested_location
|
16
16
|
@additional_context = additional_context
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def not_found?
|
20
20
|
# has to be a better way to do this!!
|
21
21
|
response.class == Net::HTTPNotFound
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
end
|
25
25
|
|
26
|
-
end
|
26
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
|
2
|
+
|
3
|
+
module MingleEvents
|
4
|
+
class HttpTest < MiniTest::Test
|
5
|
+
def setup
|
6
|
+
@http = MingleEvents::Http
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_should_return_response_body_on_success_request
|
10
|
+
stub_request(:get, "http://www.example.com/").
|
11
|
+
to_return(:status => 200, :body => "abc")
|
12
|
+
assert_equal 'abc', @http.get("http://www.example.com")
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_raise_error_when_unauthorized
|
16
|
+
stub_request(:get, "http://www.example.com/").
|
17
|
+
to_return(:status => 403, :body => "Forbidden")
|
18
|
+
assert_raises(HttpError) { @http.get("http://www.example.com") }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_raise_error_on_service_unavailable_response
|
22
|
+
stub_request(:get, "http://www.example.com/").
|
23
|
+
to_return(:status => 503, :body => "Overloaded")
|
24
|
+
assert_raises(HttpError) { @http.get("http://www.example.com", 0, 1) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_retry_on_service_unavailable_response
|
28
|
+
stub_request(:get, "http://www.example.com/").
|
29
|
+
to_return(:status => 503, :body => "Overloaded").then.
|
30
|
+
to_return(:status => 200, :body => "abc")
|
31
|
+
assert_equal 'abc', @http.get("http://www.example.com", 0, 1)
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def test_should_raise_error_on_eof
|
36
|
+
stub_request(:get, "http://www.example.com/").
|
37
|
+
to_raise(EOFError)
|
38
|
+
assert_raises(HttpError) { @http.get("http://www.example.com", 0, 1) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_retry_on_eof
|
42
|
+
stub_request(:get, "http://www.example.com/").
|
43
|
+
to_raise(EOFError).then.
|
44
|
+
to_return(:status => 200, :body => "abc")
|
45
|
+
assert_equal 'abc', @http.get("http://www.example.com", 0, 1)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mingle_events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Rice
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: "\n Mingle 3.3 introduced a new Events API in the form of an \"Atom
|
84
98
|
feed\":http://www.thoughtworks-studios.com/mingle/3.3/help/mingle_api_events.html.
|
85
99
|
The Mingle team and ThoughtWorks Studios are big believers in the use of Atom for
|
@@ -142,6 +156,7 @@ files:
|
|
142
156
|
- test/mingle_events/feed/entry_test.rb
|
143
157
|
- test/mingle_events/feed/links_test.rb
|
144
158
|
- test/mingle_events/feed/page_test.rb
|
159
|
+
- test/mingle_events/http_test.rb
|
145
160
|
- test/mingle_events/mingle_basic_auth_access_test.rb
|
146
161
|
- test/mingle_events/mingle_hmac_auth_access_test.rb
|
147
162
|
- test/mingle_events/mingle_oauth_access_test.rb
|