Skip to content

Commit 7567520

Browse files
committed
Create gem + SlackLayout + tests
1 parent cf1fc28 commit 7567520

14 files changed

+430
-2
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
# rspec failure tracking
11+
.rspec_status

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

Gemfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
# Specify your gem's dependencies in slack_layout.gemspec
6+
gemspec
7+
8+
gem "rake", "~> 13.0"
9+
10+
gem "rspec", "~> 3.0"
11+
12+
gem "slack-ruby-client"
13+
gem "slack-ruby-block-kit"

Gemfile.lock

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
PATH
2+
remote: .
3+
specs:
4+
slack_layout (0.1.0)
5+
slack-ruby-block-kit
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
diff-lcs (1.5.0)
11+
faraday (2.7.10)
12+
faraday-net_http (>= 2.0, < 3.1)
13+
ruby2_keywords (>= 0.0.4)
14+
faraday-mashify (0.1.1)
15+
faraday (~> 2.0)
16+
hashie
17+
faraday-multipart (1.0.4)
18+
multipart-post (~> 2)
19+
faraday-net_http (3.0.2)
20+
gli (2.21.1)
21+
hashie (5.0.0)
22+
multipart-post (2.3.0)
23+
rake (13.0.6)
24+
rspec (3.12.0)
25+
rspec-core (~> 3.12.0)
26+
rspec-expectations (~> 3.12.0)
27+
rspec-mocks (~> 3.12.0)
28+
rspec-core (3.12.2)
29+
rspec-support (~> 3.12.0)
30+
rspec-expectations (3.12.3)
31+
diff-lcs (>= 1.2.0, < 2.0)
32+
rspec-support (~> 3.12.0)
33+
rspec-mocks (3.12.6)
34+
diff-lcs (>= 1.2.0, < 2.0)
35+
rspec-support (~> 3.12.0)
36+
rspec-support (3.12.1)
37+
ruby2_keywords (0.0.5)
38+
slack-ruby-block-kit (0.21.0)
39+
zeitwerk (~> 2.6)
40+
slack-ruby-client (2.1.0)
41+
faraday (>= 2.0)
42+
faraday-mashify
43+
faraday-multipart
44+
gli
45+
hashie
46+
zeitwerk (2.6.9)
47+
48+
PLATFORMS
49+
x86_64-darwin-19
50+
51+
DEPENDENCIES
52+
rake (~> 13.0)
53+
rspec (~> 3.0)
54+
slack-ruby-block-kit
55+
slack-ruby-client
56+
slack_layout!
57+
58+
BUNDLED WITH
59+
2.2.22

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Borja GVO
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.

README.md

+139-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,139 @@
1-
# slack_layouts
2-
A ruby gem that introduces an easy pattern to build Slack's BlockKit layouts with ease.
1+
# SlackLayout
2+
3+
A ruby gem that makes building Slack's BlockKit layouts easy while keeping everything organized.
4+
5+
It leverages [Slack::BlockKit](https://github.com/CGA1123/slack-ruby-block-kit) by providing an inheritance pattern that makes building new layouts convenient and fast.
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'slack_layout'
13+
```
14+
15+
And then execute:
16+
17+
$ bundle install
18+
19+
Or install it yourself as:
20+
21+
$ gem install slack_layout
22+
23+
## Usage
24+
To create a new layout create a new class that inherits from SlackLayout. For example:
25+
26+
```ruby
27+
class MyCustomSlackLayout < SlackLayout
28+
attr_reader :param1, :param2
29+
30+
def initialize(param1, param2)
31+
@param1 = param1
32+
@param2 = param2
33+
super()
34+
end
35+
36+
def blocks
37+
section do |section|
38+
section.mrkdwn_field text: param1
39+
end
40+
41+
actions do |actions|
42+
actions.button(
43+
text: "Archive Now!",
44+
style: "danger",
45+
value: param2,
46+
action_id: "archive_now",
47+
emoji: true,
48+
)
49+
50+
actions.button(
51+
text: "Snooze 14 days",
52+
style: "primary",
53+
value: param2,
54+
action_id: "archive_snooze",
55+
emoji: true,
56+
)
57+
end
58+
end
59+
end
60+
```
61+
Then you can use it anywhere in your code:
62+
63+
```ruby
64+
param1 = "22"
65+
param2 = "Anything you want to use in your layout"
66+
layout = MyCustomSlackLayout.new(param1, param2)
67+
```
68+
69+
Now, we can access the layout json that Slack needs to be sent by calling `as_json` on the layout:
70+
71+
```ruby
72+
layout.as_json
73+
```
74+
75+
Which will produce the following json:
76+
77+
```json
78+
[
79+
{
80+
"type": "section",
81+
"fields": [
82+
{
83+
"type": "mrkdwn",
84+
"text": "The message for a custom layout"
85+
}
86+
]
87+
},
88+
{
89+
"type": "actions",
90+
"elements": [
91+
{
92+
"type": "button",
93+
"text": {
94+
"type": "plain_text",
95+
"text": "Archive Now!",
96+
"emoji": true
97+
},
98+
"action_id": "archive_now",
99+
"value": "22",
100+
"style": "danger"
101+
},
102+
{
103+
"type": "button",
104+
"text": {
105+
"type": "plain_text",
106+
"text": "Snooze 14 days",
107+
"emoji": true
108+
},
109+
"action_id": "archive_snooze",
110+
"value": "22",
111+
"style": "primary"
112+
}
113+
]
114+
}
115+
]
116+
```
117+
118+
Then, use it anywhere in Slack. For example, you could use it to extend the functionality of a message in one of your Slack channels.
119+
120+
```ruby
121+
# Assuming $slack is an already initialized Slack ruby client
122+
$slack.chat_postMessage(channel: channel_id, blocks: layout.as_json)
123+
```
124+
125+
For a complete guide on building with Block Kit, please visit https://api.slack.com/block-kit.
126+
127+
## Development
128+
129+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
130+
131+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132+
133+
## Contributing
134+
135+
Bug reports and pull requests are welcome at https://github.com/bgvo/slack_layout.
136+
137+
## License
138+
139+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -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

bin/console

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "slack_layout"
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__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -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

lib/slack_layout.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "slack_layout/version"
4+
require "slack-ruby-block-kit"
5+
6+
class SlackLayout
7+
attr_accessor :layout
8+
9+
def initialize
10+
Slack::BlockKit.blocks do |layout|
11+
@layout = layout
12+
end
13+
build
14+
end
15+
16+
def build
17+
blocks
18+
end
19+
20+
def blocks
21+
raise NotImplementedError
22+
end
23+
24+
def as_json
25+
layout.as_json
26+
end
27+
28+
def method_missing(method, *args, &block)
29+
layout.send(method, *args, &block)
30+
end
31+
end

lib/slack_layout/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
VERSION = "0.1.0"

slack_layout.gemspec

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "lib/slack_layout/version"
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = "slack_layout"
7+
spec.version = VERSION
8+
spec.authors = ["Borja Garcia de Vinuesa Ordovas"]
9+
spec.email = ["[email protected]"]
10+
11+
spec.summary = "Build Slack layouts with ease using BlockKit"
12+
spec.description = "Pretty easy to use gem to build Slack layouts using BlockKit."
13+
spec.homepage = "https://bgvo.io"
14+
spec.license = "MIT"
15+
spec.required_ruby_version = ">= 2.4.0"
16+
17+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18+
19+
spec.metadata["homepage_uri"] = spec.homepage
20+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22+
23+
# Specify which files should be added to the gem when it is released.
24+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25+
# spec.files = Dir.chdir(File.expand_path(__dir__)) do
26+
# `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27+
# end
28+
spec.files = ["lib/slack_layout.rb"]
29+
spec.bindir = "exe"
30+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31+
spec.require_paths = ["lib"]
32+
33+
# Uncomment to register a new dependency of your gem
34+
spec.add_runtime_dependency "slack-ruby-block-kit"
35+
36+
# For more information and examples about making a new gem, checkout our
37+
# guide at: https://bundler.io/guides/creating_gem.html
38+
end

0 commit comments

Comments
 (0)