Module: Motor::NetHttpUtils

Defined in:
lib/motor/net_http_utils.rb

Class Method Summary collapse

Class Method Details

.build_request(method_class, url, params, headers, body) ⇒ Object


19
20
21
22
23
24
25
26
27
28
# File 'lib/motor/net_http_utils.rb', line 19

def build_request(method_class, url, params, headers, body)
  uri = URI(url)
  uri.query = params.to_query

  request = method_class.new(uri)
  request.body = body if body.present?
  headers.each { |key, value| request[key] = value }

  request
end

.execute_request(request) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/motor/net_http_utils.rb', line 30

def execute_request(request)
  uri = request.uri

  Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.port == 443) do |http|
    http.request(request)
  end
end

.get(url, params = {}, headers = {}) ⇒ Object


7
8
9
10
11
# File 'lib/motor/net_http_utils.rb', line 7

def get(url, params = {}, headers = {})
  request = build_request(Net::HTTP::Get, url, params, headers, nil)

  execute_request(request)
end

.post(url, params = {}, headers = {}, body = '') ⇒ Object


13
14
15
16
17
# File 'lib/motor/net_http_utils.rb', line 13

def post(url, params = {}, headers = {}, body = '')
  request = build_request(Net::HTTP::Post, url, params, headers, body)

  execute_request(request)
end