http-requestor 0.0.3 → 0.0.4
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/README.rdoc +49 -25
- data/lib/http-requestor.rb +35 -7
- data/test/http_requestor_test.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -2,28 +2,52 @@
|
|
2
2
|
|
3
3
|
A Wrapper around Net/HTTP which allows you to perform HTTP Requests in a simple way.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install http-requestor
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
|
12
|
+
=== Initialize a domain, and send request to multiple paths
|
13
|
+
|
14
|
+
http = HttpRequestor.new("http://www.mydomain.com")
|
15
|
+
# This will initialize the HttpRequestor class
|
16
|
+
|
17
|
+
get_request = http.get(path, parameters, headers)
|
18
|
+
post_request = http.post(path, parameters, headers)
|
19
|
+
put_request = http.put(path, parameters, headers)
|
20
|
+
delete_request = http.delete(path, parameters, headers)
|
21
|
+
|
22
|
+
# path => for example: "/some_path"
|
23
|
+
# parameters => this is an optional parameter, if you want to send some parameters alongwith the request, you can pass a hash with
|
24
|
+
# headers => this is also an optional parameter, you can pass a hash with stringified keys
|
25
|
+
|
26
|
+
=== Get a response right away
|
27
|
+
|
28
|
+
HttpRequestor.request(domain, request_type, path, parameters, headers)
|
29
|
+
# domain => for example: "http://www.some_domain.com"
|
30
|
+
# request_type => GET|POST|PUT|DELETE
|
31
|
+
# path => for example: "/some_path"
|
32
|
+
# parameters => this is an optional parameter, if you want to send some parameters alongwith the request, you can pass a hash with
|
33
|
+
# headers => this is also an optional parameter, you can pass a hash with stringified keys
|
34
|
+
|
35
|
+
OR you can do it the other way
|
36
|
+
|
37
|
+
HttpRequestor.request_with_url(url, request_type, data, headers)
|
38
|
+
# url => for example: "http://www.some_domain.com/some_path_value"
|
39
|
+
# request_type => GET|POST|PUT|DELETE
|
40
|
+
# parameters => this is an optional parameter, if you want to send some parameters alongwith the request, you can pass a hash with
|
41
|
+
# headers => this is also an optional parameter, you can pass a hash with stringified keys
|
42
|
+
|
43
|
+
=== HTTP Basic Authentication
|
44
|
+
|
45
|
+
HttpRequestor.send_basic_auth_request(url, username, password)
|
46
|
+
# url => for example: "http://www.some_domain.com/some_path_value" only GET URL's supported currently
|
47
|
+
# username => basic_auth_username
|
48
|
+
# password => basic_auth_password
|
49
|
+
|
50
|
+
== Issues and Suggestions
|
51
|
+
|
52
|
+
Please report all the issues in the Github Issues Page, suggestions are also welcome.
|
53
|
+
You can also mail me at rohit0981989[at]gmail[dot]com for the same.
|
data/lib/http-requestor.rb
CHANGED
@@ -3,8 +3,12 @@ require "net/https"
|
|
3
3
|
require "uri"
|
4
4
|
|
5
5
|
class HttpRequestor
|
6
|
-
|
6
|
+
|
7
|
+
#============== Instance Methods ===================
|
8
|
+
|
9
|
+
def initialize(domain, options={})
|
7
10
|
raise InvalidURISchemeException, "Please send a valid URI scheme." if domain.match(/(http|https):\/\//).nil?
|
11
|
+
|
8
12
|
@defaults = {:domain => domain}
|
9
13
|
uri = URI.parse(@defaults[:domain])
|
10
14
|
if uri.scheme == "http"
|
@@ -16,7 +20,8 @@ class HttpRequestor
|
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
19
|
-
def get(path,data=
|
23
|
+
def get(path,data={},headers=nil)
|
24
|
+
data = data_to_query(data)
|
20
25
|
if headers == nil
|
21
26
|
response = @http.send_request('GET',path,data)
|
22
27
|
else
|
@@ -25,7 +30,8 @@ class HttpRequestor
|
|
25
30
|
response
|
26
31
|
end
|
27
32
|
|
28
|
-
def post(path,data=
|
33
|
+
def post(path,data={},headers=nil)
|
34
|
+
data = data_to_query(data)
|
29
35
|
if headers == nil
|
30
36
|
response = @http.send_request('POST',path,data)
|
31
37
|
else
|
@@ -34,7 +40,8 @@ class HttpRequestor
|
|
34
40
|
response
|
35
41
|
end
|
36
42
|
|
37
|
-
def put(path,data=
|
43
|
+
def put(path,data={},headers=nil)
|
44
|
+
data = data_to_query(data)
|
38
45
|
if headers == nil
|
39
46
|
response = @http.send_request('PUT',path,data)
|
40
47
|
else
|
@@ -43,7 +50,8 @@ class HttpRequestor
|
|
43
50
|
response
|
44
51
|
end
|
45
52
|
|
46
|
-
def delete(path,data=
|
53
|
+
def delete(path,data={},headers=nil)
|
54
|
+
data = data_to_query(data)
|
47
55
|
if headers == nil
|
48
56
|
response = @http.send_request('DELETE',path,data)
|
49
57
|
else
|
@@ -51,12 +59,16 @@ class HttpRequestor
|
|
51
59
|
end
|
52
60
|
response
|
53
61
|
end
|
62
|
+
|
63
|
+
def data_to_query(data)
|
64
|
+
return (data.nil? || data.empty?) ? "" : data.to_query
|
65
|
+
end
|
54
66
|
|
55
|
-
|
67
|
+
#============== Class Methods ===================
|
68
|
+
def self.request(domain, request_type, request_path, data={}, headers={})
|
56
69
|
request_type.upcase!
|
57
70
|
raise InvalidRequestTypeException, "Please pass a valid request type." unless valid_request_types.include?(request_type)
|
58
71
|
req = HttpRequestor.new(domain)
|
59
|
-
data = data.empty? ? "" : data.to_query
|
60
72
|
if request_type == "GET"
|
61
73
|
return req.get(request_path, data, headers)
|
62
74
|
elsif request_type == "POST"
|
@@ -67,12 +79,27 @@ class HttpRequestor
|
|
67
79
|
return req.delete(request_path, data, headers)
|
68
80
|
end
|
69
81
|
end
|
82
|
+
|
83
|
+
def self.request_with_url(url, request_type, data={}, headers={})
|
84
|
+
uri = URI.parse(url)
|
85
|
+
return self.request("#{uri.scheme}://#{uri.host}:#{uri.port}", request_type, uri.request_uri, data, headers)
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.send_basic_auth_request(url, username, password)
|
89
|
+
uri = URI.parse(url)
|
90
|
+
|
91
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
92
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
93
|
+
request.basic_auth(username, password)
|
94
|
+
http.request(request)
|
95
|
+
end
|
70
96
|
|
71
97
|
def self.valid_request_types
|
72
98
|
["GET", "POST", "UPDATE", "DELETE"]
|
73
99
|
end
|
74
100
|
end
|
75
101
|
|
102
|
+
#================ ActiveSupport Like object#to_query methods =========================
|
76
103
|
class Hash
|
77
104
|
def to_query(namespace = nil)
|
78
105
|
collect do |key, value|
|
@@ -99,6 +126,7 @@ class String
|
|
99
126
|
end
|
100
127
|
end
|
101
128
|
|
129
|
+
#================= Exceptions ======================
|
102
130
|
class InvalidRequestTypeException < Exception
|
103
131
|
end
|
104
132
|
|
data/test/http_requestor_test.rb
CHANGED
@@ -11,7 +11,7 @@ class HttpRequestorTest < Test::Unit::TestCase
|
|
11
11
|
assert_nothing_raised(InvalidURISchemeException) { HttpRequestor.new("http://www.google.com") }
|
12
12
|
assert_nothing_raised(InvalidURISchemeException) { HttpRequestor.new("https://www.yahoo.com") }
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def test_should_raise_error_on_invalid_request_type
|
16
16
|
assert_raise(InvalidRequestTypeException) { HttpRequestor.request("http://en.wikibooks.org", "OPTIONS", "/wiki/Ruby_Programming/Unit_testing#A_Simple_Introduction") }
|
17
17
|
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.4
|
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-27 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
|