http-requestor 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/http-requestor.rb +42 -29
- data/test/http_requestor_test.rb +22 -0
- metadata +6 -4
data/lib/http-requestor.rb
CHANGED
@@ -1,69 +1,76 @@
|
|
1
1
|
require "net/http"
|
2
|
+
require "net/https"
|
3
|
+
require "uri"
|
2
4
|
|
3
5
|
class HttpRequestor
|
4
6
|
def initialize(domain)
|
7
|
+
raise InvalidURISchemeException, "Please send a valid URI scheme." if domain.match(/(http|https):\/\//).nil?
|
5
8
|
@defaults = {:domain => domain}
|
9
|
+
uri = URI.parse(@defaults[:domain])
|
10
|
+
if uri.scheme == "http"
|
11
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
12
|
+
else
|
13
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
14
|
+
@http.use_ssl = true
|
15
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
16
|
+
end
|
6
17
|
end
|
7
18
|
|
8
|
-
def get(path,data,headers=nil)
|
9
|
-
uri = URI.parse(@defaults[:domain])
|
10
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
19
|
+
def get(path,data="",headers=nil)
|
11
20
|
if headers == nil
|
12
|
-
response = http.send_request('GET',path,data)
|
21
|
+
response = @http.send_request('GET',path,data)
|
13
22
|
else
|
14
|
-
response = http.send_request('GET',path,data,headers)
|
23
|
+
response = @http.send_request('GET',path,data,headers)
|
15
24
|
end
|
16
25
|
response
|
17
26
|
end
|
18
27
|
|
19
|
-
def post(path,data,headers=nil)
|
20
|
-
uri = URI.parse(@defaults[:domain])
|
21
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
28
|
+
def post(path,data="",headers=nil)
|
22
29
|
if headers == nil
|
23
|
-
response = http.send_request('POST',path,data)
|
30
|
+
response = @http.send_request('POST',path,data)
|
24
31
|
else
|
25
|
-
response = http.send_request('POST',path,data,headers)
|
32
|
+
response = @http.send_request('POST',path,data,headers)
|
26
33
|
end
|
27
34
|
response
|
28
35
|
end
|
29
36
|
|
30
|
-
def put(path,data,headers=nil)
|
31
|
-
uri = URI.parse(@defaults[:domain])
|
32
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
33
|
-
|
37
|
+
def put(path,data="",headers=nil)
|
34
38
|
if headers == nil
|
35
|
-
response = http.send_request('PUT',path,data)
|
39
|
+
response = @http.send_request('PUT',path,data)
|
36
40
|
else
|
37
|
-
response = http.send_request('PUT',path,data,headers)
|
41
|
+
response = @http.send_request('PUT',path,data,headers)
|
38
42
|
end
|
39
43
|
response
|
40
44
|
end
|
41
45
|
|
42
|
-
def delete(path,data,headers=nil)
|
43
|
-
uri = URI.parse(@defaults[:domain])
|
44
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
45
|
-
|
46
|
+
def delete(path,data="",headers=nil)
|
46
47
|
if headers == nil
|
47
|
-
response = http.send_request('DELETE',path,data)
|
48
|
+
response = @http.send_request('DELETE',path,data)
|
48
49
|
else
|
49
|
-
response = http.send_request('DELETE',path,data,headers)
|
50
|
+
response = @http.send_request('DELETE',path,data,headers)
|
50
51
|
end
|
51
52
|
response
|
52
53
|
end
|
53
54
|
|
54
|
-
def self.request(domain, request_type, request_path, data=
|
55
|
-
|
56
|
-
request
|
55
|
+
def self.request(domain, request_type, request_path, data="", headers={})
|
56
|
+
request_type.upcase!
|
57
|
+
raise InvalidRequestTypeException, "Please pass a valid request type." unless valid_request_types.include?(request_type)
|
58
|
+
req = HttpRequestor.new(domain)
|
59
|
+
data = data.empty? ? "" : data.to_query
|
57
60
|
if request_type == "GET"
|
58
|
-
return
|
61
|
+
return req.get(request_path, data, headers)
|
59
62
|
elsif request_type == "POST"
|
60
|
-
return
|
63
|
+
return req.post(request_path, data, headers)
|
61
64
|
elsif request_type == "PUT"
|
62
|
-
return
|
65
|
+
return req.put(request_path, data, headers)
|
63
66
|
elsif request_type == "DELETE"
|
64
|
-
return
|
67
|
+
return req.delete(request_path, data, headers)
|
65
68
|
end
|
66
69
|
end
|
70
|
+
|
71
|
+
def self.valid_request_types
|
72
|
+
["GET", "POST", "UPDATE", "DELETE"]
|
73
|
+
end
|
67
74
|
end
|
68
75
|
|
69
76
|
class Hash
|
@@ -90,4 +97,10 @@ class String
|
|
90
97
|
def to_param
|
91
98
|
to_s
|
92
99
|
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class InvalidRequestTypeException < Exception
|
103
|
+
end
|
104
|
+
|
105
|
+
class InvalidURISchemeException < Exception
|
93
106
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "../lib/http-requestor"
|
2
|
+
require "test/unit"
|
3
|
+
|
4
|
+
class HttpRequestorTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_should_raise_error_for_invalid_url_scheme
|
7
|
+
assert_raise(InvalidURISchemeException) { HttpRequestor.new("some://www.invalid_url_scheme.com") }
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_not_raise_error_for_valid_url_scheme
|
11
|
+
assert_nothing_raised(InvalidURISchemeException) { HttpRequestor.new("http://www.google.com") }
|
12
|
+
assert_nothing_raised(InvalidURISchemeException) { HttpRequestor.new("https://www.yahoo.com") }
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_raise_error_on_invalid_request_type
|
16
|
+
assert_raise(InvalidRequestTypeException) { HttpRequestor.request("http://en.wikibooks.org", "OPTIONS", "/wiki/Ruby_Programming/Unit_testing#A_Simple_Introduction") }
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_not_raise_error_on_valid_request_type
|
20
|
+
assert_nothing_raised(InvalidRequestTypeException) { HttpRequestor.request("http://en.wikibooks.org", "GET", "/wiki/Ruby_Programming/Unit_testing#A_Simple_Introduction") }
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-requestor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-25 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A Wrapper around Net/HTTP which allows you to perform HTTP Requests.
|
15
15
|
email: rohit0981989@gmail.com
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- lib/http-requestor.rb
|
21
21
|
- MIT-LICENSE
|
22
22
|
- README.rdoc
|
23
|
+
- test/http_requestor_test.rb
|
23
24
|
homepage: http://github.com/rohit9889/http-requestor
|
24
25
|
licenses: []
|
25
26
|
post_install_message:
|
@@ -40,8 +41,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
41
|
version: '0'
|
41
42
|
requirements: []
|
42
43
|
rubyforge_project:
|
43
|
-
rubygems_version: 1.8.
|
44
|
+
rubygems_version: 1.8.17
|
44
45
|
signing_key:
|
45
46
|
specification_version: 3
|
46
47
|
summary: A Wrapper around Net/HTTP which allows you to perform HTTP Requests.
|
47
|
-
test_files:
|
48
|
+
test_files:
|
49
|
+
- test/http_requestor_test.rb
|