Module: HTTPHelper
- Defined in:
- lib/helpers/http/http_helper.rb
Overview
A module for simplifying HTTP requests
Class Method Summary collapse
-
.delete(url, data) ⇒ Object
Makes a DELETE request to the specified URL.
-
.get(url) ⇒ Object
Makes a GET request to the specified URL.
-
.post_form(url, data) ⇒ Object
POSTs the data as a form to the specified URL.
-
.post_multipart_data(url, data) ⇒ Object
Takes an array of arrays and POSTs them as multipart form data to the specified URL.
-
.put(url, data) ⇒ Object
Makes a PUT request to the specified URL.
Class Method Details
.delete(url, data) ⇒ Object
Makes a DELETE request to the specified URL.
If the given URL is a string, it gets converted to a URI.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/helpers/http/http_helper.rb', line 87 def self.delete(url, data) raise "No form data provided!" unless data url = URI(url) unless url.class == URI::Generic http = Net::HTTP.new(url.host, url.port) http.use_ssl = true req = Net::HTTP::Delete.new(url, { 'Content-Type' => 'application/x-www-form-urlencoded' }) req.body = URI::encode_www_form(data) res = http.request(req) ret = res.body ret = JSON.parse(ret) ret end |
.get(url) ⇒ Object
Makes a GET request to the specified URL.
If the given URL is a string, it gets converted to a URI.
11 12 13 14 15 16 17 18 19 |
# File 'lib/helpers/http/http_helper.rb', line 11 def self.get(url) url = URI(url) unless url.class == URI::Generic res = Net::HTTP.get_response(url) ret = res.body ret = JSON.parse(ret) ret end |
.post_form(url, data) ⇒ Object
POSTs the data as a form to the specified URL.
If the given URL is a string, it gets converted to a URI.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/helpers/http/http_helper.rb', line 26 def self.post_form(url, data) raise "No form data provided!" unless data url = URI(url) unless url.class == URI::Generic res = Net::HTTP.post_form(url, data) ret = res.body ret = JSON.parse(ret) ret end |
.post_multipart_data(url, data) ⇒ Object
Takes an array of arrays and POSTs them as multipart form data to the specified URL.
If the given URL is a string, it gets converted to a URI.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/helpers/http/http_helper.rb', line 43 def self.post_multipart_data(url, data) raise "No form data provided!" unless data url = URI(url) unless url.class == URI::Generic http = Net::HTTP.new(url.host, url.port) http.use_ssl = true req = Net::HTTP::Post.new(url) req.set_form data, 'multipart/form-data' res = http.request(req) ret = res.body ret = JSON.parse(ret) ret end |
.put(url, data) ⇒ Object
Makes a PUT request to the specified URL.
If the given URL is a string, it gets converted to a URI.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/helpers/http/http_helper.rb', line 65 def self.put(url, data) raise "No form data provided!" unless data url = URI(url) unless url.class == URI::Generic http = Net::HTTP.new(url.host, url.port) http.use_ssl = true req = Net::HTTP::Put.new(url, { 'Content-Type' => 'application/x-www-form-urlencoded' }) req.body = URI::encode_www_form(data) res = http.request(req) ret = res.body ret = JSON.parse(ret) ret end |