itrigga-net_helper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -11,7 +11,11 @@ module Itrigga
11
11
  module_function
12
12
 
13
13
  def typhoeus_present?
14
- defined?(Typhoeus)
14
+ if defined?(Typhoeus)
15
+ true
16
+ else
17
+ false
18
+ end
15
19
  end
16
20
 
17
21
  def do_get(url, time_out=5, retries_on_timeout=5, max_redirects = 3)
@@ -34,7 +38,8 @@ module Itrigga
34
38
  end
35
39
 
36
40
  def typhoeus_request( opts={} )
37
- opts[:timeout] ||= 30000 # ms
41
+ opts[:timeout] ||= 30
42
+ opts[:timeout] *= 1000 # Typhoeus does its timeouts in ms
38
43
  opts[:follow_location] ||= true
39
44
  opts[:disable_ssl_peer_verification] = true if opts[:disable_ssl_peer_verification].nil?
40
45
 
@@ -45,21 +50,24 @@ module Itrigga
45
50
  return response.body
46
51
  elsif response.timed_out?
47
52
  # aw hell no
48
- log("got a time out")
53
+ raise IOError.new("Timed out request to #{opts[:url]} after #{opts[:timeout]}ms")
49
54
  elsif response.code == 0
50
55
  # Could not get an http response, something's wrong.
51
- log(response.curl_error_message)
56
+ raise IOError.new(response.curl_error_message)
52
57
  else
53
58
  # Received a non-successful http response.
54
- log("HTTP request failed: " + response.code.to_s)
59
+ raise IOError.new("HTTP request failed: " + response.code.to_s)
55
60
  end
56
61
  end
57
62
 
63
+ hydra = Typhoeus::Hydra.new
64
+ hydra.queue(request)
65
+ hydra.run
58
66
  end
59
67
 
60
- def get_response( url )
68
+ def get_response( url, timeout=nil )
61
69
  if typhoeus_present?
62
- typhoeus_request( url )
70
+ typhoeus_request( :url=>url, :timeout=>timeout )
63
71
  else
64
72
  Net::HTTP.get_response(URI.parse(url))
65
73
  end
@@ -69,25 +77,21 @@ module Itrigga
69
77
  resp = nil
70
78
  if defined?(SystemTimer)
71
79
  resp = SystemTimer.timeout_after(time_out) do
72
- get_response(url)
80
+ get_response(url, time_out)
73
81
  end
74
82
  else
75
83
  resp = timeout(time_out) do
76
- get_response(url)
84
+ get_response(url, time_out)
77
85
  end
78
86
  end
79
87
  resp
80
88
  end
81
89
 
82
- resp = timeout(time_out) do
83
-
84
- end
85
- resp
86
- end
87
90
 
88
91
 
89
92
  def handle_response( url, resp, retries_on_timeout=5, max_redirects = 3 )
90
93
  if resp.is_a? Net::HTTPSuccess then resp.body
94
+ elsif resp.is_a? String then resp
91
95
  elsif resp.is_a? Net::HTTPRedirection
92
96
  if max_redirects > 0
93
97
  do_get( URI.parse(url).merge(resp['location']).to_s, retries_on_timeout, max_redirects - 1 )
@@ -110,8 +114,14 @@ module Itrigga
110
114
 
111
115
  retrycount = 0
112
116
  resp = begin
113
- timeout( opts[:timeout] ) do
114
- raw_get(opts)
117
+ if defined?(SystemTimer)
118
+ SystemTimer.timeout_after(opts[:timeout]) do
119
+ raw_get(opts)
120
+ end
121
+ else
122
+ timeout( opts[:timeout] ) do
123
+ raw_get(opts)
124
+ end
115
125
  end
116
126
  rescue TimeoutError
117
127
  if(retrycount < opts[:retries_on_timeout])
@@ -126,6 +136,11 @@ module Itrigga
126
136
  end
127
137
 
128
138
  def raw_get(opts)
139
+
140
+ if typhoeus_present?
141
+ return typhoeus_request(opts)
142
+ end
143
+
129
144
  resp = nil
130
145
  establish_session_if_needed(opts)
131
146
 
@@ -9,8 +9,7 @@ describe "Itrigga::NetHelper" do
9
9
  describe "typhoeus_present?" do
10
10
  context "when Typhoeus is loaded" do
11
11
  before(:each) do
12
- class Typhoeus
13
- end
12
+ Itrigga::NetHelper.send(:const_set, "Typhoeus", Proc.new {})
14
13
  end
15
14
 
16
15
  it "should return true" do
@@ -19,6 +18,10 @@ describe "Itrigga::NetHelper" do
19
18
  end
20
19
 
21
20
  context "when Typhoeus is not loaded" do
21
+ before(:each) do
22
+ Itrigga::NetHelper.send(:remove_const, "Typhoeus")
23
+ end
24
+
22
25
  it "should return false" do
23
26
  Itrigga::NetHelper.typhoeus_present?.should == false
24
27
  end
@@ -27,6 +30,11 @@ describe "Itrigga::NetHelper" do
27
30
  end
28
31
 
29
32
  describe "do_get" do
33
+ before(:each) do
34
+ Itrigga::NetHelper.stub!(:typhoeus_present?).and_return(false)
35
+ end
36
+
37
+
30
38
  it "should get the HTTP response" do
31
39
  Itrigga::NetHelper.stub!(:handle_response).and_return("")
32
40
  Net::HTTP.should_receive( :get_response ).with( URI.parse("http://some.com") ).and_return(@mock_response)
@@ -70,6 +78,7 @@ describe "Itrigga::NetHelper" do
70
78
 
71
79
  describe "when the response is OK" do
72
80
  before(:each) do
81
+ @mock_response.stub!(:is_a?).with(String).and_return false
73
82
  @mock_response.stub!(:is_a?).with(Net::HTTPSuccess).and_return true
74
83
  end
75
84
 
@@ -80,6 +89,7 @@ describe "Itrigga::NetHelper" do
80
89
 
81
90
  describe "when the response is a redirect" do
82
91
  before(:each) do
92
+ @mock_response.stub!(:is_a?).with(String).and_return false
83
93
  @mock_response.stub!(:is_a?).with(Net::HTTPSuccess).and_return false
84
94
  @mock_response.stub!(:is_a?).with(Net::HTTPRedirection).and_return true
85
95
  @mock_response.stub!(:[]).with('location').and_return("http://some.redirect")
@@ -246,6 +256,7 @@ describe "Itrigga::NetHelper" do
246
256
  before(:each) do
247
257
  Itrigga::NetHelper.stub!(:get_with_auth).and_return( 'body' )
248
258
  Itrigga::NetHelper.stub!(:establish_session_if_needed)
259
+ Itrigga::NetHelper.stub!(:typhoeus_present?).and_return(false)
249
260
  @mock_session = mock("http session")
250
261
  @mock_session.stub!(:request_get).and_return( @mock_response = mock('request_get response') )
251
262
  @mock_response.stub!(:body).and_return('response body')
@@ -306,6 +317,7 @@ describe "Itrigga::NetHelper" do
306
317
  describe "get_with_auth" do
307
318
  before(:each) do
308
319
  Itrigga::NetHelper.stub!(:establish_session_if_needed)
320
+ Itrigga::NetHelper.stub!(:typhoeus_present).and_return(false)
309
321
  @mock_get = mock('get')
310
322
  @mock_get.stub!(:basic_auth)
311
323
  Net::HTTP::Get.stub!(:new).and_return(@mock_get)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itrigga-net_helper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Davidson