Module: EZHttp
- Defined in:
- lib/ez_http/core.rb,
lib/ez_http/version.rb
Overview
A wrapper for ruby net/http, supports http/https, RESTful methods, headers, certificate
Constant Summary collapse
- VERSION =
"1.0.4"
Class Method Summary collapse
- .Delete(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
- .Get(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
- .Post(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
- .Put(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
-
.Send(url, data = nil, method = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Net::HTTPResponse
Send http request to specified url and return responses.
-
.Upload(url, files, headers = nil, cert_path = nil) ⇒ Object
Upload files.
Class Method Details
.Delete(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
126 127 128 |
# File 'lib/ez_http/core.rb', line 126 def self.Delete(url, data=nil, content_type=nil, headers=nil, cert_path=nil) self.Send(url, data, "delete", content_type, headers, cert_path) end |
.Get(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
118 119 120 |
# File 'lib/ez_http/core.rb', line 118 def self.Get(url, data=nil, content_type=nil, headers=nil, cert_path=nil) self.Send(url, data, "get", content_type, headers, cert_path) end |
.Post(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
114 115 116 |
# File 'lib/ez_http/core.rb', line 114 def self.Post(url, data=nil, content_type=nil, headers=nil, cert_path=nil) self.Send(url, data, "post", content_type, headers, cert_path) end |
.Put(url, data = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Object
122 123 124 |
# File 'lib/ez_http/core.rb', line 122 def self.Put(url, data=nil, content_type=nil, headers=nil, cert_path=nil) self.Send(url, data, "put", content_type, headers, cert_path) end |
.Send(url, data = nil, method = nil, content_type = nil, headers = nil, cert_path = nil) ⇒ Net::HTTPResponse
Send http request to specified url and return responses
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ez_http/core.rb', line 23 def self.Send(url, data=nil, method=nil, content_type=nil, headers=nil, cert_path=nil) begin # Parse url url.strip! uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if (url.match(/^https/)) http.use_ssl = true end # Create request request_method = data.nil? ? "get" : "post" request_method = method || request_method request_method.strip! request_method.downcase! case request_method when "post" request = Net::HTTP::Post.new(uri.request_uri) when "get" request = Net::HTTP::Get.new(uri.request_uri) when "put" request = Net::HTTP::Put.new(uri.request_uri) when "delete" request = Net::HTTP::Delete.new(uri.request_uri) else request = Net::HTTP::Post.new(uri.request_uri) end rescue throw "Error in creating request" end # Set content-type request["content-type"] = content_type.strip! unless content_type.nil? # Set data (can be json or encoded query string) begin unless (data.nil?) if (data.class.to_s == "Hash" || data.respond_to?("merge")) request.body = data.to_json request["content-type"] = "application/json" if request["content-type"].nil? else if (data.include?("{") && data.include?("}")) request["content-type"] = "application/json" if request["content-type"].nil? end request.body = data.strip #request["content-type"] = "application/x-www-form-urlencoded" if request["content-type"].nil? end end rescue throw "Error in parsing data" end # Set headers begin unless (headers.nil?) headers.each do |header| key = header.split(':')[0] value = header.split(':')[1] request[key.strip.downcase] = value.strip end end rescue throw "Error in parsing headers" end # Add cert if any begin if (cert_path.nil?) http.verify_mode = OpenSSL::SSL::VERIFY_NONE else http.use_ssl = true pem = File.read(cert_path) http.cert = OpenSSL::X509::Certificate.new(pem) http.key = OpenSSL::PKey::RSA.new(pem) http.verify_mode = OpenSSL::SSL::VERIFY_PEER end rescue throw "Error in creating certificate" end # Send request and return response response = http.request(request) end |
.Upload(url, files, headers = nil, cert_path = nil) ⇒ Object
Upload files
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ez_http/core.rb', line 135 def self.Upload(url, files, headers=nil, cert_path=nil) begin # Parse url url.strip! uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if (url.match(/^https/)) http.use_ssl = true end # Create request request = Net::HTTP::Post.new(uri.request_uri) rescue throw "Error in creating request" end # Set headers begin unless (headers.nil?) headers.each do |header| key = header.split(':')[0] value = header.split(':')[1] request[key.strip.downcase] = value.strip end end rescue throw "Error in parsing headers" end # Create files boundry = "--content--boundry--keybegin--AaB03sfwegSFSWxGBgSBsFDYcRcRMi--keyend--" post_body = [] if (!files.nil? && files.length > 0) files.each_with_index do |file, i| post_body << "--#{boundry}\r\n" post_body << "content-disposition: form-data; name=\"upload[file#{i}]\"; filename=\"#{file["name"]}\"\r\n" post_body << "content-type: application/octet-stream\r\n" post_body << "\r\n" post_body << file["content"] + "\r\n" end post_body << "--#{boundry}--\r\n" request.body = post_body.join end # Set content type request["content-type"] = "multipart/form-data, boundary=#{boundry}" # Add cert if any begin if (cert_path.nil?) http.verify_mode = OpenSSL::SSL::VERIFY_NONE else http.use_ssl = true pem = File.read(cert_path) http.cert = OpenSSL::X509::Certificate.new(pem) http.key = OpenSSL::PKey::RSA.new(pem) http.verify_mode = OpenSSL::SSL::VERIFY_PEER end rescue throw "Error in creating certificate" end # Send request and return response response = http.request(request) end |