From 228aa80a48c6e263e6ed9c8a1b8632709a615d3d Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Tue, 19 Mar 2019 14:06:28 -0700 Subject: [PATCH 01/16] Added .env to .gitignore. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8d6a243f..b78e0506 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ /tmp/ # Used by dotenv library to load environment variables. -# .env +.env ## Specific to RubyMotion: .dat* From 731b4ae6c53a08a6b3379d167f43e5c6d861804d Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Tue, 19 Mar 2019 18:50:43 -0700 Subject: [PATCH 02/16] completes wave 1 but without class structure yet --- lib/channel.rb | 0 lib/slack.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- lib/top.rb | 0 lib/user.rb | 16 ++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/channel.rb create mode 100644 lib/top.rb create mode 100644 lib/user.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..fe3104b6 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,67 @@ #!/usr/bin/env ruby +require "httparty" +require "dotenv" +require "table_print" +Dotenv.load + +# list users +# list channels +# quit def main puts "Welcome to the Ada Slack CLI!" + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + pretty: 1, + } + url = "https://slack.com/api/conversations.list?" + @channels = HTTParty.get(url, query: query_parameters) + # puts @channels + url2 = "https://slack.com/api/users.list?" + @users = HTTParty.get(url2, query: query_parameters) + # loads_info + puts "there are #{@channels["channels"].length} channels and #{@users["members"].length}" + # puts channels["channels"][0]["name"] + + def lists_channels + @channels["channels"].each do |x| + puts "name: #{x["name"]}" + puts "topic: #{x["topic"]["value"]}" + puts "number of members: #{x["num_members"]}" + puts "slack id: #{x["id"]}" + end + end + + def lists_users + @users["members"].each do |x| + puts "name: #{x["name"]}" + puts "id: #{x["id"]}" + puts "real name: #{x["real_name"]}" + end + end + + def options + puts "What should we do next? (list channels/ list users/ quit):" + return gets.chomp.downcase + end + + continue = true + + while (continue) + response = options + case response + when "list channels" + lists_channels + when "list users" + lists_users + when "quit" + continue = false + end + end # TODO project puts "Thank you for using the Ada Slack CLI" end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/top.rb b/lib/top.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..10169c1b --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,16 @@ +require_relative "slack" +require_relative "top" +require "httparty" +require "dotenv" +require "table_print" +Dotenv.load + +class User + attr_reader :topic, :member_count, :ch_name + + def lists_users + @users["members"].each do |x| + puts x["name"] + end + end +end From fa070bb61a977bda4384bfd11ea574c5b0a8d7be Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Tue, 19 Mar 2019 20:52:48 -0700 Subject: [PATCH 03/16] completes coding requirements for wave 1 and adds a slack class --- lib/slack.rb | 43 ++++++++++++++++++++---------------- lib/top.rb | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index fe3104b6..0a968ad5 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,25 +7,22 @@ # list users # list channels # quit +class Slack + attr_reader :channels, :users -def main - puts "Welcome to the Ada Slack CLI!" - - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - pretty: 1, - } - url = "https://slack.com/api/conversations.list?" - @channels = HTTParty.get(url, query: query_parameters) - # puts @channels - url2 = "https://slack.com/api/users.list?" - @users = HTTParty.get(url2, query: query_parameters) - # loads_info - puts "there are #{@channels["channels"].length} channels and #{@users["members"].length}" - # puts channels["channels"][0]["name"] + def initialize() + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + pretty: 1, + } + url = "https://slack.com/api/conversations.list?" + @channels = HTTParty.get(url, query: query_parameters) + url2 = "https://slack.com/api/users.list?" + @users = HTTParty.get(url2, query: query_parameters) + end def lists_channels - @channels["channels"].each do |x| + channels["channels"].each do |x| puts "name: #{x["name"]}" puts "topic: #{x["topic"]["value"]}" puts "number of members: #{x["num_members"]}" @@ -34,12 +31,20 @@ def lists_channels end def lists_users - @users["members"].each do |x| + users["members"].each do |x| puts "name: #{x["name"]}" puts "id: #{x["id"]}" puts "real name: #{x["real_name"]}" end end +end + +def main + puts "Welcome to the Ada Slack CLI!" + + slack = Slack.new + + puts "there are #{slack.channels["channels"].length} channels and #{slack.users["members"].length}" def options puts "What should we do next? (list channels/ list users/ quit):" @@ -52,9 +57,9 @@ def options response = options case response when "list channels" - lists_channels + slack.lists_channels when "list users" - lists_users + slack.lists_users when "quit" continue = false end diff --git a/lib/top.rb b/lib/top.rb index e69de29b..ec6dbdba 100644 --- a/lib/top.rb +++ b/lib/top.rb @@ -0,0 +1,62 @@ +require "httparty" +require "dotenv" +require "table_print" +Dotenv.load + +class Slack + attr_reader :slack_id, :name + + #methods send_message(message) + #self.get(url, params) + #details + #self.list + + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + pretty: 1, + } + url = "https://slack.com/api/conversations.list?" + @channels = HTTParty.get(url, query: query_parameters) + # puts @channels + url2 = "https://slack.com/api/users.list?" + @users = HTTParty.get(url2, query: query_parameters) + # loads_info + puts "there are #{@channels["channels"].length} channels and #{@users["members"].length}" + # puts channels["channels"][0]["name"] + + def lists_channels + @channels["channels"].each do |x| + puts "name: #{x["name"]}" + puts "topic: #{x["topic"]["value"]}" + puts "number of members: #{x["num_members"]}" + puts "slack id: #{x["id"]}" + end + end + + def lists_users + @users["members"].each do |x| + puts "name: #{x["name"]}" + puts "id: #{x["id"]}" + puts "real name: #{x["real_name"]}" + end + end + + def options + puts "What should we do next? (list channels/ list users/ quit):" + return gets.chomp.downcase + end + + continue = true + + while (continue) + response = options + case response + when "list channels" + lists_channels + when "list users" + lists_users + when "quit" + continue = false + end + end +end From 8089c884b7da227e023ef47febe32fa7be6a65b0 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 20 Mar 2019 14:17:13 -0700 Subject: [PATCH 04/16] Updated the test file to require dotenv and to set up VCR.configure. --- lib/slack.rb | 4 ++-- specs/slack_spec.rb | 45 ++++++++++++++++++++++++++++++++++++++++++++ specs/test_helper.rb | 31 +++++++++++++++++++++--------- 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 specs/slack_spec.rb diff --git a/lib/slack.rb b/lib/slack.rb index 0a968ad5..b629577c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -40,7 +40,7 @@ def lists_users end def main - puts "Welcome to the Ada Slack CLI!" + puts "Welcome to the Kasey-Elle Slack CLI!" slack = Slack.new @@ -66,7 +66,7 @@ def options end # TODO project - puts "Thank you for using the Ada Slack CLI" + puts "Thank you for using the Kasey-Elle Slack CLI" end main if __FILE__ == $PROGRAM_NAME diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb new file mode 100644 index 00000000..e7ffba5c --- /dev/null +++ b/specs/slack_spec.rb @@ -0,0 +1,45 @@ +require_relative "test_helper" + +describe "SlackAPI" do + it "can send a valid message" do + VCR.use_cassette("slack_message") do + location = "Seattle" + response = get_location(location) + + expect(response["Seattle"]).wont_be_nil + expect(response["Seattle"][:lon]).must_equal "-122.3300624" + expect(response["Seattle"][:lat]).must_equal "47.6038321" + end + end + + it "will raise an exception if the search fails" do + VCR.use_cassette("location_find") do + location = "" + expect { + response = get_location(location) + }.must_raise SearchError + end + end +end + +# describe "Listing Channels and Users" do +# it "list channels" do +# VCR.use_cassette("channels_find") do +# location = "Seattle" +# response = get_location(location) + +# expect(response["Seattle"]).wont_be_nil +# expect(response["Seattle"][:lon]).must_equal "-122.3300624" +# expect(response["Seattle"][:lat]).must_equal "47.6038321" +# end +# end + +# it "list users" do +# VCR.use_cassette("location_find") do +# user = "" # name parameter in slack api +# expect { +# response = get_location(location) +# }.must_raise SearchError +# end +# end +# end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..7b929707 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,28 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'vcr' +require "webmock/minitest" +require "dotenv" +Dotenv.load + +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "vcr" +# require "webmock/minitest" <-- do we need this? Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock -end \ No newline at end of file + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } + # Don't leave our Slack token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end +end From 1b667e512de3c4cbfc82530a5fdfc015dfe738cd Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 20 Mar 2019 17:14:04 -0700 Subject: [PATCH 05/16] Set up tests for list channels. Addressed some bugs with linking files together (require statements) and circular reference with minitest. --- lib/slack.rb | 2 +- specs/slack_spec.rb | 99 ++++++++++++++++++++++++++++++++++++-------- specs/test_helper.rb | 10 +++-- 3 files changed, 89 insertions(+), 22 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index b629577c..7b6f8f72 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -47,7 +47,7 @@ def main puts "there are #{slack.channels["channels"].length} channels and #{slack.users["members"].length}" def options - puts "What should we do next? (list channels/ list users/ quit):" + puts "What should we do next? (list channels / list users / quit):" return gets.chomp.downcase end diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index e7ffba5c..bb119c71 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -1,27 +1,92 @@ require_relative "test_helper" +# require_relative "slack" +# require_relative "spec_helper" +require "pry" -describe "SlackAPI" do - it "can send a valid message" do - VCR.use_cassette("slack_message") do - location = "Seattle" - response = get_location(location) - - expect(response["Seattle"]).wont_be_nil - expect(response["Seattle"][:lon]).must_equal "-122.3300624" - expect(response["Seattle"][:lat]).must_equal "47.6038321" +describe "Slack" do + describe "list users and channels" do + it "show a list of users" do + VCR.use_cassette("slack_query") do # <-- .yml filename + return_value = Slack.new + # binding.pry + expect(return_value).must_be_kind_of Slack + expect(return_value.channels["channels"][0]["id"]).must_equal "CH41RMLP4" + end end end - it "will raise an exception if the search fails" do - VCR.use_cassette("location_find") do - location = "" - expect { - response = get_location(location) - }.must_raise SearchError - end - end + # describe "send_msg" do + # it "can send a valid message" do + # VCR.use_cassette("slack_query") do + # return_value = SlackApi.send_msg("Test message", + # "ports-api-testing") + + # expect(return_value).must_equal true + # end + # end + + # it "generates an error if given an invalid channel" do + # VCR.use_cassette("slack_query") do + # expect { + # SlackApi.send_msg("Test message", + # "bogus") + # }.must_raise SlackApi::SlackError + # end + # end + # it "will generate an error if given an invalid key" do + # real_token = ENV["SLACK_TOKEN"] + # ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" + + # VCR.use_cassette("slack_query") do + # error = expect { + # SlackApi.send_msg("Test message with invalid key", + # "ports-api-testing") + # }.must_raise SlackApi::SlackError + # expect(error.message).must_equal "Error when posting Test message with invalid key to ports-api-testing, error: invalid_auth" + # end + + # ENV["SLACK_TOKEN"] = real_token + # end + + # it "will raise an error if given an empty message" do + # VCR.use_cassette("slack_query") do + # error = expect { + # SlackApi.send_msg("", + # "ports-api-testing") + # }.must_raise SlackApi::SlackError + + # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" + # end + # end + # end end +# ---------------------------------------------------------- + +# describe "SlackAPI" do +# it "can send a valid message" do +# VCR.use_cassette("slack_query") do +# location = "Seattle" +# response = get_location(location) + +# expect(response["Seattle"]).wont_be_nil +# expect(response["Seattle"][:lon]).must_equal "-122.3300624" +# expect(response["Seattle"][:lat]).must_equal "47.6038321" +# end +# end + +# it "will raise an exception if the search fails" do +# VCR.use_cassette("location_find") do +# location = "" +# expect { +# response = get_location(location) +# }.must_raise SearchError +# end +# end +# end + +# ---------------------------------------------------------- + # describe "Listing Channels and Users" do # it "list channels" do # VCR.use_cassette("channels_find") do diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 7b929707..df6b26af 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,16 +1,18 @@ require "simplecov" SimpleCov.start -require "webmock/minitest" require "dotenv" Dotenv.load -require "minitest" +require "webmock/minitest" # <-- do we need this? + require "minitest/autorun" require "minitest/reporters" require "minitest/skip_dsl" require "vcr" -# require "webmock/minitest" <-- do we need this? + +require_relative "../lib/slack.rb" +require_relative "../specs/slack_spec" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -22,7 +24,7 @@ :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match } # Don't leave our Slack token lying around in a cassette file. - config.filter_sensitive_data("") do + config.filter_sensitive_data("SLACK_API_TOKEN") do ENV["SLACK_API_TOKEN"] end end From 474d458a630dbbf027ce3cc8a95040dfb05d8b1d Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Thu, 21 Mar 2019 10:08:15 -0700 Subject: [PATCH 06/16] some small changes from lecture --- lib/channel.rb | 15 ++++++++++ lib/slack.rb | 19 ++++++++++++- lib/top.rb | 2 +- notes_to_do.txt | 67 ++++++++++++++++++++++++++++++++++++++++++++ specs/test_helper.rb | 24 +++++++++++----- specs/user_spec.rb | 17 +++++++++++ 6 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 notes_to_do.txt create mode 100644 specs/user_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb index e69de29b..1d3755fe 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -0,0 +1,15 @@ +require_relative "slack" +require_relative "top" +require "httparty" +require "dotenv" +require "table_print" +Dotenv.load + +class Channel + attr_reader :real_name, + def lists_channels + @channels["channels"].each do |x| + puts x["name"] + end + end +end diff --git a/lib/slack.rb b/lib/slack.rb index 0a968ad5..9e5c23e8 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,6 +7,8 @@ # list users # list channels # quit +class SlackError < StandardError; end + class Slack attr_reader :channels, :users @@ -37,6 +39,15 @@ def lists_users puts "real name: #{x["real_name"]}" end end + + def select_user + end + + def select_channel + end + + def details + end end def main @@ -47,7 +58,7 @@ def main puts "there are #{slack.channels["channels"].length} channels and #{slack.users["members"].length}" def options - puts "What should we do next? (list channels/ list users/ quit):" + puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ quit):" return gets.chomp.downcase end @@ -60,6 +71,12 @@ def options slack.lists_channels when "list users" slack.lists_users + when "select user" + slack.select_user + when "select channel" + slack.select_channel + when "details" + slack.details when "quit" continue = false end diff --git a/lib/top.rb b/lib/top.rb index ec6dbdba..767b1a61 100644 --- a/lib/top.rb +++ b/lib/top.rb @@ -10,7 +10,7 @@ class Slack #self.get(url, params) #details #self.list - + self.initialize() query_parameters = { token: ENV["SLACK_API_TOKEN"], pretty: 1, diff --git a/notes_to_do.txt b/notes_to_do.txt new file mode 100644 index 00000000..5e5a0395 --- /dev/null +++ b/notes_to_do.txt @@ -0,0 +1,67 @@ +types of tests: + +add one for invalid tokens + +check for 200? basically, "ok" is true or false + +3 tests for send messaging +describe Slack do + describe "send_msg" do + it "can send a valid message" do + VCR.use_cassette("slack_message") do + return_value = SlackApi.send_msg("Test message", "apiiiii") + + expect(return_value).must_equal true + end + end + end +end + +BASE_URL = https://slack.com/api/ + +def self.send_msg(text, channel) + response = HTTParty.post( + "#{BASE_URL}chat.postMessage" + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, + body: { + token: SLACK_API_TOKEN, + channel: channel, + text: message, + }, + ) + + if response["ok"] + return true + else + raise SlackError, "Error when posting #{message} to #{channel}, error: #{response["error"]}" + end + +end + +class SlackError < StandardError; end + +it "generates an error if given an invalid channel" do + + VCR.use_cassette("slack_message") do + expect { + return_value = SlackApiWrapper.send_msg("Test message", "bogus") + }.must_raise SlackError + end + +it "will raise an error if given an empty message" do + +end + +notes: + +above, the def self.send_msg +was in + +module SlackApi + BASE_URL = + API_KEY = ENV["token"] + + then slack error line + then send_msg + + end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..ecca0347 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,25 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'vcr' +require "httparty" +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "vcr" +require "pry" +require "dotenv" +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock -end \ No newline at end of file + config.filter_sensitive_data("SLACK_API_TOKEN") do + ENV["SLACK_API_TOKEN"] + end +end + +require_relative "../lib/slack.rb" +require_relative "user_spec.rb" diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e8ede04e --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,17 @@ +require_relative "test_helper" +describe "p" do + it "~" do + VCR.use_cassette("slacker") do + slack = Slack.new + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + url = "https://slack.com/api/conversations.list?" + response = HTTParty.get(url, query: query_parameters) + + expect(slack.response["Channels"]).wont_be_nil + # expect(response["Seattle"][:lon]).must_equal "-122.3300624" + # expect(response["Seattle"][:lat]).must_equal "47.6038321" + end + end +end From 99e66c3f219aa08ed1083be5451eb5dd90e9552b Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Thu, 21 Mar 2019 16:39:21 -0700 Subject: [PATCH 07/16] Wrote and stemmed out more tests for selecting channels and users. --- .gitignore | 1 + Rakefile | 6 +++--- specs/slack_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++++ specs/test_helper.rb | 5 ++--- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b78e0506..2fbb0245 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /test/tmp/ /test/version_tmp/ /tmp/ +temp-auth-check.rb # Used by dotenv library to load environment variables. .env diff --git a/Rakefile b/Rakefile index deb52f2c..81e1e70b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,9 @@ -require 'rake/testtask' +require "rake/testtask" Rake::TestTask.new do |t| t.libs = ["lib"] - t.warning = true - t.test_files = FileList['specs/*_spec.rb'] + # t.warning = false # <-- Scarlet added this to eleminate our minitest circular warnings + t.test_files = FileList["specs/*_spec.rb"] end task default: :test diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index bb119c71..edf41f53 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -11,10 +11,51 @@ # binding.pry expect(return_value).must_be_kind_of Slack expect(return_value.channels["channels"][0]["id"]).must_equal "CH41RMLP4" + expect(return_value.users["members"][1]["name"]).must_equal "cello.elle" end end end + describe "select users and channels" do + it "select a user" do + VCR.use_cassette("slack_query") do # <-- .yml filename + user_a = user.select("UH53ZCBBR") # <-- this is "id" value for "kaseea" + user_b = user.select("kaseea") # <-- this is "name" value for "kaseea" + + expect(user_a).must_equal "UH53ZCBBR" + expect(user_b).must_equal "kaseea" + end + end + + it "select a channel" do + VCR.use_cassette("slack_query") do # <-- .yml filename + channel_a = channel.select("apiiiii:") + channel_b = channel.select("CH53ZCWDV") # <-- this is the "id" value for the general channel + expect(channel_a).must_equal "apiiiii" + expect(channel_a).must_equal "CH53ZCWDV" + end + end + + it "select a channel" do + VCR.use_cassette("slack_query") do + # TEST GOES HERE + end + end + + it "select a channel" do + VCR.use_cassette("slack_query") do + # TEST GOES HERE + end + end + + it "select a channel" do + VCR.use_cassette("slack_query") do + # TEST GOES HERE + end + end + # + end + # describe "send_msg" do # it "can send a valid message" do # VCR.use_cassette("slack_query") do diff --git a/specs/test_helper.rb b/specs/test_helper.rb index df6b26af..9654e41e 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -4,15 +4,14 @@ require "dotenv" Dotenv.load -require "webmock/minitest" # <-- do we need this? - require "minitest/autorun" require "minitest/reporters" require "minitest/skip_dsl" require "vcr" +require "webmock/minitest" # <-- do we need this? require_relative "../lib/slack.rb" -require_relative "../specs/slack_spec" +# require_relative "../specs/slack_spec" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 18e25dc90f965c42c183a3a33705c0db36165c00 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Fri, 22 Mar 2019 10:57:25 -0700 Subject: [PATCH 08/16] Added tests for selecting users and channels. Currently not working. --- specs/slack_spec.rb | 27 ++++++++++++++++++++------- specs/user_spec.rb | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index edf41f53..0b4a8944 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -19,8 +19,8 @@ describe "select users and channels" do it "select a user" do VCR.use_cassette("slack_query") do # <-- .yml filename - user_a = user.select("UH53ZCBBR") # <-- this is "id" value for "kaseea" - user_b = user.select("kaseea") # <-- this is "name" value for "kaseea" + user_a = Slack.select_user("UH53ZCBBR") # <-- this is "id" value for "kaseea" + user_b = Slack.select_user("kaseea") # <-- this is "name" value for "kaseea" expect(user_a).must_equal "UH53ZCBBR" expect(user_b).must_equal "kaseea" @@ -29,28 +29,41 @@ it "select a channel" do VCR.use_cassette("slack_query") do # <-- .yml filename - channel_a = channel.select("apiiiii:") - channel_b = channel.select("CH53ZCWDV") # <-- this is the "id" value for the general channel + channel_a = Slack.select_channel("apiiiii:") + channel_b = Slack.select_channel("CH53ZCWDV") # <-- this is the "id" value for the general channel expect(channel_a).must_equal "apiiiii" expect(channel_a).must_equal "CH53ZCWDV" end end - it "select a channel" do + it "raise an error if given an empty string" do VCR.use_cassette("slack_query") do # TEST GOES HERE + channel_x = Slack.select_channel("") + user_x = Slack.select_channel("") + + expect(channel_x).must_raise SlackError + expect(user_x).must_raise SlackError end end - it "select a channel" do + it "raise an error if given a non-existant user or channel" do VCR.use_cassette("slack_query") do # TEST GOES HERE + channel_y = Slack.select_channel("Citris Fruit") + user_y = Slack.select_user("Pomolo") + + expect(channel_y).must_raise SlackError, "No match for this user" + expect(user_y).must_raise SlackError, "No match for this user" end end - it "select a channel" do + it "raise an error if no user is selected" do VCR.use_cassette("slack_query") do # TEST GOES HERE + new_slack_instance = Slack.new + expect(new_slack_instance.chosen_user).must_equal nil # <-- do we want this to be nil or ""? + expect(new_slack_instance.chosen_user).must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method end end # diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e8ede04e..07edae72 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -9,7 +9,7 @@ url = "https://slack.com/api/conversations.list?" response = HTTParty.get(url, query: query_parameters) - expect(slack.response["Channels"]).wont_be_nil + expect(slack.channels).wont_be_nil # expect(response["Seattle"][:lon]).must_equal "-122.3300624" # expect(response["Seattle"][:lat]).must_equal "47.6038321" end From 87f5a285b55cf0e0a6ea8a738a9b7edafc65f2dc Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Fri, 22 Mar 2019 11:03:03 -0700 Subject: [PATCH 09/16] changes the format of some of the test files to match class structure --- lib/channel.rb | 6 +- lib/slack.rb | 14 ++-- lib/user.rb | 6 +- specs/slack_spec.rb | 161 ++++++++++++++++++++++++-------------------- specs/user_spec.rb | 2 +- 5 files changed, 100 insertions(+), 89 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 084a46fe..6d53bf7a 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -14,14 +14,10 @@ def initialize(name, topic, num_members, slack_id) @slack_id = slack_id end - def print + def details puts "name: #{name}" puts "topic: #{topic}" puts "number of members: #{num_members}" puts "slack id: #{slack_id}" end - - def details - print - end end diff --git a/lib/slack.rb b/lib/slack.rb index 28c61a6a..14f5781b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,7 +5,6 @@ require_relative "user" require "httparty" require "dotenv" -require "table_print" Dotenv.load class SlackError < StandardError; end @@ -38,23 +37,23 @@ def poop() def lists_channels @channels.each do |x| - x.print + x.details end end def lists_users @users.each do |x| - x.print + x.details end end def select_user(search) - user = @users.find { |x| x.user_name.downcase == search || x.user_id == search } + @users.find { |x| x.user_name.downcase == search || x.user_id.downcase == search } #add argument error if doesn't exist end def select_channel(search) - channel = @channels.find { |x| x.name.downcase == search || x.slack_id == search } + @channels.find { |x| x.name.downcase == search || x.slack_id.downcase == search } #add argument error if doesn't exist end end @@ -84,11 +83,14 @@ def options when "select user" who = gets.chomp.downcase chosen_user = slack.select_user(who) - puts chosen_user when "select channel" who = gets.chomp.downcase chosen_user = slack.select_channel(who) when "details" + # if chosen_user == "" + # puts "there is no selected user" + # options + # end binding.pry chosen_user.details when "send message" diff --git a/lib/user.rb b/lib/user.rb index 60cca2f3..05640d66 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -13,13 +13,9 @@ def initialize(user_name, user_id, real_name) @real_name = real_name end - def print + def details puts "name: #{user_name}" puts "id: #{user_id}" puts "real name: #{real_name}" end - - def details - print - end end diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index edf41f53..ef08c8a1 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -1,6 +1,4 @@ require_relative "test_helper" -# require_relative "slack" -# require_relative "spec_helper" require "pry" describe "Slack" do @@ -8,100 +6,119 @@ it "show a list of users" do VCR.use_cassette("slack_query") do # <-- .yml filename return_value = Slack.new - # binding.pry expect(return_value).must_be_kind_of Slack - expect(return_value.channels["channels"][0]["id"]).must_equal "CH41RMLP4" - expect(return_value.users["members"][1]["name"]).must_equal "cello.elle" + expect(return_value.channels[0].slack_id).must_equal "CH41RMLP4" + expect(return_value.users[1].user_name).must_equal "cello.elle" end end end describe "select users and channels" do it "select a user" do - VCR.use_cassette("slack_query") do # <-- .yml filename - user_a = user.select("UH53ZCBBR") # <-- this is "id" value for "kaseea" - user_b = user.select("kaseea") # <-- this is "name" value for "kaseea" - - expect(user_a).must_equal "UH53ZCBBR" - expect(user_b).must_equal "kaseea" + VCR.use_cassette("slack_query") do + slack = Slack.new # <-- .yml filename + user_a = slack.select_user("UH53ZCBBR") # <-- this is "id" value for "kaseea" + user_b = slack.select_user("kaseea") # <-- this is "name" value for "kaseea" + binding.pry + expect(user_a.user_id).must_equal "UH53ZCBBR" + expect(user_b.user_name).must_equal "kaseea" end end it "select a channel" do VCR.use_cassette("slack_query") do # <-- .yml filename - channel_a = channel.select("apiiiii:") - channel_b = channel.select("CH53ZCWDV") # <-- this is the "id" value for the general channel - expect(channel_a).must_equal "apiiiii" - expect(channel_a).must_equal "CH53ZCWDV" + slack = Slack.new + channel_a = slack.select_channel("apiiiii") + # channel_b = slack.select_channel("CH4BCTLHK") # <-- this is the "id" value for the general channel + expect(channel_a.name).must_equal "apiiiii" + # expect(channel_b.name).must_equal "random" end end + end - it "select a channel" do + describe "select users and channels" do + it "does details" do VCR.use_cassette("slack_query") do - # TEST GOES HERE + slack = Slack.new + channel_a = slack.select_channel("apiiiii") + user_b = slack.select_user("kaseea") + expect(channel_a).must_respond_to :details + expect(user_b).must_respond_to :details end end + end + #need a test for list channels + #need a test for list users - it "select a channel" do - VCR.use_cassette("slack_query") do - # TEST GOES HERE - end - end + #need a test for main method + #I guess ones that test the usage of the loop - it "select a channel" do - VCR.use_cassette("slack_query") do - # TEST GOES HERE - end - end - # - end + # it "select a channel" do + # VCR.use_cassette("slack_query") do + # # TEST GOES HERE + # end + # end + + # it "select a channel" do + # VCR.use_cassette("slack_query") do + # # TEST GOES HERE + # end + # end - # describe "send_msg" do - # it "can send a valid message" do - # VCR.use_cassette("slack_query") do - # return_value = SlackApi.send_msg("Test message", - # "ports-api-testing") - - # expect(return_value).must_equal true - # end - # end - - # it "generates an error if given an invalid channel" do - # VCR.use_cassette("slack_query") do - # expect { - # SlackApi.send_msg("Test message", - # "bogus") - # }.must_raise SlackApi::SlackError - # end - # end - # it "will generate an error if given an invalid key" do - # real_token = ENV["SLACK_TOKEN"] - # ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" - - # VCR.use_cassette("slack_query") do - # error = expect { - # SlackApi.send_msg("Test message with invalid key", - # "ports-api-testing") - # }.must_raise SlackApi::SlackError - # expect(error.message).must_equal "Error when posting Test message with invalid key to ports-api-testing, error: invalid_auth" - # end - - # ENV["SLACK_TOKEN"] = real_token - # end - - # it "will raise an error if given an empty message" do - # VCR.use_cassette("slack_query") do - # error = expect { - # SlackApi.send_msg("", - # "ports-api-testing") - # }.must_raise SlackApi::SlackError - - # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" - # end - # end + # it "select a channel" do + # VCR.use_cassette("slack_query") do + # # TEST GOES HERE # end + # end + # end +# describe "send_msg" do +# it "can send a valid message" do +# VCR.use_cassette("slack_query") do +# return_value = SlackApi.send_msg("Test message", +# "ports-api-testing") + +# expect(return_value).must_equal true +# end +# end + +# it "generates an error if given an invalid channel" do +# VCR.use_cassette("slack_query") do +# expect { +# SlackApi.send_msg("Test message", +# "bogus") +# }.must_raise SlackApi::SlackError +# end +# end +# it "will generate an error if given an invalid key" do +# real_token = ENV["SLACK_TOKEN"] +# ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" + +# VCR.use_cassette("slack_query") do +# error = expect { +# SlackApi.send_msg("Test message with invalid key", +# "ports-api-testing") +# }.must_raise SlackApi::SlackError +# expect(error.message).must_equal "Error when posting Test message with invalid key to ports-api-testing, error: invalid_auth" +# end + +# ENV["SLACK_TOKEN"] = real_token +# end + +# it "will raise an error if given an empty message" do +# VCR.use_cassette("slack_query") do +# error = expect { +# SlackApi.send_msg("", +# "ports-api-testing") +# }.must_raise SlackApi::SlackError + +# expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" +# end +# end +# end +# end + # ---------------------------------------------------------- # describe "SlackAPI" do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e8ede04e..07edae72 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -9,7 +9,7 @@ url = "https://slack.com/api/conversations.list?" response = HTTParty.get(url, query: query_parameters) - expect(slack.response["Channels"]).wont_be_nil + expect(slack.channels).wont_be_nil # expect(response["Seattle"][:lon]).must_equal "-122.3300624" # expect(response["Seattle"][:lat]).must_equal "47.6038321" end From ca1b2fce5c345a5e03b677fe2998815562143cee Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Fri, 22 Mar 2019 13:21:23 -0700 Subject: [PATCH 10/16] fixes bug with downcase, better matching for selection methods --- lib/slack.rb | 20 +++++++++++++------- specs/slack_spec.rb | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 14f5781b..ec4dd717 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -48,13 +48,17 @@ def lists_users end def select_user(search) - @users.find { |x| x.user_name.downcase == search || x.user_id.downcase == search } - #add argument error if doesn't exist + raise SlackError, "please give valid search" if search == nil || search == "" + user = @users.find { |x| x.user_name.downcase == search.downcase || x.user_id.downcase == search.downcase } + raise SlackError, "no user found" if user == nil + return user end def select_channel(search) - @channels.find { |x| x.name.downcase == search || x.slack_id.downcase == search } - #add argument error if doesn't exist + raise SlackError, "please give valid search" if search == nil || search == "" + channel = @channels.find { |x| x.name.downcase == search.downcase || x.slack_id.downcase == search.downcase } + raise SlackError, "no channel found" if channel == nil + return channel end end @@ -67,7 +71,7 @@ def main def options puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ quit):" - return gets.chomp.downcase + return gets.chomp end continue = true @@ -81,11 +85,13 @@ def options when "list users" slack.lists_users when "select user" - who = gets.chomp.downcase + who = gets.chomp chosen_user = slack.select_user(who) when "select channel" - who = gets.chomp.downcase + who = gets.chomp chosen_user = slack.select_channel(who) + when "send message" + slack.send_msg when "details" # if chosen_user == "" # puts "there is no selected user" diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index ceabadca..f47cc92f 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -19,7 +19,7 @@ slack = Slack.new # <-- .yml filename user_a = slack.select_user("UH53ZCBBR") # <-- this is "id" value for "kaseea" user_b = slack.select_user("kaseea") # <-- this is "name" value for "kaseea" - binding.pry + # binding.pry expect(user_a.user_id).must_equal "UH53ZCBBR" expect(user_b.user_name).must_equal "kaseea" end @@ -29,9 +29,9 @@ VCR.use_cassette("slack_query") do # <-- .yml filename slack = Slack.new channel_a = slack.select_channel("apiiiii") - # channel_b = slack.select_channel("CH4BCTLHK") # <-- this is the "id" value for the general channel + channel_b = slack.select_channel("CH4BCTLHK") # <-- this is the "id" value for the general channel expect(channel_a.name).must_equal "apiiiii" - # expect(channel_b.name).must_equal "random" + expect(channel_b.name).must_equal "random" end end end @@ -52,24 +52,24 @@ it "raise an error if given a non-existant user or channel" do VCR.use_cassette("slack_query") do - # TEST GOES HERE - channel_y = Slack.select_channel("Citris Fruit") - user_y = Slack.select_user("Pomolo") - - expect(channel_y).must_raise SlackError, "No match for this user" - expect(user_y).must_raise SlackError, "No match for this user" + slack = Slack.new + expect do + slack.select_channel("Citris Fruit") + end.must_raise SlackError + expect do + slack.select_user("Pomolo") + end.must_raise SlackError end end it "raise an error if no user is selected" do VCR.use_cassette("slack_query") do # TEST GOES HERE - new_slack_instance = Slack.new - expect(new_slack_instance.chosen_user).must_equal nil # <-- do we want this to be nil or ""? - expect(new_slack_instance.chosen_user).must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method + slack = Slack.new + # expect(chosen_user).must_equal "" # <-- do we want this to be nil or ""? + expect { slack.select_user("") }.must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method end end - # end # it "select a channel" do From 4767ec0c1c853b2514f8d7d8e038c2325751638b Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Fri, 22 Mar 2019 15:43:34 -0700 Subject: [PATCH 11/16] stopping point for Friday --- lib/slack.rb | 38 ++++++++---- specs/slack_spec.rb | 145 ++++++++++++++++---------------------------- 2 files changed, 80 insertions(+), 103 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index ec4dd717..1a2aa50f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -48,17 +48,27 @@ def lists_users end def select_user(search) - raise SlackError, "please give valid search" if search == nil || search == "" user = @users.find { |x| x.user_name.downcase == search.downcase || x.user_id.downcase == search.downcase } - raise SlackError, "no user found" if user == nil - return user end def select_channel(search) - raise SlackError, "please give valid search" if search == nil || search == "" channel = @channels.find { |x| x.name.downcase == search.downcase || x.slack_id.downcase == search.downcase } - raise SlackError, "no channel found" if channel == nil - return channel + end + + def self.send_msg(text, user) + url = "https://slack.com/api/chat.postMessage" + query_paramaters = { + token: SLACK_API_TOKEN, + channel: user, + text: text, + }, + response = HTTParty.post(url, query: query_paramaters, headers: {"Content-Type" => "application/x-www-form-urlencoded"}) + binding.pry + if response["ok"] + return true + else + raise SlackError, "Error when posting #{message} to #{channel}, error: #{response["error"]}" + end end end @@ -69,6 +79,13 @@ def main puts "there are #{slack.channels.length} channels and #{slack.users.length} members" + def name_checker(who) + while who == "" + puts "please enter a valid name/id" + who = gets.chomp + end + end + def options puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ quit):" return gets.chomp @@ -86,18 +103,17 @@ def options slack.lists_users when "select user" who = gets.chomp + name_checker(who) chosen_user = slack.select_user(who) + puts "user not found, please try another selection" if chosen_user == nil when "select channel" who = gets.chomp + name_checker(who) chosen_user = slack.select_channel(who) + puts "channel not found, please try another selection" if chosen_user == nil when "send message" slack.send_msg when "details" - # if chosen_user == "" - # puts "there is no selected user" - # options - # end - binding.pry chosen_user.details when "send message" slack.send_msg diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index f47cc92f..098a58b8 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -1,6 +1,9 @@ require_relative "test_helper" require "pry" +#potential tests +#make sure main method makes a new slack? + describe "Slack" do describe "list users and channels" do it "show a list of users" do @@ -49,26 +52,40 @@ #need a test for list channels #need a test for list users - - it "raise an error if given a non-existant user or channel" do - VCR.use_cassette("slack_query") do - slack = Slack.new - expect do - slack.select_channel("Citris Fruit") - end.must_raise SlackError - expect do - slack.select_user("Pomolo") - end.must_raise SlackError - end - end - - it "raise an error if no user is selected" do - VCR.use_cassette("slack_query") do - # TEST GOES HERE - slack = Slack.new - # expect(chosen_user).must_equal "" # <-- do we want this to be nil or ""? - expect { slack.select_user("") }.must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method + describe "send_msg" do + it "can send a valid message" do + VCR.use_cassette("slack_query") do + slack = Slack.new + return_value = slack.send_msg("sup?", + "apiiiii") + + expect(return_value).must_equal true + end end + # it "generates an error if given an invalid channel" do + # VCR.use_cassette("slack_query") do + # slack = Slack.new + # expect { + # slack.send_msg("Test message", + # "bogus") + # }.must_raise SlackError + # end + # end + # it "will generate an error if given an invalid key" do + # real_token = ENV["SLACK_TOKEN"] + # ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" + + # VCR.use_cassette("slack_query") do + # slack = Slack.new + # error = expect { + # User.send_msg("Test message with invalid key", + # "apiiiii") + # }.must_raise SlackError + # # expect(error.message).must_equal "you suck" + # end + + # ENV["SLACK_TOKEN"] = real_token + # end end end @@ -78,40 +95,10 @@ # end # end # -end - -# describe "send_msg" do -# it "can send a valid message" do -# VCR.use_cassette("slack_query") do -# return_value = SlackApi.send_msg("Test message", -# "ports-api-testing") - -# expect(return_value).must_equal true -# end -# end - -# it "generates an error if given an invalid channel" do -# VCR.use_cassette("slack_query") do -# expect { -# SlackApi.send_msg("Test message", -# "bogus") -# }.must_raise SlackApi::SlackError -# end -# end -# it "will generate an error if given an invalid key" do -# real_token = ENV["SLACK_TOKEN"] -# ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" -# VCR.use_cassette("slack_query") do -# error = expect { -# SlackApi.send_msg("Test message with invalid key", -# "ports-api-testing") -# }.must_raise SlackApi::SlackError -# expect(error.message).must_equal "Error when posting Test message with invalid key to ports-api-testing, error: invalid_auth" -# end +end -# ENV["SLACK_TOKEN"] = real_token -# end +# # it "will raise an error if given an empty message" do # VCR.use_cassette("slack_query") do @@ -127,49 +114,23 @@ # end # ---------------------------------------------------------- - -# describe "SlackAPI" do -# it "can send a valid message" do -# VCR.use_cassette("slack_query") do -# location = "Seattle" -# response = get_location(location) - -# expect(response["Seattle"]).wont_be_nil -# expect(response["Seattle"][:lon]).must_equal "-122.3300624" -# expect(response["Seattle"][:lat]).must_equal "47.6038321" -# end -# end - -# it "will raise an exception if the search fails" do -# VCR.use_cassette("location_find") do -# location = "" -# expect { -# response = get_location(location) -# }.must_raise SearchError -# end +# it "raise an error if given a non-existant user or channel" do +# VCR.use_cassette("slack_query") do +# slack = Slack.new +# expect do +# slack.select_channel("Citris Fruit") +# end.must_raise SlackError +# expect do +# slack.select_user("Pomolo") +# end.must_raise SlackError # end # end -# ---------------------------------------------------------- - -# describe "Listing Channels and Users" do -# it "list channels" do -# VCR.use_cassette("channels_find") do -# location = "Seattle" -# response = get_location(location) - -# expect(response["Seattle"]).wont_be_nil -# expect(response["Seattle"][:lon]).must_equal "-122.3300624" -# expect(response["Seattle"][:lat]).must_equal "47.6038321" -# end -# end - -# it "list users" do -# VCR.use_cassette("location_find") do -# user = "" # name parameter in slack api -# expect { -# response = get_location(location) -# }.must_raise SearchError -# end +# it "raise an error if no user is selected" do +# VCR.use_cassette("slack_query") do +# # TEST GOES HERE +# slack = Slack.new +# # expect(chosen_user).must_equal "" # <-- do we want this to be nil or ""? +# expect { slack.select_user("") }.must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method # end # end From d8a3ca15a2af9919f48f89b496a1bc931d75a750 Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Mon, 25 Mar 2019 11:32:11 -0700 Subject: [PATCH 12/16] fixes send message and comepletes wave 3 --- lib/slack.rb | 24 +++++++++++------------ specs/slack_spec.rb | 48 ++++++++++++++++++++------------------------- specs/user_spec.rb | 2 -- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 1a2aa50f..d642b261 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -55,19 +55,21 @@ def select_channel(search) channel = @channels.find { |x| x.name.downcase == search.downcase || x.slack_id.downcase == search.downcase } end - def self.send_msg(text, user) + def self.send_msg(channel, text) url = "https://slack.com/api/chat.postMessage" - query_paramaters = { - token: SLACK_API_TOKEN, - channel: user, + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + channel: channel, text: text, - }, - response = HTTParty.post(url, query: query_paramaters, headers: {"Content-Type" => "application/x-www-form-urlencoded"}) - binding.pry + } + + response = HTTParty.post(url, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + query: query_parameters) if response["ok"] return true else - raise SlackError, "Error when posting #{message} to #{channel}, error: #{response["error"]}" + raise SlackError, "Error when posting #{text} to #{channel}, error: #{response["error"]}" end end end @@ -87,7 +89,7 @@ def name_checker(who) end def options - puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ quit):" + puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ send message/ quit):" return gets.chomp end @@ -111,12 +113,10 @@ def options name_checker(who) chosen_user = slack.select_channel(who) puts "channel not found, please try another selection" if chosen_user == nil - when "send message" - slack.send_msg when "details" chosen_user.details when "send message" - slack.send_msg + Slack.send_msg(channel, text) when "quit" continue = false end diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index 098a58b8..c38f5f69 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -55,37 +55,31 @@ describe "send_msg" do it "can send a valid message" do VCR.use_cassette("slack_query") do - slack = Slack.new - return_value = slack.send_msg("sup?", - "apiiiii") - - expect(return_value).must_equal true + to_send = Slack.send_msg("apiiiii", "sup?") + expect(to_send).must_equal true + end + end + it "generates an error if given an invalid channel" do + VCR.use_cassette("slack_query") do + expect { + Slack.send_msg("bogus", "Test message") + }.must_raise SlackError end end - # it "generates an error if given an invalid channel" do - # VCR.use_cassette("slack_query") do - # slack = Slack.new - # expect { - # slack.send_msg("Test message", - # "bogus") - # }.must_raise SlackError - # end - # end - # it "will generate an error if given an invalid key" do - # real_token = ENV["SLACK_TOKEN"] - # ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" + it "will generate an error if given an invalid key" do + real_token = ENV["SLACK_TOKEN"] + ENV["SLACK_TOKEN"] = "NOT_REAL_TOKEN" - # VCR.use_cassette("slack_query") do - # slack = Slack.new - # error = expect { - # User.send_msg("Test message with invalid key", - # "apiiiii") - # }.must_raise SlackError - # # expect(error.message).must_equal "you suck" - # end + VCR.use_cassette("slack_query") do + error = expect { + Slack.send_msg("Test message with invalid key", + "apiiiii") + }.must_raise SlackError + # expect(error.message).must_equal "you suck" + end - # ENV["SLACK_TOKEN"] = real_token - # end + ENV["SLACK_TOKEN"] = real_token + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 07edae72..3da920bf 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -10,8 +10,6 @@ response = HTTParty.get(url, query: query_parameters) expect(slack.channels).wont_be_nil - # expect(response["Seattle"][:lon]).must_equal "-122.3300624" - # expect(response["Seattle"][:lat]).must_equal "47.6038321" end end end From 22b9e5719a6682badc6617c8b24b230af366d7f1 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Mar 2019 18:24:03 -0700 Subject: [PATCH 13/16] Cleaned up user interface. Fixed 'show details when no user/channel is selected' to not throw an error and crash. Worked on making 'send message' work on my machine; not done. --- lib/channel.rb | 10 ++++++---- lib/slack.rb | 32 ++++++++++++++++++++------------ lib/user.rb | 8 +++++--- specs/slack_spec.rb | 4 ++-- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 6d53bf7a..dd47e8a0 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -15,9 +15,11 @@ def initialize(name, topic, num_members, slack_id) end def details - puts "name: #{name}" - puts "topic: #{topic}" - puts "number of members: #{num_members}" - puts "slack id: #{slack_id}" + puts " + Name: #{name} + Topic: #{topic} + Number of members: #{num_members} + Slack ID: #{slack_id} + ---" end end diff --git a/lib/slack.rb b/lib/slack.rb index d642b261..a2b906b7 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,10 +15,10 @@ class Slack def initialize() @channels = [] @users = [] - poop + get_list end - def poop() + def get_list() query_parameters = { token: ENV["SLACK_API_TOKEN"], pretty: 1, @@ -75,21 +75,21 @@ def self.send_msg(channel, text) end def main - puts "Welcome to the Kasey-Elle Slack CLI!" - slack = Slack.new - puts "there are #{slack.channels.length} channels and #{slack.users.length} members" + puts "Welcome to the Kasey-Elle Slack CLI! +There are #{slack.channels.length} channels and #{slack.users.length} members in this instance of Slack." def name_checker(who) while who == "" - puts "please enter a valid name/id" + puts "Please enter a valid name/ID." who = gets.chomp end end def options - puts "What should we do next? (list channels/ list users/ select user/ select channel/ details/ send message/ quit):" + puts "What should we do next? You can: + list channels / list users / select user / select channel / show details / send message / quit" return gets.chomp end @@ -104,19 +104,27 @@ def options when "list users" slack.lists_users when "select user" + puts "Enter the name or ID of the user you wish to select." who = gets.chomp name_checker(who) chosen_user = slack.select_user(who) - puts "user not found, please try another selection" if chosen_user == nil + puts "That user was not found. Please check your spelling or try another selection." if chosen_user == nil when "select channel" + puts "Enter the name or ID of the channel you wish to select." who = gets.chomp name_checker(who) chosen_user = slack.select_channel(who) - puts "channel not found, please try another selection" if chosen_user == nil - when "details" - chosen_user.details + puts "That channel was not found. Please try again." if chosen_user == nil + when "show details" + if chosen_user == "" + puts "Please select a channel or user first." + else + chosen_user.details + end when "send message" - Slack.send_msg(channel, text) + text = gets.chomp # <-- Elle Added this and changed "chosen_user" on the next line; was "channel" + binding.pry + Slack.send_msg(chosen_user, text) when "quit" continue = false end diff --git a/lib/user.rb b/lib/user.rb index 05640d66..bc7f4575 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -14,8 +14,10 @@ def initialize(user_name, user_id, real_name) end def details - puts "name: #{user_name}" - puts "id: #{user_id}" - puts "real name: #{real_name}" + puts " + Name: #{user_name} + ID: #{user_id} + Real Name: #{real_name} + ---" end end diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index c38f5f69..26a830ec 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -1,8 +1,8 @@ require_relative "test_helper" require "pry" -#potential tests -#make sure main method makes a new slack? +# potential tests: +# make sure main method makes a new slack instance? describe "Slack" do describe "list users and channels" do From c3a8892c65348041caf2cc4ac1eb3794ed3fd666 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Tue, 26 Mar 2019 09:54:02 -0700 Subject: [PATCH 14/16] Fixed the 'send_msg' method. Can now send a message to a channel. --- lib/slack.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index a2b906b7..7a56c126 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,10 +15,10 @@ class Slack def initialize() @channels = [] @users = [] - get_list + get_lists end - def get_list() + def get_lists() query_parameters = { token: ENV["SLACK_API_TOKEN"], pretty: 1, @@ -37,13 +37,13 @@ def get_list() def lists_channels @channels.each do |x| - x.details + puts x.name end end def lists_users @users.each do |x| - x.details + puts "#{x.user_name} / #{x.real_name}" end end @@ -59,7 +59,7 @@ def self.send_msg(channel, text) url = "https://slack.com/api/chat.postMessage" query_parameters = { token: ENV["SLACK_API_TOKEN"], - channel: channel, + channel: channel.slack_id, # <-- Elle changed this, added .slack_id text: text, } @@ -69,7 +69,7 @@ def self.send_msg(channel, text) if response["ok"] return true else - raise SlackError, "Error when posting #{text} to #{channel}, error: #{response["error"]}" + raise SlackError, "Error when posting '#{text}' to #{channel}, error: #{response["error"]}" end end end @@ -135,3 +135,5 @@ def options end main if __FILE__ == $PROGRAM_NAME + +# NOTE for Elle: Run ruby lib/slack.rb from slack-cli folder to run the CLI program. From 57beebdc019b18c310568b307e5284d18f825543 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Tue, 26 Mar 2019 10:36:56 -0700 Subject: [PATCH 15/16] Got 'send_msg' working for user. Added an if/else to check for which kind of recipient (User or Channel) and set query parameters accordingly. Also put in a couple of catches for user or channel being nil, which can happen if you do 'select user/channel' and enter an invalid selection, and then select 'send message'. This now gets caught and redirects the user to the main prompt instead of crashing. TO DO: One test is now throwing an error. Need to fix test, the code works. --- lib/slack.rb | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 7a56c126..da51857e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -36,15 +36,19 @@ def get_lists() end def lists_channels + puts "\nHere are the channels:" @channels.each do |x| - puts x.name + puts " - #{x.name}" end + puts "---\n" end def lists_users + puts "\nHere are the users:" @users.each do |x| - puts "#{x.user_name} / #{x.real_name}" + puts " - #{x.user_name}" end + puts "---\n" end def select_user(search) @@ -55,13 +59,27 @@ def select_channel(search) channel = @channels.find { |x| x.name.downcase == search.downcase || x.slack_id.downcase == search.downcase } end - def self.send_msg(channel, text) + def self.send_msg(recipient, text) + if recipient == nil + puts "Please select a channel or user first." + main + end + url = "https://slack.com/api/chat.postMessage" - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - channel: channel.slack_id, # <-- Elle changed this, added .slack_id - text: text, - } + if recipient.class == Channel + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + channel: recipient.slack_id, + text: text, + } + elsif recipient.class == User + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + channel: recipient.user_id, + text: text, + # as_user: true, <-- use this in the future for allowing to send as a user + } + end response = HTTParty.post(url, headers: {"Content-Type" => "application/x-www-form-urlencoded"}, @@ -69,7 +87,7 @@ def self.send_msg(channel, text) if response["ok"] return true else - raise SlackError, "Error when posting '#{text}' to #{channel}, error: #{response["error"]}" + raise SlackError, "Error when posting '#{text}' to #{recipient}, error: #{response["error"]}" end end end @@ -116,20 +134,21 @@ def options chosen_user = slack.select_channel(who) puts "That channel was not found. Please try again." if chosen_user == nil when "show details" - if chosen_user == "" + if chosen_user == "" || chosen_user == nil puts "Please select a channel or user first." else chosen_user.details end when "send message" - text = gets.chomp # <-- Elle Added this and changed "chosen_user" on the next line; was "channel" - binding.pry + puts "What message would you like to send?" + text = gets.chomp Slack.send_msg(chosen_user, text) when "quit" continue = false + else + puts "Oops! That is not a valid option. Please try again." end end - # TODO project puts "Thank you for using the Kasey-Elle Slack CLI" end From f778e37b05e7ae395da8e57b12ff50e4d91fed74 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Tue, 26 Mar 2019 12:15:43 -0700 Subject: [PATCH 16/16] Removed unnecessary comments in spec file and tests that were unused due to refactoring or being duplicates. --- specs/slack_spec.rb | 48 --------------------------------------------- 1 file changed, 48 deletions(-) diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index 26a830ec..d62c4b94 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -50,8 +50,6 @@ end end - #need a test for list channels - #need a test for list users describe "send_msg" do it "can send a valid message" do VCR.use_cassette("slack_query") do @@ -75,56 +73,10 @@ Slack.send_msg("Test message with invalid key", "apiiiii") }.must_raise SlackError - # expect(error.message).must_equal "you suck" end ENV["SLACK_TOKEN"] = real_token end end end - - # it "select a channel" do - # VCR.use_cassette("slack_query") do - # # TEST GOES HERE - # end - # end - # - end - -# - -# it "will raise an error if given an empty message" do -# VCR.use_cassette("slack_query") do -# error = expect { -# SlackApi.send_msg("", -# "ports-api-testing") -# }.must_raise SlackApi::SlackError - -# expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" -# end -# end -# end -# end - -# ---------------------------------------------------------- -# it "raise an error if given a non-existant user or channel" do -# VCR.use_cassette("slack_query") do -# slack = Slack.new -# expect do -# slack.select_channel("Citris Fruit") -# end.must_raise SlackError -# expect do -# slack.select_user("Pomolo") -# end.must_raise SlackError -# end -# end - -# it "raise an error if no user is selected" do -# VCR.use_cassette("slack_query") do -# # TEST GOES HERE -# slack = Slack.new -# # expect(chosen_user).must_equal "" # <-- do we want this to be nil or ""? -# expect { slack.select_user("") }.must_raise SlackError # <-- also, I think we need chosen_user to be an attr_reader, or break this part into a different method -# end -# end