slack-ruby-block-kit 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +49 -0
  3. data/.gitignore +13 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +15 -0
  6. data/.rubocop_todo.yml +38 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +59 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +95 -0
  11. data/Rakefile +8 -0
  12. data/lib/slack-ruby-block-kit.rb +3 -0
  13. data/lib/slack/block_kit.rb +26 -0
  14. data/lib/slack/block_kit/blocks.rb +71 -0
  15. data/lib/slack/block_kit/composition/confirmation_dialog.rb +53 -0
  16. data/lib/slack/block_kit/composition/mrkdwn.rb +28 -0
  17. data/lib/slack/block_kit/composition/option.rb +25 -0
  18. data/lib/slack/block_kit/composition/option_group.rb +35 -0
  19. data/lib/slack/block_kit/composition/plain_text.rb +27 -0
  20. data/lib/slack/block_kit/element/button.rb +48 -0
  21. data/lib/slack/block_kit/element/channels_select.rb +48 -0
  22. data/lib/slack/block_kit/element/conversations_select.rb +49 -0
  23. data/lib/slack/block_kit/element/date_picker.rb +48 -0
  24. data/lib/slack/block_kit/element/external_select.rb +56 -0
  25. data/lib/slack/block_kit/element/image.rb +29 -0
  26. data/lib/slack/block_kit/element/overflow_menu.rb +58 -0
  27. data/lib/slack/block_kit/element/static_select.rb +81 -0
  28. data/lib/slack/block_kit/element/users_select.rb +48 -0
  29. data/lib/slack/block_kit/layout/actions.rb +138 -0
  30. data/lib/slack/block_kit/layout/context.rb +48 -0
  31. data/lib/slack/block_kit/layout/divider.rb +26 -0
  32. data/lib/slack/block_kit/layout/image.rb +37 -0
  33. data/lib/slack/block_kit/layout/section.rb +173 -0
  34. data/slack-ruby-block-kit.gemspec +37 -0
  35. metadata +161 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40f294a5c8ef52820a0db8fd474a4cba423dad298f14092217f6213b47ba45ad
4
+ data.tar.gz: e22e8b941d6d3c765c6f40735bd97ecd11f94ac4560e3ba038e0cef073a72241
5
+ SHA512:
6
+ metadata.gz: d7d952242309b0906da8e5ae6072f618a200e3604ad304ceeb323e2387ec5185cadee7e1554729f9beaf004431f0c97053d30c462d40c0c9bee6aaaa1cd91b88
7
+ data.tar.gz: 639e9d8b9b57c99022e0080233a0ecae95f958e04732a0f2bd0fc41cc1b704918fa0eb04a28d6e451a2d373f823c1fb8e40c1890d24ebe5d60af58150e6c27cb
@@ -0,0 +1,49 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5.3
6
+
7
+ working_directory: ~/repo
8
+
9
+ steps:
10
+ - checkout
11
+ - restore_cache:
12
+ keys:
13
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
14
+ - v1-dependencies-
15
+
16
+ - run:
17
+ name: bundle
18
+ command: |
19
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
20
+
21
+ - save_cache:
22
+ paths:
23
+ - ./vendor/bundle
24
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
25
+
26
+ - run:
27
+ name: rspec
28
+ command: |
29
+ mkdir /tmp/test-results
30
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
31
+ circleci tests split --split-by=timings)"
32
+
33
+ bundle exec rspec \
34
+ --format progress \
35
+ --format RspecJunitFormatter \
36
+ --out /tmp/test-results/rspec.xml \
37
+ --format progress \
38
+ $TEST_FILES
39
+
40
+ # collect reports
41
+ - store_test_results:
42
+ path: /tmp/test-results
43
+ - store_artifacts:
44
+ path: /tmp/test-results
45
+ destination: test-results
46
+
47
+ - run:
48
+ name: rubocop
49
+ command: bundle exec rubocop -p
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ require: rubocop-rspec
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.5.3
6
+
7
+ Metrics/LineLength:
8
+ Max: 120
9
+
10
+ Style/AsciiComments:
11
+ Enabled: false
12
+
13
+ Naming/FileName:
14
+ Exclude:
15
+ - lib/slack-ruby-block-kit.rb
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,38 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config --no-auto-gen-timestamp`
3
+ # using RuboCop version 0.63.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 5
10
+ # Configuration parameters: CountComments, ExcludedMethods.
11
+ # ExcludedMethods: refine
12
+ Metrics/BlockLength:
13
+ Max: 63
14
+
15
+ # Offense count: 1
16
+ # Configuration parameters: CountComments.
17
+ Metrics/ClassLength:
18
+ Max: 117
19
+
20
+ # Offense count: 3
21
+ # Configuration parameters: CountKeywordArgs.
22
+ Metrics/ParameterLists:
23
+ Max: 6
24
+
25
+ # Offense count: 5
26
+ # Configuration parameters: Max.
27
+ RSpec/ExampleLength:
28
+ Exclude:
29
+ - 'spec/lib/slack/block_kit/composition/confirmation_dialog_spec.rb'
30
+ - 'spec/lib/slack/block_kit/composition/option_group_spec.rb'
31
+ - 'spec/lib/slack/block_kit/composition/option_spec.rb'
32
+
33
+ # Offense count: 4
34
+ Style/Documentation:
35
+ Exclude:
36
+ - 'spec/**/*'
37
+ - 'test/**/*'
38
+ - 'lib/slack/block_kit.rb'
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 tenios-rb.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slack-ruby-block-kit (0.4.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ jaro_winkler (1.5.2)
12
+ parallel (1.13.0)
13
+ parser (2.6.0.0)
14
+ ast (~> 2.4.0)
15
+ powerpack (0.1.2)
16
+ rainbow (3.0.0)
17
+ rake (13.0.1)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.0)
31
+ rspec_junit_formatter (0.4.1)
32
+ rspec-core (>= 2, < 4, != 2.12.0)
33
+ rubocop (0.63.1)
34
+ jaro_winkler (~> 1.5.1)
35
+ parallel (~> 1.10)
36
+ parser (>= 2.5, != 2.5.1.1)
37
+ powerpack (~> 0.1)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (~> 1.4.0)
41
+ rubocop-rspec (1.32.0)
42
+ rubocop (>= 0.60.0)
43
+ ruby-progressbar (1.10.0)
44
+ unicode-display_width (1.4.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (< 2.0)
51
+ rake
52
+ rspec
53
+ rspec_junit_formatter
54
+ rubocop
55
+ rubocop-rspec
56
+ slack-ruby-block-kit!
57
+
58
+ BUNDLED WITH
59
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Christian Gregg
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.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ [![CircleCI](https://circleci.com/gh/CGA1123/slack_block_kit-ruby/tree/master.svg?style=svg)](https://circleci.com/gh/CGA1123/slack_block_kit-ruby/tree/master)
2
+ [![Gem Version](https://badge.fury.io/rb/slack_block_kit.svg)](https://badge.fury.io/rb/slack_block_kit)
3
+
4
+ # Slack::BlockKit
5
+
6
+ A collection of ruby objects that represent Slack's block kit.
7
+
8
+ You can play around with the block kit using [Slack's block kit builder!](https://api.slack.com/tools/block-kit-builder)
9
+
10
+ The 'blocks' availables are split in line with how Slack documents them, that is:
11
+
12
+ - `Slack::BlockKit::Layout` contains the [Layout blocks](https://api.slack.com/reference/messaging/blocks)
13
+ - `Slack::BlockKit::Element` contains the [Block elements](https://api.slack.com/reference/messaging/block-elements)
14
+ - `Slack::BlockKit::Composition` contains the [Message composition objects](https://api.slack.com/reference/messaging/composition-objects)
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'slack_block_kit'
22
+ ```
23
+
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install slack_block_kit
33
+
34
+ Finally, require this:
35
+
36
+ ```ruby
37
+ require 'slack/block_kit'
38
+ ```
39
+
40
+ ## Examples
41
+
42
+ Here are a few examples that might help you get started!
43
+
44
+ ```ruby
45
+ require 'faraday'
46
+ require 'slack/block_kit'
47
+ require 'json'
48
+
49
+ a_prebuilt_block = Slack::BlockKit::Layout::Section.new
50
+ text = Slack::BlockKit::Composition::Mrkdwn.new(text: ':wave: *hello*')
51
+ an_image = Slack::BlockKit::Element::Image.new(image_url: 'https://git.io/fjDW8', alt_text: 'a picture')
52
+ a_prebuilt_block.accessorise(an_image)
53
+ a_prebuilt_block.text = text
54
+
55
+ blocks = Slack::BlockKit.blocks do |b|
56
+ b.section do |s|
57
+ s.plain_text(text: 'Some plain text message!')
58
+ s.button(text: 'A button that is important', style: 'primary', action_id: 'id')
59
+ end
60
+
61
+ b.divider
62
+
63
+ b.context do |c|
64
+ c.mrkdwn(text: '_some italicised text for context_')
65
+ end
66
+
67
+ b.append(a_prebuilt_block)
68
+ end
69
+
70
+ webhook_url = 'https://hooks.slack.com/services/your/webhook/url'
71
+ body = { blocks: blocks.as_json, text: 'New block message!' }
72
+
73
+ response = Faraday.post(
74
+ webhook_url,
75
+ body.to_json,
76
+ 'Content-Type' => 'application/json'
77
+ )
78
+ ```
79
+
80
+ This will create a message like this:
81
+
82
+ ![example block message](https://git.io/fjDWR)
83
+
84
+ You can also check out the [`slackerduty`](https://github.com/CGA1123/slackerduty) project for some example,
85
+ [`Slackerduty::Alert`](https://github.com/CGA1123/slackerduty/blob/b33d708124ddf36d1432080ba7e16e66fefa6993/lib/slackerduty/alert.rb#L28-L34) and [`Slackerduty::Blocks`](https://github.com/CGA1123/slackerduty/blob/master/lib/slackerduty/blocks) may be helpful places to start.
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CGA1123/slack_block_kit-ruby
90
+
91
+ See [issues](https://github.com/CGA1123/slack_block_kit-ruby/issues) if you want any inspiration as to what to help with!
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './slack/block_kit'
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition; end
6
+ module Element; end
7
+ module Layout; end
8
+
9
+ VERSION = '0.4.0'
10
+
11
+ Dir[File.join(__dir__, 'block_kit', 'composition', '*.rb')].each { |file| require file }
12
+ Dir[File.join(__dir__, 'block_kit', 'element', '*.rb')].each { |file| require file }
13
+ Dir[File.join(__dir__, 'block_kit', 'layout', '*.rb')].each { |file| require file }
14
+ Dir[File.join(__dir__, 'block_kit', '*.rb')].each { |file| require file }
15
+
16
+ module_function
17
+
18
+ def blocks
19
+ blocks = Blocks.new
20
+
21
+ yield(blocks) if block_given?
22
+
23
+ blocks
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ # Blocks are a series of components that can be combined to create visually
6
+ # rich and compellingly interactive messages.
7
+ #
8
+ # https://api.slack.com/reference/messaging/blocks
9
+ class Blocks
10
+ attr_accessor :blocks
11
+
12
+ def initialize
13
+ @blocks = []
14
+
15
+ yield(self) if block_given?
16
+ end
17
+
18
+ def actions(block_id: nil)
19
+ block = Layout::Actions.new(block_id: block_id)
20
+
21
+ yield(block) if block_given?
22
+
23
+ append(block)
24
+ end
25
+
26
+ def context(block_id: nil)
27
+ block = Layout::Context.new(block_id: block_id)
28
+
29
+ yield(block) if block_given?
30
+
31
+ append(block)
32
+ end
33
+
34
+ def divider(block_id: nil)
35
+ block = Layout::Divider.new(block_id: block_id)
36
+
37
+ append(block)
38
+ end
39
+
40
+ def image(url:, alt_text:, title: nil, block_id: nil, emoji: nil)
41
+ block = Layout::Image.new(
42
+ url: url,
43
+ alt_text: alt_text,
44
+ title: title,
45
+ block_id: block_id,
46
+ emoji: emoji
47
+ )
48
+
49
+ append(block)
50
+ end
51
+
52
+ def section(block_id: nil)
53
+ block = Layout::Section.new(block_id: block_id)
54
+
55
+ yield(block) if block_given?
56
+
57
+ append(block)
58
+ end
59
+
60
+ def append(block)
61
+ @blocks << block
62
+
63
+ self
64
+ end
65
+
66
+ def as_json(*)
67
+ @blocks.map(&:as_json)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition
6
+ # An object that defines a dialog that provides a confirmation step to any
7
+ # interactive element. This dialog will ask the user to confirm their
8
+ # action by offering confirm and deny buttons.
9
+ #
10
+ # https://api.slack.com/reference/messaging/composition-objects#confirm
11
+ class ConfirmationDialog
12
+ def title(text:, emoji: nil)
13
+ @title = PlainText.new(text: text, emoji: emoji)
14
+
15
+ self
16
+ end
17
+
18
+ def confirm(text:, emoji: nil)
19
+ @confirm = PlainText.new(text: text, emoji: emoji)
20
+
21
+ self
22
+ end
23
+
24
+ def deny(text:, emoji: nil)
25
+ @deny = PlainText.new(text: text, emoji: emoji)
26
+
27
+ self
28
+ end
29
+
30
+ def plain_text(text:, emoji: nil)
31
+ @text = PlainText.new(text: text, emoji: emoji)
32
+
33
+ self
34
+ end
35
+
36
+ def mrkdwn(text:, verbatim: nil)
37
+ @text = Mrkdwn.new(text: text, verbatim: verbatim)
38
+
39
+ self
40
+ end
41
+
42
+ def as_json(*)
43
+ {
44
+ title: @title.as_json,
45
+ text: @text.as_json,
46
+ confirm: @confirm.as_json,
47
+ deny: @deny.as_json
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end