Class: Perchfall::Notify::NetHttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/perchfall/notify/net_http_client.rb

Overview

Sends a JSON payload to a URL via HTTP POST using Ruby's net/http.

This is the default http_client for Channels::Slack. Inject a fake or stub in tests — do not mock this class directly.

Constant Summary collapse

OPEN_TIMEOUT =

seconds

5
READ_TIMEOUT =

seconds

10

Instance Method Summary collapse

Instance Method Details

#post(url, payload) ⇒ Object

Parameters:

  • url (String) —

    destination URL

  • payload (Hash) —

    will be serialized to JSON

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/perchfall/notify/net_http_client.rb', line 20

def post(url, payload)
  uri = URI.parse(url)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.open_timeout = OPEN_TIMEOUT
    http.read_timeout = READ_TIMEOUT
    request = Net::HTTP::Post.new(uri.request_uri)
    request["Content-Type"] = "application/json"
    request.body = JSON.generate(payload)
    http.request(request)
  end

  return if response.is_a?(Net::HTTPSuccess)

  raise DeliveryError, "HTTP #{response.code}: #{response.body}"
rescue DeliveryError
  raise
rescue StandardError => e
  raise DeliveryError, "HTTP request failed: #{e.message}"
end