Class: Coarnotify::Http::NetHttpLayer

Inherits:
HttpLayer
  • Object
show all
Defined in:
lib/coarnotify/http.rb

Overview

Implementation of the HTTP layer using Net::HTTP. This is the default implementation used when no other implementation is supplied

Instance Method Summary collapse

Instance Method Details

#get(url, headers = {}, *args, **kwargs) ⇒ NetHttpResponse

Make an HTTP GET request to the supplied URL with the given headers

args and kwargs can be used to pass additional parameters to the Net::HTTP request, such as authentication credentials, etc.

Parameters:

  • url (String)

    the request URL

  • headers (Hash) (defaults to: {})

    HTTP headers as a hash to include in the request

  • args (Array)

    argument list (unused in this implementation)

  • kwargs (Hash)

    keyword arguments (unused in this implementation)

Returns:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/coarnotify/http.rb', line 101

def get(url, headers = {}, *args, **kwargs)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')

  request = Net::HTTP::Get.new(uri.request_uri)
  headers.each { |key, value| request[key] = value }

  response = http.request(request)
  NetHttpResponse.new(response)
end

#post(url, data, headers = {}, *args, **kwargs) ⇒ NetHttpResponse

Make an HTTP POST request to the supplied URL with the given body data, and headers

args and kwargs can be used to pass additional parameters to the Net::HTTP request, such as authentication credentials, etc.

Parameters:

  • url (String)

    the request URL

  • data (String)

    the body data

  • headers (Hash) (defaults to: {})

    HTTP headers as a hash to include in the request

  • args (Array)

    argument list (unused in this implementation)

  • kwargs (Hash)

    keyword arguments (unused in this implementation)

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/coarnotify/http.rb', line 78

def post(url, data, headers = {}, *args, **kwargs)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')

  request = Net::HTTP::Post.new(uri.request_uri)
  headers.each { |key, value| request[key] = value }
  request.body = data

  response = http.request(request)
  NetHttpResponse.new(response)
end