Class: SidekiqVigil::Notifier::HttpTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_vigil/notifier/http_transport.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Instance Method Details

#post(url, body, headers: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sidekiq_vigil/notifier/http_transport.rb', line 15

def post(url, body, headers: {})
  uri = URI.parse(url)
  raise Error, "notification URL must use HTTP or HTTPS" unless %w[http https].include?(uri.scheme)

  request = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" }.merge(headers))
  request.body = body
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.open_timeout = 5
    http.read_timeout = 10
    http.request(request)
  end
  Response.new(code: response.code, body: response.body)
rescue URI::InvalidURIError, SocketError, SystemCallError, Timeout::Error => e
  raise Error, "notification request failed (#{e.class})"
end