pagerduty 1.4.0 → 1.4.1

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
2
  SHA1:
3
- metadata.gz: 8a21cba28253d63d4b26aecb45783434ae59fa83
4
- data.tar.gz: 44a5dc31dbc4e08d30b3e5253d6ffd37c23a2996
3
+ metadata.gz: b35425e697bdee8a4b3217052d579505b4655fc5
4
+ data.tar.gz: 4861fe46da3e2c532a0b351e9519e1e7ec2323ff
5
5
  SHA512:
6
- metadata.gz: d4ad387665a715603f984b707987e126f9f90436d5f19ba690f4596cc0f75eb14dcbc8e5b6c3a85b7d74df21c769d177271e458f9034ea72c9c7315f726bcba6
7
- data.tar.gz: 7599fd83260348840ea8ce61dedb6e982ce0fab3b3b7467913f86c997bc91b7cf419d3300c9c4bf96baef6e6c9042010dec540469e509fa636105a0ca1d3d0c8
6
+ metadata.gz: 6eb7f43eeaefad73c18f9e285258ebbc4c2381cd5478445f61e5102ed10f4c95fdd1319e4c8cc3950cf3b54b03ff2639b44eedc8917a0159948bdc4be767bbf0
7
+ data.tar.gz: 4472c21d7aac15ac890ac87a565414aabd573e0f7895d8339cc3b34fe587da915320caa208edc52891285a7c7b0a2d180e8b48d174f02a86a274d192b5b496d2
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ rdoc
6
6
  *.swo
7
7
  pkg
8
8
  .bundle
9
+ /bin
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
5
+ - 1.9.3
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  pagerduty
2
2
  =========
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/pagerduty.svg)](http://badge.fury.io/rb/pagerduty)
5
+ [![Build Status](https://travis-ci.org/envato/pagerduty.svg?branch=master)](https://travis-ci.org/envato/pagerduty)
6
+
4
7
  Provides a simple interface for calling into the [Pagerduty](http://pagerduty.com) API.
5
8
 
6
9
  Installation
data/Rakefile CHANGED
@@ -1,6 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rdoc/task"
4
+
5
+ task :default => :spec
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ t.verbose = false
9
+ end
2
10
 
3
- require 'rdoc/task'
4
11
  RDoc::Task.new do |rdoc|
5
12
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
6
13
 
@@ -9,4 +16,3 @@ RDoc::Task.new do |rdoc|
9
16
  rdoc.rdoc_files.include('README*')
10
17
  rdoc.rdoc_files.include('lib/**/*.rb')
11
18
  end
12
-
@@ -1,7 +1,5 @@
1
- require 'json'
2
- require 'net/http'
3
- require 'net/https'
4
1
  require 'pagerduty/version'
2
+ require 'pagerduty/http_transport'
5
3
 
6
4
  class PagerdutyException < Exception
7
5
  attr_reader :pagerduty_instance, :api_response
@@ -23,7 +21,7 @@ class Pagerduty
23
21
 
24
22
  def trigger(description, details = {})
25
23
  resp = api_call("trigger", description, details)
26
- throw PagerdutyException.new(self, resp) unless resp["status"] == "success"
24
+ raise PagerdutyException.new(self, resp) unless resp["status"] == "success"
27
25
 
28
26
  PagerdutyIncident.new @service_key, resp["incident_key"]
29
27
  end
@@ -33,34 +31,23 @@ class Pagerduty
33
31
  end
34
32
 
35
33
  protected
36
- def api_call(event_type, description, details = {})
37
- params = { :event_type => event_type, :service_key => @service_key, :description => description, :details => details }
38
- params.merge!({ :incident_key => @incident_key }) unless @incident_key == nil
39
-
40
- url = URI.parse("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
41
- http = Net::HTTP.new(url.host, url.port)
42
- http.use_ssl = (url.scheme == 'https')
43
- rootca = '/etc/ssl/certs'
44
- if (File.directory?(rootca) && http.use_ssl?)
45
- http.ca_path = rootca
46
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
47
- http.verify_depth = 5
48
- else
49
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
- end
51
34
 
52
- req = Net::HTTP::Post.new(url.request_uri)
53
- req.body = JSON.generate(params)
35
+ def api_call(event_type, description, details = {})
36
+ params = {
37
+ :event_type => event_type,
38
+ :service_key => @service_key,
39
+ :description => description,
40
+ :details => details
41
+ }
42
+ params[:incident_key] = @incident_key if @incident_key
43
+ Pagerduty.transport.send(params)
44
+ end
54
45
 
55
- res = http.request(req)
56
- case res
57
- when Net::HTTPSuccess, Net::HTTPRedirection
58
- JSON.parse(res.body)
59
- else
60
- res.error!
46
+ class << self
47
+ def transport
48
+ Pagerduty::HttpTransport
61
49
  end
62
50
  end
63
-
64
51
  end
65
52
 
66
53
  class PagerdutyIncident < Pagerduty
@@ -72,14 +59,14 @@ class PagerdutyIncident < Pagerduty
72
59
 
73
60
  def acknowledge(description, details = {})
74
61
  resp = api_call("acknowledge", description, details)
75
- throw PagerdutyException.new(self, resp) unless resp["status"] == "success"
62
+ raise PagerdutyException.new(self, resp) unless resp["status"] == "success"
76
63
 
77
64
  self
78
65
  end
79
66
 
80
67
  def resolve(description, details = {})
81
68
  resp = api_call("resolve", description, details)
82
- throw PagerdutyException.new(self, resp) unless resp["status"] == "success"
69
+ raise PagerdutyException.new(self, resp) unless resp["status"] == "success"
83
70
 
84
71
  self
85
72
  end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ module Pagerduty::HttpTransport
7
+ class << self
8
+
9
+ HOST = "events.pagerduty.com"
10
+ PORT = 443
11
+ PATH = "/generic/2010-04-15/create_event.json"
12
+
13
+ def send(payload = {})
14
+ response = post payload.to_json
15
+ response.error! unless transported?(response)
16
+ JSON.parse(response.body)
17
+ end
18
+
19
+ private
20
+
21
+ def post(payload)
22
+ post = Net::HTTP::Post.new(PATH)
23
+ post.body = payload
24
+ http.request(post)
25
+ end
26
+
27
+ def http
28
+ http = Net::HTTP.new(HOST, PORT)
29
+ http.use_ssl = true
30
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
31
+ http.open_timeout = 60
32
+ http.read_timeout = 60
33
+ http
34
+ end
35
+
36
+ def transported?(response)
37
+ response.kind_of? Net::HTTPSuccess or response.kind_of? Net::HTTPRedirection
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  class Pagerduty
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.1"
3
3
  end
@@ -18,5 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_runtime_dependency "json", [">= 1.7.7"]
21
+ gem.add_runtime_dependency "json", ">= 1.7.7"
22
+ gem.add_development_dependency "bundler", "~> 1.6"
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "rspec-given"
22
25
  end
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+
3
+ echo "This command will setup your local dev environment, including"
4
+ echo " * bundle install"
5
+ echo
6
+
7
+ echo "Bundling..."
8
+ bundle install --binstubs bin --path .bundle
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Pagerduty::HttpTransport do
5
+ Given(:http_transport) { Pagerduty::HttpTransport }
6
+
7
+ Given(:http) { double.as_null_object }
8
+ Given { http.stub(:request => standard_response) }
9
+ Given { Net::HTTP.stub(:new => http) }
10
+ Given(:post) { double.as_null_object }
11
+ Given { Net::HTTP::Post.stub(:new => post) }
12
+
13
+ describe "::send" do
14
+ Given(:payload) { {
15
+ event_type: "trigger",
16
+ service_key: "test-srvc-key",
17
+ description: "test-desc",
18
+ details: { key: "value" },
19
+ } }
20
+
21
+ When(:response) { http_transport.send(payload) }
22
+
23
+ describe "provides the correct request" do
24
+ Then {
25
+ expect(post).to have_received(:body=).with(
26
+ '{"event_type":"trigger","service_key":"test-srvc-key","description":"test-desc","details":{"key":"value"}}'
27
+ )
28
+ }
29
+ end
30
+
31
+ describe "handles all responses" do
32
+
33
+ context "PagerDuty successfully creates the incident" do
34
+ Given { http.stub(:request => response_with_body(<<-JSON)) }
35
+ {
36
+ "status": "success",
37
+ "incident_key": "My Incident Key",
38
+ "message": "Event processed"
39
+ }
40
+ JSON
41
+ Then { expect(response).to include("status" => "success") }
42
+ Then { expect(response).to include("incident_key" => "My Incident Key") }
43
+ end
44
+
45
+ context "PagerDuty fails to create the incident" do
46
+ Given { http.stub(:request => response_with_body(<<-JSON)) }
47
+ {
48
+ "status": "failure",
49
+ "message": "Event not processed"
50
+ }
51
+ JSON
52
+ Then { expect(response).to include("status" => "failure") }
53
+ Then { expect(response).to_not include("incident_key") }
54
+ end
55
+
56
+ context "PagerDuty responds with HTTP bad request" do
57
+ Given { http.stub(:request => bad_request) }
58
+ Then { expect(response).to have_raised Net::HTTPServerException }
59
+ end
60
+ end
61
+
62
+ describe "HTTPS use" do
63
+ Then { expect(http).to have_received(:use_ssl=).with(true) }
64
+ Then { expect(http).to have_received(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) }
65
+ Then { expect(http).to_not have_received(:ca_path=) }
66
+ Then { expect(http).to_not have_received(:verify_depth=) }
67
+ end
68
+
69
+ describe "timeouts" do
70
+ Then { expect(http).to have_received(:open_timeout=).with(60) }
71
+ Then { expect(http).to have_received(:read_timeout=).with(60) }
72
+ end
73
+ end
74
+
75
+ def standard_response
76
+ response_with_body '{ "status": "success", "incident_key": "My Incident Key" }'
77
+ end
78
+
79
+ def response_with_body(body)
80
+ response = Net::HTTPSuccess.new 1.1, "200", "OK"
81
+ response.stub(:body => body)
82
+ response
83
+ end
84
+
85
+ def bad_request
86
+ Net::HTTPBadRequest.new 1.1, "400", "Bad Request"
87
+ end
88
+
89
+ end
@@ -0,0 +1,190 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Pagerduty do
5
+ Given(:pagerduty) { Pagerduty.new(service_key) }
6
+ Given(:service_key) { "a-test-service-key" }
7
+
8
+ Given(:transport) { double.as_null_object }
9
+ Given { Pagerduty.stub(:transport => transport) }
10
+
11
+ describe "#trigger" do
12
+ Given(:description) { "a-test-description" }
13
+ Given(:details) { { key: "value" } }
14
+
15
+ describe "provides the correct request" do
16
+ Given { transport.stub(:send => standard_response) }
17
+ When(:incident) { pagerduty.trigger(description, details) }
18
+ Then {
19
+ expect(transport).to have_received(:send).with(
20
+ event_type: "trigger",
21
+ service_key: "a-test-service-key",
22
+ description: "a-test-description",
23
+ details: { key: "value" }
24
+ )
25
+ }
26
+ end
27
+
28
+ describe "handles all responses" do
29
+
30
+ context "PagerDuty successfully creates the incident" do
31
+ Given {
32
+ transport.stub(:send => {
33
+ "status" => "success",
34
+ "incident_key" => "My Incident Key",
35
+ "message" => "Event processed",
36
+ })
37
+ }
38
+ When(:incident) { pagerduty.trigger(description, details) }
39
+ Then { expect(incident).to be_a PagerdutyIncident }
40
+ Then { incident.service_key == service_key }
41
+ Then { incident.incident_key == "My Incident Key" }
42
+ end
43
+
44
+ context "PagerDuty fails to create the incident" do
45
+ Given {
46
+ transport.stub(:send => {
47
+ "status" => "failure",
48
+ "message" => "Event not processed",
49
+ })
50
+ }
51
+ When(:incident) { pagerduty.trigger(description, details) }
52
+ Then { expect(incident).to have_raised PagerdutyException }
53
+ end
54
+
55
+ context "PagerDuty responds with HTTP bad request" do
56
+ Given { transport.stub(:send).and_raise(Net::HTTPServerException.new(nil, nil)) }
57
+ When(:incident) { pagerduty.trigger(description, details) }
58
+ Then { expect(incident).to have_raised Net::HTTPServerException }
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#get_incident" do
64
+ Given(:incident_key) { "a-test-incident-key" }
65
+ When(:incident) { pagerduty.get_incident(incident_key) }
66
+ Then { expect(incident).to be_a PagerdutyIncident }
67
+ Then { incident.service_key == service_key }
68
+ Then { incident.incident_key == incident_key }
69
+ end
70
+
71
+ describe PagerdutyIncident do
72
+ Given(:incident) { PagerdutyIncident.new(service_key, incident_key) }
73
+ Given(:incident_key) { "a-test-incident-key" }
74
+ Given(:description) { "a-test-description" }
75
+ Given(:details) { { key: "value" } }
76
+
77
+ describe "#acknowledge" do
78
+
79
+ describe "provides the correct request" do
80
+ Given { transport.stub(:send => standard_response) }
81
+ When(:acknowledge) { incident.acknowledge(description, details) }
82
+ Then {
83
+ expect(transport).to have_received(:send).with(
84
+ event_type: "acknowledge",
85
+ service_key: "a-test-service-key",
86
+ description: "a-test-description",
87
+ details: { key: "value" },
88
+ incident_key: "a-test-incident-key",
89
+ )
90
+ }
91
+ end
92
+
93
+ describe "handles all responses" do
94
+
95
+ context "PagerDuty successfully acknowledges the incident" do
96
+ Given {
97
+ transport.stub(:send => {
98
+ "status" => "success",
99
+ "incident_key" => "a-test-incident-key",
100
+ "message" => "Event acknowledged",
101
+ })
102
+ }
103
+ When(:acknowledge) { incident.acknowledge(description, details) }
104
+ Then { expect(acknowledge).to be incident}
105
+ end
106
+
107
+ context "PagerDuty fails to acknowledge the incident" do
108
+ Given {
109
+ transport.stub(:send => {
110
+ "status" => "failure",
111
+ "incident_key" => "a-test-incident-key",
112
+ "message" => "Event not acknowledged",
113
+ })
114
+ }
115
+ When(:acknowledge) { incident.acknowledge(description, details) }
116
+ Then { expect(acknowledge).to have_failed PagerdutyException }
117
+ end
118
+
119
+ context "PagerDuty responds with HTTP bad request" do
120
+ Given { transport.stub(:send).and_raise(Net::HTTPServerException.new(nil, nil)) }
121
+ When(:acknowledge) { incident.acknowledge(description, details) }
122
+ Then { expect(acknowledge).to have_failed Net::HTTPServerException }
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "#resolve" do
128
+
129
+ describe "provides the correct request" do
130
+ Given { transport.stub(:send => standard_response) }
131
+ When(:resolve) { incident.resolve(description, details) }
132
+ Then {
133
+ expect(transport).to have_received(:send).with(
134
+ event_type: "resolve",
135
+ service_key: "a-test-service-key",
136
+ description: "a-test-description",
137
+ details: { key: "value" },
138
+ incident_key: "a-test-incident-key",
139
+ )
140
+ }
141
+ end
142
+
143
+ describe "handles all responses" do
144
+
145
+ context "PagerDuty successfully resolves the incident" do
146
+ Given {
147
+ transport.stub(:send => {
148
+ "status" => "success",
149
+ "incident_key" => "a-test-incident-key",
150
+ "message" => "Event resolved",
151
+ })
152
+ }
153
+ When(:resolve) { incident.resolve(description, details) }
154
+ Then { expect(resolve).to be incident}
155
+ end
156
+
157
+ context "PagerDuty fails to create the incident" do
158
+ Given {
159
+ transport.stub(:send => {
160
+ "status" => "failure",
161
+ "message" => "Event not resolved",
162
+ })
163
+ }
164
+ When(:resolve) { incident.resolve(description, details) }
165
+ Then { expect(resolve).to have_failed PagerdutyException }
166
+ end
167
+
168
+ context "PagerDuty responds with HTTP bad request" do
169
+ Given { transport.stub(:send).and_raise(Net::HTTPServerException.new(nil, nil)) }
170
+ When(:resolve) { incident.resolve(description, details) }
171
+ Then { expect(resolve).to have_failed Net::HTTPServerException }
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+ def standard_response
178
+ { "status" => "success", "incident_key" => "My Incident Key" }
179
+ end
180
+
181
+ describe PagerdutyException do
182
+ Given(:pagerduty_instance) { double }
183
+ Given(:api_response) { double }
184
+
185
+ When(:pagerduty_exception) { PagerdutyException.new(pagerduty_instance, api_response) }
186
+
187
+ Then { pagerduty_exception.pagerduty_instance == pagerduty_instance }
188
+ Then { pagerduty_exception.api_response == api_response }
189
+ end
190
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ require "pagerduty"
3
+ require "rspec/given"
4
+
5
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagerduty
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Somerville
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -24,6 +24,48 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.7.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-given
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
27
69
  description: Provides a simple interface for calling into the Pagerduty API
28
70
  email:
29
71
  - charlie@charliesomerville.com
@@ -33,13 +75,19 @@ extra_rdoc_files: []
33
75
  files:
34
76
  - ".document"
35
77
  - ".gitignore"
78
+ - ".travis.yml"
36
79
  - Gemfile
37
80
  - LICENSE.txt
38
81
  - README.md
39
82
  - Rakefile
40
83
  - lib/pagerduty.rb
84
+ - lib/pagerduty/http_transport.rb
41
85
  - lib/pagerduty/version.rb
42
86
  - pagerduty.gemspec
87
+ - script/setup.sh
88
+ - spec/pagerduty/http_transport_spec.rb
89
+ - spec/pagerduty_spec.rb
90
+ - spec/spec_helper.rb
43
91
  homepage: http://github.com/envato/pagerduty
44
92
  licenses:
45
93
  - MIT
@@ -60,8 +108,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
108
  version: '0'
61
109
  requirements: []
62
110
  rubyforge_project:
63
- rubygems_version: 2.2.0
111
+ rubygems_version: 2.2.2
64
112
  signing_key:
65
113
  specification_version: 4
66
114
  summary: Pagerduty API client library
67
- test_files: []
115
+ test_files:
116
+ - spec/pagerduty/http_transport_spec.rb
117
+ - spec/pagerduty_spec.rb
118
+ - spec/spec_helper.rb