From 0690957b0b89341511d8d056d44c8c823bf771ad Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 19 Mar 2019 12:27:11 -0700 Subject: [PATCH 01/24] Created verification.rb to check connection to Slack API. --- lib/slack.rb | 4 ++-- lib/verification.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lib/verification.rb diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..8bce87a0 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,9 +3,9 @@ def main puts "Welcome to the Ada Slack CLI!" - # TODO project + # # 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/verification.rb b/lib/verification.rb new file mode 100644 index 00000000..cc89eade --- /dev/null +++ b/lib/verification.rb @@ -0,0 +1,20 @@ +require "httparty" +require "pry" +require "dotenv" +Dotenv.load + +BASE_URL = "https://slack.com/api/channels.list" +query = { + token: ENV["SLACK_API_TOKEN"], +} + +response = HTTParty.get(BASE_URL, query: query) + +response["channels"].each do |channel| + puts channel["name"] +end + +# code snips to save: +# puts channel["topic"]["value"] # gets topic +# puts channel["members"].length # gets # of members +# puts channel["id"] # gets slack id for channel From de7b750e00debe40dc41b0d694a4d7ef06bfd5db Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Tue, 19 Mar 2019 13:47:01 -0700 Subject: [PATCH 02/24] Wrote first test for channel class (list_channels) --- lib/channel.rb | 33 +++++++++++++++++++++++++++++++++ lib/slack.rb | 21 +++++++++++++++++---- specs/channel_spec.rb | 14 ++++++++++++++ specs/test_helper.rb | 14 +++++++++++++- 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 lib/channel.rb create mode 100644 specs/channel_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..11fabe77 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,33 @@ +require "httparty" +require "pry" +require "dotenv" +Dotenv.load + +class Channel + + + # def initialize + + # end + + BASE_URL = "https://slack.com/api/channels.list" + + def self.list_channels + + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + response["channels"].each do |channel| + puts channel["name"] + end +end +end + + + # code snips to save: + # puts channel["topic"]["value"] # gets topic + # puts channel["members"].length # gets # of members + # puts channel["id"] # gets slack id for channel + diff --git a/lib/slack.rb b/lib/slack.rb index 8bce87a0..41823af2 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,10 +2,23 @@ def main puts "Welcome to the Ada Slack CLI!" + puts "The workspace has x channels and x users" + ask_again = "Would you like to 'list users', 'list channels', or 'quit'?" + puts ask_again + response = gets.chomp - # # TODO project + until response == 'quit' + if response == 'list users' + #list user method (should see list of all users in workspace including username, real name, and slack id) + puts ask_again + response = gets.chomp + elsif response == 'list channels' + # list channel method (should see list of all channels in workspace including channel name, topic, member count and slack id) + puts ask_again + response = gets.chomp + end + end - puts "Thank you for using the Ada Slack CLI" + end - -main if __FILE__ == $PROGRAM_NAME +# main if __FILE__ == $PROGRAM_NAME diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..62d3871d --- /dev/null +++ b/specs/channel_spec.rb @@ -0,0 +1,14 @@ +require_relative 'test_helper.rb' + +describe "self.list" do + it "list all channels" do + VCR.use_cassette("list_channels") do + response = list + + expect(response).wont_be_nil + expect(response[0]).must_equal "general" + expect(response.length).must_equal 3 + end + end +end + diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..4f768b1f 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -5,11 +5,23 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require "webmock/minitest" require 'vcr' +require_relative '../lib/channel.rb' +require_relative '../lib/slack.rb' + 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.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 token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end +end From 3268e0cf52385447be43984fdcc0e30e80c8fdad Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 19 Mar 2019 14:21:08 -0700 Subject: [PATCH 03/24] passed tests for list_channel method --- lib/channel.rb | 34 +++++++++++++++++----------------- lib/verification.rb | 3 +-- specs/channel_spec.rb | 17 +++++++++++------ 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 11fabe77..93176fb9 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,7 +4,6 @@ Dotenv.load class Channel - # def initialize @@ -12,22 +11,23 @@ class Channel BASE_URL = "https://slack.com/api/channels.list" - def self.list_channels - - query = { - token: ENV["SLACK_API_TOKEN"], - } + # class SlackError < StandardError; end - response = HTTParty.get(BASE_URL, query: query) - response["channels"].each do |channel| - puts channel["name"] + def self.list_channels + query = { + token: ENV["SLACK_API_TOKEN"], + } + channel_list = {} + response = HTTParty.get(BASE_URL, query: query) + + if response.code != 200 + raise ArgumentError, "There was an error. The code is #{response.error}." + else + response["channels"].each do |channel| + channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + # binding.pry + end + end + return channel_list end end -end - - - # code snips to save: - # puts channel["topic"]["value"] # gets topic - # puts channel["members"].length # gets # of members - # puts channel["id"] # gets slack id for channel - diff --git a/lib/verification.rb b/lib/verification.rb index cc89eade..b0c8d6dd 100644 --- a/lib/verification.rb +++ b/lib/verification.rb @@ -11,9 +11,8 @@ response = HTTParty.get(BASE_URL, query: query) response["channels"].each do |channel| - puts channel["name"] + print channel["name"] end - # code snips to save: # puts channel["topic"]["value"] # gets topic # puts channel["members"].length # gets # of members diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 62d3871d..f43fc1ed 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,14 +1,19 @@ -require_relative 'test_helper.rb' +require_relative "test_helper.rb" describe "self.list" do it "list all channels" do VCR.use_cassette("list_channels") do - response = list + response = Channel.list_channels expect(response).wont_be_nil - expect(response[0]).must_equal "general" + expect(response.keys.include?("general")).must_equal true + expect(response["general"]).must_equal "topic" => "Company-wide announcements and work-based matters", + "member count" => 2, + "id" => "CH2SKTKPC" expect(response.length).must_equal 3 end - end -end - + end + it "will raise an error if the response code is not 200" do + #fill this in later? + end +end From 92c94494c0e7e9700aed2239f8ed1e979e3d9f57 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Tue, 19 Mar 2019 14:35:05 -0700 Subject: [PATCH 04/24] Trying to solve git pull issue --- lib/channel.rb | 34 +++++++++++++++++----------------- lib/user.rb | 0 specs/test_helper.rb | 1 + specs/user_spec.rb | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 lib/user.rb create mode 100644 specs/user_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb index 93176fb9..11fabe77 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,6 +4,7 @@ Dotenv.load class Channel + # def initialize @@ -11,23 +12,22 @@ class Channel BASE_URL = "https://slack.com/api/channels.list" - # class SlackError < StandardError; end - def self.list_channels - query = { - token: ENV["SLACK_API_TOKEN"], - } - channel_list = {} - response = HTTParty.get(BASE_URL, query: query) - - if response.code != 200 - raise ArgumentError, "There was an error. The code is #{response.error}." - else - response["channels"].each do |channel| - channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} - # binding.pry - end - end - return channel_list + + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + response["channels"].each do |channel| + puts channel["name"] end end +end + + + # code snips to save: + # puts channel["topic"]["value"] # gets topic + # puts channel["members"].length # gets # of members + # puts channel["id"] # gets slack id for channel + diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 4f768b1f..14d92793 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,6 +10,7 @@ require_relative '../lib/channel.rb' require_relative '../lib/slack.rb' +require_relative '../lib/user.rb' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..b6a7edb3 --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,15 @@ +require_relative 'test_helper' + +describe "self.list" do + it "list all users" do + VCR.use_cassette("list_users") do + response = User.list_users + + expect(response).wont_be_nil + expect(response.keys.include?("aribray")).must_equal true + expect(response["aribray"]).must_equal "real_name" => "Ariana Bray", + "id" => "CH2SKTKPC" + expect(response.length).must_equal 2 + end + end +end From e27145fda7e4e12abbf341815d28415d83b4a974 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Tue, 19 Mar 2019 14:38:40 -0700 Subject: [PATCH 05/24] Fixed git pull issue --- lib/channel.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 11fabe77..a8e43a78 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,7 +4,6 @@ Dotenv.load class Channel - # def initialize @@ -12,18 +11,26 @@ class Channel BASE_URL = "https://slack.com/api/channels.list" + # class SlackError < StandardError; end + def self.list_channels - - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) - response["channels"].each do |channel| - puts channel["name"] + query = { + token: ENV["SLACK_API_TOKEN"], + } + channel_list = {} + response = HTTParty.get(BASE_URL, query: query) + + if response.code != 200 + raise ArgumentError, "There was an error. The code is #{response.error}." + else + response["channels"].each do |channel| + channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + # binding.pry + end + end + return channel_list end end -end # code snips to save: From 8c63b504ac3edee656eee6d925dde1e976e524ff Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 19 Mar 2019 15:17:44 -0700 Subject: [PATCH 06/24] Added tests for user, wrote method to pass tests, got slack.rb CLI running --- lib/channel.rb | 14 +++++--------- lib/slack.rb | 20 +++++++++++--------- lib/user.rb | 39 +++++++++++++++++++++++++++++++++++++++ specs/user_spec.rb | 8 ++++---- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index a8e43a78..04232644 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,9 +4,10 @@ Dotenv.load class Channel + # attr_accessor :list_channels, :channel_list # def initialize - + # @channel_list = {} # end BASE_URL = "https://slack.com/api/channels.list" @@ -17,9 +18,11 @@ def self.list_channels query = { token: ENV["SLACK_API_TOKEN"], } - channel_list = {} + response = HTTParty.get(BASE_URL, query: query) + channel_list = {} + if response.code != 200 raise ArgumentError, "There was an error. The code is #{response.error}." else @@ -31,10 +34,3 @@ def self.list_channels return channel_list end end - - - # code snips to save: - # puts channel["topic"]["value"] # gets topic - # puts channel["members"].length # gets # of members - # puts channel["id"] # gets slack id for channel - diff --git a/lib/slack.rb b/lib/slack.rb index 41823af2..0acd4964 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,24 +1,26 @@ #!/usr/bin/env ruby +require_relative "user" +require_relative "channel" def main puts "Welcome to the Ada Slack CLI!" - puts "The workspace has x channels and x users" + puts "The workspace has #{Channel.list_channels.length} channels and #{User.list_users.length} users" ask_again = "Would you like to 'list users', 'list channels', or 'quit'?" puts ask_again response = gets.chomp - until response == 'quit' - if response == 'list users' - #list user method (should see list of all users in workspace including username, real name, and slack id) + until response == "quit" + if response == "list users" + # user = User.new + puts User.list_users puts ask_again response = gets.chomp - elsif response == 'list channels' - # list channel method (should see list of all channels in workspace including channel name, topic, member count and slack id) + elsif response == "list channels" + puts Channel.list_channels puts ask_again response = gets.chomp end end - - end -# main if __FILE__ == $PROGRAM_NAME + +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index e69de29b..33e82b0f 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -0,0 +1,39 @@ +require "httparty" +require "pry" +require "dotenv" +Dotenv.load + +# module SlackAPI +class User + # attr_accessor :list_users, :user_list + + # def initialize + # @user_list = {} + # end + + BASE_URL = "https://slack.com/api/users.list" + + # class SlackError < StandardError; end + + def self.list_users + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + + user_list = {} + + if response.code != 200 + raise ArgumentError, "There was an error. The code is #{response.error}." + else + response["members"].each do |member| + user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + # binding.pry + end + end + return user_list + end +end + +# end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b6a7edb3..05ef610c 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,4 +1,4 @@ -require_relative 'test_helper' +require_relative "test_helper" describe "self.list" do it "list all users" do @@ -7,9 +7,9 @@ expect(response).wont_be_nil expect(response.keys.include?("aribray")).must_equal true - expect(response["aribray"]).must_equal "real_name" => "Ariana Bray", - "id" => "CH2SKTKPC" - expect(response.length).must_equal 2 + expect(response["aribray"]).must_equal "real name" => "Ariana Bray", + "slack id" => "UH3UT3SJK" + expect(response.length).must_equal 3 end end end From 446832371d28cfc762da8a5f1b92db0e97ea4e3a Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Tue, 19 Mar 2019 16:08:30 -0700 Subject: [PATCH 07/24] Wrote tests and code for select user method --- lib/slack.rb | 8 +++++++- lib/user.rb | 21 ++++++++++++++++++++- specs/user_spec.rb | 11 +++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 0acd4964..e1cc8866 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,7 +5,7 @@ def main puts "Welcome to the Ada Slack CLI!" puts "The workspace has #{Channel.list_channels.length} channels and #{User.list_users.length} users" - ask_again = "Would you like to 'list users', 'list channels', or 'quit'?" + ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details', or 'quit'?" puts ask_again response = gets.chomp @@ -19,6 +19,12 @@ def main puts Channel.list_channels puts ask_again response = gets.chomp + elsif response == 'select user' + # get instance of user + elsif response == 'select channel' + # get instance of channel + elsif response == 'details' + # get details for selected end end end diff --git a/lib/user.rb b/lib/user.rb index 33e82b0f..85686065 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -34,6 +34,25 @@ def self.list_users end return user_list end -end + + def select_user(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + selected_user = "" + response["members"].each do |member| + if member["id"] == identifier + selected_user = identifier + elsif member["name"] == identifier + selected_user = identifier + end + end + return selected_user + end + + +end # end of class # end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 05ef610c..de9aa53a 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -13,3 +13,14 @@ end end end + +describe "select_user" do + it "selects a user as the current recipient" do + user = User.new + VCR.use_cassette("select_user") do + response = user.select_user("UH3UT3SJK") + + expect(response).must_equal "UH3UT3SJK" + end + end +end From e646028abff04a2f297694b7cbabc11eb0021412 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 19 Mar 2019 16:22:12 -0700 Subject: [PATCH 08/24] Added tests and methods for select channel --- lib/channel.rb | 17 +++++++++++++++++ lib/slack.rb | 20 +++++++++++++++----- specs/channel_spec.rb | 17 +++++++++++++++++ specs/user_spec.rb | 10 ++++++++-- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 04232644..159e718c 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -33,4 +33,21 @@ def self.list_channels end return channel_list end + + def select_channel(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + selected_channel = "" + response["channels"].each do |channel| + if channel["id"] == identifier + selected_channel = identifier + elsif channel["name"] == identifier + selected_channel = identifier + end + end + return selected_channel + end end diff --git a/lib/slack.rb b/lib/slack.rb index e1cc8866..219c1a87 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -19,11 +19,21 @@ def main puts Channel.list_channels puts ask_again response = gets.chomp - elsif response == 'select user' - # get instance of user - elsif response == 'select channel' - # get instance of channel - elsif response == 'details' + elsif response == "select user" + puts "What is the user id or Slack name?" + identifier = gets.chomp + user = User.new + selected_user = user.select_user(identifier) + puts ask_again + response = gets.chomp + elsif response == "select channel" + puts "What is the user id or Slack name?" + identifier = gets.chomp + channel = Channel.new + selected_channel = channel.select_user(identifier) + puts ask_again + response = gets.chomp + elsif response == "details" # get details for selected end end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index f43fc1ed..760648c7 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -17,3 +17,20 @@ #fill this in later? end end + +describe "select_channel" do + it "selects a channel as the current recipient when user inputs channel ID" do + channel = Channel.new + VCR.use_cassette("select_channel") do + response = channel.select_channel("CH2SKTKPC") + expect(response).must_equal "CH2SKTKPC" + end + end + it "selects a channel as the current recipient when user inputs channel name" do + channel = Channel.new + VCR.use_cassette("select_channel") do + response = channel.select_channel("general") + expect(response).must_equal "general" + end + end +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index de9aa53a..7afaa255 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -15,12 +15,18 @@ end describe "select_user" do - it "selects a user as the current recipient" do + it "selects a user as the current recipient when user inputs ID" do user = User.new VCR.use_cassette("select_user") do response = user.select_user("UH3UT3SJK") - expect(response).must_equal "UH3UT3SJK" end end + it "selects a user as the current recipient when user inputs name" do + user = User.new + VCR.use_cassette("select_user") do + response = user.select_user("aribray") + expect(response).must_equal "aribray" + end + end end From 806f6cdad1f1dc4ad213290dbb6e2790276fec74 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 19 Mar 2019 16:28:44 -0700 Subject: [PATCH 09/24] Started details part of slack.rb --- lib/slack.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 219c1a87..0924fa80 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -24,6 +24,9 @@ def main identifier = gets.chomp user = User.new selected_user = user.select_user(identifier) + if selected_user == "" + puts "There is no user with that identifier" + end puts ask_again response = gets.chomp elsif response == "select channel" @@ -31,10 +34,13 @@ def main identifier = gets.chomp channel = Channel.new selected_channel = channel.select_user(identifier) + if selected_channel == "" + puts "There is no channel with that identifier" + end puts ask_again response = gets.chomp elsif response == "details" - # get details for selected + # selected_user or selected_channel is parameter for get details method, loop through api response and return hash with details end end end From a588b632a2e886144c51dbb2a8b2bd2f442facef Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Wed, 20 Mar 2019 10:28:57 -0700 Subject: [PATCH 10/24] moved dotevn to test_helper file --- lib/channel.rb | 3 --- lib/user.rb | 5 ----- specs/test_helper.rb | 21 ++++++++++++--------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 159e718c..0d006153 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,7 +1,4 @@ -require "httparty" require "pry" -require "dotenv" -Dotenv.load class Channel # attr_accessor :list_channels, :channel_list diff --git a/lib/user.rb b/lib/user.rb index 85686065..7d5f4a9e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,7 +1,4 @@ -require "httparty" require "pry" -require "dotenv" -Dotenv.load # module SlackAPI class User @@ -51,8 +48,6 @@ def select_user(identifier) end return selected_user end - - end # end of class # end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 14d92793..32d8e755 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,16 +1,19 @@ -require 'simplecov' +require "simplecov" SimpleCov.start +require "dotenv" +Dotenv.load -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' +require "httparty" +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" require "webmock/minitest" -require 'vcr' +require "vcr" -require_relative '../lib/channel.rb' -require_relative '../lib/slack.rb' -require_relative '../lib/user.rb' +require_relative "../lib/channel.rb" +require_relative "../lib/slack.rb" +require_relative "../lib/user.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 38cd541c8f3f0a80d724149e674c8226a557fdf7 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Wed, 20 Mar 2019 14:30:39 -0700 Subject: [PATCH 11/24] Added module, added SlackError & tests for SlackError, added see_details method & tests to user --- lib/channel.rb | 80 +++++++++++++++++--------------- lib/user.rb | 104 ++++++++++++++++++++++++++---------------- specs/channel_spec.rb | 14 ++++-- specs/user_spec.rb | 76 +++++++++++++++++++++--------- 4 files changed, 172 insertions(+), 102 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 0d006153..b562650f 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,50 +1,56 @@ require "pry" -class Channel - # attr_accessor :list_channels, :channel_list +module SlackAPI + class SlackError < StandardError; end - # def initialize - # @channel_list = {} - # end + class Channel + # attr_accessor :list_channels, :channel_list - BASE_URL = "https://slack.com/api/channels.list" + # def initialize + # @channel_list = {} + # end - # class SlackError < StandardError; end + BASE_URL = "https://slack.com/api/channels.list" - def self.list_channels - query = { - token: ENV["SLACK_API_TOKEN"], - } + def self.list_channels + query = { + token: ENV["SLACK_API_TOKEN"], + } - response = HTTParty.get(BASE_URL, query: query) + response = HTTParty.get(BASE_URL, query: query) - channel_list = {} + channel_list = {} - if response.code != 200 - raise ArgumentError, "There was an error. The code is #{response.error}." - else - response["channels"].each do |channel| - channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} - # binding.pry + if response["ok"] != true # write a test for this + raise ArgumentError, "There was an error. The code is #{response.error}." + else + response["channels"].each do |channel| + channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + # binding.pry + end end + return channel_list end - return channel_list - end - - def select_channel(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) - selected_channel = "" - response["channels"].each do |channel| - if channel["id"] == identifier - selected_channel = identifier - elsif channel["name"] == identifier - selected_channel = identifier + + def select_channel(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + + selected_channel = "" + response["channels"].each do |channel| + if channel["id"] == identifier + selected_channel = identifier + elsif channel["name"] == identifier + selected_channel = identifier + end + end + if selected_channel == "" + raise SlackAPI::SlackError, "That is not a vaild channel" end + return selected_channel end - return selected_channel - end -end + end #end of class +end #end of module diff --git a/lib/user.rb b/lib/user.rb index 7d5f4a9e..7bf15228 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,53 +1,79 @@ require "pry" -# module SlackAPI -class User - # attr_accessor :list_users, :user_list +module SlackAPI + class SlackError < StandardError; end - # def initialize - # @user_list = {} - # end + class User + # attr_accessor :list_users, :user_list - BASE_URL = "https://slack.com/api/users.list" + # def initialize + # @user_list = {} + # end - # class SlackError < StandardError; end + BASE_URL = "https://slack.com/api/users.list" - def self.list_users - query = { - token: ENV["SLACK_API_TOKEN"], - } + def self.list_users + query = { + token: ENV["SLACK_API_TOKEN"], + } - response = HTTParty.get(BASE_URL, query: query) + response = HTTParty.get(BASE_URL, query: query) - user_list = {} + user_list = {} - if response.code != 200 - raise ArgumentError, "There was an error. The code is #{response.error}." - else - response["members"].each do |member| - user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} - # binding.pry + if response["ok"] + response["members"].each do |member| + user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + # binding.pry + end + else + raise SlackAPI::SlackError, "There was an error. The code is #{response.error}." end + return user_list end - return user_list - end - - def select_user(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) - selected_user = "" - response["members"].each do |member| - if member["id"] == identifier - selected_user = identifier - elsif member["name"] == identifier - selected_user = identifier + + def select_user(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + selected_user = "" + response["members"].each do |member| + if member["id"] == identifier + selected_user = identifier + elsif member["name"] == identifier + selected_user = identifier + end + end + if selected_user == "" + raise SlackAPI::SlackError, "That is not a valid user" end + return selected_user end - return selected_user - end -end # end of class -# end + def see_details(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + + user_details = {} + + if response.code != 200 # change to ok true/false instead of this + raise ArgumentError, "There was an error. The code is #{response.error}." + else + response["members"].each do |member| + if member["id"] == identifier || member["name"] == identifier + # binding.pry + user_details[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + end + end + # binding.pry + return user_details + # add conditional if member does not exist + end + end + end # end of class +end # end of module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 760648c7..498196da 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -3,7 +3,7 @@ describe "self.list" do it "list all channels" do VCR.use_cassette("list_channels") do - response = Channel.list_channels + response = SlackAPI::Channel.list_channels expect(response).wont_be_nil expect(response.keys.include?("general")).must_equal true @@ -14,23 +14,29 @@ end end it "will raise an error if the response code is not 200" do - #fill this in later? + #fill this in later end end describe "select_channel" do it "selects a channel as the current recipient when user inputs channel ID" do - channel = Channel.new + channel = SlackAPI::Channel.new VCR.use_cassette("select_channel") do response = channel.select_channel("CH2SKTKPC") expect(response).must_equal "CH2SKTKPC" end end it "selects a channel as the current recipient when user inputs channel name" do - channel = Channel.new + channel = SlackAPI::Channel.new VCR.use_cassette("select_channel") do response = channel.select_channel("general") expect(response).must_equal "general" end end + it "raises an error if the channel is not valid" do + channel = SlackAPI::Channel.new + VCR.use_cassette("select_channel") do + expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 7afaa255..488733fe 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,32 +1,64 @@ require_relative "test_helper" +describe SlackAPI do + describe "self.list" do + it "list all users" do + VCR.use_cassette("list_users") do + response = SlackAPI::User.list_users -describe "self.list" do - it "list all users" do - VCR.use_cassette("list_users") do - response = User.list_users - - expect(response).wont_be_nil - expect(response.keys.include?("aribray")).must_equal true - expect(response["aribray"]).must_equal "real name" => "Ariana Bray", - "slack id" => "UH3UT3SJK" - expect(response.length).must_equal 3 + expect(response).wont_be_nil + expect(response.keys.include?("aribray")).must_equal true + expect(response["aribray"]).must_equal "real name" => "Ariana Bray", + "slack id" => "UH3UT3SJK" + expect(response.length).must_equal 3 + end end + # it "raises error if response 'ok' is false" do + # real_token = ENV["SLACK_API_TOKEN"] + # ENV["SLACK_API_TOKEN"] = "badtoken" + + # VCR.use_cassette("list_users") do + # return_value = SlackAPI::User.list_users + # expect (return_value).must_equal true + # end + # ENV["SLACK_API_TOKEN"] = real_token + # end end -end -describe "select_user" do - it "selects a user as the current recipient when user inputs ID" do - user = User.new - VCR.use_cassette("select_user") do - response = user.select_user("UH3UT3SJK") - expect(response).must_equal "UH3UT3SJK" + describe "select_user" do + it "selects a user as the current recipient when user inputs ID" do + user = SlackAPI::User.new + VCR.use_cassette("select_user") do + response = user.select_user("UH3UT3SJK") + expect(response).must_equal "UH3UT3SJK" + end + end + it "selects a user as the current recipient when user inputs name" do + user = SlackAPI::User.new + VCR.use_cassette("select_user") do + response = user.select_user("aribray") + expect(response).must_equal "aribray" + end + end + it "raises a SlackError if the user's name or id is invalid" do + user = SlackAPI::User.new + VCR.use_cassette("select_user") do + expect { user.select_user("fakeuser") }.must_raise SlackAPI::SlackError + end end end - it "selects a user as the current recipient when user inputs name" do - user = User.new - VCR.use_cassette("select_user") do - response = user.select_user("aribray") - expect(response).must_equal "aribray" + + describe "see_details" do + it "shows details for selected user" do + user = SlackAPI::User.new + VCR.use_cassette("select_user") do + selected_user = user.select_user("UH3UT3SJK") + user_details = user.see_details(selected_user) + + expect(user_details).must_be_kind_of Hash + expect(user_details.length).must_equal 1 + expect(user_details["aribray"]).must_equal "real name" => "Ariana Bray", + "slack id" => "UH3UT3SJK" + end end end end From 53c7eac91dc3e09326fce53de3d6526d00b89569 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Wed, 20 Mar 2019 15:33:09 -0700 Subject: [PATCH 12/24] added tests for user details. Trying to fix cli for details prompt --- lib/channel.rb | 26 ++++++++++++++++++++++++-- lib/slack.rb | 25 ++++++++++++++++++------- lib/user.rb | 24 ++++++++++++------------ specs/channel_spec.rb | 15 +++++++++++++++ 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index b562650f..6fef03a6 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,4 +1,5 @@ require "pry" +require "httparty" module SlackAPI class SlackError < StandardError; end @@ -22,10 +23,10 @@ def self.list_channels channel_list = {} if response["ok"] != true # write a test for this - raise ArgumentError, "There was an error. The code is #{response.error}." + raise SlackAPI::SlackError, "There was an error. The code is #{response["error"]}." else response["channels"].each do |channel| - channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + channel_list[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } # binding.pry end end @@ -52,5 +53,26 @@ def select_channel(identifier) end return selected_channel end + + def see_details(identifier) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(BASE_URL, query: query) + + channel_details = {} + + if response["ok"] != true + raise SlackAPI::SlackError, "This channel is not valid." + else + response["channels"].each do |channel| + if channel["id"] == identifier || channel["name"] == identifier + channel_details[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + return channel_details + end + end + end + end end #end of class end #end of module diff --git a/lib/slack.rb b/lib/slack.rb index 0924fa80..37a3d749 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,10 +1,13 @@ #!/usr/bin/env ruby require_relative "user" require_relative "channel" +require "httparty" +require "dotenv" +Dotenv.load def main puts "Welcome to the Ada Slack CLI!" - puts "The workspace has #{Channel.list_channels.length} channels and #{User.list_users.length} users" + puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users" ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details', or 'quit'?" puts ask_again response = gets.chomp @@ -12,17 +15,18 @@ def main until response == "quit" if response == "list users" # user = User.new - puts User.list_users + puts SlackAPI::User.list_users puts ask_again response = gets.chomp elsif response == "list channels" - puts Channel.list_channels + puts SlackAPI::Channel.list_channels puts ask_again response = gets.chomp elsif response == "select user" + select_response = "select user" puts "What is the user id or Slack name?" identifier = gets.chomp - user = User.new + user = SlackAPI::User.new selected_user = user.select_user(identifier) if selected_user == "" puts "There is no user with that identifier" @@ -30,17 +34,24 @@ def main puts ask_again response = gets.chomp elsif response == "select channel" + select_response = "select channel" puts "What is the user id or Slack name?" identifier = gets.chomp - channel = Channel.new - selected_channel = channel.select_user(identifier) + channel = SlackAPI::Channel.new + selected_channel = channel.select_channel(identifier) if selected_channel == "" puts "There is no channel with that identifier" end puts ask_again response = gets.chomp elsif response == "details" - # selected_user or selected_channel is parameter for get details method, loop through api response and return hash with details + # if select_response == "select user" + details = user.see_details(selected_user) + # puts details + # elsif select_response == "select channel" + # details = channel.see_details(selected_channel) + # puts details + # end end end end diff --git a/lib/user.rb b/lib/user.rb index 7bf15228..5c691055 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,4 +1,5 @@ require "pry" +require "httparty" module SlackAPI class SlackError < StandardError; end @@ -23,7 +24,7 @@ def self.list_users if response["ok"] response["members"].each do |member| - user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + user_list[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } # binding.pry end else @@ -39,6 +40,7 @@ def select_user(identifier) response = HTTParty.get(BASE_URL, query: query) selected_user = "" + response["members"].each do |member| if member["id"] == identifier selected_user = identifier @@ -61,19 +63,17 @@ def see_details(identifier) user_details = {} - if response.code != 200 # change to ok true/false instead of this - raise ArgumentError, "There was an error. The code is #{response.error}." - else - response["members"].each do |member| - if member["id"] == identifier || member["name"] == identifier - # binding.pry - user_details[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} - end + # if response["ok"] != true + # raise SlackAPI::SlackError, "There was an error." # add error message later + # else + response["members"].each do |member| + if member["id"] == identifier || member["name"] == identifier + user_details[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } + return user_details end - # binding.pry - return user_details - # add conditional if member does not exist end end + + # end end # end of class end # end of module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 498196da..e29edb52 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -40,3 +40,18 @@ end end end + +describe "see_details" do + it "shows details for a selected channel" do + channel = SlackAPI::Channel.new + VCR.use_cassette("see_details") do + selected_channel = channel.select_channel("CH2SKTKPC") + channel_details = channel.see_details(selected_channel) + + expect(channel_details).must_be_kind_of Hash + expect(channel_details.length).must_equal 1 + expect(channel_details["general"]).must_equal "topic" => "Company-wide announcements and work-based matters", + "member count" => 2, "id" => "CH2SKTKPC" + end + end +end From dd1a21ce95f245713cdc82203fe2c30a9a057376 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Wed, 20 Mar 2019 17:08:49 -0700 Subject: [PATCH 13/24] fixed bug in details output in CLI, added recipient.rb --- lib/recipient.rb | 27 +++++++++++++++++++++++++++ lib/slack.rb | 18 +++++++++++------- lib/user.rb | 6 ++---- 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 lib/recipient.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..169c1251 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,27 @@ +module SlackAPI + class Recipient + def api_wrapper + end + + BASE_URL = "https://slack.com/api/" + + def self.list(recipient, sub_url) + query = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get("#{BASE_URL}#{sub_url}", query: query) + + return response + # iterate through recipient depending on if it is user or channel + end + + def self.select(identifier) + raise NotImplementedError, "Implement me in a child class" + end + + def self.see_details(identifier) + raise NotImplementedError, "Implement me in a child class" + end + end +end diff --git a/lib/slack.rb b/lib/slack.rb index 37a3d749..fd33bd8e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -45,13 +45,17 @@ def main puts ask_again response = gets.chomp elsif response == "details" - # if select_response == "select user" - details = user.see_details(selected_user) - # puts details - # elsif select_response == "select channel" - # details = channel.see_details(selected_channel) - # puts details - # end + if select_response == "select user" + details = user.see_details(selected_user) + puts details + puts ask_again + response = gets.chomp + elsif select_response == "select channel" + details = channel.see_details(selected_channel) + puts details + puts ask_again + response = gets.chomp + end end end end diff --git a/lib/user.rb b/lib/user.rb index 5c691055..38e8933c 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -24,7 +24,7 @@ def self.list_users if response["ok"] response["members"].each do |member| - user_list[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } + user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} # binding.pry end else @@ -68,12 +68,10 @@ def see_details(identifier) # else response["members"].each do |member| if member["id"] == identifier || member["name"] == identifier - user_details[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } + user_details[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} return user_details end end end - - # end end # end of class end # end of module From 7d2956a008faae302cbf41fc47cacf9d5af35caa Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Thu, 21 Mar 2019 15:21:12 -0700 Subject: [PATCH 14/24] added recipient class and made User and Channel inherit from recipient --- lib/channel.rb | 35 +++++++++++------------------------ lib/recipient.rb | 23 ++++++++++------------- lib/user.rb | 38 ++++++++++++++++++-------------------- specs/channel_spec.rb | 15 +++++++++------ specs/test_helper.rb | 1 + specs/user_spec.rb | 2 +- 6 files changed, 50 insertions(+), 64 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 6fef03a6..1a249e95 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,24 +1,19 @@ require "pry" require "httparty" +require_relative "recipient" module SlackAPI class SlackError < StandardError; end - class Channel - # attr_accessor :list_channels, :channel_list - - # def initialize - # @channel_list = {} + class Channel < Recipient + # def initialize() + # super("channels.list") # end - BASE_URL = "https://slack.com/api/channels.list" + BASE_URL = "https://slack.com/api/" def self.list_channels - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) + response = Recipient.get("channels.list") channel_list = {} @@ -26,19 +21,15 @@ def self.list_channels raise SlackAPI::SlackError, "There was an error. The code is #{response["error"]}." else response["channels"].each do |channel| - channel_list[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} # binding.pry end end return channel_list end - def select_channel(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) + def select_channel(identifier) # maybe in parent class? + response = Recipient.get("channels.list") selected_channel = "" response["channels"].each do |channel| @@ -55,11 +46,7 @@ def select_channel(identifier) end def see_details(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) + response = Recipient.get("channels.list") channel_details = {} @@ -68,7 +55,7 @@ def see_details(identifier) else response["channels"].each do |channel| if channel["id"] == identifier || channel["name"] == identifier - channel_details[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + channel_details[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} return channel_details end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 169c1251..9a6e2f3f 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,27 +1,24 @@ +# require "httparty" +# require "dotenv" +# Dotenv.load + module SlackAPI class Recipient - def api_wrapper - end - BASE_URL = "https://slack.com/api/" - def self.list(recipient, sub_url) + # def initialize(sub_url) + # @sub_url = sub_url + # end + + def self.get(sub_url) query = { token: ENV["SLACK_API_TOKEN"], } + # puts sub_url response = HTTParty.get("#{BASE_URL}#{sub_url}", query: query) return response - # iterate through recipient depending on if it is user or channel - end - - def self.select(identifier) - raise NotImplementedError, "Implement me in a child class" - end - - def self.see_details(identifier) - raise NotImplementedError, "Implement me in a child class" end end end diff --git a/lib/user.rb b/lib/user.rb index 38e8933c..9280a082 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,24 +1,19 @@ require "pry" require "httparty" +require_relative "recipient" module SlackAPI class SlackError < StandardError; end - class User - # attr_accessor :list_users, :user_list + class User < Recipient + # def initialize() + # super("users.list") + # end - # def initialize - # @user_list = {} - # end - - BASE_URL = "https://slack.com/api/users.list" + BASE_URL = "https://slack.com/api/" def self.list_users - query = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(BASE_URL, query: query) + response = Recipient.get("users.list") user_list = {} @@ -34,11 +29,13 @@ def self.list_users end def select_user(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } + # query = { + # token: ENV["SLACK_API_TOKEN"], + # } + + # response = HTTParty.get(BASE_URL, query: query) + response = Recipient.get("users.list") - response = HTTParty.get(BASE_URL, query: query) selected_user = "" response["members"].each do |member| @@ -55,11 +52,12 @@ def select_user(identifier) end def see_details(identifier) - query = { - token: ENV["SLACK_API_TOKEN"], - } + # query = { + # token: ENV["SLACK_API_TOKEN"], + # } - response = HTTParty.get(BASE_URL, query: query) + # response = HTTParty.get(BASE_URL, query: query) + response = Recipient.get("users.list") user_details = {} diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e29edb52..5fc91797 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,9 +1,10 @@ -require_relative "test_helper.rb" +require_relative "test_helper" describe "self.list" do it "list all channels" do VCR.use_cassette("list_channels") do response = SlackAPI::Channel.list_channels + puts response expect(response).wont_be_nil expect(response.keys.include?("general")).must_equal true @@ -20,21 +21,23 @@ describe "select_channel" do it "selects a channel as the current recipient when user inputs channel ID" do - channel = SlackAPI::Channel.new + channel_url = "channels.list" + channel = SlackAPI::Channel.new() + VCR.use_cassette("select_channel") do response = channel.select_channel("CH2SKTKPC") expect(response).must_equal "CH2SKTKPC" end end it "selects a channel as the current recipient when user inputs channel name" do - channel = SlackAPI::Channel.new + channel = SlackAPI::Channel.new() VCR.use_cassette("select_channel") do response = channel.select_channel("general") expect(response).must_equal "general" end end it "raises an error if the channel is not valid" do - channel = SlackAPI::Channel.new + channel = SlackAPI::Channel.new() VCR.use_cassette("select_channel") do expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError end @@ -43,10 +46,10 @@ describe "see_details" do it "shows details for a selected channel" do - channel = SlackAPI::Channel.new + channel = SlackAPI::Channel.new() VCR.use_cassette("see_details") do selected_channel = channel.select_channel("CH2SKTKPC") - channel_details = channel.see_details(selected_channel) + channel_details = channel.see_details("CH2SKTKPC") expect(channel_details).must_be_kind_of Hash expect(channel_details.length).must_equal 1 diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 32d8e755..760e9d4f 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -14,6 +14,7 @@ require_relative "../lib/channel.rb" require_relative "../lib/slack.rb" require_relative "../lib/user.rb" +require_relative "../lib/recipient.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 488733fe..238c3f40 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,7 +4,7 @@ it "list all users" do VCR.use_cassette("list_users") do response = SlackAPI::User.list_users - + puts response expect(response).wont_be_nil expect(response.keys.include?("aribray")).must_equal true expect(response["aribray"]).must_equal "real name" => "Ariana Bray", From 948b30b54d0eeab1a33241ec7d8e00c30d577186 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Thu, 21 Mar 2019 15:36:45 -0700 Subject: [PATCH 15/24] Added send msg method to channel class and wrote test --- lib/channel.rb | 22 +++++++++++++++++----- lib/user.rb | 18 ------------------ specs/channel_spec.rb | 11 +++++++++++ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 1a249e95..983e8cac 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -6,10 +6,6 @@ module SlackAPI class SlackError < StandardError; end class Channel < Recipient - # def initialize() - # super("channels.list") - # end - BASE_URL = "https://slack.com/api/" def self.list_channels @@ -22,7 +18,6 @@ def self.list_channels else response["channels"].each do |channel| channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} - # binding.pry end end return channel_list @@ -61,5 +56,22 @@ def see_details(identifier) end end end + + def send_msg(recipient, text) + response = HTTParty.post( + "#{BASE_URL}chat.postMessage", + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + body: { + token: ENV["SLACK_API_TOKEN"], + channel: recipient, + text: text, + }, + ) + if response["ok"] + return true + else + raise SlackApi::SlackError, "Error when posting #{text} to #{channel}, error: #{response["error"]}" + end + end end #end of class end #end of module diff --git a/lib/user.rb b/lib/user.rb index 9280a082..31fe1d91 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -6,10 +6,6 @@ module SlackAPI class SlackError < StandardError; end class User < Recipient - # def initialize() - # super("users.list") - # end - BASE_URL = "https://slack.com/api/" def self.list_users @@ -20,7 +16,6 @@ def self.list_users if response["ok"] response["members"].each do |member| user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} - # binding.pry end else raise SlackAPI::SlackError, "There was an error. The code is #{response.error}." @@ -29,11 +24,6 @@ def self.list_users end def select_user(identifier) - # query = { - # token: ENV["SLACK_API_TOKEN"], - # } - - # response = HTTParty.get(BASE_URL, query: query) response = Recipient.get("users.list") selected_user = "" @@ -52,18 +42,10 @@ def select_user(identifier) end def see_details(identifier) - # query = { - # token: ENV["SLACK_API_TOKEN"], - # } - - # response = HTTParty.get(BASE_URL, query: query) response = Recipient.get("users.list") user_details = {} - # if response["ok"] != true - # raise SlackAPI::SlackError, "There was an error." # add error message later - # else response["members"].each do |member| if member["id"] == identifier || member["name"] == identifier user_details[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 5fc91797..fbe7c3c5 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -58,3 +58,14 @@ end end end + +describe "send_msg" do + it "sends a message to selected channel" do + channel = SlackAPI::Channel.new() + VCR.use_cassette("see_details") do + selected_channel = channel.select_channel("CH2SKTKPC") + msg = channel.send_msg("CH2SKTKPC", "This is a test message!") + expect(msg).must_equal true + end + end +end From 6c9e142cd67a9b33c978adab6f44f2158e441fd3 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Thu, 21 Mar 2019 16:17:10 -0700 Subject: [PATCH 16/24] Wrote send_msg tests and code for user and channel --- lib/channel.rb | 8 ++++---- lib/user.rb | 20 ++++++++++++++++++-- specs/channel_spec.rb | 38 +++++++++++++++++++++++++++++++++++--- specs/user_spec.rb | 29 ++++++++++++++++++++++++++++- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 983e8cac..8f17571b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,7 +17,7 @@ def self.list_channels raise SlackAPI::SlackError, "There was an error. The code is #{response["error"]}." else response["channels"].each do |channel| - channel_list[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + channel_list[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } end end return channel_list @@ -50,7 +50,7 @@ def see_details(identifier) else response["channels"].each do |channel| if channel["id"] == identifier || channel["name"] == identifier - channel_details[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + channel_details[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } return channel_details end end @@ -60,7 +60,7 @@ def see_details(identifier) def send_msg(recipient, text) response = HTTParty.post( "#{BASE_URL}chat.postMessage", - headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + headers: { "Content-Type" => "application/x-www-form-urlencoded" }, body: { token: ENV["SLACK_API_TOKEN"], channel: recipient, @@ -70,7 +70,7 @@ def send_msg(recipient, text) if response["ok"] return true else - raise SlackApi::SlackError, "Error when posting #{text} to #{channel}, error: #{response["error"]}" + raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error: #{response["error"]}" end end end #end of class diff --git a/lib/user.rb b/lib/user.rb index 31fe1d91..0f1e7399 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -15,7 +15,7 @@ def self.list_users if response["ok"] response["members"].each do |member| - user_list[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + user_list[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } end else raise SlackAPI::SlackError, "There was an error. The code is #{response.error}." @@ -48,10 +48,26 @@ def see_details(identifier) response["members"].each do |member| if member["id"] == identifier || member["name"] == identifier - user_details[member["name"]] = {"real name" => member["real_name"], "slack id" => member["id"]} + user_details[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } return user_details end end end + + def send_msg(recipient, text) + response = HTTParty.post("#{BASE_URL}chat.postMessage", + headers: { "Content-Type" => "application/x-www-form-urlencoded" }, + body: { + token: ENV["SLACK_API_TOKEN"], + channel: recipient, + text: text, + }) + + if response["ok"] == false + raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error #{response["error"]}" + else + return true + end + end end # end of class end # end of module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index fbe7c3c5..35147913 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -4,7 +4,6 @@ it "list all channels" do VCR.use_cassette("list_channels") do response = SlackAPI::Channel.list_channels - puts response expect(response).wont_be_nil expect(response.keys.include?("general")).must_equal true @@ -62,10 +61,43 @@ describe "send_msg" do it "sends a message to selected channel" do channel = SlackAPI::Channel.new() - VCR.use_cassette("see_details") do - selected_channel = channel.select_channel("CH2SKTKPC") + VCR.use_cassette("send_msg") do + channel.select_channel("CH2SKTKPC") msg = channel.send_msg("CH2SKTKPC", "This is a test message!") expect(msg).must_equal true end end + + it "generates an error if given an invalid channel" do + VCR.use_cassette("send_msg") do + expect { + SlackAPI::Channel.new.send_msg("bogus", "Test message") + }.must_raise SlackAPI::SlackError + end + end + + # it "will generate an error if given an invalid key" do + # real_token = ENV["SLACK_API_TOKEN"] + # ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN" + + # VCR.use_cassette("send_msg") do + # error = expect { + # SlackAPI::Channel.new.send_msg("general", "Test message with invalid key") + # }.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_API_TOKEN"] = real_token + # end + + it "will raise an error if given an empty message" do + VCR.use_cassette("send_msg") do + error = expect { + SlackAPI::Channel.new.send_msg("general", + "") + }.must_raise SlackAPI::SlackError + + # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 238c3f40..0458b91d 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,7 +4,6 @@ it "list all users" do VCR.use_cassette("list_users") do response = SlackAPI::User.list_users - puts response expect(response).wont_be_nil expect(response.keys.include?("aribray")).must_equal true expect(response["aribray"]).must_equal "real name" => "Ariana Bray", @@ -61,4 +60,32 @@ end end end + + describe "send_usr_msg" do + it "sends a message to selected user" do + VCR.use_cassette("send_usr_msg") do + msg = SlackAPI::User.new.send_msg("UH3UT3SJK", "This is a test message!") + expect(msg).must_equal true + end + end + + it "generates an error if given an invalid user" do + VCR.use_cassette("send_usr_msg") do + expect { + SlackAPI::User.new.send_msg("bob's your uncle", "Test message") + }.must_raise SlackAPI::SlackError + end + end + + it "will raise an error if given an empty message" do + VCR.use_cassette("send_usr_msg") do + error = expect { + SlackAPI::User.new.send_msg("UH3UT3SJK", + "") + }.must_raise SlackAPI::SlackError + + # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" + end + end + end end From e5099a64219fa6dce0669081fd1301aeaab0f333 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Thu, 21 Mar 2019 16:32:04 -0700 Subject: [PATCH 17/24] Added CLI code for send_msg method --- lib/slack.rb | 20 +++++++++++++------- lib/user.rb | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index fd33bd8e..81dfdfa2 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -8,7 +8,7 @@ def main puts "Welcome to the Ada Slack CLI!" puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users" - ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details', or 'quit'?" + ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details', 'send message', or 'quit'?" puts ask_again response = gets.chomp @@ -27,8 +27,8 @@ def main puts "What is the user id or Slack name?" identifier = gets.chomp user = SlackAPI::User.new - selected_user = user.select_user(identifier) - if selected_user == "" + selected_recipient = user.select_user(identifier) + if selected_recipient == "" puts "There is no user with that identifier" end puts ask_again @@ -38,24 +38,30 @@ def main puts "What is the user id or Slack name?" identifier = gets.chomp channel = SlackAPI::Channel.new - selected_channel = channel.select_channel(identifier) - if selected_channel == "" + selected_recipient = channel.select_channel(identifier) + if selected_recipient == "" puts "There is no channel with that identifier" end puts ask_again response = gets.chomp elsif response == "details" if select_response == "select user" - details = user.see_details(selected_user) + details = user.see_details(selected_recipient) puts details puts ask_again response = gets.chomp elsif select_response == "select channel" - details = channel.see_details(selected_channel) + details = channel.see_details(selected_recipient) puts details puts ask_again response = gets.chomp end + elsif response == "send message" + puts "What would you like to say?" + text = gets.chomp + msg = SlackAPI::User.new.send_msg(selected_recipient, text) + puts ask_again + response = gets.chomp end end end diff --git a/lib/user.rb b/lib/user.rb index 0f1e7399..acb7a5ce 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -30,9 +30,9 @@ def select_user(identifier) response["members"].each do |member| if member["id"] == identifier - selected_user = identifier + selected_user = member["id"] elsif member["name"] == identifier - selected_user = identifier + selected_user = member["id"] end end if selected_user == "" From 8821a24ac03e6577e9972f028077a4f03d452b65 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Thu, 21 Mar 2019 16:33:11 -0700 Subject: [PATCH 18/24] Fixed test error for selected user response --- specs/user_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 0458b91d..e0f55841 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -35,7 +35,7 @@ user = SlackAPI::User.new VCR.use_cassette("select_user") do response = user.select_user("aribray") - expect(response).must_equal "aribray" + expect(response).must_equal "UH3UT3SJK" end end it "raises a SlackError if the user's name or id is invalid" do From 33211e7eb11fc484bc6a947ce2993c25fb0bc241 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Thu, 21 Mar 2019 19:11:53 -0700 Subject: [PATCH 19/24] Changed data structure for list user, list channel, user details, and channel details to accomodate table print. Updated slack.rb to use table print --- lib/channel.rb | 8 ++++---- lib/slack.rb | 8 ++++---- lib/user.rb | 15 ++++++++++----- specs/channel_spec.rb | 5 +---- specs/user_spec.rb | 8 +++----- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 8f17571b..1dc5d79b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -11,13 +11,13 @@ class Channel < Recipient def self.list_channels response = Recipient.get("channels.list") - channel_list = {} + channel_list = [] if response["ok"] != true # write a test for this raise SlackAPI::SlackError, "There was an error. The code is #{response["error"]}." else response["channels"].each do |channel| - channel_list[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + channel_list << {"name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} end end return channel_list @@ -50,7 +50,7 @@ def see_details(identifier) else response["channels"].each do |channel| if channel["id"] == identifier || channel["name"] == identifier - channel_details[channel["name"]] = { "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + channel_details[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} return channel_details end end @@ -60,7 +60,7 @@ def see_details(identifier) def send_msg(recipient, text) response = HTTParty.post( "#{BASE_URL}chat.postMessage", - headers: { "Content-Type" => "application/x-www-form-urlencoded" }, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, body: { token: ENV["SLACK_API_TOKEN"], channel: recipient, diff --git a/lib/slack.rb b/lib/slack.rb index 81dfdfa2..0bf01117 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,6 +2,7 @@ require_relative "user" require_relative "channel" require "httparty" +require "table_print" require "dotenv" Dotenv.load @@ -14,12 +15,11 @@ def main until response == "quit" if response == "list users" - # user = User.new - puts SlackAPI::User.list_users + tp SlackAPI::User.list_users, "name", "real name", "slack id" puts ask_again response = gets.chomp elsif response == "list channels" - puts SlackAPI::Channel.list_channels + tp SlackAPI::Channel.list_channels, "name", "topic", "member count", "id" puts ask_again response = gets.chomp elsif response == "select user" @@ -47,7 +47,7 @@ def main elsif response == "details" if select_response == "select user" details = user.see_details(selected_recipient) - puts details + tp details, "name", "real name", "slack id" puts ask_again response = gets.chomp elsif select_response == "select channel" diff --git a/lib/user.rb b/lib/user.rb index acb7a5ce..b87e61bf 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,12 +11,13 @@ class User < Recipient def self.list_users response = Recipient.get("users.list") - user_list = {} + user_list = [] #changed this to an array to use table print if response["ok"] response["members"].each do |member| - user_list[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } + user_list << {"name" => member["name"], "real name" => member["real_name"], "slack id" => member["id"]} end + # user_list is now an array of hashes else raise SlackAPI::SlackError, "There was an error. The code is #{response.error}." end @@ -44,11 +45,15 @@ def select_user(identifier) def see_details(identifier) response = Recipient.get("users.list") - user_details = {} + user_details = [] response["members"].each do |member| if member["id"] == identifier || member["name"] == identifier - user_details[member["name"]] = { "real name" => member["real_name"], "slack id" => member["id"] } + user_details << + {"name" => member["name"], + "real name" => member["real_name"], + "slack id" => member["id"]} + return user_details end end @@ -56,7 +61,7 @@ def see_details(identifier) def send_msg(recipient, text) response = HTTParty.post("#{BASE_URL}chat.postMessage", - headers: { "Content-Type" => "application/x-www-form-urlencoded" }, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, body: { token: ENV["SLACK_API_TOKEN"], channel: recipient, diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 35147913..1036e062 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -6,10 +6,7 @@ response = SlackAPI::Channel.list_channels expect(response).wont_be_nil - expect(response.keys.include?("general")).must_equal true - expect(response["general"]).must_equal "topic" => "Company-wide announcements and work-based matters", - "member count" => 2, - "id" => "CH2SKTKPC" + expect(response[0]).must_equal "name" => "general", "topic" => "Company-wide announcements and work-based matters", "member count" => 2, "id" => "CH2SKTKPC" expect(response.length).must_equal 3 end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e0f55841..4cdea8f6 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -5,8 +5,7 @@ VCR.use_cassette("list_users") do response = SlackAPI::User.list_users expect(response).wont_be_nil - expect(response.keys.include?("aribray")).must_equal true - expect(response["aribray"]).must_equal "real name" => "Ariana Bray", + expect(response[2]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" expect(response.length).must_equal 3 end @@ -53,10 +52,9 @@ selected_user = user.select_user("UH3UT3SJK") user_details = user.see_details(selected_user) - expect(user_details).must_be_kind_of Hash + expect(user_details).must_be_kind_of Array expect(user_details.length).must_equal 1 - expect(user_details["aribray"]).must_equal "real name" => "Ariana Bray", - "slack id" => "UH3UT3SJK" + expect(user_details[0]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" end end end From ec223d61535d6b4b57e28d412cea50f372a8e97f Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Thu, 21 Mar 2019 19:30:57 -0700 Subject: [PATCH 20/24] Added send message method to Recipient class and updated CLI --- lib/channel.rb | 17 ----------------- lib/recipient.rb | 16 ++++++++++++++++ lib/slack.rb | 2 +- lib/user.rb | 16 ---------------- specs/channel_spec.rb | 2 +- 5 files changed, 18 insertions(+), 35 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 1dc5d79b..d0fe3e79 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -56,22 +56,5 @@ def see_details(identifier) end end end - - def send_msg(recipient, text) - response = HTTParty.post( - "#{BASE_URL}chat.postMessage", - headers: {"Content-Type" => "application/x-www-form-urlencoded"}, - body: { - token: ENV["SLACK_API_TOKEN"], - channel: recipient, - text: text, - }, - ) - if response["ok"] - return true - else - raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error: #{response["error"]}" - end - end end #end of class end #end of module diff --git a/lib/recipient.rb b/lib/recipient.rb index 9a6e2f3f..3dedd03d 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -20,5 +20,21 @@ def self.get(sub_url) return response end + + def send_msg(recipient, text) + response = HTTParty.post("#{BASE_URL}chat.postMessage", + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + body: { + token: ENV["SLACK_API_TOKEN"], + channel: recipient, + text: text, + }) + + if response["ok"] == false + raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error #{response["error"]}" + else + return true + end + end end end diff --git a/lib/slack.rb b/lib/slack.rb index 0bf01117..ff58e8eb 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -59,7 +59,7 @@ def main elsif response == "send message" puts "What would you like to say?" text = gets.chomp - msg = SlackAPI::User.new.send_msg(selected_recipient, text) + msg = SlackAPI::Recipient.new.send_msg(selected_recipient, text) puts ask_again response = gets.chomp end diff --git a/lib/user.rb b/lib/user.rb index b87e61bf..53763ec3 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -58,21 +58,5 @@ def see_details(identifier) end end end - - def send_msg(recipient, text) - response = HTTParty.post("#{BASE_URL}chat.postMessage", - headers: {"Content-Type" => "application/x-www-form-urlencoded"}, - body: { - token: ENV["SLACK_API_TOKEN"], - channel: recipient, - text: text, - }) - - if response["ok"] == false - raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error #{response["error"]}" - else - return true - end - end end # end of class end # end of module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 1036e062..e20eade8 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -59,7 +59,7 @@ it "sends a message to selected channel" do channel = SlackAPI::Channel.new() VCR.use_cassette("send_msg") do - channel.select_channel("CH2SKTKPC") + # channel.select_channel("CH2SKTKPC") msg = channel.send_msg("CH2SKTKPC", "This is a test message!") expect(msg).must_equal true end From a7cec70877b2eb8eacabb58ece05cad28efdfd6a Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Fri, 22 Mar 2019 11:17:14 -0700 Subject: [PATCH 21/24] added tests for error message --- lib/channel.rb | 20 ++++++++------------ lib/recipient.rb | 9 --------- lib/slack.rb | 8 ++++++-- lib/user.rb | 5 ++--- specs/channel_spec.rb | 38 ++++++++++++++++---------------------- specs/user_spec.rb | 23 ++++++++++++----------- 6 files changed, 44 insertions(+), 59 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index d0fe3e79..ce7488c8 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -13,8 +13,8 @@ def self.list_channels channel_list = [] - if response["ok"] != true # write a test for this - raise SlackAPI::SlackError, "There was an error. The code is #{response["error"]}." + if response["ok"] != true + raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}" else response["channels"].each do |channel| channel_list << {"name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} @@ -23,7 +23,7 @@ def self.list_channels return channel_list end - def select_channel(identifier) # maybe in parent class? + def select_channel(identifier) response = Recipient.get("channels.list") selected_channel = "" @@ -43,16 +43,12 @@ def select_channel(identifier) # maybe in parent class? def see_details(identifier) response = Recipient.get("channels.list") - channel_details = {} + channel_details = [] - if response["ok"] != true - raise SlackAPI::SlackError, "This channel is not valid." - else - response["channels"].each do |channel| - if channel["id"] == identifier || channel["name"] == identifier - channel_details[channel["name"]] = {"topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} - return channel_details - end + response["channels"].each do |channel| + if channel["id"] == identifier || channel["name"] == identifier + channel_details << {"name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + return channel_details end end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 3dedd03d..45321edc 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,20 +1,11 @@ -# require "httparty" -# require "dotenv" -# Dotenv.load - module SlackAPI class Recipient BASE_URL = "https://slack.com/api/" - # def initialize(sub_url) - # @sub_url = sub_url - # end - def self.get(sub_url) query = { token: ENV["SLACK_API_TOKEN"], } - # puts sub_url response = HTTParty.get("#{BASE_URL}#{sub_url}", query: query) diff --git a/lib/slack.rb b/lib/slack.rb index ff58e8eb..92378a09 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -9,7 +9,7 @@ def main puts "Welcome to the Ada Slack CLI!" puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users" - ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details', 'send message', or 'quit'?" + ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details' (select user or channel first), 'send message' (select user or channel first), or 'quit'?" puts ask_again response = gets.chomp @@ -52,7 +52,11 @@ def main response = gets.chomp elsif select_response == "select channel" details = channel.see_details(selected_recipient) - puts details + tp details, "name", "topic", "member count", "id" + puts ask_again + response = gets.chomp + else + puts "Select a user or channel first" puts ask_again response = gets.chomp end diff --git a/lib/user.rb b/lib/user.rb index 53763ec3..244d8ba8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,15 +11,14 @@ class User < Recipient def self.list_users response = Recipient.get("users.list") - user_list = [] #changed this to an array to use table print + user_list = [] if response["ok"] response["members"].each do |member| user_list << {"name" => member["name"], "real name" => member["real_name"], "slack id" => member["id"]} end - # user_list is now an array of hashes else - raise SlackAPI::SlackError, "There was an error. The code is #{response.error}." + raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}" end return user_list end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e20eade8..43de7f88 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -6,12 +6,22 @@ response = SlackAPI::Channel.list_channels expect(response).wont_be_nil - expect(response[0]).must_equal "name" => "general", "topic" => "Company-wide announcements and work-based matters", "member count" => 2, "id" => "CH2SKTKPC" + expect(response[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" expect(response.length).must_equal 3 end end - it "will raise an error if the response code is not 200" do - #fill this in later + it "will raise an error if given an invalid key" do + real_token = ENV["SLACK_API_TOKEN"] + ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN" + + VCR.use_cassette("list_channels_2") do + error = expect { + SlackAPI::Channel.list_channels + }.must_raise SlackAPI::SlackError + expect(error.message).must_equal "There was an error. The error message is invalid_auth" + end + + ENV["SLACK_API_TOKEN"] = real_token end end @@ -44,13 +54,12 @@ it "shows details for a selected channel" do channel = SlackAPI::Channel.new() VCR.use_cassette("see_details") do - selected_channel = channel.select_channel("CH2SKTKPC") + # selected_channel = channel.select_channel("CH2SKTKPC") channel_details = channel.see_details("CH2SKTKPC") - expect(channel_details).must_be_kind_of Hash + expect(channel_details).must_be_kind_of Array expect(channel_details.length).must_equal 1 - expect(channel_details["general"]).must_equal "topic" => "Company-wide announcements and work-based matters", - "member count" => 2, "id" => "CH2SKTKPC" + expect(channel_details[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" end end end @@ -59,7 +68,6 @@ it "sends a message to selected channel" do channel = SlackAPI::Channel.new() VCR.use_cassette("send_msg") do - # channel.select_channel("CH2SKTKPC") msg = channel.send_msg("CH2SKTKPC", "This is a test message!") expect(msg).must_equal true end @@ -73,20 +81,6 @@ end end - # it "will generate an error if given an invalid key" do - # real_token = ENV["SLACK_API_TOKEN"] - # ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN" - - # VCR.use_cassette("send_msg") do - # error = expect { - # SlackAPI::Channel.new.send_msg("general", "Test message with invalid key") - # }.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_API_TOKEN"] = real_token - # end - it "will raise an error if given an empty message" do VCR.use_cassette("send_msg") do error = expect { diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 4cdea8f6..b7ab644f 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -10,16 +10,19 @@ expect(response.length).must_equal 3 end end - # it "raises error if response 'ok' is false" do - # real_token = ENV["SLACK_API_TOKEN"] - # ENV["SLACK_API_TOKEN"] = "badtoken" + it "will raise an error if given an invalid key" do + real_token = ENV["SLACK_API_TOKEN"] + ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN" - # VCR.use_cassette("list_users") do - # return_value = SlackAPI::User.list_users - # expect (return_value).must_equal true - # end - # ENV["SLACK_API_TOKEN"] = real_token - # end + VCR.use_cassette("list_users_2") do + error = expect { + SlackAPI::User.list_users + }.must_raise SlackAPI::SlackError + expect(error.message).must_equal "There was an error. The error message is invalid_auth" + end + + ENV["SLACK_API_TOKEN"] = real_token + end end describe "select_user" do @@ -81,8 +84,6 @@ SlackAPI::User.new.send_msg("UH3UT3SJK", "") }.must_raise SlackAPI::SlackError - - # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" end end end From 6470849dfc89871c906fb0a1deeda923747d2bd8 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Fri, 22 Mar 2019 11:41:59 -0700 Subject: [PATCH 22/24] Added workspace and moved see_details and select_recipient methods to workspace. Moved associated tests to workspace --- lib/channel.rb | 32 +---------------- lib/slack.rb | 9 ++--- lib/user.rb | 35 ------------------ lib/workspace.rb | 71 ++++++++++++++++++++++++++++++++++++ specs/channel_spec.rb | 70 ++++++++++++++++++------------------ specs/test_helper.rb | 1 + specs/user_spec.rb | 68 +++++++++++++++++------------------ specs/workspace_spec.rb | 79 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 226 insertions(+), 139 deletions(-) create mode 100644 lib/workspace.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb index ce7488c8..278999ec 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,40 +17,10 @@ def self.list_channels raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}" else response["channels"].each do |channel| - channel_list << {"name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} + channel_list << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } end end return channel_list end - - def select_channel(identifier) - response = Recipient.get("channels.list") - - selected_channel = "" - response["channels"].each do |channel| - if channel["id"] == identifier - selected_channel = identifier - elsif channel["name"] == identifier - selected_channel = identifier - end - end - if selected_channel == "" - raise SlackAPI::SlackError, "That is not a vaild channel" - end - return selected_channel - end - - def see_details(identifier) - response = Recipient.get("channels.list") - - channel_details = [] - - response["channels"].each do |channel| - if channel["id"] == identifier || channel["name"] == identifier - channel_details << {"name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"]} - return channel_details - end - end - end end #end of class end #end of module diff --git a/lib/slack.rb b/lib/slack.rb index 92378a09..2f66e672 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require_relative "user" require_relative "channel" +require_relative "workspace" require "httparty" require "table_print" require "dotenv" @@ -26,7 +27,7 @@ def main select_response = "select user" puts "What is the user id or Slack name?" identifier = gets.chomp - user = SlackAPI::User.new + user = SlackAPI::Workspace.new selected_recipient = user.select_user(identifier) if selected_recipient == "" puts "There is no user with that identifier" @@ -37,7 +38,7 @@ def main select_response = "select channel" puts "What is the user id or Slack name?" identifier = gets.chomp - channel = SlackAPI::Channel.new + channel = SlackAPI::Workspace.new selected_recipient = channel.select_channel(identifier) if selected_recipient == "" puts "There is no channel with that identifier" @@ -46,12 +47,12 @@ def main response = gets.chomp elsif response == "details" if select_response == "select user" - details = user.see_details(selected_recipient) + details = user.see_user_details(selected_recipient) tp details, "name", "real name", "slack id" puts ask_again response = gets.chomp elsif select_response == "select channel" - details = channel.see_details(selected_recipient) + details = channel.see_channel_details(selected_recipient) tp details, "name", "topic", "member count", "id" puts ask_again response = gets.chomp diff --git a/lib/user.rb b/lib/user.rb index 244d8ba8..528b69a2 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -22,40 +22,5 @@ def self.list_users end return user_list end - - def select_user(identifier) - response = Recipient.get("users.list") - - selected_user = "" - - response["members"].each do |member| - if member["id"] == identifier - selected_user = member["id"] - elsif member["name"] == identifier - selected_user = member["id"] - end - end - if selected_user == "" - raise SlackAPI::SlackError, "That is not a valid user" - end - return selected_user - end - - def see_details(identifier) - response = Recipient.get("users.list") - - user_details = [] - - response["members"].each do |member| - if member["id"] == identifier || member["name"] == identifier - user_details << - {"name" => member["name"], - "real name" => member["real_name"], - "slack id" => member["id"]} - - return user_details - end - end - end end # end of class end # end of module diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..a33ac701 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,71 @@ +require_relative "recipient" +require "httparty" + +module SlackAPI + class Workspace + def select_channel(identifier) + response = Recipient.get("channels.list") + + selected_channel = "" + response["channels"].each do |channel| + if channel["id"] == identifier + selected_channel = identifier + elsif channel["name"] == identifier + selected_channel = identifier + end + end + if selected_channel == "" + raise SlackAPI::SlackError, "That is not a vaild channel" + end + return selected_channel + end + + def see_channel_details(identifier) + response = Recipient.get("channels.list") + + channel_details = [] + + response["channels"].each do |channel| + if channel["id"] == identifier || channel["name"] == identifier + channel_details << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } + return channel_details + end + end + end + + def select_user(identifier) + response = Recipient.get("users.list") + + selected_user = "" + + response["members"].each do |member| + if member["id"] == identifier + selected_user = member["id"] + elsif member["name"] == identifier + selected_user = member["id"] + end + end + if selected_user == "" + raise SlackAPI::SlackError, "That is not a valid user" + end + return selected_user + end + + def see_user_details(identifier) + response = Recipient.get("users.list") + + user_details = [] + + response["members"].each do |member| + if member["id"] == identifier || member["name"] == identifier + user_details << + { "name" => member["name"], + "real name" => member["real_name"], + "slack id" => member["id"] } + + return user_details + end + end + end + end # end of class +end # end of module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 43de7f88..39a752b0 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -25,44 +25,44 @@ end end -describe "select_channel" do - it "selects a channel as the current recipient when user inputs channel ID" do - channel_url = "channels.list" - channel = SlackAPI::Channel.new() +# describe "select_channel" do +# it "selects a channel as the current recipient when user inputs channel ID" do +# channel_url = "channels.list" +# channel = SlackAPI::Channel.new() - VCR.use_cassette("select_channel") do - response = channel.select_channel("CH2SKTKPC") - expect(response).must_equal "CH2SKTKPC" - end - end - it "selects a channel as the current recipient when user inputs channel name" do - channel = SlackAPI::Channel.new() - VCR.use_cassette("select_channel") do - response = channel.select_channel("general") - expect(response).must_equal "general" - end - end - it "raises an error if the channel is not valid" do - channel = SlackAPI::Channel.new() - VCR.use_cassette("select_channel") do - expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError - end - end -end +# VCR.use_cassette("select_channel") do +# response = channel.select_channel("CH2SKTKPC") +# expect(response).must_equal "CH2SKTKPC" +# end +# end +# it "selects a channel as the current recipient when user inputs channel name" do +# channel = SlackAPI::Channel.new() +# VCR.use_cassette("select_channel") do +# response = channel.select_channel("general") +# expect(response).must_equal "general" +# end +# end +# it "raises an error if the channel is not valid" do +# channel = SlackAPI::Channel.new() +# VCR.use_cassette("select_channel") do +# expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError +# end +# end +# end -describe "see_details" do - it "shows details for a selected channel" do - channel = SlackAPI::Channel.new() - VCR.use_cassette("see_details") do - # selected_channel = channel.select_channel("CH2SKTKPC") - channel_details = channel.see_details("CH2SKTKPC") +# describe "see_details" do +# it "shows details for a selected channel" do +# channel = SlackAPI::Channel.new() +# VCR.use_cassette("see_details") do +# # selected_channel = channel.select_channel("CH2SKTKPC") +# channel_details = channel.see_details("CH2SKTKPC") - expect(channel_details).must_be_kind_of Array - expect(channel_details.length).must_equal 1 - expect(channel_details[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" - end - end -end +# expect(channel_details).must_be_kind_of Array +# expect(channel_details.length).must_equal 1 +# expect(channel_details[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" +# end +# end +# end describe "send_msg" do it "sends a message to selected channel" do diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 760e9d4f..dc312462 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -15,6 +15,7 @@ require_relative "../lib/slack.rb" require_relative "../lib/user.rb" require_relative "../lib/recipient.rb" +require_relative "../lib/workspace.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b7ab644f..271594b4 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -25,42 +25,42 @@ end end - describe "select_user" do - it "selects a user as the current recipient when user inputs ID" do - user = SlackAPI::User.new - VCR.use_cassette("select_user") do - response = user.select_user("UH3UT3SJK") - expect(response).must_equal "UH3UT3SJK" - end - end - it "selects a user as the current recipient when user inputs name" do - user = SlackAPI::User.new - VCR.use_cassette("select_user") do - response = user.select_user("aribray") - expect(response).must_equal "UH3UT3SJK" - end - end - it "raises a SlackError if the user's name or id is invalid" do - user = SlackAPI::User.new - VCR.use_cassette("select_user") do - expect { user.select_user("fakeuser") }.must_raise SlackAPI::SlackError - end - end - end + # describe "select_user" do + # it "selects a user as the current recipient when user inputs ID" do + # user = SlackAPI::User.new + # VCR.use_cassette("select_user") do + # response = user.select_user("UH3UT3SJK") + # expect(response).must_equal "UH3UT3SJK" + # end + # end + # it "selects a user as the current recipient when user inputs name" do + # user = SlackAPI::User.new + # VCR.use_cassette("select_user") do + # response = user.select_user("aribray") + # expect(response).must_equal "UH3UT3SJK" + # end + # end + # it "raises a SlackError if the user's name or id is invalid" do + # user = SlackAPI::User.new + # VCR.use_cassette("select_user") do + # expect { user.select_user("fakeuser") }.must_raise SlackAPI::SlackError + # end + # end + # end - describe "see_details" do - it "shows details for selected user" do - user = SlackAPI::User.new - VCR.use_cassette("select_user") do - selected_user = user.select_user("UH3UT3SJK") - user_details = user.see_details(selected_user) + # describe "see_details" do + # it "shows details for selected user" do + # user = SlackAPI::User.new + # VCR.use_cassette("select_user") do + # selected_user = user.select_user("UH3UT3SJK") + # user_details = user.see_details(selected_user) - expect(user_details).must_be_kind_of Array - expect(user_details.length).must_equal 1 - expect(user_details[0]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" - end - end - end + # expect(user_details).must_be_kind_of Array + # expect(user_details.length).must_equal 1 + # expect(user_details[0]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" + # end + # end + # end describe "send_usr_msg" do it "sends a message to selected user" do diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..32ef1abd --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1,79 @@ +require_relative "test_helper" + +describe "select_channel" do + it "selects a channel as the current recipient when user inputs channel ID" do + channel_url = "channels.list" + channel = SlackAPI::Workspace.new() + + VCR.use_cassette("select_channel") do + response = channel.select_channel("CH2SKTKPC") + expect(response).must_equal "CH2SKTKPC" + end + end + it "selects a channel as the current recipient when user inputs channel name" do + channel = SlackAPI::Workspace.new() + VCR.use_cassette("select_channel") do + response = channel.select_channel("general") + expect(response).must_equal "general" + end + end + it "raises an error if the channel is not valid" do + channel = SlackAPI::Workspace.new() + VCR.use_cassette("select_channel") do + expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError + end + end +end + +describe "see_details" do + it "shows details for a selected channel" do + channel = SlackAPI::Workspace.new() + VCR.use_cassette("see_details_2") do + selected_channel = channel.select_channel("CH2SKTKPC") + channel_details = channel.see_channel_details("CH2SKTKPC") + + expect(channel_details).must_be_kind_of Array + expect(channel_details.length).must_equal 1 + expect(channel_details[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" + end + end +end + +# TESTS FOR USERS + +describe "select_user" do + it "selects a user as the current recipient when user inputs ID" do + user = SlackAPI::Workspace.new + VCR.use_cassette("select_user") do + response = user.select_user("UH3UT3SJK") + expect(response).must_equal "UH3UT3SJK" + end + end + it "selects a user as the current recipient when user inputs name" do + user = SlackAPI::Workspace.new + VCR.use_cassette("select_user") do + response = user.select_user("aribray") + expect(response).must_equal "UH3UT3SJK" + end + end + it "raises a SlackError if the user's name or id is invalid" do + user = SlackAPI::Workspace.new + VCR.use_cassette("select_user") do + expect { user.select_user("fakeuser") }.must_raise SlackAPI::SlackError + end + end +end + +describe "see_details" do + it "shows details for selected user" do + user = SlackAPI::Workspace.new + VCR.use_cassette("see_details_1") do + selected_user = user.select_user("UH3UT3SJK") + user_details = user.see_user_details(selected_user) + + expect(user_details).must_be_kind_of Array + expect(user_details.length).must_equal 1 + expect(user_details[0]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" + end + end +end From 6934c0c240ca4f9a38740bacf10e86604731864e Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Fri, 22 Mar 2019 13:17:05 -0700 Subject: [PATCH 23/24] Updated formatting and functionality on slack.rb --- lib/slack.rb | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 2f66e672..2a245b2f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -10,63 +10,63 @@ def main puts "Welcome to the Ada Slack CLI!" puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users" - ask_again = "Would you like to 'list users', 'list channels', 'select user', 'select channel', 'details' (select user or channel first), 'send message' (select user or channel first), or 'quit'?" + ask_again = "Select an option: \n1. list users \n2. list channels \n3. select user \n4. select channel \n5. details (select user or channel first)\n6. send message (select user or channel first) \n7. quit" puts ask_again - response = gets.chomp + response = gets.chomp.downcase - until response == "quit" - if response == "list users" + until response == "7" || response == "quit" + if response == "list users" || response == "1" tp SlackAPI::User.list_users, "name", "real name", "slack id" puts ask_again - response = gets.chomp - elsif response == "list channels" + response = gets.chomp.downcase + elsif response == "list channels" || response == "2" tp SlackAPI::Channel.list_channels, "name", "topic", "member count", "id" puts ask_again - response = gets.chomp - elsif response == "select user" + response = gets.chomp.downcase + elsif response == "select user" || response == "3" select_response = "select user" puts "What is the user id or Slack name?" - identifier = gets.chomp + identifier = gets.chomp.downcase user = SlackAPI::Workspace.new selected_recipient = user.select_user(identifier) if selected_recipient == "" puts "There is no user with that identifier" end puts ask_again - response = gets.chomp - elsif response == "select channel" + response = gets.chomp.downcase + elsif response == "select channel" || response == "4" select_response = "select channel" puts "What is the user id or Slack name?" - identifier = gets.chomp + identifier = gets.chomp.downcase channel = SlackAPI::Workspace.new selected_recipient = channel.select_channel(identifier) if selected_recipient == "" puts "There is no channel with that identifier" end puts ask_again - response = gets.chomp - elsif response == "details" + response = gets.chomp.downcase + elsif response == "details" || response == "5" if select_response == "select user" details = user.see_user_details(selected_recipient) tp details, "name", "real name", "slack id" puts ask_again - response = gets.chomp + response = gets.chomp.downcase elsif select_response == "select channel" details = channel.see_channel_details(selected_recipient) tp details, "name", "topic", "member count", "id" puts ask_again - response = gets.chomp + response = gets.chomp.downcase else puts "Select a user or channel first" puts ask_again - response = gets.chomp + response = gets.chomp.downcase end - elsif response == "send message" + elsif response == "send message" || response == "6" puts "What would you like to say?" - text = gets.chomp + text = gets.chomp.downcase msg = SlackAPI::Recipient.new.send_msg(selected_recipient, text) puts ask_again - response = gets.chomp + response = gets.chomp.downcase end end end From 63a8f966755b7f54ddda5c57e9ddc7569d8c11ac Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Fri, 22 Mar 2019 13:21:18 -0700 Subject: [PATCH 24/24] Removed commented out code --- specs/channel_spec.rb | 39 --------------------------------------- specs/user_spec.rb | 37 ------------------------------------- 2 files changed, 76 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 39a752b0..d74d3907 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -25,45 +25,6 @@ end end -# describe "select_channel" do -# it "selects a channel as the current recipient when user inputs channel ID" do -# channel_url = "channels.list" -# channel = SlackAPI::Channel.new() - -# VCR.use_cassette("select_channel") do -# response = channel.select_channel("CH2SKTKPC") -# expect(response).must_equal "CH2SKTKPC" -# end -# end -# it "selects a channel as the current recipient when user inputs channel name" do -# channel = SlackAPI::Channel.new() -# VCR.use_cassette("select_channel") do -# response = channel.select_channel("general") -# expect(response).must_equal "general" -# end -# end -# it "raises an error if the channel is not valid" do -# channel = SlackAPI::Channel.new() -# VCR.use_cassette("select_channel") do -# expect { channel.select_channel("fake channel") }.must_raise SlackAPI::SlackError -# end -# end -# end - -# describe "see_details" do -# it "shows details for a selected channel" do -# channel = SlackAPI::Channel.new() -# VCR.use_cassette("see_details") do -# # selected_channel = channel.select_channel("CH2SKTKPC") -# channel_details = channel.see_details("CH2SKTKPC") - -# expect(channel_details).must_be_kind_of Array -# expect(channel_details.length).must_equal 1 -# expect(channel_details[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" -# end -# end -# end - describe "send_msg" do it "sends a message to selected channel" do channel = SlackAPI::Channel.new() diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 271594b4..e960c6da 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -25,43 +25,6 @@ end end - # describe "select_user" do - # it "selects a user as the current recipient when user inputs ID" do - # user = SlackAPI::User.new - # VCR.use_cassette("select_user") do - # response = user.select_user("UH3UT3SJK") - # expect(response).must_equal "UH3UT3SJK" - # end - # end - # it "selects a user as the current recipient when user inputs name" do - # user = SlackAPI::User.new - # VCR.use_cassette("select_user") do - # response = user.select_user("aribray") - # expect(response).must_equal "UH3UT3SJK" - # end - # end - # it "raises a SlackError if the user's name or id is invalid" do - # user = SlackAPI::User.new - # VCR.use_cassette("select_user") do - # expect { user.select_user("fakeuser") }.must_raise SlackAPI::SlackError - # end - # end - # end - - # describe "see_details" do - # it "shows details for selected user" do - # user = SlackAPI::User.new - # VCR.use_cassette("select_user") do - # selected_user = user.select_user("UH3UT3SJK") - # user_details = user.see_details(selected_user) - - # expect(user_details).must_be_kind_of Array - # expect(user_details.length).must_equal 1 - # expect(user_details[0]).must_equal "name" => "aribray", "real name" => "Ariana Bray", "slack id" => "UH3UT3SJK" - # end - # end - # end - describe "send_usr_msg" do it "sends a message to selected user" do VCR.use_cassette("send_usr_msg") do