slack_messaging 1.4.0 → 3.0.0

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: 3508ee5d59522e0a8d3c931e165bee408d707240fc4c3a1aadbabd2d5b6799f9
4
- data.tar.gz: 67bc57eb2943f0f9f801eac68e25988ee4b06ac20835dd2f46b24b4599d213d5
3
+ metadata.gz: e6742192033a64bb2935e5af6486ea356d54684f501515f22eb8a24c200fff61
4
+ data.tar.gz: 0d880c5cd858c778f06c9047e911927a1cb2ab2ee6b53a89b20b9e23a61ad2a9
5
5
  SHA512:
6
- metadata.gz: 0aa1c61dcb454f5beef05a9ecc13da9e16a258812952013675d2bc5a4b3230d6f80f9b08d47d3de0168a6fb85a180fe1a9d2b78e6b68474c6ae615fdf7639081
7
- data.tar.gz: dd9967c7e2347435a1194961f03f7d5329f45cb839a47130cc1bb901cd324234e854a457a274cfccc54c9903cd322157f06dc0b61baa9e283d21daa549c7dc51
6
+ metadata.gz: 8458be91785ae94772e960f5f0705a2a611b36c286f694d0eb0ac4abe86fdd9d6867aa89d45dc9c44d721dbdb7bd9f34365ca5121f1d85ac0fd027d33439dbf4
7
+ data.tar.gz: 7485f64ff53624785968f06a25e109a8042aa1e7e63412d7c7a40b78da8209f643e06f62130b3c8e7ba8d0677db2445950b2a9fca0ceb5199b03c71d10592bd3
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in slack_messaging.gemspec
data/Gemfile.lock CHANGED
@@ -1,29 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slack_messaging (1.4.0)
4
+ slack_messaging (3.0.0)
5
5
  activesupport (~> 6.0)
6
6
  gli (~> 2.10)
7
7
  hashie (~> 4.1)
8
8
  highline (~> 2.0)
9
- httparty
9
+ httparty (~> 0.18)
10
+ json (~> 2.5)
10
11
  rack (~> 2.2)
11
12
 
12
13
  GEM
13
14
  remote: https://rubygems.org/
14
15
  specs:
15
- activesupport (6.0.3.4)
16
+ activesupport (6.1.2.1)
16
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
- i18n (>= 0.7, < 2)
18
- minitest (~> 5.1)
19
- tzinfo (~> 1.1)
20
- zeitwerk (~> 2.2, >= 2.2.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ zeitwerk (~> 2.3)
22
+ ast (2.4.2)
21
23
  coderay (1.1.3)
22
- concurrent-ruby (1.1.7)
24
+ concurrent-ruby (1.1.8)
23
25
  diff-lcs (1.4.4)
24
- faker (2.14.0)
26
+ faker (2.15.1)
25
27
  i18n (>= 1.6, < 2)
26
- ffi (1.13.1)
28
+ ffi (1.14.2)
27
29
  formatador (0.2.5)
28
30
  gli (2.19.2)
29
31
  guard (2.16.2)
@@ -45,9 +47,10 @@ GEM
45
47
  httparty (0.18.1)
46
48
  mime-types (~> 3.0)
47
49
  multi_xml (>= 0.5.2)
48
- i18n (1.8.5)
50
+ i18n (1.8.8)
49
51
  concurrent-ruby (~> 1.0)
50
- listen (3.3.1)
52
+ json (2.5.1)
53
+ listen (3.4.1)
51
54
  rb-fsevent (~> 0.10, >= 0.10.3)
52
55
  rb-inotify (~> 0.9, >= 0.9.10)
53
56
  lumberjack (1.2.8)
@@ -55,50 +58,70 @@ GEM
55
58
  mime-types (3.3.1)
56
59
  mime-types-data (~> 3.2015)
57
60
  mime-types-data (3.2020.1104)
58
- minitest (5.14.2)
61
+ minitest (5.14.3)
59
62
  multi_xml (0.6.0)
60
63
  nenv (0.3.0)
61
64
  notiffany (0.1.3)
62
65
  nenv (~> 0.1)
63
66
  shellany (~> 0.0)
64
- pry (0.13.1)
67
+ parallel (1.20.1)
68
+ parser (3.0.0.0)
69
+ ast (~> 2.4.1)
70
+ pry (0.14.0)
65
71
  coderay (~> 1.1)
66
72
  method_source (~> 1.0)
67
73
  rack (2.2.3)
68
- rake (13.0.1)
74
+ rainbow (3.0.0)
75
+ rake (13.0.3)
69
76
  rb-fsevent (0.10.4)
70
77
  rb-inotify (0.10.1)
71
78
  ffi (~> 1.0)
79
+ regexp_parser (2.1.1)
80
+ rexml (3.2.4)
72
81
  rspec (3.10.0)
73
82
  rspec-core (~> 3.10.0)
74
83
  rspec-expectations (~> 3.10.0)
75
84
  rspec-mocks (~> 3.10.0)
76
- rspec-core (3.10.0)
85
+ rspec-core (3.10.1)
77
86
  rspec-support (~> 3.10.0)
78
- rspec-expectations (3.10.0)
87
+ rspec-expectations (3.10.1)
79
88
  diff-lcs (>= 1.2.0, < 2.0)
80
89
  rspec-support (~> 3.10.0)
81
- rspec-mocks (3.10.0)
90
+ rspec-mocks (3.10.2)
82
91
  diff-lcs (>= 1.2.0, < 2.0)
83
92
  rspec-support (~> 3.10.0)
84
- rspec-support (3.10.0)
93
+ rspec-support (3.10.2)
94
+ rubocop (1.10.0)
95
+ parallel (~> 1.10)
96
+ parser (>= 3.0.0.0)
97
+ rainbow (>= 2.2.2, < 4.0)
98
+ regexp_parser (>= 1.8, < 3.0)
99
+ rexml
100
+ rubocop-ast (>= 1.2.0, < 2.0)
101
+ ruby-progressbar (~> 1.7)
102
+ unicode-display_width (>= 1.4.0, < 3.0)
103
+ rubocop-ast (1.4.1)
104
+ parser (>= 2.7.1.5)
105
+ ruby-progressbar (1.11.0)
85
106
  shellany (0.0.1)
86
- thor (1.0.1)
87
- thread_safe (0.3.6)
88
- tzinfo (1.2.8)
89
- thread_safe (~> 0.1)
90
- zeitwerk (2.4.1)
107
+ thor (1.1.0)
108
+ tzinfo (2.0.4)
109
+ concurrent-ruby (~> 1.0)
110
+ unicode-display_width (2.0.0)
111
+ zeitwerk (2.4.2)
91
112
 
92
113
  PLATFORMS
93
- ruby
114
+ x86_64-darwin-19
94
115
 
95
116
  DEPENDENCIES
96
- bundler (~> 2.1)
97
- faker
117
+ bundler (~> 2.2)
118
+ faker (~> 2.15)
98
119
  guard-rspec (~> 4.3)
120
+ pry (~> 0.13)
99
121
  rake (~> 13.0)
100
122
  rspec (~> 3.9)
123
+ rubocop
101
124
  slack_messaging!
102
125
 
103
126
  BUNDLED WITH
104
- 2.1.4
127
+ 2.2.9
data/Guardfile CHANGED
@@ -1,4 +1,6 @@
1
- guard :rspec, cmd: 'bundle exec rspec', all_on_start: true, all_after_pass: 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/QuoteExample.png ADDED
Binary file
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Slack Messaging [![Maintainability](https://api.codeclimate.com/v1/badges/c74baada70ad96048dc7/maintainability)](https://codeclimate.com/github/emmasax4/slack_messaging/maintainability) ![Default](https://github.com/emmasax4/slack_messaging/workflows/Default/badge.svg)
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,47 +26,61 @@ 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
- channel: "#<AWESOME CHANNEL NAME>"
31
+ channel: <AWESOME CHANNEL NAME>
32
32
  username: <AWESOME USER NAME>
33
- webhook_url: <WEBHOOK URL>
34
- icon_emoji: ":<SOME EMOJI>:"
33
+ webhook_url: <SLACK WEBHOOK URL>
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, which are all defined in [`lib/slack_messaging/random_message.rb`](https://github.com/emmasax4/slack_messaging/blob/main/lib/slack_messaging/random_message.rb). Feel free to change the messages or add more to cater what you'd like slack_messaging to say.
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:
68
82
 
69
- <img src="https://github.com/emmasax4/slack_messaging/blob/main/OutputFile.png" width="1000">
83
+ <img src="https://github.com/emmahsax/slack_messaging/blob/main/QuoteExample.png" width="500">
70
84
 
71
85
  I hope you enjoy printing fun and specialized messages to Slack!
72
86
 
@@ -76,11 +90,11 @@ To run the tests, run `bundle exec rspec` from the command line. GitHub Actions
76
90
 
77
91
  ## Contributing
78
92
 
79
- To submit a feature request, bug ticket, etc, please submit an official [GitHub Issue](https://github.com/emmasax4/slack_messaging/issues/new).
93
+ To submit a feature request, bug ticket, etc, please submit an official [GitHub Issue](https://github.com/emmahsax/slack_messaging/issues/new).
80
94
 
81
- To report any security vulnerabilities, please view this project's [Security Policy](https://github.com/emmasax4/slack_messaging/security/policy).
95
+ To report any security vulnerabilities, please view this project's [Security Policy](https://github.com/emmahsax/slack_messaging/security/policy).
82
96
 
83
- This repository does have a standard [Code of Conduct](https://github.com/emmasax4/slack_messaging/blob/main/.github/code_of_conduct.md).
97
+ When interacting with this repository, please follow [Contributor Covenant's Code of Conduct](https://contributor-covenant.org).
84
98
 
85
99
  ## Releasing
86
100
 
@@ -88,7 +102,7 @@ To make a new release of this gem:
88
102
 
89
103
  1. Merge the pull request via the big green button
90
104
  2. Run `git tag vX.X.X` and `git push --tag`
91
- 3. Make a new release [here](https://github.com/emmasax4/slack_messaging/releases/new)
105
+ 3. Make a new release [here](https://github.com/emmahsax/slack_messaging/releases/new)
92
106
  4. Run `gem build *.gemspec`
93
107
  5. Run `gem push *.gem` to push the new gem to RubyGems
94
108
  6. Run `rm *.gem` to clean up your local repository
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,31 @@ 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
+ "
18
23
 
19
24
  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)
25
+ command 'send' do |c|
26
+ pre do |global, _command, _options, _args|
27
+ SlackMessaging::Config.load(global[:config])
28
+ true
29
+ end
30
+
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
+ command 'setup' do |c|
38
+ c.action do
39
+ SlackMessaging::Setup.execute
40
+ end
30
41
  end
31
42
 
32
43
  exit run(ARGV)
@@ -1,9 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'hashie'
3
5
  require 'httparty'
4
6
  require 'highline'
7
+ require 'json'
8
+
9
+ files = "#{File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'slack_messaging'))}/**/*.rb"
5
10
 
6
- Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'slack_messaging')) + '/**/*.rb'].each do |file|
11
+ Dir[files].each do |file|
7
12
  require_relative file
8
13
  end
9
14
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class Config
3
5
  def self.config
@@ -9,8 +11,6 @@ module SlackMessaging
9
11
  config
10
12
  end
11
13
 
12
- private
13
-
14
14
  def self.config_data
15
15
  @config_data ||= Hashie::Mash.new
16
16
  end
@@ -21,6 +21,7 @@ module SlackMessaging
21
21
 
22
22
  def self.load_config(file)
23
23
  raise MissingConfig, "Missing configuration file: #{file}" unless File.exist?(file)
24
+
24
25
  YAML.load_file(file).each { |key, value| config_data.assign_property(key, value) }
25
26
  end
26
27
  end
@@ -1,13 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class DefaultPaths
3
5
  def self.config
4
- File.join(self.home, '.slack_messaging.yml')
6
+ File.join(home, '.slack_messaging.yml')
5
7
  end
6
8
 
7
- private
8
-
9
9
  def self.home
10
- ENV['HOME'] ? ENV['HOME'] : '.'
10
+ ENV['HOME'] || '.'
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackMessaging
4
+ class HighlineCli
5
+ def ask(prompt)
6
+ highline_client.ask(prompt) do |conf|
7
+ conf.readline = true
8
+ end.to_s
9
+ end
10
+
11
+ def ask_yes_no(prompt)
12
+ answer = highline_client.ask(prompt) do |conf|
13
+ conf.readline = true
14
+ end.to_s
15
+
16
+ answer.empty? ? true : !!(answer =~ /^y/i)
17
+ end
18
+
19
+ def ask_options(prompt, choices)
20
+ choices_as_string_options = ''.dup
21
+ choices.each { |choice| choices_as_string_options << "#{choices.index(choice) + 1}. #{choice}\n" }
22
+ compiled_prompt = "#{prompt}\n#{choices_as_string_options.strip}"
23
+
24
+ index = highline_client.ask(compiled_prompt) do |conf|
25
+ conf.readline = true
26
+ end.to_i - 1
27
+
28
+ choices[index]
29
+ end
30
+
31
+ private def highline_client
32
+ @highline_client ||= HighLine.new
33
+ end
34
+ end
35
+ 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,30 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
4
  class RandomMessage
3
- MESSAGE_ARRAY = [
4
- "A true friend is someone who thinks that you are a good egg even though he knows that you are slightly cracked.\n--Bernard Meltzer",
5
- "If you can't make it good, at least make it look good.\n--Bill Gates",
6
- "I'm convinced of this: Good done anywhere is good done everywhere.\n--Maya Angelou",
7
- "The real trouble with reality is that there's no background music.\n--Anonymous",
8
- "Whatever you are, be a good one.\n--Abraham Lincoln",
9
- "Good, better, best. Never let it rest. 'Til your good is better and your better is best.\n--St. Jerome",
10
- "Despite everything, I believe that people are really good at heart.\n--Anne Frank",
11
- "Life is 10% what happens to you and 90% how you react to it.\n--Charles R. Swindoll",
12
- "The way to get started is to quit talking and begin doing.\n--Walt Disney",
13
- "A creative man is motivated by the desire to achieve, not by the desire to beat others.\n--Ayn Rand",
14
- "Problems are not stop signs, they are guidelines.\n--Robert H. Schuller",
15
- "Correction does much, but encouragement does more.\n--Johann Wolfgang von Goethe",
16
- "Positive anything is better than negative nothing.\n--Elbert Hubbard",
17
- "To succeed, you need to find something to hold on to, something to motivate you, something to inspire you.\n--Joyce Meyer",
18
- "If you're not making mistakes, then you're not doing anything. I'm positive that a doer makes mistakes.\n--John Wooden",
19
- "The best way to deal with other people is to just let them be other people.\n--Anonymous",
20
- "Talk to yourself like you would to someone you love.\n--Brenee Brown",
21
- "Don't let small minds convince you that your dreams are too big.\n--Anonymous",
22
- ]
23
-
24
- attr_accessor :text
25
-
26
- def initialize
27
- self.text = MESSAGE_ARRAY[rand(MESSAGE_ARRAY.length)]
5
+ def self.acquire_random_quote
6
+ random_quote = HTTParty.get('http://api.quotable.io/random', headers: { 'Content-Type': 'application/json' }).body
7
+ quote_content = JSON.parse(random_quote)['content']
8
+ quote_author = JSON.parse(random_quote)['author']
9
+ "\"#{quote_content}\"\n—#{quote_author}"
28
10
  end
29
11
  end
30
12
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackMessaging
4
+ class Send
5
+ attr_accessor :options
6
+
7
+ def self.execute(args, _options = nil)
8
+ if args.empty?
9
+ message = SlackMessaging::RandomMessage.acquire_random_quote
10
+ print_message(message)
11
+ else
12
+ args.each do |arg_message|
13
+ print_message(arg_message)
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.print_message(message)
19
+ SlackMessaging::NotifySlack.new(message).perform
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackMessaging
4
+ class Setup
5
+ def self.execute
6
+ if config_file_exists?
7
+ answer = highline.ask_yes_no(
8
+ "It looks like the #{default_config} file already exists. Do you wish to replace it? (y/n)"
9
+ )
10
+ puts
11
+ else
12
+ answer = true
13
+ end
14
+
15
+ create_or_update_config_file(ask_config_questions) if answer
16
+ end
17
+
18
+ def self.create_or_update_config_file(answers)
19
+ contents = generate_config_file(answers)
20
+ puts "\nCreating or updating your #{default_config} file..."
21
+ File.open(default_config, 'w') { |file| file.puts contents }
22
+ puts "\nDone!"
23
+ end
24
+
25
+ def self.generate_config_file(answers)
26
+ file_contents = ''.dup
27
+ file_contents << "slack:\n"
28
+ file_contents << " channel: #{answers[:channel].tr('#', '').strip}\n"
29
+ file_contents << " username: #{answers[:username].strip}\n"
30
+ file_contents << " webhook_url: #{answers[:webhook_url].strip}\n"
31
+ file_contents << " icon_emoji: '#{answers[:icon_emoji].strip}'"
32
+ file_contents
33
+ end
34
+
35
+ def self.config_file_exists?
36
+ File.exist?(default_config)
37
+ end
38
+
39
+ # rubocop:disable Metrics/MethodLength:
40
+ def self.ask_config_questions
41
+ answers = {}
42
+
43
+ answers[:webhook_url] = ask_question(
44
+ "What is your Slack webhook URL? If you don't have one yet, please navigate" \
45
+ ' to https://api.slack.com/messaging/webhooks to create one, and then come back' \
46
+ ' here and paste it in the Terminal.'
47
+ )
48
+
49
+ unless answers[:webhook_url]
50
+ puts "\nExiting because Slack webhoook is required..."
51
+ exit
52
+ end
53
+
54
+ answers[:channel] = ask_question(
55
+ "\nWhat slack channel do you wish to post to? (default is \"#general\")"
56
+ ) || 'general'
57
+
58
+ answers[:username] = ask_question(
59
+ "\nWhat slack username do you wish to post as? (default is \"Let's Get Quoty\")"
60
+ ) || "Let's Get Quoty"
61
+
62
+ answers[:icon_emoji] = ask_question(
63
+ "\nWhat emoji would you like to post with (include the colons at the beginning and end" \
64
+ ' of the emoji name)? (default is ":mailbox_with_mail:")'
65
+ ) || ':mailbox_with_mail:'
66
+
67
+ answers
68
+ end
69
+ # rubocop:enable Metrics/MethodLength
70
+
71
+ def self.ask_question(prompt)
72
+ answer = highline.ask(prompt)
73
+ answer.empty? ? nil : answer
74
+ end
75
+
76
+ def self.highline
77
+ @highline ||= SlackMessaging::HighlineCli.new
78
+ end
79
+
80
+ def self.default_config
81
+ SlackMessaging::DefaultPaths.config
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackMessaging
2
- VERSION = '1.4.0'
4
+ VERSION = '3.0.0'
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
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'slack_messaging'
5
+
6
+ describe SlackMessaging::HighlineCli do
7
+ let(:response) { double(:response, readline: true, to_i: 3) }
8
+ let(:highline_client) { double(:highline_cli, ask: response) }
9
+
10
+ before do
11
+ allow(HighLine).to receive(:new).and_return(highline_client)
12
+ end
13
+
14
+ describe '#ask' do
15
+ it 'should ask the highline client ask' do
16
+ expect(highline_client).to receive(:ask)
17
+ subject.ask(Faker::Lorem.sentence)
18
+ end
19
+
20
+ it 'should return a string' do
21
+ expect(subject.ask(Faker::Lorem.sentence)).to be_a(String)
22
+ end
23
+ end
24
+
25
+ describe '#ask_yes_no' do
26
+ it 'should ask the highline client ask' do
27
+ expect(highline_client).to receive(:ask)
28
+ subject.ask_yes_no(Faker::Lorem.sentence)
29
+ end
30
+
31
+ it 'should return a boolean' do
32
+ expect(subject.ask_yes_no(Faker::Lorem.sentence)).to be_falsey
33
+ end
34
+
35
+ it 'should return true if we say yes' do
36
+ allow(response).to receive(:to_s).and_return('y')
37
+ expect(subject.ask_yes_no(Faker::Lorem.sentence)).to be_truthy
38
+ end
39
+ end
40
+
41
+ describe '#ask_options' do
42
+ it 'should ask the highline client ask' do
43
+ expect(highline_client).to receive(:ask)
44
+ subject.ask_options(Faker::Lorem.sentence, %w[one two three])
45
+ end
46
+
47
+ it 'should return a string from the options' do
48
+ expect(subject.ask_options(Faker::Lorem.sentence, %w[one two three])).to be_a(String)
49
+ end
50
+ end
51
+ 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,14 +28,16 @@ describe SlackMessaging::NotifySlack do
26
28
  SlackMessaging::Config.load(Faker::Lorem.word)
27
29
  end
28
30
 
31
+ subject { SlackMessaging::NotifySlack }
32
+
29
33
  it 'should call HTTParty' do
30
34
  expect(HTTParty).to receive(:post)
31
- message = SlackMessaging::NotifySlack.new(sentence)
35
+ message = subject.new(sentence)
32
36
  message.perform
33
37
  end
34
38
 
35
- it 'should define certain values' do
36
- message = SlackMessaging::NotifySlack.new(sentence)
39
+ it 'should define certain values' do
40
+ message = subject.new(sentence)
37
41
  expect(message.text).to eq(sentence)
38
42
  expect(message.channel).to eq(channel)
39
43
  expect(message.username).to eq(username)
@@ -1,24 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'slack_messaging'
3
5
 
4
6
  describe SlackMessaging::RandomMessage do
7
+ let(:quote_object) { double(:quote_object, body: quote_json) }
8
+
9
+ let(:quote_json) do
10
+ "{\"_id\":\"4MRaRRKq4Tcg\",
11
+ \"tags\":[\"famous-quotes\"],
12
+ \"content\":\"There are two ways of spreading light: to be the candle or the mirror that reflects it.\",
13
+ \"author\":\"Edith Wharton\",\"length\":87
14
+ }"
15
+ end
16
+
17
+ before do
18
+ allow(HTTParty).to receive(:get).and_return(quote_object)
19
+ end
20
+
21
+ subject { SlackMessaging::RandomMessage }
22
+
5
23
  it 'should get a string message' do
6
- message = SlackMessaging::RandomMessage.new
7
- expect(message.text).to be_instance_of(String)
24
+ message = subject.acquire_random_quote
25
+ expect(message).to be_instance_of(String)
26
+ end
27
+
28
+ it 'should get a message that includes a newline' do
29
+ message = subject.acquire_random_quote
30
+ expect(message.include?("\n")).to eq(true)
8
31
  end
9
32
 
10
- it 'should get a message greater than 25 characters' do
11
- message = SlackMessaging::RandomMessage.new
12
- expect(message.text.length).to be >= 50
33
+ it 'should get a message that includes a ' do
34
+ message = subject.acquire_random_quote
35
+ expect(message.include?('—')).to eq(true)
13
36
  end
14
37
 
15
- it 'should get a message that includes a new line' do
16
- message = SlackMessaging::RandomMessage.new
17
- expect(message.text.include?("\n")).to eq(true)
38
+ it 'should use HTTParty to ping an API' do
39
+ expect(HTTParty).to receive(:get)
40
+ subject.acquire_random_quote
18
41
  end
19
42
 
20
- it 'should get a message that includes a --' do
21
- message = SlackMessaging::RandomMessage.new
22
- expect(message.text.include?('--')).to eq(true)
43
+ it 'should have the JSON parse the response content twice' do
44
+ expect(JSON).to receive(:parse).at_least(2).times.and_return(JSON.parse(quote_json))
45
+ subject.acquire_random_quote
23
46
  end
24
47
  end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'slack_messaging'
5
+
6
+ describe SlackMessaging::Setup do
7
+ let(:response) { double(:response, readline: true, to_s: Faker::Lorem.sentence) }
8
+ let(:highline_cli) { double(:highline_cli, ask: response, ask_yes_no: true) }
9
+ let(:answers) do
10
+ {
11
+ channel: Faker::Lorem.word,
12
+ username: Faker::Lorem.word,
13
+ webhook_url: Faker::Internet.url,
14
+ icon_emoji: ":#{Faker::Lorem.word}:"
15
+ }
16
+ end
17
+
18
+ before do
19
+ allow(SlackMessaging::HighlineCli).to receive(:new).and_return(highline_cli)
20
+ allow(subject).to receive(:puts)
21
+ end
22
+
23
+ after do
24
+ SlackMessaging::Setup.instance_variable_set('@highline', nil)
25
+ end
26
+
27
+ subject { SlackMessaging::Setup }
28
+
29
+ describe '#self.execute' do
30
+ it 'should ask a question if the config file exists' do
31
+ allow(subject).to receive(:config_file_exists?).and_return(true)
32
+ allow(File).to receive(:exists?).and_return(true)
33
+ expect(highline_cli).to receive(:ask_yes_no).and_return(true)
34
+ allow(subject).to receive(:create_or_update_config_file).and_return(true)
35
+ allow(subject).to receive(:ask_config_questions).and_return(true)
36
+ subject.execute
37
+ end
38
+
39
+ it 'should call to create or update the config file' do
40
+ allow(File).to receive(:exists?).and_return(true)
41
+ allow(highline_cli).to receive(:ask_yes_no).and_return(true)
42
+ expect(subject).to receive(:create_or_update_config_file).and_return(true)
43
+ expect(subject).to receive(:ask_config_questions).and_return(true)
44
+ subject.execute
45
+ end
46
+
47
+ it 'should skip if the user opts not to continue' do
48
+ allow(subject).to receive(:config_file_exists?).and_return(true)
49
+ allow(File).to receive(:exists?).and_return(true)
50
+ allow(highline_cli).to receive(:ask_yes_no).and_return(false)
51
+ expect(subject).not_to receive(:create_or_update_config_file)
52
+ expect(subject).not_to receive(:ask_config_questions)
53
+ subject.execute
54
+ end
55
+ end
56
+
57
+ describe '#self.create_or_update_config_file' do
58
+ it 'should generate the file based on the answers to the questions' do
59
+ expect(subject).to receive(:generate_config_file)
60
+ allow(File).to receive(:open).and_return(nil)
61
+ subject.send(:create_or_update_config_file, answers)
62
+ end
63
+
64
+ it 'should open the file for writing' do
65
+ allow(subject).to receive(:generate_config_file)
66
+ expect(File).to receive(:open).and_return(nil)
67
+ subject.send(:create_or_update_config_file, answers)
68
+ end
69
+ end
70
+
71
+ describe '#self.generate_config_file' do
72
+ it 'returns a string' do
73
+ expect(subject.send(:generate_config_file, answers)).to be_a(String)
74
+ end
75
+ end
76
+
77
+ describe '#self.config_file_exists?' do
78
+ it 'should return true if the file exists' do
79
+ allow(File).to receive(:exist?).and_return(true)
80
+ expect(subject.send(:config_file_exists?)).to eq(true)
81
+ end
82
+
83
+ it 'should return false if the file does not exist' do
84
+ allow(File).to receive(:exist?).and_return(false)
85
+ expect(subject.send(:config_file_exists?)).to eq(false)
86
+ end
87
+ end
88
+
89
+ describe '#self.ask_question' do
90
+ it 'should use highline to ask a question' do
91
+ expect(highline_cli).to receive(:ask).and_return('')
92
+ subject.send(:ask_question, Faker::Lorem.sentence)
93
+ end
94
+
95
+ it 'should return nil if the highline client gets an empty string' do
96
+ allow(highline_cli).to receive(:ask).and_return('')
97
+ expect(subject.send(:ask_question, Faker::Lorem.sentence)).to be_nil
98
+ end
99
+
100
+ it 'should return the answer if it is given' do
101
+ answer = Faker::Lorem.sentence
102
+ allow(highline_cli).to receive(:ask).and_return(answer)
103
+ expect(subject.send(:ask_question, Faker::Lorem.sentence)).to be(answer)
104
+ end
105
+ end
106
+
107
+ describe '#self.ask_config_questions' do
108
+ it 'should call to ask at least four questions' do
109
+ expect(subject).to receive(:ask_question).at_least(4).times
110
+ subject.send(:ask_config_questions)
111
+ end
112
+
113
+ it 'should exit if the slack URL is not given' do
114
+ allow(subject).to receive(:ask_question).and_return(nil)
115
+ expect { subject.send(:ask_config_questions) }.to raise_error(SystemExit)
116
+ end
117
+
118
+ it 'should return the defaults if nothing is given' do
119
+ slack_url = Faker::Internet.url
120
+ allow(subject).to receive(:ask_question).with(
121
+ "\nWhat is your Slack webhook URL? If you don't have one yet, please navigate" \
122
+ ' to https://api.slack.com/messaging/webhooks to create one, and then come back' \
123
+ ' here and paste it in the Terminal.'
124
+ ).and_return(slack_url)
125
+ allow(subject).to receive(:ask_question).and_return(nil)
126
+ expect(subject.send(:ask_config_questions)).to eq(
127
+ {
128
+ channel: 'general',
129
+ username: "Let's Get Quoty",
130
+ webhook_url: slack_url,
131
+ icon_emoji: ':mailbox_with_mail:'
132
+ }
133
+ )
134
+ end
135
+ end
136
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faker'
4
+ require 'pry'
2
5
 
3
6
  # This file was generated by the `rspec --init` command. Conventionally, all
4
7
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
@@ -33,7 +36,7 @@ RSpec.configure do |config|
33
36
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
37
  end
35
38
 
36
- config.filter_run :focus => true
39
+ config.filter_run focus: true
37
40
  config.run_all_when_everything_filtered = true
38
41
 
39
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: 1.4.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Sax
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-19 00:00:00.000000000 Z
11
+ date: 2021-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -70,16 +70,30 @@ dependencies:
70
70
  name: httparty
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '0.18'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '0.18'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.5'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rack
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -100,28 +114,28 @@ dependencies:
100
114
  requirements:
101
115
  - - "~>"
102
116
  - !ruby/object:Gem::Version
103
- version: '2.1'
117
+ version: '2.2'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: '2.1'
124
+ version: '2.2'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: faker
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - ">="
129
+ - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: '0'
131
+ version: '2.15'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - ">="
136
+ - - "~>"
123
137
  - !ruby/object:Gem::Version
124
- version: '0'
138
+ version: '2.15'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: guard-rspec
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,20 @@ dependencies:
136
150
  - - "~>"
137
151
  - !ruby/object:Gem::Version
138
152
  version: '4.3'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.13'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.13'
139
167
  - !ruby/object:Gem::Dependency
140
168
  name: rake
141
169
  requirement: !ruby/object:Gem::Requirement
@@ -164,9 +192,22 @@ dependencies:
164
192
  - - "~>"
165
193
  - !ruby/object:Gem::Version
166
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: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
167
209
  description: Sending Personalized Slack Messages to a Slack channel of your choice.
168
210
  email:
169
- - emma.sax4@gmail.com
170
211
  executables:
171
212
  - slack-messaging
172
213
  extensions: []
@@ -176,22 +217,26 @@ files:
176
217
  - Gemfile.lock
177
218
  - Guardfile
178
219
  - LICENSE
179
- - OutputFile.png
220
+ - QuoteExample.png
180
221
  - README.md
181
222
  - Rakefile
182
223
  - bin/slack-messaging
183
224
  - lib/slack_messaging.rb
184
225
  - lib/slack_messaging/config.rb
185
226
  - lib/slack_messaging/default_paths.rb
227
+ - lib/slack_messaging/highline_cli.rb
186
228
  - lib/slack_messaging/notify_slack.rb
187
229
  - lib/slack_messaging/random_message.rb
188
- - lib/slack_messaging/slack.rb
230
+ - lib/slack_messaging/send.rb
231
+ - lib/slack_messaging/setup.rb
189
232
  - lib/slack_messaging/version.rb
190
233
  - spec/slack_messaging/config_spec.rb
234
+ - spec/slack_messaging/highline_cli_spec.rb
191
235
  - spec/slack_messaging/notify_slack_spec.rb
192
236
  - spec/slack_messaging/random_message_spec.rb
237
+ - spec/slack_messaging/setup_spec.rb
193
238
  - spec/spec_helper.rb
194
- homepage: https://github.com/emmasax4/slack_messaging
239
+ homepage: https://github.com/emmahsax/slack_messaging
195
240
  licenses:
196
241
  - MIT
197
242
  metadata: {}
@@ -203,19 +248,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
203
248
  requirements:
204
249
  - - ">="
205
250
  - !ruby/object:Gem::Version
206
- version: '0'
251
+ version: '2.4'
207
252
  required_rubygems_version: !ruby/object:Gem::Requirement
208
253
  requirements:
209
254
  - - ">="
210
255
  - !ruby/object:Gem::Version
211
256
  version: '0'
212
257
  requirements: []
213
- rubygems_version: 3.1.4
258
+ rubygems_version: 3.2.3
214
259
  signing_key:
215
260
  specification_version: 4
216
261
  summary: Personalized Slack Messages
217
262
  test_files:
218
263
  - spec/spec_helper.rb
219
264
  - spec/slack_messaging/config_spec.rb
220
- - spec/slack_messaging/random_message_spec.rb
265
+ - spec/slack_messaging/highline_cli_spec.rb
221
266
  - spec/slack_messaging/notify_slack_spec.rb
267
+ - spec/slack_messaging/random_message_spec.rb
268
+ - spec/slack_messaging/setup_spec.rb
data/OutputFile.png DELETED
Binary file
@@ -1,20 +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.new
8
- print_message(message.text)
9
- else
10
- args.each do |message|
11
- print_message(message)
12
- end
13
- end
14
- end
15
-
16
- def self.print_message(message)
17
- SlackMessaging::NotifySlack.new(message).perform
18
- end
19
- end
20
- end