Module: HttpHelper
- Defined in:
- lib/unit/utils/http_helper.rb
Constant Summary collapse
- VALUES =
[:"filter[searchRadius]"].freeze
Class Method Summary collapse
- .append_key(root_key, key) ⇒ Object
- .delete(url, headers:, body: nil) ⇒ Object
- .encode(value, key = nil) ⇒ Object
- .get(url, headers:, params: nil) ⇒ Object
- .make_request(net_http, url, headers, body: nil, params: nil) ⇒ Object
- .patch(url, body:, headers:) ⇒ Object
- .post(url, headers:, body: nil) ⇒ Object
- .put(url, body:, headers:) ⇒ Object
- .response_check(response) ⇒ Object
- .value_check(value, key = nil) ⇒ Object
Class Method Details
.append_key(root_key, key) ⇒ Object
66 67 68 |
# File 'lib/unit/utils/http_helper.rb', line 66 def self.append_key(root_key, key) root_key.nil? ? key : "#{root_key}[#{key}]" end |
.delete(url, headers:, body: nil) ⇒ Object
25 26 27 |
# File 'lib/unit/utils/http_helper.rb', line 25 def self.delete(url, headers:, body: nil) make_request(Net::HTTP::Delete, url, headers, body: body) end |
.encode(value, key = nil) ⇒ Object
62 63 64 |
# File 'lib/unit/utils/http_helper.rb', line 62 def self.encode(value, key = nil) value.instance_of?(Hash) && value.key?(VALUES.map { |val| val }) ? value.map { |k, v| "#{k}=#{v}" }.join("&") : value_check(value, key) end |
.get(url, headers:, params: nil) ⇒ Object
9 10 11 |
# File 'lib/unit/utils/http_helper.rb', line 9 def self.get(url, headers:, params: nil) make_request(Net::HTTP::Get, url, headers, params: params) end |
.make_request(net_http, url, headers, body: nil, params: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/unit/utils/http_helper.rb', line 29 def self.make_request(net_http, url, headers, body: nil, params: nil) uri = params.nil? ? URI(url) : URI("#{url}?#{encode(params)}") host = uri.host.to_s port = uri.port = { use_ssl: uri.scheme == "https" } Net::HTTP.start(host, port, ) do |http| request = net_http.new uri, headers request.body = body unless body.nil? response = http.request request response.body = response_check(response) response end end |
.patch(url, body:, headers:) ⇒ Object
21 22 23 |
# File 'lib/unit/utils/http_helper.rb', line 21 def self.patch(url, body:, headers:) make_request(Net::HTTP::Patch, url, headers, body: body) end |
.post(url, headers:, body: nil) ⇒ Object
13 14 15 |
# File 'lib/unit/utils/http_helper.rb', line 13 def self.post(url, headers:, body: nil) make_request(Net::HTTP::Post, url, headers, body: body) end |
.put(url, body:, headers:) ⇒ Object
17 18 19 |
# File 'lib/unit/utils/http_helper.rb', line 17 def self.put(url, body:, headers:) make_request(Net::HTTP::Put, url, headers, body: body) end |
.response_check(response) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/unit/utils/http_helper.rb', line 44 def self.response_check(response) if response.body.include?("html") || response.body.include?("PDF") response.body else JSON.parse(response.body) end end |
.value_check(value, key = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/unit/utils/http_helper.rb', line 52 def self.value_check(value, key = nil) case value when Hash then value.map { |k, v| encode(v, append_key(key, k)) }.join("&") when Array then value.map { |v| encode(v, "#{key}[]") }.join("&") when nil then "" else "#{key}=#{CGI.escape(value.to_s)}" end end |