pd_event_v2 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e58d9de22d4605cf35d2bd096b8c1b4c5c389bb24190d1b4f21223bf53c45e8c
4
+ data.tar.gz: bc0e61b89e8ba97eb340e2f3b430a62b944dcfedfa8fb87659daea18604939bf
5
+ SHA512:
6
+ metadata.gz: 85f5777d327aa17be3427b7a172fc0dfe30473e809cb85b1c5a6f49e0376b67c3aa256b1de2748c07b157aec9b1220c7dffdfe7bbc111c71c6f2c2d82ad1cefd
7
+ data.tar.gz: 5e7ec4f6343e163e5816674dfde544207676801f2e1179bd85f62411133a0ac7d9178272eee869e0fe01bdd95de25127dcdfbe74cc9e289da284ccbda50cab56
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /test.rb
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - test.rb
5
+ - vendor/bundle/**/*
6
+ Metrics/LineLength:
7
+ Max: 120
8
+ Style/TrailingCommaInArrayLiteral:
9
+ EnforcedStyleForMultiline: comma
10
+ Style/TrailingCommaInHashLiteral:
11
+ EnforcedStyleForMultiline: comma
12
+ Style/Documentation:
13
+ Enabled: false
14
+ Metrics/AbcSize:
15
+ Enabled: false
16
+ Metrics/MethodLength:
17
+ Enabled: false
18
+ Metrics/CyclomaticComplexity:
19
+ Enabled: false
20
+ Metrics/PerceivedComplexity:
21
+ Enabled: false
22
+ Metrics/BlockLength:
23
+ Enabled: false
@@ -0,0 +1,11 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.6
7
+ - 2.5.5
8
+ - 2.6.4
9
+ before_install: gem install bundler -v 2.0.2
10
+ script:
11
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in pd_event_v2.gemspec
6
+ gemspec
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pd_event_v2 (0.1.0)
5
+ faraday (~> 0.17)
6
+ faraday_middleware
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ diff-lcs (1.3)
13
+ faraday (0.17.1)
14
+ multipart-post (>= 1.2, < 3)
15
+ faraday_middleware (0.13.1)
16
+ faraday (>= 0.7.4, < 1.0)
17
+ jaro_winkler (1.5.4)
18
+ multipart-post (2.1.1)
19
+ parallel (1.19.1)
20
+ parser (2.6.5.0)
21
+ ast (~> 2.4.0)
22
+ rainbow (3.0.0)
23
+ rake (10.5.0)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-expectations (3.9.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.0)
37
+ rubocop (0.77.0)
38
+ jaro_winkler (~> 1.5.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.6)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 1.7)
44
+ ruby-progressbar (1.10.1)
45
+ unicode-display_width (1.6.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler
52
+ pd_event_v2!
53
+ rake
54
+ rspec (>= 3)
55
+ rubocop
56
+
57
+ BUNDLED WITH
58
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Genki Sugawara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # PdEventV2
2
+
3
+ [PagerDuty Events API v2](https://v2.developer.pagerduty.com/docs/events-api-v2) Ruby Client.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/pd_event_v2.svg)](http://badge.fury.io/rb/pd_event_v2)
6
+ [![Build Status](https://travis-ci.org/winebarrel/pd_event_v2.svg?branch=master)](https://travis-ci.org/winebarrel/pd_event_v2)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'pd_event_v2'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install pd_event_v2
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require 'pd_event_v2'
28
+
29
+ # routing_key: Integration Key
30
+ client = PdEventV2::Client.new(routing_key: 'ZAPZAPZAP')
31
+
32
+ # see https://v2.developer.pagerduty.com/v2/docs/send-an-event-events-api-v2
33
+ res = client.trigger(
34
+ payload: {
35
+ summary: 'Example alert on host1.example.com',
36
+ source: 'monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003',
37
+ severity: 'info'
38
+ }
39
+ )
40
+
41
+ p res #=> {
42
+ # 'status' => 'success',
43
+ # 'message' => 'Event processed',
44
+ # 'dedup_key' => '3c053fc438cc4608b8382b28adc0af8d'
45
+ # }
46
+ ```
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new do |task|
9
+ task.options = %w[-c .rubocop.yml]
10
+ end
11
+
12
+ task default: %i[rubocop spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'pd_event_v2'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ require 'pd_event_v2/client'
7
+ require 'pd_event_v2/version'
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdEventV2
4
+ class Client
5
+ INITIALIZE_OPTIONS = %i[
6
+ routing_key
7
+ debug
8
+ ].freeze
9
+
10
+ DEFAULT_ADAPTERS = [
11
+ Faraday::Adapter::NetHttp,
12
+ Faraday::Adapter::Test,
13
+ ].freeze
14
+
15
+ USER_AGENT = "PagerDuty Events API v2 Ruby Client #{PdEventV2::VERSION}"
16
+
17
+ # https://v2.developer.pagerduty.com/docs/events-api-v2#making-a-request
18
+ DEFAULT_API_URL = 'https://events.pagerduty.com/v2/enqueue'
19
+
20
+ # https://v2.developer.pagerduty.com/docs/events-api-v2#event-action
21
+ EVENT_ACTIONS = %i[
22
+ trigger
23
+ acknowledge
24
+ resolve
25
+ ].freeze
26
+
27
+ # https://v2.developer.pagerduty.com/v2/docs/send-an-event-events-api-v2
28
+ PAYLOAD_KEYS = {
29
+ summary: true,
30
+ source: true,
31
+ severity: true,
32
+ timestamp: false,
33
+ component: false,
34
+ group: false,
35
+ class: false,
36
+ custom_details: false,
37
+ }.freeze
38
+
39
+ def initialize(options)
40
+ unless options.is_a?(Hash)
41
+ raise ArgumentError, "wrong type argument (given: #{options}:#{options.class}, expected Hash)"
42
+ end
43
+
44
+ @options = {}
45
+
46
+ INITIALIZE_OPTIONS.each do |key|
47
+ @options[key] = options.delete(key)
48
+ end
49
+
50
+ raise ArgumentError, ':routing_key is required for initialize' unless @options[:routing_key]
51
+
52
+ options[:url] ||= DEFAULT_API_URL
53
+
54
+ @conn = Faraday.new(options) do |faraday|
55
+ faraday.request :url_encoded
56
+ faraday.response :json, content_type: /\bjson\b/
57
+ faraday.response :raise_error
58
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: true if @options[:debug]
59
+
60
+ yield(faraday) if block_given?
61
+
62
+ unless DEFAULT_ADAPTERS.any? { |i| faraday.builder.handlers.include?(i) }
63
+ faraday.adapter Faraday.default_adapter
64
+ end
65
+ end
66
+
67
+ @conn.headers[:user_agent] = USER_AGENT
68
+ end
69
+
70
+ EVENT_ACTIONS.each do |action|
71
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
72
+ def #{action}(**args)
73
+ send_event(event_action: '#{action}', **args)
74
+ end
75
+ RUBY
76
+ end
77
+
78
+ private
79
+
80
+ # https://v2.developer.pagerduty.com/v2/docs/send-an-event-events-api-v2
81
+ def send_event(event_action:, dedup_key: nil, payload:, images: nil, links: nil)
82
+ unless payload.is_a?(Hash)
83
+ raise ArgumentError, "wrong type payload (given: #{payload}:#{payload.class}, expected Hash)"
84
+ end
85
+
86
+ payload = payload.map do |key, val|
87
+ [key.to_sym, val]
88
+ end.to_h
89
+
90
+ PAYLOAD_KEYS.each do |key, required|
91
+ raise ArgumentError, "missing payload key: #{key}" if required && !payload.key?(key)
92
+ end
93
+
94
+ routing_key = @options.fetch(:routing_key)
95
+
96
+ params = {
97
+ routing_key: routing_key,
98
+ event_action: event_action,
99
+ payload: payload,
100
+ }
101
+
102
+ params[:dedup_key] = dedup_key if dedup_key
103
+ params[:images] = images if images
104
+ params[:links] = links if links
105
+
106
+ res = @conn.post do |req|
107
+ req.body = JSON.dump(params)
108
+ yield(req) if block_given?
109
+ end
110
+
111
+ res.body
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdEventV2
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'pd_event_v2/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'pd_event_v2'
9
+ spec.version = PdEventV2::VERSION
10
+ spec.authors = ['winebarrel']
11
+ spec.email = ['sugawara@winebarrel.jp']
12
+
13
+ spec.summary = 'PagerDuty Events API v2 Ruby Client.'
14
+ spec.description = spec.summary
15
+ spec.homepage = 'https://github.com/winebarrel/pd_event_v2'
16
+ spec.license = 'MIT'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'faraday', '~> 0.17'
29
+ spec.add_dependency 'faraday_middleware'
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'rspec', '>= 3'
33
+ spec.add_development_dependency 'rubocop'
34
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pd_event_v2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
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: rake
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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: PagerDuty Events API v2 Ruby Client.
98
+ email:
99
+ - sugawara@winebarrel.jp
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/pd_event_v2.rb
116
+ - lib/pd_event_v2/client.rb
117
+ - lib/pd_event_v2/version.rb
118
+ - pd_event_v2.gemspec
119
+ homepage: https://github.com/winebarrel/pd_event_v2
120
+ licenses:
121
+ - MIT
122
+ metadata:
123
+ homepage_uri: https://github.com/winebarrel/pd_event_v2
124
+ source_code_uri: https://github.com/winebarrel/pd_event_v2
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.0.3
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: PagerDuty Events API v2 Ruby Client.
144
+ test_files: []