Module: Okay::HTTP

Defined in:
lib/okay/http.rb

Overview

A wrapper around Net::HTTP, focused on ease of use and flexibility.

Constant Summary collapse

RedirectLimitError =
Class.new(StandardError)
DEFAULT_REDIRECT_LIMIT =
10

Class Method Summary collapse

Class Method Details

.get(url, parameters: {}, headers: {}) ⇒ Object

Make an HTTP GET request.

Parameters:

  • The URL to request.

  • (defaults to: {})

    A hash representing a query string.



56
57
58
# File 'lib/okay/http.rb', line 56

def self.get(url, parameters: {}, headers: {})
  send_request(:Get, url, parameters, nil, headers)
end

.post(url, data: nil, form_data: nil, headers: {}) ⇒ Object

Make an HTTP POST request.

Parameters:

  • The URL to request.

  • (defaults to: nil)

    Raw data to for the body of the request.

  • (defaults to: nil)

    Data for the request body, encoded as though it were a form.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/okay/http.rb', line 66

def self.post(url, data: nil, form_data: nil, headers: {})
  if !data.nil? && !form_data.nil?
    raise ArgumentError,
          "cannot specify data and form_data arguments simultaneously."
  end

  if form_data.nil?
    body = data
  else
    body = URI.encode_www_form(form_data)
  end

  send_request(:Post, url, nil, body, headers)
end