slack_messaging 2.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed1a41806773a2013660b33d83b26c0e66e674df00c67a5f669b2e891baf24c5
4
- data.tar.gz: d57f53b3c1510be3be48b580667308bee30a7eae8500584fe7870e7ce5abc116
3
+ metadata.gz: a73075c6e11593cc450ed0822939778510aadbe4b76ca4f2a7d8174a7592b1af
4
+ data.tar.gz: 50a35bda4778b110200d3ad8ada3f353b2df4b342ecec91286a8fc993907f6db
5
5
  SHA512:
6
- metadata.gz: '09a09b96d5ead64faa64566187efc058491fbdf44b0ef1bce5f0eee20f8185f8e496333afa079b6e0956e9cab461c262017712a83daa29da238c89203a6efa9b'
7
- data.tar.gz: 7d9cdd032f88b3aeafc365a1f9784f56bb750f6a285b405fb662cd5cc46e28ffad2bf43471a075a1301ca1c68bc78555c4aaea2ab8b8088e7e46fe4e93409cdd
6
+ metadata.gz: 5766b459a88ced1e95b8ef00a52d09cac308503ca009ec9a8484e6a05ba50a255f49d74368d7c1104da3ab85d6705f6e25b98cf06dc98916dfb53f7d9cf685ad
7
+ data.tar.gz: ba98335745cffc5f902f8b72a6f3bdc2eca626868d18f48aba876d164509afb6eb3ac5175b577fefde47bcb0dd76981172e61c8987f457b8a30e34e97e3158ae
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
- # Specify your gem's dependencies in slack_messaging.gemspec
4
5
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,129 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slack_messaging (3.1.2)
5
+ activesupport (~> 6.0)
6
+ gli (~> 2.10)
7
+ hashie (~> 4.1)
8
+ highline_wrapper (~> 1.1)
9
+ httparty (~> 0.18)
10
+ json (~> 2.5)
11
+ rack (~> 2.2)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ activesupport (6.1.3)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ zeitwerk (~> 2.3)
22
+ ast (2.4.2)
23
+ coderay (1.1.3)
24
+ concurrent-ruby (1.1.8)
25
+ diff-lcs (1.4.4)
26
+ faker (2.16.0)
27
+ i18n (>= 1.6, < 2)
28
+ ffi (1.15.0)
29
+ formatador (0.2.5)
30
+ gli (2.20.0)
31
+ guard (2.16.2)
32
+ formatador (>= 0.2.4)
33
+ listen (>= 2.7, < 4.0)
34
+ lumberjack (>= 1.0.12, < 2.0)
35
+ nenv (~> 0.1)
36
+ notiffany (~> 0.0)
37
+ pry (>= 0.9.12)
38
+ shellany (~> 0.0)
39
+ thor (>= 0.18.1)
40
+ guard-compat (1.2.1)
41
+ guard-rspec (4.7.3)
42
+ guard (~> 2.1)
43
+ guard-compat (~> 1.1)
44
+ rspec (>= 2.99.0, < 4.0)
45
+ hashie (4.1.0)
46
+ highline (2.0.3)
47
+ highline_wrapper (1.1.0)
48
+ highline (~> 2.0)
49
+ httparty (0.18.1)
50
+ mime-types (~> 3.0)
51
+ multi_xml (>= 0.5.2)
52
+ i18n (1.8.9)
53
+ concurrent-ruby (~> 1.0)
54
+ json (2.5.1)
55
+ listen (3.4.1)
56
+ rb-fsevent (~> 0.10, >= 0.10.3)
57
+ rb-inotify (~> 0.9, >= 0.9.10)
58
+ lumberjack (1.2.8)
59
+ method_source (1.0.0)
60
+ mime-types (3.3.1)
61
+ mime-types-data (~> 3.2015)
62
+ mime-types-data (3.2021.0225)
63
+ minitest (5.14.4)
64
+ multi_xml (0.6.0)
65
+ nenv (0.3.0)
66
+ notiffany (0.1.3)
67
+ nenv (~> 0.1)
68
+ shellany (~> 0.0)
69
+ parallel (1.20.1)
70
+ parser (3.0.0.0)
71
+ ast (~> 2.4.1)
72
+ pry (0.14.0)
73
+ coderay (~> 1.1)
74
+ method_source (~> 1.0)
75
+ rack (2.2.3)
76
+ rainbow (3.0.0)
77
+ rake (13.0.3)
78
+ rb-fsevent (0.10.4)
79
+ rb-inotify (0.10.1)
80
+ ffi (~> 1.0)
81
+ regexp_parser (2.1.1)
82
+ rexml (3.2.4)
83
+ rspec (3.10.0)
84
+ rspec-core (~> 3.10.0)
85
+ rspec-expectations (~> 3.10.0)
86
+ rspec-mocks (~> 3.10.0)
87
+ rspec-core (3.10.1)
88
+ rspec-support (~> 3.10.0)
89
+ rspec-expectations (3.10.1)
90
+ diff-lcs (>= 1.2.0, < 2.0)
91
+ rspec-support (~> 3.10.0)
92
+ rspec-mocks (3.10.2)
93
+ diff-lcs (>= 1.2.0, < 2.0)
94
+ rspec-support (~> 3.10.0)
95
+ rspec-support (3.10.2)
96
+ rubocop (1.11.0)
97
+ parallel (~> 1.10)
98
+ parser (>= 3.0.0.0)
99
+ rainbow (>= 2.2.2, < 4.0)
100
+ regexp_parser (>= 1.8, < 3.0)
101
+ rexml
102
+ rubocop-ast (>= 1.2.0, < 2.0)
103
+ ruby-progressbar (~> 1.7)
104
+ unicode-display_width (>= 1.4.0, < 3.0)
105
+ rubocop-ast (1.4.1)
106
+ parser (>= 2.7.1.5)
107
+ ruby-progressbar (1.11.0)
108
+ shellany (0.0.1)
109
+ thor (1.1.0)
110
+ tzinfo (2.0.4)
111
+ concurrent-ruby (~> 1.0)
112
+ unicode-display_width (2.0.0)
113
+ zeitwerk (2.4.2)
114
+
115
+ PLATFORMS
116
+ x86_64-darwin-20
117
+
118
+ DEPENDENCIES
119
+ bundler (~> 2.2)
120
+ faker (~> 2.15)
121
+ guard-rspec (~> 4.3)
122
+ pry (~> 0.13)
123
+ rake (~> 13.0)
124
+ rspec (~> 3.9)
125
+ rubocop (~> 1.10)
126
+ slack_messaging!
127
+
128
+ BUNDLED WITH
129
+ 2.2.14
data/Guardfile CHANGED
@@ -1,4 +1,6 @@
1
- guard :rspec, cmd: 'bundle exec rspec', all_on_start: true do
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec', all_on_start: true do
2
4
  watch(%r{^spec/.+_spec\.rb$})
3
5
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
6
  watch('spec/spec_helper.rb') { 'spec' }
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Slack Messaging [![Maintainability](https://api.codeclimate.com/v1/badges/9aabbea68d6522f4b308/maintainability)](https://codeclimate.com/github/emmahsax/slack_messaging/maintainability) [![Main](https://github.com/emmahsax/slack_messaging/actions/workflows/main.yml/badge.svg)](https://github.com/emmahsax/slack_messaging/actions/workflows/main.yml)
1
+ # Slack Messaging [![Main](https://github.com/emmahsax/slack_messaging/actions/workflows/main.yml/badge.svg)](https://github.com/emmahsax/slack_messaging/actions/workflows/main.yml)
2
2
 
3
3
  This is a simple project designed to post messages to a given Slack channel as a bot.
4
4
 
@@ -26,42 +26,56 @@ gem install slack_messaging
26
26
 
27
27
  This project requires a config file that should look like this:
28
28
 
29
- ```
29
+ ```yml
30
30
  slack:
31
31
  channel: <AWESOME CHANNEL NAME>
32
32
  username: <AWESOME USER NAME>
33
33
  webhook_url: <SLACK WEBHOOK URL>
34
- icon_emoji: ":<SOME EMOJI>:"
34
+ icon_emoji: ':<SOME EMOJI>:'
35
35
  ```
36
36
 
37
- The default is for the file to be named `~/.slack_messaging.yml`, but a different path can be passed in like this:
37
+ To generate this file at `~/.slack_messaging.yml`, please run this command:
38
38
 
39
+ ```bash
40
+ slack-messaging setup
39
41
  ```
40
- $ slack-messaging --config="/PATH/TO/FILE/config.yml" slack
42
+
43
+ To obtain the webhook URL, go to [this link](https://api.slack.com/messaging/webhooks).
44
+
45
+ If you'd like to create the config file at a different directory, I recommend using the `setup` command, and then manually moving the file to your desired location:
46
+
47
+ ```bash
48
+ slack-messaging setup
49
+ # Walk through the prompts to create the file
50
+ mv ~/.slack_messaging.yml /PATH/TO/FILE/config.yml
41
51
  ```
42
52
 
43
- To obtain the webhook url, go to [this link](https://api.slack.com/incoming-webhooks).
53
+ And then you can pass in that specific file location like this:
44
54
 
45
- Okay, now the project will be ready to rock and roll.
55
+ ```bash
56
+ slack-messaging --config="/PATH/TO/FILE/config.yml" send
57
+ ```
58
+
59
+ Once the config file is set up, the project is ready to go!
46
60
 
47
61
  To print a friendly message to Slack, run:
48
62
 
49
- ```
50
- slack-messaging slack
63
+ ```bash
64
+ slack-messaging send
51
65
  ```
52
66
 
53
- from the main directory. Here, no specific message is being given to print to Slack, so slack_messaging will choose a random quote. The random quotes are selected using the [Quotable API](http://api.quotable.io/).
67
+ Here, no specific message is being given to print to Slack, so slack_messaging will choose a random quote. The random quotes are selected using the [Quotable API](http://api.quotable.io/).
54
68
 
55
69
  However, what if you wanted to print something specific? Well, you can! Just run:
56
70
 
57
- ```
58
- slack-messaging slack "MESSAGE 1"
71
+ ```bash
72
+ slack-messaging send 'MESSAGE 1'
59
73
  ```
60
74
 
61
75
  You can even print multiple messages at once:
62
76
 
63
- ```
64
- slack-messaging slack "MESSAGE 1" "MESSAGE 2" "MESSAGE 3" ... "MESSAGE N"
77
+ ```bash
78
+ slack-messaging send 'MESSAGE 1' 'MESSAGE 2' 'MESSAGE 3' ... 'MESSAGE N'
65
79
  ```
66
80
 
67
81
  The output of slack_messaging will look something like this:
@@ -72,7 +86,7 @@ I hope you enjoy printing fun and specialized messages to Slack!
72
86
 
73
87
  ### Tests
74
88
 
75
- To run the tests, run `bundle exec rspec` from the command line. GitHub Actions will also run the tests upon every commit to make sure they're up to date and that everything is working correctly. Locally, you can also run `bundle exec guard` to automatically run tests as you develop! There are currently only tests for the `notify_slack` and `random_message` classes. Feel free to help us add more!
89
+ To run the tests, run `bundle exec rspec` from the command-line. GitHub Actions will also run the tests upon every commit to make sure they're up to date and that everything is working correctly. Locally, you can also run `bundle exec guard` to automatically run tests as you develop!
76
90
 
77
91
  ## Contributing
78
92
 
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
data/bin/slack-messaging CHANGED
@@ -1,4 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
2
5
  require 'rubygems'
3
6
  require 'gli'
4
7
  require_relative '../lib/slack_messaging'
@@ -10,23 +13,32 @@ version SlackMessaging::VERSION
10
13
 
11
14
  wrap_help_text :verbatim
12
15
 
13
- flag [:config], :desc => 'SlackMessaging config file path', :default_value => SlackMessaging::DefaultPaths.config
16
+ flag [:config], desc: 'Slack Messaging config file path', default_value: SlackMessaging::DefaultPaths.config
14
17
 
15
- program_long_desc """
18
+ program_long_desc "
16
19
  DOCUMENTATION
17
- """
20
+ For documentation and help in setting up your configuration files,
21
+ see Slack Messaging's GitHub repo: https://github.com/emmahsax/slack_messaging
22
+ "
23
+
24
+ pre do |global, _command, _options, _args|
25
+ SlackMessaging::Config.load(global[:config])
26
+ true
27
+ end
18
28
 
19
29
  desc 'Prints a variety of messages to Slack'
20
- command 'slack' do |c|
21
- c.action do |global_options, options, args|
22
- SlackMessaging::Slack.execute(args, options)
30
+ command 'send' do |c|
31
+ c.action do |_global_options, options, args|
32
+ SlackMessaging::Send.execute(args, options)
23
33
  end
24
34
  end
25
35
 
26
- pre do |global,command,options,args|
27
- SlackMessaging::Config.load(global[:config])
28
- SlackMessaging::Config.set_config_options(global)
29
- true
36
+ desc 'Sets up a Slack Messaging config file at ~/.slack_messaging.yml'
37
+ skips_pre
38
+ command 'setup' do |c|
39
+ c.action do
40
+ SlackMessaging::Setup.execute
41
+ end
30
42
  end
31
43
 
32
44
  exit run(ARGV)
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'hashie'
3
5
  require 'httparty'
4
- require 'highline'
6
+ require 'highline_wrapper'
5
7
  require 'json'
6
8
 
7
- Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'slack_messaging')) + '/**/*.rb'].each do |file|
9
+ files = "#{File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'slack_messaging'))}/**/*.rb"
10
+
11
+ Dir[files].each do |file|
8
12
  require_relative file
9
13
  end
10
14
 
@@ -1,27 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class Config
3
- def self.config
4
- config_data.to_hash
5
- end
5
+ class << self
6
+ def method_missing(method, args = false)
7
+ config_data.send(method, args)
8
+ end
6
9
 
7
- def self.load(path)
8
- load_config(path)
9
- config
10
- end
10
+ def load(path)
11
+ load_config(path)
12
+ config
13
+ end
11
14
 
12
- private
15
+ private def config
16
+ config_data.to_hash
17
+ end
13
18
 
14
- def self.config_data
15
- @config_data ||= Hashie::Mash.new
16
- end
19
+ private def config_data
20
+ @config_data ||= Hashie::Mash.new
21
+ end
17
22
 
18
- def self.method_missing(method, args = false)
19
- config_data.send(method, args)
20
- end
23
+ private def load_config(file)
24
+ raise StandardError, "Missing configuration file: #{file}" unless File.exist?(file)
21
25
 
22
- def self.load_config(file)
23
- raise MissingConfig, "Missing configuration file: #{file}" unless File.exist?(file)
24
- YAML.load_file(file).each { |key, value| config_data.assign_property(key, value) }
26
+ YAML.load_file(file).each { |key, value| config_data.assign_property(key, value) }
27
+ end
25
28
  end
26
29
  end
27
30
  end
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class DefaultPaths
3
- def self.config
4
- File.join(self.home, '.slack_messaging.yml')
5
- end
6
-
7
- private
5
+ class << self
6
+ def config
7
+ File.join(home, '.slack_messaging.yml')
8
+ end
8
9
 
9
- def self.home
10
- ENV['HOME'] ? ENV['HOME'] : '.'
10
+ private def home
11
+ ENV['HOME'] || '.'
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class NotifySlack
3
5
  attr_accessor :text, :channel, :webhook_url, :username, :icon_url, :icon_emoji
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class RandomMessage
3
5
  def self.acquire_random_quote
4
6
  random_quote = HTTParty.get('http://api.quotable.io/random', headers: { 'Content-Type': 'application/json' }).body
5
7
  quote_content = JSON.parse(random_quote)['content']
6
8
  quote_author = JSON.parse(random_quote)['author']
7
- return "\"#{quote_content}\"" + "\n" + "—#{quote_author}"
9
+ "\"#{quote_content}\"\n—#{quote_author}"
8
10
  end
9
11
  end
10
12
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackMessaging
4
+ class Send
5
+ class << self
6
+ attr_accessor :options
7
+
8
+ def execute(args, _options = nil)
9
+ if args.empty?
10
+ message = SlackMessaging::RandomMessage.acquire_random_quote
11
+ print_message(message)
12
+ else
13
+ args.each do |arg_message|
14
+ print_message(arg_message)
15
+ end
16
+ end
17
+ end
18
+
19
+ private def print_message(message)
20
+ SlackMessaging::NotifySlack.new(message).perform
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackMessaging
4
+ class Setup
5
+ class << self
6
+ # rubocop:disable Style/ConditionalAssignment
7
+ def execute
8
+ if config_file_exists?
9
+ answer = highline.ask_yes_no(
10
+ "It looks like the #{default_config} file already exists. Do you wish to replace it? (y/n)",
11
+ { required: true }
12
+ )
13
+ else
14
+ answer = true
15
+ end
16
+
17
+ create_or_update_config_file(ask_config_questions) if answer
18
+ end
19
+ # rubocop:enable Style/ConditionalAssignment
20
+
21
+ private def create_or_update_config_file(answers)
22
+ contents = generate_config_file(answers)
23
+ puts "Creating or updating your #{default_config} file..."
24
+ File.open(default_config, 'w') { |file| file.puts contents }
25
+ puts 'Done!'
26
+ end
27
+
28
+ private def generate_config_file(answers)
29
+ file_contents = ''.dup
30
+ file_contents << "slack:\n"
31
+ file_contents << " channel: #{answers[:channel].tr('#', '').strip}\n"
32
+ file_contents << " username: #{answers[:username].strip}\n"
33
+ file_contents << " webhook_url: #{answers[:webhook_url].strip}\n"
34
+ file_contents << " icon_emoji: '#{answers[:icon_emoji].strip}'"
35
+ file_contents
36
+ end
37
+
38
+ private def config_file_exists?
39
+ File.exist?(default_config)
40
+ end
41
+
42
+ # rubocop:disable Metrics/MethodLength
43
+ private def ask_config_questions
44
+ answers = {}
45
+
46
+ answers[:webhook_url] = ask_question(
47
+ "What is your Slack webhook URL? If you don't have one yet, please navigate" \
48
+ ' to https://api.slack.com/messaging/webhooks to create one, and then come back' \
49
+ ' here and paste it in the Terminal.',
50
+ nil,
51
+ required: true
52
+ )
53
+
54
+ answers[:channel] = ask_question(
55
+ 'What slack channel do you wish to post to? (default is "#general")',
56
+ '#general'
57
+ )
58
+
59
+ answers[:username] = ask_question(
60
+ "What slack username do you wish to post as? (default is \"Let's Get Quoty\")",
61
+ "Let's Get Quoty"
62
+ )
63
+
64
+ answers[:icon_emoji] = ask_question(
65
+ 'What emoji would you like to post with (include the colons at the beginning and end' \
66
+ ' of the emoji name)? (default is ":mailbox_with_mail:")',
67
+ ':mailbox_with_mail:'
68
+ )
69
+
70
+ answers
71
+ end
72
+ # rubocop:enable Metrics/MethodLength
73
+
74
+ private def ask_question(prompt, default, required: false)
75
+ highline.ask(prompt, { default: default, required: required })
76
+ end
77
+
78
+ private def highline
79
+ @highline ||= HighlineWrapper.new
80
+ end
81
+
82
+ private def default_config
83
+ SlackMessaging::DefaultPaths.config
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
- VERSION = '2.1.1'
4
+ VERSION = '3.1.2'
3
5
  end
@@ -1,17 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'slack_messaging'
3
5
 
4
6
  describe SlackMessaging::Config do
7
+ subject { SlackMessaging::Config }
8
+
5
9
  context 'config key methods' do
6
10
  it 'should return nil when not set' do
7
- expect(SlackMessaging::Config.doesnt_exist).to eql(nil)
8
- expect(SlackMessaging::Config.doesnt_exist?).to eql(false)
11
+ expect(subject.doesnt_exist).to eql(nil)
12
+ expect(subject.doesnt_exist?).to eql(false)
9
13
  end
10
14
 
11
15
  it 'should return the config value when set' do
12
16
  config_value = Faker::Lorem.word
13
- SlackMessaging::Config.new_value = config_value
14
- expect(SlackMessaging::Config.new_value).to eql(config_value)
17
+ subject.new_value = config_value
18
+ expect(subject.new_value).to eql(config_value)
15
19
  end
16
20
  end
17
21
 
@@ -21,13 +25,13 @@ describe SlackMessaging::Config do
21
25
 
22
26
  let(:config_file) do
23
27
  {
24
- 'domain': domain,
25
- 'slack': {
26
- 'slack_option': true,
27
- 'username': Faker::Name.name,
28
- 'icon_url': Faker::Internet.url,
29
- 'channel': Faker::Lorem.word,
30
- 'webhook': Faker::Internet.url
28
+ domain: domain,
29
+ slack: {
30
+ slack_option: true,
31
+ username: Faker::Name.name,
32
+ icon_url: Faker::Internet.url,
33
+ channel: Faker::Lorem.word,
34
+ webhook: Faker::Internet.url
31
35
  }
32
36
  }
33
37
  end
@@ -35,19 +39,19 @@ describe SlackMessaging::Config do
35
39
  before do
36
40
  allow(YAML).to receive(:load_file).and_return(config_file)
37
41
  allow(File).to receive(:exist?).and_return(true)
38
- SlackMessaging::Config.load(Faker::Lorem.word)
42
+ subject.load(Faker::Lorem.word)
39
43
  end
40
44
 
41
45
  it 'calling a method corresponding to a key in the file should return the value' do
42
- expect(SlackMessaging::Config.domain).to eql(domain)
43
- expect(SlackMessaging::Config.slack).to be_kind_of(Hash)
44
- expect(SlackMessaging::Config.slack[:slack_option]).to eql(true)
46
+ expect(subject.domain).to eql(domain)
47
+ expect(subject.slack).to be_kind_of(Hash)
48
+ expect(subject.slack[:slack_option]).to eql(true)
45
49
  end
46
50
 
47
51
  it 'overwriting values should work' do
48
- expect(SlackMessaging::Config.slack).to be_kind_of(Hash)
49
- SlackMessaging::Config.slack = sentence
50
- expect(SlackMessaging::Config.slack).to eql(sentence)
52
+ expect(subject.slack).to be_kind_of(Hash)
53
+ subject.slack = sentence
54
+ expect(subject.slack).to eql(sentence)
51
55
  end
52
56
  end
53
57
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'slack_messaging'
3
5
 
@@ -10,12 +12,12 @@ describe SlackMessaging::NotifySlack do
10
12
 
11
13
  let(:config_file) do
12
14
  {
13
- 'slack': {
14
- 'slack_option': true,
15
- 'username': username,
16
- 'icon_emoji': emoji,
17
- 'channel': channel,
18
- 'webhook_url': webhook
15
+ slack: {
16
+ slack_option: true,
17
+ username: username,
18
+ icon_emoji: emoji,
19
+ channel: channel,
20
+ webhook_url: webhook
19
21
  }
20
22
  }
21
23
  end
@@ -26,18 +28,18 @@ describe SlackMessaging::NotifySlack do
26
28
  SlackMessaging::Config.load(Faker::Lorem.word)
27
29
  end
28
30
 
31
+ subject { SlackMessaging::NotifySlack.new(sentence) }
32
+
29
33
  it 'should call HTTParty' do
30
34
  expect(HTTParty).to receive(:post)
31
- message = SlackMessaging::NotifySlack.new(sentence)
32
- message.perform
35
+ subject.perform
33
36
  end
34
37
 
35
- it 'should define certain values' do
36
- message = SlackMessaging::NotifySlack.new(sentence)
37
- expect(message.text).to eq(sentence)
38
- expect(message.channel).to eq(channel)
39
- expect(message.username).to eq(username)
40
- expect(message.webhook_url).to eq(webhook)
41
- expect(message.icon_emoji).to eq(emoji)
38
+ it 'should define certain values' do
39
+ expect(subject.text).to eq(sentence)
40
+ expect(subject.channel).to eq(channel)
41
+ expect(subject.username).to eq(username)
42
+ expect(subject.webhook_url).to eq(webhook)
43
+ expect(subject.icon_emoji).to eq(emoji)
42
44
  end
43
45
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'slack_messaging'
3
5
 
@@ -16,28 +18,30 @@ describe SlackMessaging::RandomMessage do
16
18
  allow(HTTParty).to receive(:get).and_return(quote_object)
17
19
  end
18
20
 
21
+ subject { SlackMessaging::RandomMessage }
22
+
19
23
  it 'should get a string message' do
20
- message = SlackMessaging::RandomMessage.acquire_random_quote
24
+ message = subject.acquire_random_quote
21
25
  expect(message).to be_instance_of(String)
22
26
  end
23
27
 
24
28
  it 'should get a message that includes a newline' do
25
- message = SlackMessaging::RandomMessage.acquire_random_quote
29
+ message = subject.acquire_random_quote
26
30
  expect(message.include?("\n")).to eq(true)
27
31
  end
28
32
 
29
33
  it 'should get a message that includes a —' do
30
- message = SlackMessaging::RandomMessage.acquire_random_quote
34
+ message = subject.acquire_random_quote
31
35
  expect(message.include?('—')).to eq(true)
32
36
  end
33
37
 
34
38
  it 'should use HTTParty to ping an API' do
35
39
  expect(HTTParty).to receive(:get)
36
- SlackMessaging::RandomMessage.acquire_random_quote
40
+ subject.acquire_random_quote
37
41
  end
38
42
 
39
43
  it 'should have the JSON parse the response content twice' do
40
44
  expect(JSON).to receive(:parse).at_least(2).times.and_return(JSON.parse(quote_json))
41
- SlackMessaging::RandomMessage.acquire_random_quote
45
+ subject.acquire_random_quote
42
46
  end
43
47
  end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'slack_messaging'
5
+
6
+ describe SlackMessaging::Setup do
7
+ let(:highline_wrapper) { double(:highline_wrapper, ask: Faker::Lorem.word, ask_yes_no: true) }
8
+ let(:answers) do
9
+ {
10
+ channel: Faker::Lorem.word,
11
+ username: Faker::Lorem.word,
12
+ webhook_url: Faker::Internet.url,
13
+ icon_emoji: ":#{Faker::Lorem.word}:"
14
+ }
15
+ end
16
+
17
+ before do
18
+ allow(HighlineWrapper).to receive(:new).and_return(highline_wrapper)
19
+ allow(subject).to receive(:puts)
20
+ end
21
+
22
+ after do
23
+ SlackMessaging::Setup.instance_variable_set('@highline', nil)
24
+ end
25
+
26
+ subject { SlackMessaging::Setup }
27
+
28
+ describe '#self.execute' do
29
+ it 'should ask a question if the config file exists' do
30
+ allow(subject).to receive(:config_file_exists?).and_return(true)
31
+ allow(File).to receive(:exists?).and_return(true)
32
+ expect(highline_wrapper).to receive(:ask_yes_no).and_return(true)
33
+ allow(subject).to receive(:create_or_update_config_file).and_return(true)
34
+ allow(subject).to receive(:ask_config_questions).and_return(true)
35
+ subject.execute
36
+ end
37
+
38
+ it 'should call to create or update the config file' do
39
+ allow(File).to receive(:exists?).and_return(true)
40
+ allow(highline_wrapper).to receive(:ask_yes_no).and_return(true)
41
+ expect(subject).to receive(:create_or_update_config_file).and_return(true)
42
+ expect(subject).to receive(:ask_config_questions).and_return(true)
43
+ subject.execute
44
+ end
45
+
46
+ it 'should skip if the user opts not to continue' do
47
+ allow(subject).to receive(:config_file_exists?).and_return(true)
48
+ allow(File).to receive(:exists?).and_return(true)
49
+ allow(highline_wrapper).to receive(:ask_yes_no).and_return(false)
50
+ expect(subject).not_to receive(:create_or_update_config_file)
51
+ expect(subject).not_to receive(:ask_config_questions)
52
+ subject.execute
53
+ end
54
+ end
55
+
56
+ describe '#self.create_or_update_config_file' do
57
+ it 'should generate the file based on the answers to the questions' do
58
+ expect(subject).to receive(:generate_config_file)
59
+ allow(File).to receive(:open).and_return(nil)
60
+ subject.send(:create_or_update_config_file, answers)
61
+ end
62
+
63
+ it 'should open the file for writing' do
64
+ allow(subject).to receive(:generate_config_file)
65
+ expect(File).to receive(:open).and_return(nil)
66
+ subject.send(:create_or_update_config_file, answers)
67
+ end
68
+ end
69
+
70
+ describe '#self.generate_config_file' do
71
+ it 'returns a string' do
72
+ expect(subject.send(:generate_config_file, answers)).to be_a(String)
73
+ end
74
+ end
75
+
76
+ describe '#self.config_file_exists?' do
77
+ it 'should return true if the file exists' do
78
+ allow(File).to receive(:exist?).and_return(true)
79
+ expect(subject.send(:config_file_exists?)).to eq(true)
80
+ end
81
+
82
+ it 'should return false if the file does not exist' do
83
+ allow(File).to receive(:exist?).and_return(false)
84
+ expect(subject.send(:config_file_exists?)).to eq(false)
85
+ end
86
+ end
87
+
88
+ describe '#self.ask_question' do
89
+ it 'should use highline to ask a question' do
90
+ expect(highline_wrapper).to receive(:ask).and_return('')
91
+ subject.send(:ask_question, Faker::Lorem.sentence, nil)
92
+ end
93
+
94
+ it 'should return nil if the highline client gets an empty string' do
95
+ allow(highline_wrapper).to receive(:ask).and_return(nil)
96
+ expect(subject.send(:ask_question, Faker::Lorem.sentence, nil)).to be_nil
97
+ end
98
+
99
+ it 'should return the answer if it is given' do
100
+ answer = Faker::Lorem.sentence
101
+ allow(highline_wrapper).to receive(:ask).and_return(answer)
102
+ expect(subject.send(:ask_question, Faker::Lorem.sentence, :default)).to be(answer)
103
+ end
104
+ end
105
+
106
+ describe '#self.ask_config_questions' do
107
+ it 'should call to ask at least four questions' do
108
+ expect(subject).to receive(:ask_question).at_least(4).times.and_return(Faker::Lorem.word)
109
+ subject.send(:ask_config_questions)
110
+ end
111
+
112
+ it 'should return the defaults if nothing is given' do
113
+ slack_url = Faker::Internet.url
114
+ defaults = {
115
+ channel: 'general',
116
+ username: "Let's Get Quoty",
117
+ webhook_url: slack_url,
118
+ icon_emoji: ':mailbox_with_mail:'
119
+ }
120
+ allow(subject).to receive(:ask_question).with(
121
+ "What is your Slack webhook URL? If you don't have one yet, please navigate to https://api.slack.com/messaging/webhooks to create one, and then come back here and paste it in the Terminal.",
122
+ nil,
123
+ required: true
124
+ ).and_return(slack_url)
125
+ allow(subject).to receive(:ask_question).with(
126
+ 'What slack channel do you wish to post to? (default is "#general")',
127
+ '#general'
128
+ ).and_return('general')
129
+ allow(subject).to receive(:ask_question).with(
130
+ "What slack username do you wish to post as? (default is \"Let's Get Quoty\")",
131
+ "Let's Get Quoty"
132
+ ).and_return("Let's Get Quoty")
133
+ allow(subject).to receive(:ask_question).with(
134
+ 'What emoji would you like to post with (include the colons at the beginning and end of the emoji name)? (default is ":mailbox_with_mail:")',
135
+ ':mailbox_with_mail:'
136
+ ).and_return(':mailbox_with_mail:')
137
+ expect(subject.send(:ask_config_questions)).to eq(defaults)
138
+ end
139
+ end
140
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faker'
2
4
  require 'pry'
3
5
 
@@ -34,7 +36,7 @@ RSpec.configure do |config|
34
36
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
37
  end
36
38
 
37
- config.filter_run :focus => true
39
+ config.filter_run focus: true
38
40
  config.run_all_when_everything_filtered = true
39
41
 
40
42
  # rspec-mocks config goes here. You can use an alternate test double
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_messaging
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Sax
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-21 00:00:00.000000000 Z
11
+ date: 2021-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: highline
56
+ name: highline_wrapper
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.0'
61
+ version: '1.1'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '2.0'
68
+ version: '1.1'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: httparty
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +192,20 @@ dependencies:
192
192
  - - "~>"
193
193
  - !ruby/object:Gem::Version
194
194
  version: '3.9'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '1.10'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '1.10'
195
209
  description: Sending Personalized Slack Messages to a Slack channel of your choice.
196
210
  email:
197
211
  executables:
@@ -200,6 +214,7 @@ extensions: []
200
214
  extra_rdoc_files: []
201
215
  files:
202
216
  - Gemfile
217
+ - Gemfile.lock
203
218
  - Guardfile
204
219
  - LICENSE
205
220
  - QuoteExample.png
@@ -211,11 +226,13 @@ files:
211
226
  - lib/slack_messaging/default_paths.rb
212
227
  - lib/slack_messaging/notify_slack.rb
213
228
  - lib/slack_messaging/random_message.rb
214
- - lib/slack_messaging/slack.rb
229
+ - lib/slack_messaging/send.rb
230
+ - lib/slack_messaging/setup.rb
215
231
  - lib/slack_messaging/version.rb
216
232
  - spec/slack_messaging/config_spec.rb
217
233
  - spec/slack_messaging/notify_slack_spec.rb
218
234
  - spec/slack_messaging/random_message_spec.rb
235
+ - spec/slack_messaging/setup_spec.rb
219
236
  - spec/spec_helper.rb
220
237
  homepage: https://github.com/emmahsax/slack_messaging
221
238
  licenses:
@@ -229,7 +246,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
246
  requirements:
230
247
  - - ">="
231
248
  - !ruby/object:Gem::Version
232
- version: '0'
249
+ version: '2.4'
233
250
  required_rubygems_version: !ruby/object:Gem::Requirement
234
251
  requirements:
235
252
  - - ">="
@@ -245,3 +262,4 @@ test_files:
245
262
  - spec/slack_messaging/config_spec.rb
246
263
  - spec/slack_messaging/notify_slack_spec.rb
247
264
  - spec/slack_messaging/random_message_spec.rb
265
+ - spec/slack_messaging/setup_spec.rb
@@ -1,22 +0,0 @@
1
- module SlackMessaging
2
- class Slack
3
- attr_accessor :options
4
-
5
- def self.execute(args, options = nil)
6
- if args.empty?
7
- message = SlackMessaging::RandomMessage.acquire_random_quote
8
- print_message(message)
9
- else
10
- args.each do |message|
11
- print_message(message)
12
- end
13
- end
14
- end
15
-
16
- private
17
-
18
- def self.print_message(message)
19
- SlackMessaging::NotifySlack.new(message).perform
20
- end
21
- end
22
- end