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.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pager_duty/connection.rb', line 123

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.



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

def api_version
  @api_version
end

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

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



165
166
167
# File 'lib/pager_duty/connection.rb', line 165

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

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



148
149
150
151
152
153
154
155
# File 'lib/pager_duty/connection.rb', line 148

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



161
162
163
# File 'lib/pager_duty/connection.rb', line 161

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

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



157
158
159
# File 'lib/pager_duty/connection.rb', line 157

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

#run_request(method, path, options) ⇒ Object



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

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