pager_duty-connection 0.2.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b49a8ad70aa198241f8afcb8371dd05a8712ddbd
4
- data.tar.gz: 2289369bc4346458b5073f3f36c57a2499978bb5
2
+ SHA256:
3
+ metadata.gz: 4c392576dc99307e392b659a3bcdc08a71e93b7a9d1e4d1c1f2d0d1757c78cec
4
+ data.tar.gz: 710475fc120cf3d3ae18f4eeaeebfbc78d878c80aacc612acbc72fc759405fe5
5
5
  SHA512:
6
- metadata.gz: bfa193c9691bd50973c71827f66c811827b3f9353b3ff89bce0768bad2383e943a7328b75eb011b7920deac7a352fd89fa67f7a77733ea152ba63abd9ab62c83
7
- data.tar.gz: ba0c41b2de447447e538b9ff8ea0c3c08bd0a338df34befc87baa0ad89013b71ad08b332e319634db9fc069b6fb538c12c5c36b88e3af91d48b5fab5f8bec0a5
6
+ metadata.gz: 22907cfe9371c5767c9341037a14a0df90e5d410faa2302973295a70a8a10d9453f565c3f5d10986d96976d11a6e0d59598e3a8035b2ef391ced4e2f5ef13722
7
+ data.tar.gz: f0e3c25d24760085ebdaa6dc0c3838e85cc7ead08a12e75e1aa113f8f2be0aa0a2de5e736348a5faf249339cb5cf0759fc5abc492062f287599d2030f0509767
data/README.md CHANGED
@@ -25,6 +25,8 @@ And this is what it doesn't do:
25
25
  * have methods for individual API calls that are possible (ie `find_incident`, `list_users`, etc)
26
26
  * provide [will_paginate](https://github.com/mislav/will_paginate) or [kaminari](https://github.com/amatsuda/kaminari) paginated arrays (They aren't super documented for building a library that works well with them, and have different APIs)
27
27
 
28
+ **Note**: v1 of the Pager Duty REST API is no longer supported with this gem. Please either upgrade to v2 of the API [(v2 Migration Documentation)](https://v2.developer.pagerduty.com/docs/migrating-to-api-v2) or do not upgrade past version [0.2.0 of this gem](https://github.com/technicalpickles/pager_duty-connection/tree/v0.2.0).
29
+
28
30
  ## Installation
29
31
 
30
32
  Add this line to your application's Gemfile:
@@ -45,15 +47,18 @@ Or install it yourself as:
45
47
  Working code is worth a thousand words. The basics:
46
48
 
47
49
  ```ruby
48
- # setup the connection
49
- pagerduty = PagerDuty::Connection.new(account, token)
50
+ # setup the connection with API token
51
+ pagerduty = PagerDuty::Connection.new(token)
52
+
53
+ # setup the connection with OAuth2 token
54
+ pagerduty = PagerDuty::Connection.new(token, token_type: :Bearer)
50
55
 
51
- # 4 main methods: get, post, put, and delete:
56
+ # 4 main methods: `get`, `post`, `put`, and `delete`:
52
57
 
53
- response = pagerduty.get('some/relative/path', :some => 'request', :parameter => 'to pass')
54
- response = pagerduty.post('some/relative/path', :some => 'request', :parameter => 'to pass')
55
- response = pagerduty.delete('some/relative/path', :some => 'request', :parameter => 'to pass')
56
- response = pagerduty.put('some/relative/path', :some => 'request', :parameter => 'to pass')
58
+ response = pagerduty.get('some/relative/path', params)
59
+ response = pagerduty.post('some/relative/path', params)
60
+ response = pagerduty.delete('some/relative/path', params)
61
+ response = pagerduty.put('some/relative/path', params)
57
62
 
58
63
  # use something like irb or pry to poke around the responses
59
64
  # the contents will vary a bit between call, ie:
@@ -65,6 +70,25 @@ response = pagerduty.get('incidents/YYZ')
65
70
  response # the hash/object that represents the array
66
71
  ```
67
72
 
73
+ `get`, `post`, `put`, and `delete` all take a common parameter `params`.
74
+ This parameter contains the query parameters, body, and custom headers
75
+ needed to perform the request. Params is structured as follows:
76
+
77
+ ```ruby
78
+ params = {
79
+ query_params: {
80
+ param1: "ABCD",
81
+ ids: [ "id1", "id2", "id3" ] # Faraday takes care of encoding the arrays to be `?ids[]=id1&ids[]=id2&ids[]=id3..`
82
+ }, {
83
+ body: { ... }, # Whatever needs to be sent in a `PUT` or `POST` request body
84
+ }, {
85
+ headers: {
86
+ from: "testuser@test.com" # Some requests require a From header
87
+ }
88
+ }
89
+ }
90
+ ```
91
+
68
92
  For more advanced and realistic examples, check out the examples directory:
69
93
 
70
94
  * [shifts-with-incidents-and-log-entries](examples/shifts-with-incidents-and-log-entries.rb)
@@ -75,7 +99,7 @@ In general, you can get/put/post/delete a path, with some attributes. Use the [R
75
99
  If you are working in Rails, and using only a single PagerDuty account, you'll probably want an initializer:
76
100
 
77
101
  ```ruby
78
- $pagerduty = PagerDuty::Connection.new('your-subdomain', 'your-token')
102
+ $pagerduty = PagerDuty::Connection.new('your-token')
79
103
  ```
80
104
 
81
105
  And if you are using [dotenv](https://github.com/bkeepers/dotenv), you can use environment variables, and stash them in .env:
@@ -1,17 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'pry'
4
3
  require 'dotenv'
5
4
  Dotenv.load ".env.development", '.env'
6
5
 
7
- account = ENV['PAGERDUTY_ACCOUNT'] || raise("Missing ENV['PAGERDUTY_ACCOUNT'], add to .env.development")
8
6
  token = ENV['PAGERDUTY_TOKEN'] || raise("Missing ENV['PAGERDUTY_TOKEN'], add to .env.development")
9
7
 
10
8
  require 'pager_duty/connection'
11
- $pagerduty = PagerDuty::Connection.new(account, token)
9
+ $pagerduty = PagerDuty::Connection.new(token)
12
10
 
13
- # http://developer.pagerduty.com/documentation/rest/users/list
11
+ # https://v2.developer.pagerduty.com/v2/page/api-reference#!/Users/get_users
14
12
  response = $pagerduty.get('users')
15
- response.users.each do |user|
16
- puts "#{user.name}: #{user.email}"
13
+ response['users'].each do |user|
14
+ puts "#{user['name']}: #{user['email']}"
17
15
  end
@@ -3,11 +3,10 @@
3
3
  require 'dotenv'
4
4
  Dotenv.load ".env.development", '.env'
5
5
 
6
- account = ENV['PAGERDUTY_ACCOUNT'] || raise("Missing ENV['PAGERDUTY_ACCOUNT'], add to .env.development")
7
6
  token = ENV['PAGERDUTY_TOKEN'] || raise("Missing ENV['PAGERDUTY_TOKEN'], add to .env.development")
8
7
 
9
8
  require 'pager_duty/connection'
10
- $pagerduty = PagerDuty::Connection.new(account, token)
9
+ $pagerduty = PagerDuty::Connection.new(token)
11
10
 
12
11
  schedule_id = ENV['PAGERDUTY_SCHEDULE_ID'] || raise("Missing ENV['PAGERDUTY_SCHEDULE_ID'], add to .env.development")
13
12
 
@@ -15,33 +14,33 @@ schedule_id = ENV['PAGERDUTY_SCHEDULE_ID'] || raise("Missing ENV['PAGERDUTY_SCHE
15
14
  time_since = 1.day.ago
16
15
  time_until = Time.now
17
16
 
18
- # http://developer.pagerduty.com/documentation/rest/schedules/entries
19
- response = $pagerduty.get("schedules/#{schedule_id}/entries", 'since' => time_since, 'until' => time_until, :overflow => true)
17
+ # https://v2.developer.pagerduty.com/v2/page/api-reference#!/On-Calls/get_oncalls
18
+ response = $pagerduty.get("oncalls", query_params: { since: time_since, until: time_until, schedule_ids: [schedule_id] })
20
19
 
21
- entries = response['entries'] # note, probably a bug, but response.entries doesn't do what you think it does
20
+ entries = response['oncalls']
22
21
 
23
22
  entries.each do |entry|
24
- puts "#{entry.start} - #{entry.end}: #{entry.user.name}"
23
+ puts "#{entry['start']} - #{entry['end']}: #{entry['user']['summary']}"
25
24
 
26
25
  # find incidents during that shift
27
- # http://developer.pagerduty.com/documentation/rest/incidents/list
28
- response = $pagerduty.get('incidents', :since => entry['start'], :until => entry['end'])
26
+ # https://v2.developer.pagerduty.com/v2/page/api-reference#!/Incidents/get_incidents
27
+ response = $pagerduty.get('incidents', query_params: { since: entry['start'], until: entry['end'], user_ids: [entry['user']['id']] })
29
28
 
30
- response.incidents.each do |incident|
29
+ response['incidents'].each do |incident|
31
30
  puts "\t#{incident.id}"
32
31
 
33
32
  # find log entries (acknowledged, notifications, etc) for incident:
34
- # http://developer.pagerduty.com/documentation/rest/log_entries/incident_log_entries
33
+ # https://v2.developer.pagerduty.com/v2/page/api-reference#!/Incidents/get_incidents_id_log_entries
35
34
  response = $pagerduty.get("incidents/#{incident.id}/log_entries")
36
35
 
37
36
  # select just the notes
38
- notes = response.log_entries.select do |log_entry|
39
- log_entry.channel && log_entry.channel.type == 'note'
37
+ notes = response['log_entries'].select do |log_entry|
38
+ log_entry['channel'] && log_entry['channel']['type'] == 'note'
40
39
  end
41
40
 
42
41
  # and print them out:
43
42
  notes.each do |log_entry|
44
- puts "\t\t#{log_entry.channel.summary}"
43
+ puts "\t\t#{log_entry['channel']['summary']}"
45
44
  end
46
45
  end
47
46
  end
@@ -8,7 +8,8 @@ module PagerDuty
8
8
 
9
9
  class Connection
10
10
  attr_accessor :connection
11
- attr_accessor :api_version
11
+
12
+ API_VERSION = 2
12
13
 
13
14
  class FileNotFoundError < RuntimeError
14
15
  end
@@ -16,6 +17,9 @@ module PagerDuty
16
17
  class ApiError < RuntimeError
17
18
  end
18
19
 
20
+ class RateLimitError < RuntimeError
21
+ end
22
+
19
23
  class RaiseFileNotFoundOn404 < Faraday::Middleware
20
24
  def call(env)
21
25
  response = @app.call env
@@ -45,6 +49,17 @@ module PagerDuty
45
49
  end
46
50
  end
47
51
 
52
+ class RaiseRateLimitOn429 < Faraday::Middleware
53
+ def call(env)
54
+ response = @app.call env
55
+ if response.status == 429
56
+ raise RateLimitError, response.env[:url].to_s
57
+ end
58
+
59
+ response
60
+ end
61
+ end
62
+
48
63
  class ConvertTimesParametersToISO8601 < Faraday::Middleware
49
64
  TIME_KEYS = [:since, :until]
50
65
  def call(env)
@@ -56,7 +71,7 @@ module PagerDuty
56
71
  end
57
72
  end
58
73
 
59
- response = @app.call env
74
+ @app.call env
60
75
  end
61
76
  end
62
77
 
@@ -131,58 +146,69 @@ module PagerDuty
131
146
  end
132
147
  end
133
148
 
134
- def initialize(account, token, api_version = 1)
135
- @api_version = api_version
149
+ def initialize(token, token_type: :Token, debug: false)
136
150
  @connection = Faraday.new do |conn|
137
- conn.url_prefix = "https://#{account}.pagerduty.com/api/v#{api_version}"
151
+ conn.url_prefix = "https://api.pagerduty.com/"
138
152
 
139
- # use token authentication: http://developer.pagerduty.com/documentation/rest/authentication
140
- conn.token_auth token
153
+ token_arg =
154
+ case token_type
155
+ when :Token then { token: token }
156
+ when :Bearer then token
157
+ else raise ArgumentError, "invalid token_type: #{token_type.inspect}"
158
+ end
159
+ conn.authorization(token_type, token_arg)
141
160
 
142
161
  conn.use RaiseApiErrorOnNon200
143
162
  conn.use RaiseFileNotFoundOn404
163
+ conn.use RaiseRateLimitOn429
144
164
 
145
165
  conn.use ConvertTimesParametersToISO8601
146
166
 
147
167
  # use json
148
168
  conn.request :json
169
+ conn.headers[:accept] = "application/vnd.pagerduty+json;version=#{API_VERSION}"
149
170
 
150
171
  # json back, mashify it
151
172
  conn.use ParseTimeStrings
152
173
  conn.response :mashify
153
174
  conn.response :json
175
+ conn.response :logger, ::Logger.new(STDOUT), bodies: true if debug
154
176
 
155
177
  conn.adapter Faraday.default_adapter
156
178
  end
157
179
  end
158
180
 
159
- def get(path, options = {})
160
- # paginate anything being 'get'ed, because the offset/limit isn't intutive
161
- page = (options.delete(:page) || 1).to_i
162
- limit = (options.delete(:limit) || 100).to_i
181
+ def get(path, request = {})
182
+ # paginate anything being 'get'ed, because the offset/limit isn't intuitive
183
+ request[:query_params] = {} if !request[:query_params]
184
+ page = (request[:query_params].delete(:page) || 1).to_i
185
+ limit = (request[:query_params].delete(:limit) || 100).to_i
163
186
  offset = (page - 1) * limit
187
+ request[:query_params] = request[:query_params].merge(offset: offset, limit: limit)
164
188
 
165
- run_request(:get, path, options.merge(:offset => offset, :limit => limit))
189
+ run_request(:get, path, **request)
166
190
  end
167
191
 
168
- def put(path, options = {})
169
- run_request(:put, path, options)
192
+ def put(path, request = {})
193
+ run_request(:put, path, **request)
170
194
  end
171
195
 
172
- def post(path, options = {})
173
- run_request(:post, path, options)
196
+ def post(path, request = {})
197
+ run_request(:post, path, **request)
174
198
  end
175
199
 
176
- def delete(path, options = {})
177
- run_request(:delete, path, options)
200
+ def delete(path, request = {})
201
+ run_request(:delete, path, **request)
178
202
  end
179
203
 
180
- def run_request(method, path, options)
204
+ private
205
+
206
+ def run_request(method, path, body: {}, headers: {}, query_params: {})
181
207
  path = path.gsub(/^\//, '') # strip leading slash, to make sure relative things happen on the connection
182
- headers = nil
183
- response = connection.run_request(method, path, options, headers)
208
+
209
+ connection.params = query_params
210
+ response = connection.run_request(method, path, body, headers)
184
211
  response.body
185
212
  end
186
-
187
213
  end
188
214
  end
@@ -1,5 +1,5 @@
1
1
  module PagerDuty
2
2
  class Connection
3
- VERSION = "0.2.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -17,8 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency "faraday", "~> 0.8", "< 0.10"
21
- gem.add_dependency "faraday_middleware", "~> 0.9.0"
22
- gem.add_dependency "activesupport", ">= 3.2", "< 5.0"
23
- gem.add_dependency "hashie", ">= 1.2", "< 2.2"
20
+ gem.add_dependency "faraday", "~> 0.8", "< 1.0"
21
+ gem.add_dependency "faraday_middleware", "~> 0.8", "< 1.0"
22
+ gem.add_dependency "activesupport", ">= 3.2", "< 7.0"
23
+ gem.add_dependency "hashie", ">= 1.2"
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pager_duty-connection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Nichols
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0.8'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '0.10'
22
+ version: '1.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,21 +29,27 @@ dependencies:
29
29
  version: '0.8'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '0.10'
32
+ version: '1.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: faraday_middleware
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.9.0
39
+ version: '0.8'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.0'
40
43
  type: :runtime
41
44
  prerelease: false
42
45
  version_requirements: !ruby/object:Gem::Requirement
43
46
  requirements:
44
47
  - - "~>"
45
48
  - !ruby/object:Gem::Version
46
- version: 0.9.0
49
+ version: '0.8'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.0'
47
53
  - !ruby/object:Gem::Dependency
48
54
  name: activesupport
49
55
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +59,7 @@ dependencies:
53
59
  version: '3.2'
54
60
  - - "<"
55
61
  - !ruby/object:Gem::Version
56
- version: '5.0'
62
+ version: '7.0'
57
63
  type: :runtime
58
64
  prerelease: false
59
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -63,7 +69,7 @@ dependencies:
63
69
  version: '3.2'
64
70
  - - "<"
65
71
  - !ruby/object:Gem::Version
66
- version: '5.0'
72
+ version: '7.0'
67
73
  - !ruby/object:Gem::Dependency
68
74
  name: hashie
69
75
  requirement: !ruby/object:Gem::Requirement
@@ -71,9 +77,6 @@ dependencies:
71
77
  - - ">="
72
78
  - !ruby/object:Gem::Version
73
79
  version: '1.2'
74
- - - "<"
75
- - !ruby/object:Gem::Version
76
- version: '2.2'
77
80
  type: :runtime
78
81
  prerelease: false
79
82
  version_requirements: !ruby/object:Gem::Requirement
@@ -81,9 +84,6 @@ dependencies:
81
84
  - - ">="
82
85
  - !ruby/object:Gem::Version
83
86
  version: '1.2'
84
- - - "<"
85
- - !ruby/object:Gem::Version
86
- version: '2.2'
87
87
  description: Ruby API wrapper for the PagerDuty REST API
88
88
  email:
89
89
  - josh@technicalpickles.com
@@ -121,11 +121,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.4.8
124
+ rubygems_version: 3.0.3
126
125
  signing_key:
127
126
  specification_version: 4
128
127
  summary: Written with the power of faraday, pager_duty-connection tries to be a simple
129
128
  and usable Ruby API wrapper for the PagerDuty REST API
130
129
  test_files: []
131
- has_rdoc: