|
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). |
0 commit comments