pager_duty-connection 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b49a8ad70aa198241f8afcb8371dd05a8712ddbd
4
- data.tar.gz: 2289369bc4346458b5073f3f36c57a2499978bb5
3
+ metadata.gz: 2edadc8cc2179700bff1ceff0eab24ba5f1913cb
4
+ data.tar.gz: 8f82af15f4a0be5ecdd5e056c4f666a727fd2e69
5
5
  SHA512:
6
- metadata.gz: bfa193c9691bd50973c71827f66c811827b3f9353b3ff89bce0768bad2383e943a7328b75eb011b7920deac7a352fd89fa67f7a77733ea152ba63abd9ab62c83
7
- data.tar.gz: ba0c41b2de447447e538b9ff8ea0c3c08bd0a338df34befc87baa0ad89013b71ad08b332e319634db9fc069b6fb538c12c5c36b88e3af91d48b5fab5f8bec0a5
6
+ metadata.gz: 0e6816595e813810936fbec1ed7266f4a7de75fa44ae537bcfa70c40bb60b357f2741f4b897b3c5f30f804a4f9218e4cb4a8b1ad3ec582efe0f064bad7b19d21
7
+ data.tar.gz: 0c963e5d7238f716be86cd2b2991bd8d58c0feab04876432e56441d01b2aabc1d477fe0ad9cb39386b2176909aced13db4d146ceba8f144d2f883f2638e3acb7
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:
@@ -46,14 +48,14 @@ Working code is worth a thousand words. The basics:
46
48
 
47
49
  ```ruby
48
50
  # setup the connection
49
- pagerduty = PagerDuty::Connection.new(account, token)
51
+ pagerduty = PagerDuty::Connection.new(token, version)
50
52
 
51
- # 4 main methods: get, post, put, and delete:
53
+ # 4 main methods: `get`, `post`, `put`, and `delete`:
52
54
 
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')
55
+ response = pagerduty.get('some/relative/path', params)
56
+ response = pagerduty.post('some/relative/path', params)
57
+ response = pagerduty.delete('some/relative/path', params)
58
+ response = pagerduty.put('some/relative/path', params)
57
59
 
58
60
  # use something like irb or pry to poke around the responses
59
61
  # the contents will vary a bit between call, ie:
@@ -65,6 +67,25 @@ response = pagerduty.get('incidents/YYZ')
65
67
  response # the hash/object that represents the array
66
68
  ```
67
69
 
70
+ `get`, `post`, `put`, and `delete` all take a common parameter `params`.
71
+ This parameter contains the query parameters, body, and custom headers
72
+ needed to perform the request. Params is structured as follows:
73
+
74
+ ```ruby
75
+ params = {
76
+ query_params: {
77
+ param1: "ABCD",
78
+ ids: [ "id1", "id2", "id3" ] # Faraday takes care of encoding the arrays to be `?ids[]=id1&ids[]=id2&ids[]=id3..`
79
+ }, {
80
+ body: { ... }, # Whatever needs to be sent in a `PUT` or `POST` request body
81
+ }, {
82
+ headers: {
83
+ from: "testuser@test.com" # Some requests require a From header
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
68
89
  For more advanced and realistic examples, check out the examples directory:
69
90
 
70
91
  * [shifts-with-incidents-and-log-entries](examples/shifts-with-incidents-and-log-entries.rb)
@@ -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,64 @@ 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, 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
153
  # use token authentication: http://developer.pagerduty.com/documentation/rest/authentication
140
154
  conn.token_auth token
141
155
 
142
156
  conn.use RaiseApiErrorOnNon200
143
157
  conn.use RaiseFileNotFoundOn404
158
+ conn.use RaiseRateLimitOn429
144
159
 
145
160
  conn.use ConvertTimesParametersToISO8601
146
161
 
147
162
  # use json
148
163
  conn.request :json
164
+ conn.headers[:accept] = "application/vnd.pagerduty+json;version=#{API_VERSION}"
149
165
 
150
166
  # json back, mashify it
151
167
  conn.use ParseTimeStrings
152
168
  conn.response :mashify
153
169
  conn.response :json
170
+ conn.response :logger, ::Logger.new(STDOUT), bodies: true if debug
154
171
 
155
172
  conn.adapter Faraday.default_adapter
156
173
  end
157
174
  end
158
175
 
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
176
+ def get(path, request = {})
177
+ # paginate anything being 'get'ed, because the offset/limit isn't intuitive
178
+ request[:query_params] = {} if !request[:query_params]
179
+ page = (request[:query_params].delete(:page) || 1).to_i
180
+ limit = (request[:query_params].delete(:limit) || 100).to_i
163
181
  offset = (page - 1) * limit
182
+ request[:query_params] = request[:query_params].merge(offset: offset, limit: limit)
164
183
 
165
- run_request(:get, path, options.merge(:offset => offset, :limit => limit))
184
+ run_request(:get, path, request)
166
185
  end
167
186
 
168
- def put(path, options = {})
169
- run_request(:put, path, options)
187
+ def put(path, request = {})
188
+ run_request(:put, path, request)
170
189
  end
171
190
 
172
- def post(path, options = {})
173
- run_request(:post, path, options)
191
+ def post(path, request = {})
192
+ run_request(:post, path, request)
174
193
  end
175
194
 
176
- def delete(path, options = {})
177
- run_request(:delete, path, options)
195
+ def delete(path, request = {})
196
+ run_request(:delete, path, request)
178
197
  end
179
198
 
180
- def run_request(method, path, options)
199
+ private
200
+
201
+ def run_request(method, path, body: {}, headers: {}, query_params: {})
181
202
  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)
203
+
204
+ connection.params = query_params
205
+ response = connection.run_request(method, path, body, headers)
184
206
  response.body
185
207
  end
186
-
187
208
  end
188
209
  end
@@ -1,5 +1,5 @@
1
1
  module PagerDuty
2
2
  class Connection
3
- VERSION = "0.2.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -20,5 +20,5 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency "faraday", "~> 0.8", "< 0.10"
21
21
  gem.add_dependency "faraday_middleware", "~> 0.9.0"
22
22
  gem.add_dependency "activesupport", ">= 3.2", "< 5.0"
23
- gem.add_dependency "hashie", ">= 1.2", "< 2.2"
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.0.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: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -71,9 +71,6 @@ dependencies:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: '1.2'
74
- - - "<"
75
- - !ruby/object:Gem::Version
76
- version: '2.2'
77
74
  type: :runtime
78
75
  prerelease: false
79
76
  version_requirements: !ruby/object:Gem::Requirement
@@ -81,9 +78,6 @@ dependencies:
81
78
  - - ">="
82
79
  - !ruby/object:Gem::Version
83
80
  version: '1.2'
84
- - - "<"
85
- - !ruby/object:Gem::Version
86
- version: '2.2'
87
81
  description: Ruby API wrapper for the PagerDuty REST API
88
82
  email:
89
83
  - josh@technicalpickles.com
@@ -122,10 +116,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
116
  version: '0'
123
117
  requirements: []
124
118
  rubyforge_project:
125
- rubygems_version: 2.4.8
119
+ rubygems_version: 2.6.11
126
120
  signing_key:
127
121
  specification_version: 4
128
122
  summary: Written with the power of faraday, pager_duty-connection tries to be a simple
129
123
  and usable Ruby API wrapper for the PagerDuty REST API
130
124
  test_files: []
131
- has_rdoc: