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, ParseTimeStrings, RaiseApiErrorOnNon200, RaiseFileNotFoundOn404, RaiseForbiddenOn403, RaiseRateLimitOn429, RaiseUnauthorizedOn401, RateLimitError, UnauthorizedError

Constant Summary collapse

API_VERSION =
2
API_PREFIX =
"https://api.pagerduty.com/"
VERSION =
"2.3.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.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/pager_duty/connection.rb', line 182

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
      conn.request :token_auth, token
    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.response :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



245
246
247
# File 'lib/pager_duty/connection.rb', line 245

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

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/pager_duty/connection.rb', line 219

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)

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

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



241
242
243
# File 'lib/pager_duty/connection.rb', line 241

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

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



237
238
239
# File 'lib/pager_duty/connection.rb', line 237

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