slackdraft 1.0.0 → 1.0.1

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: 7dcad482dd1834021d0d51df6e59ca8b0ef8bc69
4
- data.tar.gz: 055da9c748a5f1ee1a02fb9a2bf608b4cc400d65
3
+ metadata.gz: b66329da3ad9134da94bf8553fe434c9a1bb7627
4
+ data.tar.gz: 2c5cd66d67e33b167f454e8d9245969c13447e29
5
5
  SHA512:
6
- metadata.gz: 8aa49f4ceba089f62f776f8092c58b9d62d306d3d240a4593ca2f7dda4d8044487bc1ebc1fa940dbcbb1e54e47df06cf62e99f99649a678cfc9540393019a286
7
- data.tar.gz: 8b5113394c3e2be2c356173b29e2f5b6152577606c3dcdaee9b68d6678658e128fa1a36c80951697cc74aeb68620f13ea3281fec47262a8979362f46b8cedaa4
6
+ metadata.gz: 9c06d2e9d5a4397b8d3d69ebc0f08f01a5379b1e17a08757c8910118ab2bd02fb1baddfff55b80e5940bdc90d62d65e8d409ff9aa483c43c6353414da1a3a523
7
+ data.tar.gz: c9203c65521f94a7d8401e065e548e6547188fe24c07d905de50ed9825ea65eba51772a9598ea6583105cf296b8f82651212b206679b6028037708d4469ee895
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  /.bundle/
2
2
  /vendor/
3
+ Gemfile.lock
@@ -7,24 +7,29 @@ require 'slackdraft'
7
7
  url = "https://hooks.slack.com/services/xxxxxxxxxx"
8
8
 
9
9
  # Create instance
10
- slack = Slackdraft::Attachment.new(url)
11
- slack.fallback = "Fallback text!!!!"
12
- slack.color = "#439FE0"
13
- slack.pretext ="Hmm where?"
10
+ attachment = Slackdraft::Attachment.new url
14
11
 
15
- slack.author_name = "Mike"
16
- slack.author_link = "https://github.com/mikemackintosh"
17
- slack.author_icon = ":skull:"
12
+ attachment.text = "Test"
13
+ attachment.username = "johndoe"
14
+ attachment.channel = "#slackdraft"
18
15
 
19
- slack.title = "By Mike Mackintosh"
20
- slack.title_link = "https://github.com/mikemackintosh"
16
+ attachment.fallback = "Fallback text!!!!"
17
+ attachment.color = "#439FE0"
18
+ attachment.pretext ="Hmm where?"
21
19
 
22
- slack.text = "This is pretty cool"
20
+ attachment.author_name = "Mike"
21
+ attachment.author_link = "https://github.com/mikemackintosh"
22
+ attachment.author_icon = ":skull:"
23
23
 
24
- slack.add_field("col 1", "blahblahblah")
25
- slack.add_field("col 2", "derpherp")
24
+ attachment.title = "By Mike Mackintosh"
25
+ attachment.title_link = "https://github.com/mikemackintosh"
26
+
27
+ attachment.message = "This is pretty cool"
28
+
29
+ attachment.add_field("col 1", "blahblahblah")
30
+ attachment.add_field("col 2", "derpherp")
26
31
 
27
32
  # Send it, and check response
28
- if slack.send!
33
+ if attachment.send!
29
34
  Slackdraft::success("Message successful")
30
35
  end
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'slackdraft'
5
+
6
+ # Set URL
7
+ url = "https://hooks.slack.com/services/xxxxxxxxxx"
8
+
9
+ # Create instance
10
+ slack = Slackdraft::Message.new(url)
11
+ slack.text = "Test"
12
+ slack.username = "johndoe"
13
+ slack.channel = "#slackdraft"
14
+
15
+ # Create an Attachment
16
+ attachment = Slackdraft::Attachment.new
17
+ attachment.fallback = "Fallback text!!!!"
18
+ attachment.color = "#439FE0"
19
+ attachment.pretext ="Hmm where?"
20
+
21
+ attachment.author_name = "Mike"
22
+ attachment.author_link = "https://github.com/mikemackintosh"
23
+ attachment.author_icon = ":skull:"
24
+
25
+ attachment.title = "By Mike Mackintosh"
26
+ attachment.title_link = "https://github.com/mikemackintosh"
27
+
28
+ attachment.message = "This is pretty cool"
29
+
30
+ attachment.add_field("col 1", "blahblahblah")
31
+ attachment.add_field("col 2", "derpherp")
32
+
33
+ # Add the attachment back to Slack::Message
34
+ slack.add_attachment(attachment)
35
+
36
+ # Send it, and check response
37
+ if slack.send!
38
+ Slackdraft::success("Message successful")
39
+ end
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path('../../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ # TODO: Change color based on alert type
6
+
7
+ # Require Slackdraft
8
+ require 'slackdraft'
9
+
10
+ # Set the webhook
11
+ WEBHOOK = ENV['SLACK_EP'] ||= "https://...."
12
+
13
+ # Create instance
14
+ slack = Slackdraft::Format::Alert.new WEBHOOK
15
+
16
+ # Username to send message as
17
+ slack.username = "MikeMackintosh"
18
+
19
+ # Where to send alert to
20
+ slack.channel = "#slackdraft"
21
+
22
+ # Service that is alerting
23
+ slack.service = "Slack Communications"
24
+
25
+ # Header message (smaller text)
26
+ slack.header = "New Alert Received!"
27
+
28
+ # Alert message
29
+ slack.alert = "Shit is going down"
30
+
31
+ # Severity
32
+ slack.severity = 4
33
+
34
+ # Name and link to originator bot
35
+ slack.bot = "SlackDraft Alert"
36
+ slack.botlink = "http://github.com/mikemackintosh"
37
+
38
+ # Add a link here to your runbook/wiki on how to fix
39
+ slack.runbook = "https://github.com/mikemackintosh"
40
+
41
+ # Send it, and check response
42
+ if slack.send!
43
+ Slackdraft::success("Message successful")
44
+ end
data/lib/slackdraft.rb CHANGED
@@ -8,6 +8,7 @@ require 'slackdraft/base'
8
8
  require 'slackdraft/message'
9
9
  require 'slackdraft/version'
10
10
  require 'slackdraft/attachment'
11
+ require 'slackdraft/format/init'
11
12
 
12
13
  module Slackdraft
13
14
 
@@ -1,22 +1,29 @@
1
1
  module Slackdraft
2
2
 
3
- class Attachment < Base
3
+ class Attachment < Slackdraft::Message
4
4
 
5
5
  fattr fallback: "This message was sent with Slackdraft"
6
- fattr color: "#439FE0"
7
- fattr pretext: nil
6
+ fattr color: "#439FE0"
7
+ fattr pretext: nil
8
8
 
9
9
  fattr author_name: nil
10
10
  fattr author_link: nil
11
11
  fattr author_icon: nil
12
12
 
13
- fattr title: nil
13
+ fattr title: nil
14
14
  fattr title_link: nil
15
15
 
16
- fattr text: nil
17
- fattr fields: []
16
+ fattr message: nil
17
+ fattr fields: []
18
18
 
19
- fattr image_url: nil
19
+ fattr image_url: nil
20
+
21
+ # Capture the url if we pass it in
22
+ def initialize(url=nil)
23
+ unless url.nil?
24
+ @target = url
25
+ end
26
+ end
20
27
 
21
28
  # Short defines if it's 1 column or not, default: not
22
29
  def add_field(title, value, short=true)
@@ -24,7 +31,7 @@ module Slackdraft
24
31
  end
25
32
 
26
33
  # Generate the payload for slack attachments
27
- def generate_payload
34
+ def generate_attachment
28
35
  payload = {}
29
36
  payload[:fallback] = self.fallback unless self.fallback.nil?
30
37
  payload[:color] = self.color unless self.color.nil?
@@ -34,7 +41,7 @@ module Slackdraft
34
41
  payload[:author_icon] = self.author_icon unless self.author_icon.nil?
35
42
  payload[:title] = self.title unless self.title.nil?
36
43
  payload[:title_link] = self.title_link unless self.title_link.nil?
37
- payload[:text] = self.text unless self.text.nil?
44
+ payload[:text] = self.message unless self.message.nil?
38
45
 
39
46
  unless self.fields.nil?
40
47
  payload[:fields] = self.fields if self.fields.length > 0
@@ -42,11 +49,20 @@ module Slackdraft
42
49
 
43
50
  payload[:image_url] = self.image_url unless self.image_url.nil?
44
51
 
45
- puts payload.inspect
46
-
47
52
  payload
48
53
  end
49
54
 
55
+ # To hash, for use in Message
56
+ def to_h
57
+ generate_attachment
58
+ end
59
+
60
+ # Send it directly from Attachment
61
+ def send!
62
+ add_attachment(generate_attachment)
63
+ super
64
+ end
65
+
50
66
  end
51
67
 
52
68
  end
@@ -0,0 +1,126 @@
1
+ module Slackdraft
2
+
3
+ class Format
4
+
5
+ fattr :service
6
+ fattr :type
7
+ fattr :alert
8
+ fattr header: nil
9
+ fattr severity: nil
10
+ fattr runbook: nil
11
+ fattr bot: "slackdraft"
12
+ fattr botlink: nil
13
+ #fattr message: nil
14
+
15
+ class Alert < Slackdraft::Attachment
16
+ def send!
17
+ parse
18
+ super
19
+ end
20
+
21
+ # Parse the alert message
22
+ def parse
23
+ @title = @service
24
+
25
+ unless @header.nil?
26
+ @text = @header
27
+ end
28
+
29
+ @message = @alert
30
+ @fallback = @alert
31
+
32
+ unless @bot.nil?
33
+ @author_name = @bot
34
+ unless @botlink.nil?
35
+ @author_name = @botlink
36
+ end
37
+ end
38
+
39
+ unless @severity.nil?
40
+ add_field("Severity", @severity)
41
+ end
42
+
43
+ unless @runbook.nil?
44
+ add_field("Runbook", @runbook)
45
+ end
46
+ end
47
+
48
+ # Set the alert text
49
+ def service=(service)
50
+ @service = service
51
+ end
52
+ def service(service)
53
+ @service = service
54
+ end
55
+ alias_method :system=, :service=
56
+ alias_method :system, :service
57
+
58
+
59
+ # Set the alert text
60
+ def bot=(bot)
61
+ @bot = bot
62
+ end
63
+ def bot(bot)
64
+ @bot = bot
65
+ end
66
+ alias_method :alerter=, :bot=
67
+ alias_method :alerter, :bot
68
+
69
+ # Set the alert text
70
+ def botlink=(botlink)
71
+ @botlink = botlink
72
+ end
73
+ def botlink(botlink)
74
+ @botlink = botlink
75
+ end
76
+ alias_method :alerterlink=, :botlink=
77
+ alias_method :alerterlink, :botlink
78
+
79
+ # Set the alert text
80
+ def type=(type)
81
+ @type = type
82
+ end
83
+ def type(type)
84
+ @type = type
85
+ end
86
+
87
+ # Set the alert text
88
+ def alert=(alert)
89
+ @alert = alert
90
+ end
91
+ def alert(alert)
92
+ @alert = alert
93
+ end
94
+
95
+ # Set the alert text
96
+ def header=(header)
97
+ @header = header
98
+ end
99
+ def alert(header)
100
+ @header = header
101
+ end
102
+
103
+ # Set the severity
104
+ def sev=(severity)
105
+ @severity = severity
106
+ end
107
+ def sev(severity)
108
+ @severity = severity
109
+ end
110
+ alias_method :severity, :sev
111
+ alias_method :severity=, :sev=
112
+
113
+ # Set the severity
114
+ def runbook=(runbook)
115
+ @runbook = runbook
116
+ end
117
+ def runbook(runbook)
118
+ @runbook = runbook
119
+ end
120
+ alias_method :fix_url, :runbook
121
+
122
+ end
123
+
124
+ end
125
+
126
+ end
@@ -0,0 +1 @@
1
+ require_relative 'alert'
@@ -7,6 +7,7 @@ module Slackdraft
7
7
  fattr icon_emoji: ':fire:'
8
8
  fattr icon_url: ''
9
9
  fattr text: ''
10
+ fattr attachments: []
10
11
 
11
12
  # Set the api call username
12
13
  def username
@@ -20,6 +21,11 @@ module Slackdraft
20
21
  @channel
21
22
  end
22
23
 
24
+ # Add an attachment
25
+ def add_attachment(attachment)
26
+ self.attachments.push(attachment.to_h)
27
+ end
28
+
23
29
  # Set the icon URL and default to the emoji
24
30
  def icon_url
25
31
  @icon_url || icon_emoji
@@ -30,10 +36,12 @@ module Slackdraft
30
36
  return ':fire:' if @icon_emoji.nil?
31
37
  end
32
38
 
39
+ # Reference a channel
33
40
  def refchannel(channel)
34
41
  "<#C024BE7LR|#{channel}>"
35
42
  end
36
43
 
44
+ # Reference a user
37
45
  def refuser(user)
38
46
  "<@U024BE7LH|#{user}>"
39
47
  end
@@ -46,17 +54,17 @@ module Slackdraft
46
54
  end
47
55
 
48
56
  @text
49
-
50
57
  end
51
58
 
52
59
  # Generate the payload if stuff was provided
53
60
  def generate_payload
54
61
  payload = {}
55
- payload[:channel] = self.channel unless self.channel.nil?
56
- payload[:username] = self.username unless self.username.nil?
57
- payload[:icon_url] = self.icon_url unless self.icon_url.nil?
58
- payload[:icon_emoji] = self.icon_emoji unless self.icon_emoji.nil?
59
- payload[:text] = self.text unless self.text.nil?
62
+ payload[:channel] = self.channel unless self.channel.nil?
63
+ payload[:username] = self.username unless self.username.nil?
64
+ payload[:icon_url] = self.icon_url unless self.icon_url.nil?
65
+ payload[:icon_emoji] = self.icon_emoji unless self.icon_emoji.nil?
66
+ payload[:text] = self.text unless self.text.nil?
67
+ payload[:attachments] = self.attachments unless self.attachments.empty?
60
68
 
61
69
  payload
62
70
  end
@@ -1,3 +1,3 @@
1
1
  module Slackdraft
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackdraft
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Mackintosh
@@ -134,17 +134,21 @@ files:
134
134
  - .rspec
135
135
  - .travis.yml
136
136
  - Gemfile
137
- - Gemfile.lock
138
137
  - LICENSE.txt
139
138
  - README.md
140
139
  - Rakefile
141
140
  - bin/slackdraft
142
141
  - examples/attachment.rb
142
+ - examples/attachment_long.rb
143
+ - examples/formatted/alert.rb
143
144
  - lib/slackdraft.rb
144
145
  - lib/slackdraft/attachment.rb
145
146
  - lib/slackdraft/base.rb
147
+ - lib/slackdraft/format/alert.rb
148
+ - lib/slackdraft/format/init.rb
146
149
  - lib/slackdraft/message.rb
147
150
  - lib/slackdraft/version.rb
151
+ - slackdraft-1.0.0.gem
148
152
  - slackdraft.gemspec
149
153
  homepage: http://github.com/mikemackintosh/slackdraft
150
154
  licenses:
data/Gemfile.lock DELETED
@@ -1,53 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- slackdraft (0.7.7)
5
- fattr
6
- httparty
7
- methadone
8
- paint
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- addressable (2.3.7)
14
- crack (0.4.2)
15
- safe_yaml (~> 1.0.0)
16
- diff-lcs (1.2.5)
17
- fattr (2.2.2)
18
- httparty (0.13.3)
19
- json (~> 1.8)
20
- multi_xml (>= 0.5.2)
21
- json (1.8.2)
22
- methadone (1.8.0)
23
- bundler
24
- multi_xml (0.5.5)
25
- paint (0.9.0)
26
- rake (10.4.2)
27
- rspec (3.2.0)
28
- rspec-core (~> 3.2.0)
29
- rspec-expectations (~> 3.2.0)
30
- rspec-mocks (~> 3.2.0)
31
- rspec-core (3.2.0)
32
- rspec-support (~> 3.2.0)
33
- rspec-expectations (3.2.0)
34
- diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.2.0)
36
- rspec-mocks (3.2.0)
37
- diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.2.0)
39
- rspec-support (3.2.1)
40
- safe_yaml (1.0.4)
41
- webmock (1.20.4)
42
- addressable (>= 2.3.6)
43
- crack (>= 0.3.2)
44
-
45
- PLATFORMS
46
- ruby
47
-
48
- DEPENDENCIES
49
- bundler
50
- rake
51
- rspec
52
- slackdraft!
53
- webmock