ez_http 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -4
- data/ez_http.gemspec +15 -9
- data/lib/ez_http.rb +54 -22
- metadata +5 -3
data/README.md
CHANGED
@@ -3,17 +3,24 @@
|
|
3
3
|
A helper wrapper around net/http, supports http/https(with/without certificate), post/get requests, one method call does everything.
|
4
4
|
|
5
5
|
## How to use it
|
6
|
-
|
6
|
+
|
7
|
+
# default it will send a post request to specified url with the hash object as json
|
8
|
+
response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"})
|
9
|
+
# or
|
10
|
+
response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"}, "post", "application/json")
|
11
|
+
# or with certificate
|
12
|
+
response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"}, "post", "application/json", "/path_to_cert.pem")
|
13
|
+
|
7
14
|
puts response.body
|
8
15
|
|
9
16
|
## Installation
|
10
17
|
|
11
|
-
Add the following line to your "Gemfile"
|
12
|
-
gem "ez_http"
|
18
|
+
Add the following line to your "Gemfile"
|
19
|
+
gem "ez_http"
|
13
20
|
then execute bundle install
|
14
21
|
|
15
22
|
See here for more details:
|
16
|
-
|
23
|
+
[http://rubygems.org/gems/ez_http](http://rubygems.org/gems/ez_htt "EZHttp RubyGem Page")
|
17
24
|
|
18
25
|
## Author
|
19
26
|
|
data/ez_http.gemspec
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
|
3
|
-
|
2
|
+
|
3
|
+
# Basic info
|
4
|
+
s.name = "ez_http"
|
5
|
+
s.version = "1.0.2"
|
4
6
|
s.platform = Gem::Platform::RUBY
|
5
|
-
s.date =
|
7
|
+
s.date = "2012-04-04"
|
6
8
|
s.summary = "Make http/https request easier"
|
7
9
|
s.description = "A helper wrapper around net/http, supports http/https(with/without certificate), post/get requests, one method call does everything."
|
8
10
|
s.authors = ["Tianyu Huang"]
|
9
|
-
s.email =
|
10
|
-
s.
|
11
|
-
|
12
|
-
|
11
|
+
s.email = ["tianhsky@yahoo.com"]
|
12
|
+
s.homepage = "http://rubygems.org/gems/ez_http"
|
13
|
+
|
14
|
+
# Dependencies
|
15
|
+
# s.required_rubygems_version = ">= 1.3.6"
|
13
16
|
|
14
|
-
#
|
15
|
-
|
17
|
+
# Files
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.extra_rdoc_files = ["README.md", "doc/index.html"]
|
20
|
+
|
21
|
+
end
|
data/lib/ez_http.rb
CHANGED
@@ -4,7 +4,12 @@ require "openssl"
|
|
4
4
|
|
5
5
|
# A helper wrapper around net/http, supports http/https(with/without certificate), post/get requests, one method call does everything.
|
6
6
|
# How to use:
|
7
|
-
# response = EZHttp.Send("https://www.example.com:83/api",
|
7
|
+
# response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"})
|
8
|
+
# or
|
9
|
+
# response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"}, "post", "application/json")
|
10
|
+
# or
|
11
|
+
# response = EZHttp.Send("https://www.example.com:83/api", {"key1"=>"value1"}, "post", "application/json", "/path_to_cert.pem")
|
12
|
+
#
|
8
13
|
# puts response.body
|
9
14
|
#
|
10
15
|
# @author Tianyu Huang [tianhsky@yahoo.com]
|
@@ -13,48 +18,75 @@ module EZHttp
|
|
13
18
|
|
14
19
|
# Send request to specified url and will return responses
|
15
20
|
# @param [String] url: to send request to
|
16
|
-
# @param [String] method: is "get" or "post", if nil default is "post"
|
17
21
|
# @param [Hash] data: to send
|
22
|
+
# @param [String] method: can be "get"/"post"/"delete"/"put", if nil default is "post"
|
18
23
|
# @param [String] content_type: if nil default is "application/json"
|
19
24
|
# @param [String] cert_path: to the certificate file, set nil if none
|
20
25
|
# @return [Net::HTTPResponse] response from remote server, example to access its fields: response.body, response.status
|
21
26
|
#
|
22
|
-
def self.Send(url,
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
def self.Send(url, data, method="post", content_type="application/json", cert_path=nil)
|
28
|
+
|
29
|
+
# Hack to make it compatible with version <= 1.0.1
|
30
|
+
unless (method.class.to_s == "String")
|
31
|
+
if (method.respond_to?("merge"))
|
32
|
+
temp_data = method
|
33
|
+
method = data
|
34
|
+
data = temp_data
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
26
38
|
|
27
|
-
|
28
|
-
|
39
|
+
# Parse url
|
40
|
+
begin
|
41
|
+
uri = URI.parse(url)
|
42
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
43
|
+
if (url.match(/^https/))
|
44
|
+
http.use_ssl = true
|
45
|
+
end
|
46
|
+
rescue
|
47
|
+
throw "Error in url"
|
29
48
|
end
|
30
49
|
|
31
|
-
# Create request obj
|
32
|
-
request = nil
|
33
50
|
|
34
|
-
|
35
|
-
|
51
|
+
# Create request
|
52
|
+
begin
|
53
|
+
method = method || "post"
|
54
|
+
case method.downcase
|
36
55
|
when "post"
|
37
56
|
request = Net::HTTP::Post.new(uri.request_uri)
|
38
57
|
when "get"
|
39
58
|
request = Net::HTTP::Get.new(uri.request_uri)
|
59
|
+
when "put"
|
60
|
+
request = Net::HTTP::Put.new(uri.request_uri)
|
61
|
+
when "delete"
|
62
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
40
63
|
else
|
41
64
|
request = Net::HTTP::Post.new(uri.request_uri)
|
65
|
+
end
|
66
|
+
|
67
|
+
request.set_form_data(data)
|
68
|
+
request["Content-Type"] = content_type || "application/json"
|
69
|
+
rescue
|
70
|
+
throw "Error in creating request"
|
42
71
|
end
|
43
72
|
|
44
|
-
request.set_form_data(data)
|
45
|
-
request["Content-Type"] = content_type || "application/json"
|
46
73
|
|
47
74
|
# Add cert if any
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
75
|
+
begin
|
76
|
+
if (cert_path.nil?)
|
77
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
78
|
+
else
|
79
|
+
http.use_ssl = true
|
80
|
+
pem = File.read(cert_path)
|
81
|
+
http.cert = OpenSSL::X509::Certificate.new(pem)
|
82
|
+
http.key = OpenSSL::PKey::RSA.new(pem)
|
83
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
84
|
+
end
|
85
|
+
rescue
|
86
|
+
throw "Error in creating certificate"
|
56
87
|
end
|
57
88
|
|
89
|
+
|
58
90
|
# Send request and return response
|
59
91
|
response = http.request(request)
|
60
92
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ez_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,14 +9,16 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A helper wrapper around net/http, supports http/https(with/without certificate),
|
15
15
|
post/get requests, one method call does everything.
|
16
|
-
email:
|
16
|
+
email:
|
17
|
+
- tianhsky@yahoo.com
|
17
18
|
executables: []
|
18
19
|
extensions: []
|
19
20
|
extra_rdoc_files:
|
21
|
+
- README.md
|
20
22
|
- doc/index.html
|
21
23
|
files:
|
22
24
|
- .gitignore
|