itrigga-net_helper 0.0.3 → 0.1.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.0.3
1
+ 0.1.0
@@ -9,6 +9,10 @@ module Itrigga
9
9
  module NetHelper
10
10
 
11
11
  module_function
12
+
13
+ def typhoeus_present?
14
+ defined?(Typhoeus)
15
+ end
12
16
 
13
17
  def do_get(url, time_out=5, retries_on_timeout=5, max_redirects = 3)
14
18
  retrycount = 0
@@ -28,10 +32,55 @@ module Itrigga
28
32
  end
29
33
 
30
34
  end
35
+
36
+ def typhoeus_request( opts={} )
37
+ opts[:timeout] ||= 30000 # ms
38
+ opts[:follow_location] ||= true
39
+ opts[:disable_ssl_peer_verification] = true if opts[:disable_ssl_peer_verification].nil?
40
+
41
+ request = Typhoeus::Request.new(opts[:url], opts)
42
+
43
+ request.on_complete do |response|
44
+ if response.success?
45
+ return response.body
46
+ elsif response.timed_out?
47
+ # aw hell no
48
+ log("got a time out")
49
+ elsif response.code == 0
50
+ # Could not get an http response, something's wrong.
51
+ log(response.curl_error_message)
52
+ else
53
+ # Received a non-successful http response.
54
+ log("HTTP request failed: " + response.code.to_s)
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ def get_response( url )
61
+ if typhoeus_present?
62
+ typhoeus_request( url )
63
+ else
64
+ Net::HTTP.get_response(URI.parse(url))
65
+ end
66
+ end
31
67
 
32
68
  def get_with_timeout( url, time_out)
69
+ resp = nil
70
+ if defined?(SystemTimer)
71
+ resp = SystemTimer.timeout_after(time_out) do
72
+ get_response(url)
73
+ end
74
+ else
75
+ resp = timeout(time_out) do
76
+ get_response(url)
77
+ end
78
+ end
79
+ resp
80
+ end
81
+
33
82
  resp = timeout(time_out) do
34
- Net::HTTP.get_response(URI.parse(url))
83
+
35
84
  end
36
85
  resp
37
86
  end
@@ -1,10 +1,30 @@
1
- require 'spec_helper'
1
+ require File.join(File.dirname(__FILE__), 'spec_helper' )
2
2
 
3
3
  describe "Itrigga::NetHelper" do
4
4
  before(:each) do
5
5
  @mock_response = mock("response")
6
6
  @mock_response.stub!(:body).and_return("body")
7
7
  end
8
+
9
+ describe "typhoeus_present?" do
10
+ context "when Typhoeus is loaded" do
11
+ before(:each) do
12
+ class Typhoeus
13
+ end
14
+ end
15
+
16
+ it "should return true" do
17
+ Itrigga::NetHelper.typhoeus_present?.should == true
18
+ end
19
+ end
20
+
21
+ context "when Typhoeus is not loaded" do
22
+ it "should return false" do
23
+ Itrigga::NetHelper.typhoeus_present?.should == false
24
+ end
25
+ end
26
+
27
+ end
8
28
 
9
29
  describe "do_get" do
10
30
  it "should get the HTTP response" do
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,8 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
16
  require 'itrigga/core_ext'
17
17
  require 'itrigga/net_helper'
18
18
 
19
+ Dir.chdir(File.join(File.dirname(__FILE__), '..'))
20
+
19
21
  RSpec.configure do |config|
20
22
  # some (optional) config here
21
23
  end
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: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Davidson
@@ -15,11 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-22 00:00:00 +01:00
18
+ date: 2011-08-30 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- type: :development
23
22
  prerelease: false
24
23
  name: hpricot
25
24
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -32,8 +31,8 @@ dependencies:
32
31
  - 0
33
32
  version: "0"
34
33
  requirement: *id001
35
- - !ruby/object:Gem::Dependency
36
34
  type: :development
35
+ - !ruby/object:Gem::Dependency
37
36
  prerelease: false
38
37
  name: bundler
39
38
  version_requirements: &id002 !ruby/object:Gem::Requirement
@@ -48,8 +47,8 @@ dependencies:
48
47
  - 0
49
48
  version: 1.0.0
50
49
  requirement: *id002
51
- - !ruby/object:Gem::Dependency
52
50
  type: :development
51
+ - !ruby/object:Gem::Dependency
53
52
  prerelease: false
54
53
  name: jeweler
55
54
  version_requirements: &id003 !ruby/object:Gem::Requirement
@@ -64,8 +63,8 @@ dependencies:
64
63
  - 4
65
64
  version: 1.6.4
66
65
  requirement: *id003
67
- - !ruby/object:Gem::Dependency
68
66
  type: :development
67
+ - !ruby/object:Gem::Dependency
69
68
  prerelease: false
70
69
  name: rcov
71
70
  version_requirements: &id004 !ruby/object:Gem::Requirement
@@ -78,8 +77,8 @@ dependencies:
78
77
  - 0
79
78
  version: "0"
80
79
  requirement: *id004
81
- - !ruby/object:Gem::Dependency
82
80
  type: :development
81
+ - !ruby/object:Gem::Dependency
83
82
  prerelease: false
84
83
  name: rspec
85
84
  version_requirements: &id005 !ruby/object:Gem::Requirement
@@ -92,8 +91,8 @@ dependencies:
92
91
  - 0
93
92
  version: "0"
94
93
  requirement: *id005
95
- - !ruby/object:Gem::Dependency
96
94
  type: :development
95
+ - !ruby/object:Gem::Dependency
97
96
  prerelease: false
98
97
  name: json_pure
99
98
  version_requirements: &id006 !ruby/object:Gem::Requirement
@@ -106,8 +105,8 @@ dependencies:
106
105
  - 0
107
106
  version: "0"
108
107
  requirement: *id006
109
- - !ruby/object:Gem::Dependency
110
108
  type: :development
109
+ - !ruby/object:Gem::Dependency
111
110
  prerelease: false
112
111
  name: itrigga-core_ext
113
112
  version_requirements: &id007 !ruby/object:Gem::Requirement
@@ -120,8 +119,8 @@ dependencies:
120
119
  - 0
121
120
  version: "0"
122
121
  requirement: *id007
123
- - !ruby/object:Gem::Dependency
124
122
  type: :development
123
+ - !ruby/object:Gem::Dependency
125
124
  prerelease: false
126
125
  name: fastercsv
127
126
  version_requirements: &id008 !ruby/object:Gem::Requirement
@@ -136,6 +135,7 @@ dependencies:
136
135
  - 4
137
136
  version: 1.5.4
138
137
  requirement: *id008
138
+ type: :development
139
139
  description: Provides various more convenient wrappers around Net::HTTP methods
140
140
  email: aldavidson@trigga.com
141
141
  executables: []