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, ParseTimeStrings, RaiseApiErrorOnNon200, RaiseFileNotFoundOn404

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, token, api_version = 1) ⇒ Connection

Returns a new instance of Connection.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pager_duty/connection.rb', line 134

def initialize(, token, api_version = 1)
  @api_version = api_version
  @connection = Faraday.new do |conn|
    conn.url_prefix = "https://#{}.pagerduty.com/api/v#{api_version}"

    # use token authentication: http://developer.pagerduty.com/documentation/rest/authentication
    conn.token_auth token

    conn.use RaiseApiErrorOnNon200
    conn.use RaiseFileNotFoundOn404

    conn.use ConvertTimesParametersToISO8601

    # use json
    conn.request :json

    # json back, mashify it
    conn.use ParseTimeStrings
    conn.response :mashify
    conn.response :json

    conn.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#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, options = {}) ⇒ Object



176
177
178
# File 'lib/pager_duty/connection.rb', line 176

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

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



159
160
161
162
163
164
165
166
# File 'lib/pager_duty/connection.rb', line 159

def get(path, options = {})
  # paginate anything being 'get'ed, because the offset/limit isn't intutive
  page = (options.delete(:page) || 1).to_i
  limit = (options.delete(:limit) || 100).to_i
  offset = (page - 1) * limit

  run_request(:get, path, options.merge(:offset => offset, :limit => limit))
end

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



172
173
174
# File 'lib/pager_duty/connection.rb', line 172

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

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



168
169
170
# File 'lib/pager_duty/connection.rb', line 168

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

#run_request(method, path, options) ⇒ Object



180
181
182
183
184
185
# File 'lib/pager_duty/connection.rb', line 180

def run_request(method, path, options)
  path = path.gsub(/^\//, '') # strip leading slash, to make sure relative things happen on the connection
  headers = nil
  response = connection.run_request(method, path, options, headers)
  response.body
end