discord-notifier 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
+ SHA1:
3
+ metadata.gz: e41269eca4f21db07c035912dad45faad3c539e1
4
+ data.tar.gz: a21a0062df3506c83a23f5e5e896903bce6af3a1
5
+ SHA512:
6
+ metadata.gz: 65b4a8566e174b2bf25a246a58c07c31f3590dea6cc88c5f60c34343286a6cc722abb429d519b3df30f95880b164b5887435fd2d6275bea33b5e9036d7c4eff7
7
+ data.tar.gz: e712272a5e281853adea41d595a255935759978daa3d9b2ff03d15dd7643bc5bd01828ca964710b25106fed7e49295009014c17c2a0da735a32b53a0ddc2d684
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require_relative 'discord_notifier/embed'
4
+
5
+ module Discord
6
+ Config = Struct.new(:url, :username, :avatar_url, :wait)
7
+
8
+ class Notifier
9
+ @@config = Config.new
10
+
11
+ def self.setup
12
+ yield @@config
13
+ end
14
+
15
+ def self.message(content, config = {})
16
+ params = @@config.to_h.merge(config).compact
17
+
18
+ case content
19
+ when String
20
+ params[:content] = content
21
+ when Embed
22
+ params[:embeds] = [content.data]
23
+ when Array
24
+ params[:embeds] = content.map { |embed| embed.data }
25
+ # when Attachment
26
+ else
27
+ raise ArgumentError, 'Unsupported content type'
28
+ end
29
+
30
+ uri = endpoint(params)
31
+ Net::HTTP.post(uri, params.to_json, "Content-Type": "application/json")
32
+ end
33
+
34
+ def self.endpoint(config)
35
+ uri = URI(config[:url])
36
+ uri.query = URI.encode_www_form(wait: true) if config[:wait]
37
+ return uri
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,87 @@
1
+ module Discord
2
+ class Embed
3
+ attr_reader :data
4
+
5
+ def initialize(&block)
6
+ @data = {}
7
+ self.instance_exec(&block) if block_given?
8
+ end
9
+
10
+ def title(str)
11
+ raise ArgumentError, 'Title Embed data must be a String' unless str.is_a?(String)
12
+ @data[:title] = str
13
+ end
14
+
15
+ def description(str)
16
+ raise ArgumentError, 'Description Embed data must be a String' unless str.is_a?(String)
17
+ @data[:description] = str
18
+ end
19
+
20
+ def url(str)
21
+ raise ArgumentError, 'URL Embed data must be a String' unless str.is_a?(String)
22
+ @data[:url] = str
23
+ end
24
+
25
+ def timestamp(date)
26
+ raise ArgumentError, 'Timestamp Embed data must be a Date' unless date.is_a?(DateTime)
27
+ @data[:timestamp] = date
28
+ end
29
+
30
+ def color(val)
31
+ case val
32
+ when String
33
+ @data[:color] = val.delete('#').to_i(16)
34
+ when Integer
35
+ raise ArgumentError, 'Color must be 24 bit' if val >= 16_777_216
36
+ @data[:color] = val
37
+ else
38
+ raise ArgumentError, 'Color must be hex or 24 bit int'
39
+ end
40
+ end
41
+
42
+ def thumbnail(params)
43
+ raise ArgumentError, "Thumbnail Embed data must be a Hash" unless params.is_a?(Hash)
44
+
45
+ @data[:thumbnail] = params
46
+ end
47
+
48
+ def video(params)
49
+ raise ArgumentError, "Video Embed data must be a Hash" unless params.is_a?(Hash)
50
+
51
+ @data[:video] = params
52
+ end
53
+
54
+ def image(params)
55
+ raise ArgumentError, "Image Embed data must be a Hash" unless params.is_a?(Hash)
56
+
57
+ @data[:image] = params
58
+ end
59
+
60
+ def provider(params)
61
+ raise ArgumentError, "Provider Embed data must be a Hash" unless params.is_a?(Hash)
62
+
63
+ @data[:provider] = params
64
+ end
65
+
66
+ def author(params)
67
+ raise ArgumentError, "Author Embed data must be a Hash" unless params.is_a?(Hash)
68
+
69
+ @data[:author] = params
70
+ end
71
+
72
+ def footer(params)
73
+ raise ArgumentError, "Footer Embed data must be a Hash" unless params.is_a?(Hash)
74
+
75
+ @data[:footer] = params
76
+ end
77
+
78
+ def add_field(params)
79
+ raise ArgumentError, "Field Embed data must be a Hash" unless params.is_a?(Hash)
80
+
81
+ @data[:fields] ||= []
82
+ @data[:fields] << params
83
+ end
84
+
85
+ alias :field :add_field
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ module Discord
2
+ class Notifier
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class Discord::EmbedTest < Minitest::Test
4
+ def test_that_it_has_a_data_hash
5
+ embed = Discord::Embed.new
6
+ assert embed.data.is_a? Hash
7
+ end
8
+
9
+ def test_embed_fields_exist
10
+ embed = Discord::Embed.new
11
+ fields = %w(title description url timestamp color footer image thumbnail
12
+ video provider author)
13
+
14
+ fields.each do |field|
15
+ assert embed.public_methods.include?(field.to_sym), "#{field} not found"
16
+ end
17
+
18
+ assert embed.public_methods.include? :add_field
19
+ assert embed.public_methods.include? :field
20
+ end
21
+
22
+ def test_multiple_fields
23
+ embed = Discord::Embed.new do
24
+ add_field name: "Field 1"
25
+ add_field name: "Field 2"
26
+ add_field name: "Field 3"
27
+ end
28
+
29
+ assert_equal embed.data[:fields].size, 3
30
+ end
31
+
32
+ def test_embed_field_argument_types
33
+ embed = Discord::Embed.new
34
+
35
+ assert_raises(ArgumentError) { embed.title({ value: 'Hash' }) }
36
+ embed.title 'String Value'
37
+
38
+ assert_raises(ArgumentError) { embed.description({ value: 'Hash' }) }
39
+ embed.description 'String Value'
40
+
41
+ assert_raises(ArgumentError) { embed.url({ value: 'Hash' }) }
42
+ embed.url 'String Value'
43
+
44
+ assert_raises(ArgumentError) { embed.thumbnail('String Value') }
45
+ embed.thumbnail value: 'Hash Value'
46
+
47
+ assert_raises(ArgumentError) { embed.video('String Value') }
48
+ embed.video value: 'Hash Value'
49
+
50
+ assert_raises(ArgumentError) { embed.image('String Value') }
51
+ embed.image value: 'Hash Value'
52
+
53
+ assert_raises(ArgumentError) { embed.provider('String Value') }
54
+ embed.provider value: 'Hash Value'
55
+
56
+ assert_raises(ArgumentError) { embed.author('String Value') }
57
+ embed.author value: 'Hash Value'
58
+
59
+ assert_raises(ArgumentError) { embed.footer('String Value') }
60
+ embed.footer value: 'Hash Value'
61
+
62
+ assert_raises(ArgumentError) { embed.field('String Value') }
63
+ embed.field value: 'Hash Value'
64
+
65
+ assert_raises(ArgumentError) { embed.color({ hex: 'value' }) }
66
+ assert_raises(ArgumentError) { embed.color(16_777_500) }
67
+ embed.color 0x008000
68
+ val = embed.data[:color]
69
+ embed.color '#008000'
70
+ assert_equal val, embed.data[:color]
71
+
72
+ assert_raises(ArgumentError) { embed.timestamp({ value: 'Hash' }) }
73
+ embed.timestamp DateTime.new(2001,2,3,4,5,6)
74
+ end
75
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Discord::NotifierTest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil Discord::Notifier::VERSION
6
+ end
7
+ end
@@ -0,0 +1,215 @@
1
+ require 'test_helper'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ class Discord::NotifierTest < Minitest::Test
6
+ def teardown
7
+ Discord::Notifier.setup do |config|
8
+ config.url = nil
9
+ config.username = nil
10
+ config.avatar_url = nil
11
+ config.wait = nil
12
+ end
13
+ end
14
+
15
+ def test_configuration
16
+ Discord::Notifier.setup do |config|
17
+ config.url = 'http://test.com'
18
+ config.username = 'Gem Test'
19
+ config.avatar_url = 'http://avatar.com/discord.png'
20
+ end
21
+
22
+ expected_config = Discord::Config.new 'http://test.com',
23
+ 'Gem Test',
24
+ 'http://avatar.com/discord.png',
25
+ nil
26
+
27
+ Discord::Notifier.setup do |config|
28
+ assert_equal expected_config, config
29
+ end
30
+ end
31
+
32
+ def test_string_message
33
+ expected_payload = {
34
+ url: 'http://test.com',
35
+ username: 'Gem Test',
36
+ avatar_url: 'http://avatar.com/discord.png',
37
+ content: 'String Message'
38
+ }.to_json
39
+
40
+ @mock = Minitest::Mock.new
41
+ @mock.expect(:post, true, [JSON.parse(expected_payload)])
42
+
43
+ Discord::Notifier.setup do |config|
44
+ config.url = 'http://test.com'
45
+ config.username = 'Gem Test'
46
+ config.avatar_url = 'http://avatar.com/discord.png'
47
+ end
48
+
49
+ Net::HTTP.stub :post, ->(uri, params, headers) {
50
+ @mock.post JSON.parse(params)
51
+ } do
52
+ Discord::Notifier.message "String Message"
53
+ end
54
+
55
+ @mock.verify
56
+ end
57
+
58
+ def test_embed_message
59
+ expected_payload = {
60
+ url: 'http://test.com',
61
+ username: 'Gem Test',
62
+ avatar_url: 'http://avatar.com/discord.png',
63
+ embeds: [{
64
+ title: 'Embed Message Test',
65
+ description: 'Sending an embed through Discord Notifier',
66
+ url: 'http://github.com/ianmitchell/discord_notifier',
67
+ color: 0x008000,
68
+ thumbnail: {
69
+ url: 'http://avatar.com/discord.png'
70
+ },
71
+ author: {
72
+ name: 'Ian Mitchell',
73
+ url: 'http://ianmitchell.io'
74
+ },
75
+ footer: {
76
+ text: 'Mini MiniTest Test'
77
+ },
78
+ fields: [
79
+ {
80
+ name: 'Content',
81
+ value: 'This is a content section'
82
+ },
83
+ {
84
+ name: 'Subsection',
85
+ value: 'This is a content subsection'
86
+ }
87
+ ]
88
+ }]
89
+ }.to_json
90
+
91
+ @mock = Minitest::Mock.new
92
+ @mock.expect(:post, true, [JSON.parse(expected_payload)])
93
+
94
+ Discord::Notifier.setup do |config|
95
+ config.url = 'http://test.com'
96
+ config.username = 'Gem Test'
97
+ config.avatar_url = 'http://avatar.com/discord.png'
98
+ end
99
+
100
+ Net::HTTP.stub :post, ->(uri, params, headers) {
101
+ @mock.post JSON.parse(params)
102
+ } do
103
+ embed = Discord::Embed.new do
104
+ title 'Embed Message Test'
105
+ description 'Sending an embed through Discord Notifier'
106
+ url 'http://github.com/ianmitchell/discord_notifier'
107
+ color 0x008000
108
+ thumbnail url: 'http://avatar.com/discord.png'
109
+ author name: 'Ian Mitchell',
110
+ url: 'http://ianmitchell.io'
111
+ footer text: 'Mini MiniTest Test'
112
+ add_field name: 'Content', value: 'This is a content section'
113
+ add_field name: 'Subsection', value: 'This is a content subsection'
114
+ end
115
+
116
+ Discord::Notifier.message embed
117
+ end
118
+
119
+ @mock.verify
120
+ end
121
+
122
+ def test_custom_config_message
123
+ @custom_config = {
124
+ url: 'http://custom.com',
125
+ username: 'Gem Config Test',
126
+ avatar_url: 'http://avatar.com/slack.png',
127
+ wait: true
128
+ }
129
+
130
+ expected_payload = {
131
+ content: 'String Message'
132
+ }.merge(@custom_config).to_json
133
+
134
+ @mock = Minitest::Mock.new
135
+ @mock.expect(:post, true, [JSON.parse(expected_payload)])
136
+
137
+ Discord::Notifier.setup do |config|
138
+ config.url = 'http://test.com'
139
+ config.username = 'Gem Test'
140
+ config.avatar_url = 'http://avatar.com/discord.png'
141
+ config.wait = true
142
+ end
143
+
144
+ Net::HTTP.stub :post, ->(uri, params, headers) {
145
+ @mock.post JSON.parse(params)
146
+ } do
147
+ Discord::Notifier.message "String Message", @custom_config
148
+ end
149
+
150
+ @mock.verify
151
+ end
152
+
153
+ def test_multiple_embeds
154
+ expected_payload = {
155
+ url: 'http://test.com',
156
+ username: 'Gem Test',
157
+ avatar_url: 'http://avatar.com/discord.png',
158
+ embeds: [
159
+ {
160
+ title: 'Embed Message Test',
161
+ description: 'Sending an embed through Discord Notifier',
162
+ url: 'http://github.com/ianmitchell/discord_notifier',
163
+ },
164
+ {
165
+ title: 'Second Embed Message Test',
166
+ description: 'Sending an embed through Discord Notifier',
167
+ url: 'http://github.com/ianmitchell/discord_notifier',
168
+ }
169
+ ]
170
+ }.to_json
171
+
172
+ @mock = Minitest::Mock.new
173
+ @mock.expect(:post, true, [JSON.parse(expected_payload)])
174
+
175
+ Discord::Notifier.setup do |config|
176
+ config.url = 'http://test.com'
177
+ config.username = 'Gem Test'
178
+ config.avatar_url = 'http://avatar.com/discord.png'
179
+ end
180
+
181
+ Net::HTTP.stub :post, ->(uri, params, headers) {
182
+ @mock.post JSON.parse(params)
183
+ } do
184
+ embed_one = Discord::Embed.new do
185
+ title 'Embed Message Test'
186
+ description 'Sending an embed through Discord Notifier'
187
+ url 'http://github.com/ianmitchell/discord_notifier'
188
+ end
189
+
190
+ embed_two = Discord::Embed.new do
191
+ title 'Second Embed Message Test'
192
+ description 'Sending an embed through Discord Notifier'
193
+ url 'http://github.com/ianmitchell/discord_notifier'
194
+ end
195
+
196
+ Discord::Notifier.message [embed_one, embed_two]
197
+ end
198
+
199
+ @mock.verify
200
+ end
201
+
202
+ def test_incorrect_message_type
203
+ assert_raises ArgumentError do
204
+ Discord::Notifier.message 42
205
+ end
206
+ end
207
+
208
+ def test_endpoint
209
+ endpoint = Discord::Notifier.endpoint(url: 'http://test.com')
210
+ assert endpoint.eql? URI('http://test.com')
211
+
212
+ endpoint = Discord::Notifier.endpoint(url: 'http://test.com', wait: true)
213
+ assert endpoint.eql? URI('http://test.com?wait=true')
214
+ end
215
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'discord_notifier'
3
+ require 'discord_notifier/version'
4
+
5
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discord-notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian Mitchell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - ian.mitchel1@live.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/discord_notifier.rb
63
+ - lib/discord_notifier/embed.rb
64
+ - lib/discord_notifier/version.rb
65
+ - test/discord/discord_notifier/embed_test.rb
66
+ - test/discord/discord_notifier/version_test.rb
67
+ - test/discord/discord_notifier_test.rb
68
+ - test/test_helper.rb
69
+ homepage: https://github.com/ianmitchell/discord-notifier
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A minimal wrapper for posting Discord Webhooks and Embeds
93
+ test_files:
94
+ - test/discord/discord_notifier/embed_test.rb
95
+ - test/discord/discord_notifier/version_test.rb
96
+ - test/discord/discord_notifier_test.rb
97
+ - test/test_helper.rb