Sha256: 73b40dcacf49070d5c31955a8b5511c7b157ccc6dcf666077cb73a23f8167269

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "json"
require "net/https"

module Pagerduty
  # @private
  class HttpTransport
    HOST = "events.pagerduty.com"
    PORT = 443
    private_constant :HOST, :PORT

    def initialize(config)
      @path = config.fetch(:path)
      @proxy = config[:proxy] || {}
    end

    def send_payload(payload)
      response = post(payload.to_json)
      response.error! unless transported?(response)
      JSON.parse(response.body)
    end

    private

    def post(payload)
      post = Net::HTTP::Post.new(@path)
      post.body = payload
      http.request(post)
    end

    def http
      http = http_proxy.new(HOST, PORT)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.open_timeout = 60
      http.read_timeout = 60
      http
    end

    def http_proxy
      Net::HTTP.Proxy(
        @proxy[:host],
        @proxy[:port],
        @proxy[:username],
        @proxy[:password],
      )
    end

    def transported?(response)
      response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagerduty-4.0.1 lib/pagerduty/http_transport.rb
pagerduty-4.0.0 lib/pagerduty/http_transport.rb
pagerduty-3.0.0 lib/pagerduty/http_transport.rb