Class: PdEventV2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pd_event_v2/client.rb

Constant Summary collapse

INITIALIZE_OPTIONS =
%i[
  routing_key
  debug
].freeze
DEFAULT_ADAPTERS =
[
  Faraday::Adapter::NetHttp,
  Faraday::Adapter::Test,
].freeze
USER_AGENT =
"PagerDuty Events API v2 Ruby Client #{PdEventV2::VERSION}"
DEFAULT_API_URL =
'https://events.pagerduty.com/v2/enqueue'
EVENT_ACTIONS =
%i[
  trigger
  acknowledge
  resolve
].freeze
PAYLOAD_KEYS =
{
  summary: true,
  source: true,
  severity: true,
  timestamp: false,
  component: false,
  group: false,
  class: false,
  custom_details: false,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pd_event_v2/client.rb', line 39

def initialize(options)
  unless options.is_a?(Hash)
    raise ArgumentError, "wrong type argument (given: #{options}:#{options.class}, expected Hash)"
  end

  @options = {}

  INITIALIZE_OPTIONS.each do |key|
    @options[key] = options.delete(key)
  end

  raise ArgumentError, ':routing_key is required for initialize' unless @options[:routing_key]

  options[:url] ||= DEFAULT_API_URL

  @conn = Faraday.new(options) do |faraday|
    faraday.request :url_encoded
    faraday.response :json, content_type: /\bjson\b/
    faraday.response :raise_error
    faraday.response :logger, ::Logger.new(STDOUT), bodies: true if @options[:debug]

    yield(faraday) if block_given?

    unless DEFAULT_ADAPTERS.any? { |i| faraday.builder.handlers.include?(i) }
      faraday.adapter Faraday.default_adapter
    end
  end

  @conn.headers[:user_agent] = USER_AGENT
end