slack_messenger 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d6ae454046f542a25563ea04c4b951190a0ab6a
4
+ data.tar.gz: 9b844f70cd74566c45a2811a19062788c844285b
5
+ SHA512:
6
+ metadata.gz: bf66d653c8c241b8c9e8ea77361e14f1028adac287a5b3cd437b7fe262b9be50deb374d907d16e08cb61dd8b193b031a8c8da90f05238ff607abc868063099d1
7
+ data.tar.gz: a0e4907a612392c028ff3e492040970060bcfaf46337080c254f4f88679f537d8bfef7c237cec7ca92f70239ce8ae0b99e1dde2270cbf2773ac8a95bd2f0e5f4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slack_messenger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 JT
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,120 @@
1
+ # SlackMessenger
2
+
3
+ SlackMessenger is an easy-to-use gem to build and send messages via Slack's Incoming Webhooks API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'slack_messenger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install slack_messenger
20
+
21
+ ## Usage
22
+
23
+ ### Basic Usage
24
+
25
+ First, setup a default API endpoint. You can create an Incoming Webhook integration on your Slack account. Use the Webhook URL provided by the integration.
26
+
27
+ By default, the API endpoint is set to use `ENV['SLACK_API_ENDPOINT']`, but you can customize this in your initializers.
28
+
29
+ Additionally, you may customize the default colour of your attachments here.
30
+
31
+ ```ruby
32
+ # initializers/slack_messenger.rb
33
+ SlackMessenger.configure do |config|
34
+ config.default_api = SlackMessenger::Api.new ENV['SOME_OTHER_VARIABLE']
35
+ config.attachment_color = '#eeeeee' # Color of sidebar on attachments. Set to a hex value.
36
+ end
37
+ ```
38
+
39
+ Sending a message is easy once you have an endpoint:
40
+
41
+ ```ruby
42
+ message = SlackMessenger::Message.new "Message Text"
43
+ message.send! # Your message is sent!
44
+ ```
45
+
46
+ If you need further customization, you can add attachments and attachment fields to provide more information.
47
+
48
+ ```ruby
49
+ # Attachment Field
50
+ field = SlackMessenger::AttachmentField.new title: "New Field", value: "Field Text", short: false
51
+
52
+ # Message Attachment
53
+ attachment = SlackMessenger::Attachment.new text: "Attachment text", color: '#000000', fields: field
54
+
55
+ # Create and sent message
56
+ message = SlackMessenger::Message.new text: "Message Text", attachments: attachment
57
+ message.send!
58
+ ```
59
+
60
+ Attachments and fields can be passed as an array or as single objects.
61
+
62
+ ### Multiple Endpoints
63
+
64
+ At this stage, SlackMessenger only supports one default endpoint. Optionally, when sending message, you may create and pass another Api object when sending a message. This will override the default.
65
+
66
+ ```ruby
67
+ message = SlackMessenger::Message.new text: "Message Text"
68
+ second_api = SlackMessenger::Api.new ENV['SECOND_ENDPOINT']
69
+
70
+ message.send! second_api # Overrides the configured default_api
71
+ ```
72
+
73
+ #### `Api` Options
74
+ Api objects only take a single `endpoint` argument
75
+
76
+ #### `Message` Options
77
+ Messages can take 2 options
78
+ ```ruby
79
+ :text # Main body of message
80
+ :attachments # An array or single instance of Attachment objects
81
+ ```
82
+
83
+ #### `Attachment` Options
84
+ Attachments can take a number of options:
85
+ ```ruby
86
+ :fallback # Required plain-text summary of the attachment
87
+ :color # A hex value. Overrides the default attachment_color
88
+ :pretext # Optional text that appears above the attachment block
89
+ :author_name # Optional text that appears at the head of attachment block
90
+ :author_link # A URL for the author_namee (turns author_name into a link if set)
91
+ :author_icon # A URL pointing to an image for the author_name
92
+ :title # Bolded text that appears at head of attachment block (below author if set)
93
+ :title_link # A URL for title (turns title into a link if set)
94
+ :text # Text that appears in attachment
95
+ :fields # An array or single instance of AttachmentField objects
96
+ :image_url # URL pointing to an image. Appears at bottom of attachment block
97
+ :thumb_url # URL pointing to an image. Appears at bottom of attachment block (smaller)
98
+ :footer # Text that appears at bottom of attachment block
99
+ :footer_icon # URL of icon that appears in footer
100
+ :ts # Optional timestamp (in seconds eg Time.now.to_i)
101
+ ```
102
+
103
+ #### `AttachmentField` Options
104
+ AttachmentFields can take 3 options
105
+ ```ruby
106
+ title: # Bolded text at the top of field
107
+ value: # Unformatted text that appears in body of field
108
+ short: # Boolean value that sets width of field to 50% if true, otherwise is 100% of message width
109
+ ```
110
+
111
+
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jt-platterz/slack_messenger.
116
+
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slack_messenger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -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
@@ -0,0 +1,28 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ module SlackMessenger
5
+ class Api
6
+ def initialize(endpoint)
7
+ @endpoint = endpoint
8
+ end
9
+
10
+ def endpoint
11
+ @endpoint
12
+ end
13
+
14
+ def self.send(message, api = nil)
15
+ api = SlackMessenger.default_api if api.nil?
16
+
17
+ raise "default_api not set" if api.nil?
18
+ raise "No slack endpoint set" if api.endpoint.nil?
19
+
20
+ result = HTTParty.post(api.endpoint, body: message.as_json.to_json)
21
+ if result.code == 200
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ module SlackMessenger
2
+ class Attachment
3
+ def initialize(options = {})
4
+ @fallback = options[:fallback]
5
+ @color = options[:color] || SlackMessenger.attachment_color
6
+ @pretext = options[:pretext]
7
+ @author_name = options[:author_name]
8
+ @author_link = options[:author_link]
9
+ @author_icon = options[:author_icon]
10
+ @title = options[:title]
11
+ @title_link = options[:title_link]
12
+ @text = options[:text] || ""
13
+
14
+ if options.has_key? :fields
15
+ @fields = options[:fields].is_a?(Array) ? options[:fields] : [options[:fields]]
16
+ end
17
+
18
+ @image_url = options[:image_url]
19
+ @thumb_url = options[:thumb_url]
20
+ @footer = options[:footer]
21
+ @footer_icon = options[:footer_icon]
22
+ @ts = options[:ts] || false
23
+ end
24
+
25
+ def as_json
26
+ hash = {}
27
+ hash[:fallback] = @fallback unless @fallback.nil?
28
+ hash[:color] = @color
29
+ hash[:pretext] = @pretext unless @pretext.nil?
30
+ hash[:author_name] = @author_name unless @author_name.nil?
31
+ hash[:author_link] = @author_name unless @author_link.nil?
32
+ hash[:author_icon] = @author_icon unless @author_icon.nil?
33
+ hash[:title] = @title unless @title.nil?
34
+ hash[:title_link] = @title_link unless @title_link.nil?
35
+ hash[:text] = @text unless @text.nil?
36
+ hash[:fields] = @fields.map(&:as_json) unless @fields.nil? || @fields.empty?
37
+ hash[:image_url] = @image_url unless @image_url.nil?
38
+ hash[:thumb_url] = @thumb_url unless @thumb_url.nil?
39
+ hash[:footer] = @footer unless @footer.nil?
40
+ hash[:footer_icon] = @footer_icon unless @footer_icon.nil?
41
+ hash[:ts] = Time.now.to_i if @ts
42
+
43
+ hash
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ module SlackMessenger
2
+ class AttachmentField
3
+ def initialize(options = {})
4
+ @title = options[:title]
5
+ @value = options[:value] || "Attachment Text"
6
+ @short = options[:short]
7
+ end
8
+
9
+ def as_json
10
+ hash = {value: @value}
11
+ hash[:title] = @title unless @title.nil?
12
+ hash[:short] = @title unless @short.nil?
13
+ hash
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module SlackMessenger
2
+ class Message
3
+ def initialize(options = {})
4
+ @text = options[:text] || ""
5
+
6
+ if options.has_key? :attachments
7
+ @attachments = options[:attachments].is_a?(Array) ? options[:attachments] : [options[:attachments]]
8
+ end
9
+ end
10
+
11
+ def send! override_api = nil
12
+ Api.send(self, override_api)
13
+ end
14
+
15
+ def as_json
16
+ hash = {text: @text}
17
+ hash[:attachments] = @attachments.map(&:as_json) unless @attachments.nil? || @attachments.empty?
18
+ hash
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module SlackMessenger
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require "slack_messenger/version"
2
+ require "slack_messenger/api"
3
+ require "slack_messenger/message"
4
+ require "slack_messenger/attachment"
5
+ require "slack_messenger/attachment_field"
6
+
7
+ module SlackMessenger
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configure
13
+ self.configuration ||= Configuration.new
14
+ yield(configuration)
15
+ end
16
+
17
+ def self.default_api
18
+ configuration.default_api
19
+ end
20
+
21
+ def self.attachment_color
22
+ configuration.attachment_color
23
+ end
24
+
25
+ class Configuration
26
+ attr_accessor :default_api
27
+ attr_accessor :attachment_color
28
+
29
+ def initialize
30
+ self.default_api = SlackMessenger::Api.new ENV['SLACK_API_ENDPOINT']
31
+ self.attachment_color = '#000000'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slack_messenger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slack_messenger"
8
+ spec.version = SlackMessenger::VERSION
9
+ spec.authors = ["Josh Tate"]
10
+ spec.email = ["josh@joshtate.me"]
11
+
12
+ spec.summary = "Easy sending of Slack notifications"
13
+ spec.homepage = "https://github.com/jt-platterz/slack_messenger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "httparty", "~> 0.13"
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack_messenger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Tate
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-19 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
69
+ description:
70
+ email:
71
+ - josh@joshtate.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/slack_messenger.rb
86
+ - lib/slack_messenger/api.rb
87
+ - lib/slack_messenger/attachment.rb
88
+ - lib/slack_messenger/attachment_field.rb
89
+ - lib/slack_messenger/message.rb
90
+ - lib/slack_messenger/version.rb
91
+ - slack_messenger.gemspec
92
+ homepage: https://github.com/jt-platterz/slack_messenger
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Easy sending of Slack notifications
116
+ test_files: []
117
+ has_rdoc: