hey-you-slack 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 986aefb9851549a114d5cf7cc1e3baa86d0b8bf130bc43fa38c1049c89cead94
4
+ data.tar.gz: d58f338a5d208d88c4518ccce006868680e5547ba2f7300150f9b280a065e4df
5
+ SHA512:
6
+ metadata.gz: 78af35b992bde2f3516a3c053b95b9db4b8c9186eece2cc96c7495cce61baf32b3ee04d9c0def7097b32e1a9a41c74f77ba26324db58d840518b6de78b550c6d
7
+ data.tar.gz: 0fa09f7b2e78b75a4aef2e4015c3337e4e34c14f7990ecef891d6dc3e9fb9919036a5dd0c0f75975e34b11eaf10e65a4823b1e3e85ba4145edf7d739afd7aa00
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
12
+ Gemfile.lock
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ - 2.4.2
6
+ - 2.5.3
7
+ - 2.6.1
8
+ before_install: gem install bundler
9
+ scripts:
10
+ - bundle exec rspec
11
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'byebug'
6
+
7
+ # Specify your gem's dependencies in hey-you-slack.gemspec
8
+ gemspec
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Hey, You, Slack!
2
+ [![Build Status](https://travis-ci.com/QNester/hey-you-slack.svg?branch=master)](https://travis-ci.com/QNester/hey-you-slack#)
3
+ [![Gem Version](https://badge.fury.io/rb/hey-you-slack.svg)](https://badge.fury.io/rb/hey-you-slack)
4
+
5
+ Slack notifications via [hey-you gem](https://github.com/QNester/hey-you).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hey-you-slack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hey-you-slack
22
+
23
+ ## Usage
24
+
25
+ After load gem you can send notifications to slack via [hey-you](https://github.com/QNester/hey-you).
26
+
27
+ For example:
28
+ ```yaml
29
+ # config/notifications.yml
30
+ events:
31
+ signed_up:
32
+ # ...
33
+ slack:
34
+ username: Greeter
35
+ text: 'User with email %{email} was signed up'
36
+ webhook_name: 'channel_name' # key from config.slack.webhooks
37
+ icon_emoji: ':grin:'
38
+ ```
39
+
40
+ ```ruby
41
+ # config/initalizers/hey-you.rb
42
+ HeyYou::Config.configure do
43
+ # [Hash] required - list of slack-webhooks for your applications
44
+ config.slack.webhooks = {
45
+ channel_name: 'http://webhooks.slack/channel_webhook'
46
+ }
47
+
48
+ # [String] optional - default name of author notifications messages in slack chats.
49
+ config.slack.slack_username = 'MyApp_Notifier'
50
+ end
51
+ ```
52
+
53
+ ```ruby
54
+ # // somewhere in your app
55
+ builder = Builder.new('events.signed_up', email: created_user.email)
56
+ HeyYou::Channels::Slack.send!(builder) #=> { success: true }
57
+ # Notification will be received in slack chat :)
58
+ ```
59
+
60
+ ## Development
61
+
62
+ 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.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
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,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'byebug'
5
+ require "hey-you-slack"
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__)
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,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hey_you_slack/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hey-you-slack"
8
+ spec.version = HeyYouSlack::VERSION
9
+ spec.authors = ["Sergey Nesterov"]
10
+ spec.email = ["qnesterr@gmail.com"]
11
+
12
+ spec.summary = "Slack notification via gem `hey-you`."
13
+ spec.description = "This gem extend core gem `hey-you` for send notifications to slack with webhooks."
14
+ spec.homepage = "https://github.com/QNester/hey-you-slack"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/QNester/hey-you-slack"
21
+ spec.metadata["changelog_uri"] = "https://github.com/QNester/hey-you-slack/CHANGELOG.md"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_runtime_dependency "hey-you", '~> 0.1.8'
37
+ spec.add_runtime_dependency "httparty", '~> 0.16'
38
+
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "webmock", '~> 3.4'
42
+ spec.add_development_dependency "ffaker", '~> 2.9'
43
+ end
@@ -0,0 +1,11 @@
1
+ require 'hey-you'
2
+ require 'hey_you_slack/version'
3
+ require_relative 'hey_you/config/slack'
4
+ require_relative 'hey_you/builder/slack'
5
+ require_relative 'hey_you/channels/slack'
6
+
7
+ module HeyYouSlack
8
+ CHANNEL_NAME = 'slack'.freeze
9
+
10
+ HeyYou::Config.instance.registrate_channel(CHANNEL_NAME)
11
+ end
@@ -0,0 +1,25 @@
1
+ module HeyYou
2
+ class Builder
3
+ class Slack < Base
4
+ attr_reader :username, :text, :icon_emoji, :icon_url, :webhook_name
5
+
6
+ def build
7
+ @webhook_name = ch_data.fetch('webhook_name', nil)
8
+ @username = ch_data.fetch('username', nil) || Config.config.slack.slack_username
9
+ @text = interpolate(ch_data.fetch('text'), options)
10
+ @icon_emoji = ch_data.fetch('icon_emoji', nil)
11
+ @icon_url = ch_data.fetch('icon_url', nil)
12
+ end
13
+
14
+ def to_hash
15
+ excluded = [:@data, :@key, :@options]
16
+ self.instance_variables.inject({}) do |memo, var|
17
+ unless excluded.include?(var)
18
+ memo[var.to_s.gsub('@', '')] = instance_variable_get(var)
19
+ end
20
+ memo
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,77 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ module HeyYou
5
+ module Channels
6
+ class Slack < Base
7
+ WEBHOOK_ERR_MSG_TMPL = "slack webhook `%{key}` was not configured".freeze
8
+
9
+ class << self
10
+ # @param [HeyYou::Builder] builder - builder with notifications texts and settings
11
+ # @option [String/Symbol] to - key from `config.slack.webhooks` hash
12
+ def send!(builder, **options)
13
+ raise CredentialsNotExists unless credentials_present?
14
+
15
+ if options[:to]&.is_a?(String) || options[:to]&.is_a?(Symbol)
16
+ return send_to_webhook(builder, options)
17
+ end
18
+
19
+ if builder.slack&.webhook_name
20
+ options.delete(:to)
21
+ return send_to_webhook(builder, options)
22
+ end
23
+
24
+ send_to_multiple_webhooks(builder, options)
25
+ end
26
+
27
+ private
28
+
29
+ def send_to_webhook(builder, **options)
30
+ hook_key = options[:to]&.to_sym || builder.slack.webhook_name.to_sym
31
+ webhook = config.slack.webhooks[hook_key]
32
+
33
+ if webhook == nil || webhook == ""
34
+ raise WebhookError, WEBHOOK_ERR_MSG_TMPL % { key: hook_key }
35
+ end
36
+
37
+ [make_request_to_webhook(hook_key, webhook, builder)]
38
+ end
39
+
40
+ def send_to_multiple_webhooks(builder, **options)
41
+ webhooks = config.slack.webhooks
42
+
43
+ if options[:to]&.is_a?(Array)
44
+ begin
45
+ webhooks = config.slack.webhooks.dup.keep_if { |k, _| options[:to].include?(k) }
46
+ rescue KeyError => err
47
+ raise WebhookError, WEBHOOK_ERR_MSG_TMPL % { key: err.key }
48
+ end
49
+ end
50
+
51
+ webhooks.map do |webhook_name, webhook|
52
+ make_request_to_webhook(webhook_name, webhook, builder)
53
+ end
54
+ end
55
+
56
+ def make_request_to_webhook(webhook_name, webhook, builder)
57
+ config.logger&.info("Send notification to webhook #{webhook_name}")
58
+ response = HTTParty.post(
59
+ webhook,
60
+ body: builder.slack.to_hash.to_json,
61
+ :headers => {
62
+ 'Content-Type' => 'application/json',
63
+ 'Accept' => 'application/json'
64
+ }
65
+ )
66
+ config.logger&.info("Response from slack: #{response.body}")
67
+ { webhook_name => response.body == 'ok' }
68
+ end
69
+
70
+ def required_credentials
71
+ [:webhooks]
72
+ end
73
+ end
74
+ class WebhookError < StandardError; end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+
3
+ module HeyYou
4
+ class Config
5
+ class Slack
6
+ extend Configurable
7
+ attr_accessor :webhooks, :slack_username
8
+
9
+ DEFAULT_USERNAME = 'Hey, you'.freeze
10
+
11
+ def initialize
12
+ @slack_username = DEFAULT_USERNAME
13
+ end
14
+
15
+ def webhooks=(webhooks_hash)
16
+ return ArgumentError, 'webhooks must be a Hash' unless webhooks_hash.is_a?(Hash)
17
+
18
+ @webhooks = webhooks_hash.inject({}) do |memo, (k, v)|
19
+ memo[k.to_sym] = v
20
+ memo
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module HeyYouSlack
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hey-you-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Nesterov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hey-you
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.16'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ffaker
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ description: This gem extend core gem `hey-you` for send notifications to slack with
98
+ webhooks.
99
+ email:
100
+ - qnesterr@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - hey-you-slack.gemspec
114
+ - lib/hey-you-slack.rb
115
+ - lib/hey_you/builder/slack.rb
116
+ - lib/hey_you/channels/slack.rb
117
+ - lib/hey_you/config/slack.rb
118
+ - lib/hey_you_slack/version.rb
119
+ homepage: https://github.com/QNester/hey-you-slack
120
+ licenses: []
121
+ metadata:
122
+ homepage_uri: https://github.com/QNester/hey-you-slack
123
+ source_code_uri: https://github.com/QNester/hey-you-slack
124
+ changelog_uri: https://github.com/QNester/hey-you-slack/CHANGELOG.md
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.0.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Slack notification via gem `hey-you`.
144
+ test_files: []