ez_http 1.0.3 → 1.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.md +97 -19
- data/bin/ezhttp +116 -0
- data/doc/EZHttp.html +548 -74
- data/doc/_index.html +1 -1
- data/doc/file.README.html +104 -20
- data/doc/index.html +104 -20
- data/doc/method_list.html +40 -0
- data/doc/top-level-namespace.html +1 -1
- data/ez_http.gemspec +13 -5
- data/lib/ez_http.rb +2 -118
- data/lib/ez_http/core.rb +205 -0
- data/lib/ez_http/version.rb +3 -0
- metadata +28 -7
data/lib/ez_http/core.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
#ruby lib
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "openssl"
|
5
|
+
|
6
|
+
#gem
|
7
|
+
require "json"
|
8
|
+
|
9
|
+
# A wrapper for ruby net/http, supports http/https, RESTful methods, headers, certificate
|
10
|
+
# @author Tianyu Huang [tianhsky@yahoo.com]
|
11
|
+
#
|
12
|
+
module EZHttp
|
13
|
+
|
14
|
+
# Send http request to specified url and return responses
|
15
|
+
# @param [String] url: to send request to
|
16
|
+
# @param [Hash/String] data: hash to send or encoded query string or stringified hash
|
17
|
+
# @param [String] method: choose from "get"/"post"/"delete"/"put", if nil default is "post"
|
18
|
+
# @param [String] content_type: ie."application/json"
|
19
|
+
# @param [Array[String]] headers: is an array of Strings, ie.["encoded_key:encoded_value","content-type:application/json"];
|
20
|
+
# @param [String] cert_path: to the certificate file, don't specify or set nil if none
|
21
|
+
# @return [Net::HTTPResponse] response from remote server, example to access its fields: response.body
|
22
|
+
#
|
23
|
+
def self.Send(url, data=nil, method=nil, content_type=nil, headers=nil, cert_path=nil)
|
24
|
+
|
25
|
+
begin
|
26
|
+
# Parse url
|
27
|
+
url.strip!
|
28
|
+
uri = URI.parse(url)
|
29
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
30
|
+
if (url.match(/^https/))
|
31
|
+
http.use_ssl = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create request
|
35
|
+
request_method = data.nil? ? "get" : "post"
|
36
|
+
request_method = method || request_method
|
37
|
+
request_method.strip!
|
38
|
+
request_method.downcase!
|
39
|
+
case request_method
|
40
|
+
when "post"
|
41
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
42
|
+
when "get"
|
43
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
44
|
+
when "put"
|
45
|
+
request = Net::HTTP::Put.new(uri.request_uri)
|
46
|
+
when "delete"
|
47
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
48
|
+
else
|
49
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
50
|
+
end
|
51
|
+
rescue
|
52
|
+
throw "Error in creating request"
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
# Set content-type
|
57
|
+
request["content-type"] = content_type.strip! unless content_type.nil?
|
58
|
+
|
59
|
+
|
60
|
+
# Set data (can be json or encoded query string)
|
61
|
+
begin
|
62
|
+
unless (data.nil?)
|
63
|
+
if (data.class.to_s == "Hash" || data.respond_to?("merge"))
|
64
|
+
request.body = data.to_json
|
65
|
+
request["content-type"] = "application/json" if request["content-type"].nil?
|
66
|
+
else
|
67
|
+
if (data.include?("{") && data.include?("}"))
|
68
|
+
request["content-type"] = "application/json" if request["content-type"].nil?
|
69
|
+
end
|
70
|
+
request.body = data.strip
|
71
|
+
#request["content-type"] = "application/x-www-form-urlencoded" if request["content-type"].nil?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
rescue
|
75
|
+
throw "Error in parsing data"
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
# Set headers
|
80
|
+
begin
|
81
|
+
unless (headers.nil?)
|
82
|
+
headers.each do |header|
|
83
|
+
key = header.split(':')[0]
|
84
|
+
value = header.split(':')[1]
|
85
|
+
request[key.strip.downcase] = value.strip
|
86
|
+
end
|
87
|
+
end
|
88
|
+
rescue
|
89
|
+
throw "Error in parsing headers"
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# Add cert if any
|
94
|
+
begin
|
95
|
+
if (cert_path.nil?)
|
96
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
97
|
+
else
|
98
|
+
http.use_ssl = true
|
99
|
+
pem = File.read(cert_path)
|
100
|
+
http.cert = OpenSSL::X509::Certificate.new(pem)
|
101
|
+
http.key = OpenSSL::PKey::RSA.new(pem)
|
102
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
103
|
+
end
|
104
|
+
rescue
|
105
|
+
throw "Error in creating certificate"
|
106
|
+
end
|
107
|
+
|
108
|
+
# Send request and return response
|
109
|
+
response = http.request(request)
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def self.Post(url, data=nil, content_type=nil, headers=nil, cert_path=nil)
|
115
|
+
self.Send(url, data, "post", content_type, headers, cert_path)
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.Get(url, data=nil, content_type=nil, headers=nil, cert_path=nil)
|
119
|
+
self.Send(url, data, "get", content_type, headers, cert_path)
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.Put(url, data=nil, content_type=nil, headers=nil, cert_path=nil)
|
123
|
+
self.Send(url, data, "put", content_type, headers, cert_path)
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.Delete(url, data=nil, content_type=nil, headers=nil, cert_path=nil)
|
127
|
+
self.Send(url, data, "delete", content_type, headers, cert_path)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Upload files
|
131
|
+
# @param [String] url: server to upload
|
132
|
+
# @param [Hash] files: [{"name","content"}]
|
133
|
+
# @param [Array[String]] headers: is an array of Strings, ie.["authorization:Basic twwtws33vsfesfsd=="];
|
134
|
+
#
|
135
|
+
def self.Upload(url, files, headers=nil, cert_path=nil)
|
136
|
+
|
137
|
+
begin
|
138
|
+
# Parse url
|
139
|
+
url.strip!
|
140
|
+
uri = URI.parse(url)
|
141
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
142
|
+
if (url.match(/^https/))
|
143
|
+
http.use_ssl = true
|
144
|
+
end
|
145
|
+
|
146
|
+
# Create request
|
147
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
148
|
+
rescue
|
149
|
+
throw "Error in creating request"
|
150
|
+
end
|
151
|
+
|
152
|
+
# Set headers
|
153
|
+
begin
|
154
|
+
unless (headers.nil?)
|
155
|
+
headers.each do |header|
|
156
|
+
key = header.split(':')[0]
|
157
|
+
value = header.split(':')[1]
|
158
|
+
request[key.strip.downcase] = value.strip
|
159
|
+
end
|
160
|
+
end
|
161
|
+
rescue
|
162
|
+
throw "Error in parsing headers"
|
163
|
+
end
|
164
|
+
|
165
|
+
# Create files
|
166
|
+
boundry = "--content--boundry--keybegin--AaB03sfwegSFSWxGBgSBsFDYcRcRMi--keyend--"
|
167
|
+
post_body = []
|
168
|
+
|
169
|
+
if (!files.nil? && files.length > 0)
|
170
|
+
files.each_with_index do |file, i|
|
171
|
+
post_body << "--#{boundry}\r\n"
|
172
|
+
post_body << "content-disposition: form-data; name=\"upload[file#{i}]\"; filename=\"#{file["name"]}\"\r\n"
|
173
|
+
post_body << "content-type: application/octet-stream\r\n"
|
174
|
+
post_body << "\r\n"
|
175
|
+
post_body << file["content"] + "\r\n"
|
176
|
+
end
|
177
|
+
post_body << "--#{boundry}--\r\n"
|
178
|
+
|
179
|
+
request.body = post_body.join
|
180
|
+
end
|
181
|
+
|
182
|
+
# Set content type
|
183
|
+
request["content-type"] = "multipart/form-data, boundary=#{boundry}"
|
184
|
+
|
185
|
+
# Add cert if any
|
186
|
+
begin
|
187
|
+
if (cert_path.nil?)
|
188
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
189
|
+
else
|
190
|
+
http.use_ssl = true
|
191
|
+
pem = File.read(cert_path)
|
192
|
+
http.cert = OpenSSL::X509::Certificate.new(pem)
|
193
|
+
http.key = OpenSSL::PKey::RSA.new(pem)
|
194
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
195
|
+
end
|
196
|
+
rescue
|
197
|
+
throw "Error in creating certificate"
|
198
|
+
end
|
199
|
+
|
200
|
+
# Send request and return response
|
201
|
+
response = http.request(request)
|
202
|
+
end
|
203
|
+
|
204
|
+
end
|
205
|
+
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
13
|
-
dependencies:
|
14
|
-
|
12
|
+
date: 2012-04-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.6
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.6.6
|
30
|
+
description: A wrapper for ruby net/http, supports http/https, RESTful methods, headers,
|
31
|
+
certificate and file uploads
|
15
32
|
email:
|
16
33
|
- tianhsky@yahoo.com
|
17
|
-
executables:
|
34
|
+
executables:
|
35
|
+
- ezhttp
|
18
36
|
extensions: []
|
19
37
|
extra_rdoc_files:
|
20
38
|
- README.md
|
@@ -23,6 +41,7 @@ files:
|
|
23
41
|
- .gitignore
|
24
42
|
- LICENSE
|
25
43
|
- README.md
|
44
|
+
- bin/ezhttp
|
26
45
|
- doc/EZHttp.html
|
27
46
|
- doc/_index.html
|
28
47
|
- doc/class_list.html
|
@@ -40,6 +59,8 @@ files:
|
|
40
59
|
- doc/top-level-namespace.html
|
41
60
|
- ez_http.gemspec
|
42
61
|
- lib/ez_http.rb
|
62
|
+
- lib/ez_http/core.rb
|
63
|
+
- lib/ez_http/version.rb
|
43
64
|
homepage: http://rubygems.org/gems/ez_http
|
44
65
|
licenses: []
|
45
66
|
post_install_message:
|
@@ -60,9 +81,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
81
|
version: '0'
|
61
82
|
requirements: []
|
62
83
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.8.
|
84
|
+
rubygems_version: 1.8.22
|
64
85
|
signing_key:
|
65
86
|
specification_version: 3
|
66
|
-
summary: Make http/https
|
87
|
+
summary: Make http/https requests easier
|
67
88
|
test_files: []
|
68
89
|
has_rdoc:
|