Module: HttpHelper

Defined in:
lib/unit/utils/http_helper.rb

Constant Summary collapse

VALUES =
[:"filter[searchRadius]"].freeze
RESPONSE_TYPES =
%w[delete image].freeze

Class Method Summary collapse

Class Method Details

.append_key(root_key, key) ⇒ Object



67
68
69
# File 'lib/unit/utils/http_helper.rb', line 67

def self.append_key(root_key, key)
  root_key.nil? ? key : "#{root_key}[#{key}]"
end

.delete(url, headers:, body: nil, response_type: nil) ⇒ Object



26
27
28
# File 'lib/unit/utils/http_helper.rb', line 26

def self.delete(url, headers:, body: nil, response_type: nil)
  make_request(Net::HTTP::Delete, url, headers, body: body, response_type: response_type)
end

.encode(value, key = nil) ⇒ Object



63
64
65
# File 'lib/unit/utils/http_helper.rb', line 63

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, response_type: nil) ⇒ Object



10
11
12
# File 'lib/unit/utils/http_helper.rb', line 10

def self.get(url, headers:, params: nil, response_type: nil)
  make_request(Net::HTTP::Get, url, headers, params: params, response_type: response_type)
end

.make_request(net_http, url, headers, body: nil, params: nil, response_type: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/unit/utils/http_helper.rb', line 30

def self.make_request(net_http, url, headers, body: nil, params: nil, response_type: nil)
  uri = params.nil? ? URI(url) : URI("#{url}?#{encode(params)}")
  host = uri.host.to_s
  port = uri.port
  options = { use_ssl: uri.scheme == "https" }

  Net::HTTP.start(host, port, options) do |http|
    request = net_http.new uri, headers
    request.body = body unless body.nil?
    response = http.request request
    response.body = response_check(response, response_type)
    response
  end
end

.patch(url, body:, headers:) ⇒ Object



22
23
24
# File 'lib/unit/utils/http_helper.rb', line 22

def self.patch(url, body:, headers:)
  make_request(Net::HTTP::Patch, url, headers, body: body)
end

.post(url, headers:, body: nil) ⇒ Object



14
15
16
# File 'lib/unit/utils/http_helper.rb', line 14

def self.post(url, headers:, body: nil)
  make_request(Net::HTTP::Post, url, headers, body: body)
end

.put(url, body:, headers:) ⇒ Object



18
19
20
# File 'lib/unit/utils/http_helper.rb', line 18

def self.put(url, body:, headers:)
  make_request(Net::HTTP::Put, url, headers, body: body)
end

.response_check(response, response_type = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/unit/utils/http_helper.rb', line 45

def self.response_check(response, response_type = nil)
  if RESPONSE_TYPES.include?(response_type) || response.body.include?("html") || response.body.include?("PDF")
    response.body
  else
    JSON.parse(response.body)
  end
end

.value_check(value, key = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/unit/utils/http_helper.rb', line 53

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