Class: PagerDuty::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pager_duty/connection.rb,
lib/pager_duty/connection/version.rb

Defined Under Namespace

Classes: ApiError, ConvertTimesParametersToISO8601, FileNotFoundError, ForbiddenError, Mashify, ParseTimeStrings, RaiseApiErrorOnNon200, RaiseFileNotFoundOn404, RaiseForbiddenOn403, RaiseRateLimitOn429, RaiseUnauthorizedOn401, RateLimitError, UnauthorizedError

Constant Summary collapse

API_VERSION =
2
API_PREFIX =
"https://api.pagerduty.com/"
VERSION =
"3.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, token_type: :Token, url: API_PREFIX, debug: false) ⇒ Connection

Returns a new instance of Connection.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pager_duty/connection.rb', line 203

def initialize(token, token_type: :Token, url: API_PREFIX, debug: false)
  @connection = Faraday.new do |conn|
    conn.url_prefix = url

    case token_type
    when :Token
      if faraday_v1?
        conn.request :token_auth, token
      else
        conn.request :authorization, 'Token', token
      end
    when :Bearer
      conn.request :authorization, 'Bearer', token
    else raise ArgumentError, "invalid token_type: #{token_type.inspect}"
    end

    conn.use ConvertTimesParametersToISO8601

    # use json
    conn.request :json
    conn.headers[:accept] = "application/vnd.pagerduty+json;version=#{API_VERSION}"

    # json back, mashify it
    conn.use ParseTimeStrings
    conn.use Mashify
    conn.response :json
    conn.response :logger, ::Logger.new(STDOUT), bodies: true if debug

    # Because Faraday::Middleware executes in reverse order of
    # calls to conn.use, status code error handling goes at the
    # end of the block so that it runs first
    conn.use RaiseApiErrorOnNon200
    conn.use RaiseFileNotFoundOn404
    conn.use RaiseRateLimitOn429
    conn.use RaiseForbiddenOn403
    conn.use RaiseUnauthorizedOn401

    conn.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/pager_duty/connection.rb', line 10

def connection
  @connection
end

Instance Method Details

#delete(path, request = {}) ⇒ Object



271
272
273
# File 'lib/pager_duty/connection.rb', line 271

def delete(path, request = {})
  run_request(:delete, path, **request)
end

#get(path, request = {}) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/pager_duty/connection.rb', line 244

def get(path, request = {})
  # The run_request() method body argument defaults to {}, which is incorrect for GET requests
  # https://github.com/technicalpickles/pager_duty-connection/issues/56
  # NOTE: PagerDuty support discourages GET requests with bodies, but not throwing an ArgumentError to prevent breaking
  #   corner-case implementations.
  request[:body] = nil if !request[:body]

  # paginate anything being 'get'ed, because the offset/limit isn't intuitive
  request[:query_params] = {} if !request[:query_params]
  page = request[:query_params].fetch(:page, 1).to_i
  limit = request[:query_params].fetch(:limit, 100).to_i
  offset = (page - 1) * limit

  query_params = request[:query_params].merge(offset: offset, limit: limit)
  query_params.delete(:page)

  run_request(:get, path, **request.merge(query_params: query_params))
end

#post(path, request = {}) ⇒ Object



267
268
269
# File 'lib/pager_duty/connection.rb', line 267

def post(path, request = {})
  run_request(:post, path, **request)
end

#put(path, request = {}) ⇒ Object



263
264
265
# File 'lib/pager_duty/connection.rb', line 263

def put(path, request = {})
  run_request(:put, path, **request)
end