Sha256: 1c9abce108c446e117cbfb3f14be4bb2da0c9290175ba6084ae3838f476e1c26

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require "json"
require "net/https"

class Pagerduty
  # @api private
  class HttpTransport
    HOST = "events.pagerduty.com".freeze
    PORT = 443
    PATH = "/generic/2010-04-15/create_event.json".freeze

    def initialize(options = {})
      @options = options
    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(
        @options[:proxy_host],
        @options[:proxy_port],
        @options[:proxy_username],
        @options[:proxy_password],
      )
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagerduty-2.1.1 lib/pagerduty/http_transport.rb