From 1b0fb1177079588839148fe4003ef12bcdfa5273 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 19 Mar 2019 14:01:55 -0700 Subject: [PATCH 01/27] api verification completed and initial user interface added --- lib/slack.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..76bffdd1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,33 @@ #!/usr/bin/env ruby +require "dotenv" +require "httparty" + +Dotenv.load + +class Channel + BASE_URL = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + + query_parameters = { 'token': key } + + response = HTTParty.get(BASE_URL, query: query_parameters) + + puts "Here are a list of your channels for this Workspace:" + response["channels"].each do |channel| + puts channel["name"] + end +end def main puts "Welcome to the Ada Slack CLI!" + puts "What would you like to do?:" + puts "list users" + puts "list channels" + puts "quit" - # TODO project + user_selection = gets.chomp 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 From d3fdbefe6f5f0623b8085f06efc60f755bdc9b03 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Tue, 19 Mar 2019 15:45:34 -0700 Subject: [PATCH 02/27] added list and get methods to User and Channel classes --- lib/channel.rb | 22 ++++++++++++++++++++++ lib/recipient.rb | 15 +++++++++++++++ lib/slack.rb | 16 ++++++++-------- lib/user.rb | 23 +++++++++++++++++++++++ lib/workspace.rb | 0 specs/channel_spec.rb | 14 ++++++++++++++ specs/recipient_spec.rb | 3 +++ specs/user_spec.rb | 15 +++++++++++++++ specs/workspace_spec.rb | 0 9 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 lib/channel.rb create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 lib/workspace.rb create mode 100644 specs/channel_spec.rb create mode 100644 specs/recipient_spec.rb create mode 100644 specs/user_spec.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..49e7092e --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,22 @@ +require "dotenv" +require "httparty" + +Dotenv.load + +class Channel #< Recipient + BASE_URL = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + + query_parameters = {'token': key} + + def get(url, parameters) + response = HTTParty.get(url, query: parameters) + end + + def list + puts "Here are a list of your channels for this Workspace:" + response["channels"].each do |channel| + puts channel["name"] + end + end +end diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..06b17aac --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,15 @@ +class Recipient + def initialize + @name = name + @slack_id = slack_id + end + + def self.list + # return all instances of the recipient (either Users or Channels) + end + + def self.get(url, parameters) + # call the API to return information + response = HTTParty.get(url, query: parameters) + end +end diff --git a/lib/slack.rb b/lib/slack.rb index 76bffdd1..e60d4a28 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -5,17 +5,17 @@ Dotenv.load class Channel - BASE_URL = "https://slack.com/api/channels.list" - key = ENV["SLACK_API_TOKEN"] + # BASE_URL = "https://slack.com/api/channels.list" + # key = ENV["SLACK_API_TOKEN"] - query_parameters = { 'token': key } + # query_parameters = { 'token': key } - response = HTTParty.get(BASE_URL, query: query_parameters) + # response = HTTParty.get(BASE_URL, query: query_parameters) - puts "Here are a list of your channels for this Workspace:" - response["channels"].each do |channel| - puts channel["name"] - end + # puts "Here are a list of your channels for this Workspace:" + # response["channels"].each do |channel| + # puts channel["name"] + # end end def main diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..ecd91ed9 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,23 @@ +require "dotenv" +require "httparty" + +Dotenv.load + +class User #< Recipient + + BASE_URL = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + query_parameters = {'token': key} + + def get(url, parameters) + response = HTTParty.get(url, query: parameters) + end + + def list + puts "Here are a list of your users for this Workspace:" + response["members"].each do |member| + puts member["name"] + end + end +end diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..5cc262fd --- /dev/null +++ b/specs/channel_spec.rb @@ -0,0 +1,14 @@ +#tests --> CHANNELS + +#0. make sure that what's being returned is in the format we want (i.e. a hash or array) + +#1. make sure that what is returned are actual channels +# expect(channels.list) includes "general" + +#2. make sure that the right things are being returned for each users +# expect { one component of generals's user info is } channel name +# expect { one component of generals's user info is } slack_id +# expect { one component of generals's user info is } topic +# expect { one component of generals's user info is } user count + +#3. expect if there are NO channels -> return empty array diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..70a5de70 --- /dev/null +++ b/specs/recipient_spec.rb @@ -0,0 +1,3 @@ +# tests: + +# should have a slack_id and name diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..71d533ef --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,15 @@ +# users: + +#tests --> + +#0. make sure that what's being returned is in the format we want (i.e. a hash or array) + +#1. make sure that what is returned are actual users +# expect(users.list) includes Amy + +#2. make sure that the right things are being returned for each users +# expect { one component of Amy's user info is } real name +# expect { one component of Amy's user info is } slack_id +# expect { one component of Amy's user info is } username + +#3. expect if there are NO users -> return empty array diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..e69de29b From 3f7eafcc1608820d2008b39fe4066e5cc83eafe5 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Tue, 19 Mar 2019 16:13:07 -0700 Subject: [PATCH 03/27] implemented self.list for User and Channel in slack.rb file --- lib/channel.rb | 21 ++++++++++++++------- lib/slack.rb | 26 ++++++++++---------------- lib/user.rb | 24 ++++++++++++++++-------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 49e7092e..00dc82bd 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,19 +4,26 @@ Dotenv.load class Channel #< Recipient - BASE_URL = "https://slack.com/api/channels.list" - key = ENV["SLACK_API_TOKEN"] + # BASE_URL = "https://slack.com/api/channels.list" + # key = ENV["SLACK_API_TOKEN"] - query_parameters = {'token': key} + # query_parameters = {'token': key} + + # def get(url, parameters) + # response = HTTParty.get(url, query: parameters) + # end + + def self.list + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + + parameters = {'token': key} - def get(url, parameters) response = HTTParty.get(url, query: parameters) - end - def list puts "Here are a list of your channels for this Workspace:" response["channels"].each do |channel| - puts channel["name"] + puts "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" end end end diff --git a/lib/slack.rb b/lib/slack.rb index e60d4a28..46bbc74e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,23 +1,11 @@ #!/usr/bin/env ruby -require "dotenv" -require "httparty" +# require "dotenv" +# require "httparty" +require_relative "user" +require_relative "channel" Dotenv.load -class Channel - # BASE_URL = "https://slack.com/api/channels.list" - # key = ENV["SLACK_API_TOKEN"] - - # query_parameters = { 'token': key } - - # response = HTTParty.get(BASE_URL, query: query_parameters) - - # puts "Here are a list of your channels for this Workspace:" - # response["channels"].each do |channel| - # puts channel["name"] - # end -end - def main puts "Welcome to the Ada Slack CLI!" puts "What would you like to do?:" @@ -27,6 +15,12 @@ def main user_selection = gets.chomp + if user_selection == "list users" + return User.list + elsif user_selection == "list channels" + return Channel.list + end + puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index ecd91ed9..31d55788 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -4,20 +4,28 @@ Dotenv.load class User #< Recipient - - BASE_URL = "https://slack.com/api/users.list" - key = ENV["SLACK_API_TOKEN"] + attr_reader :url, :key - query_parameters = {'token': key} + # url = "https://slack.com/api/users.list" + # key = ENV["SLACK_API_TOKEN"] + + # query_parameters = {'token': key} + + # def get(url, parameters) + # response = HTTParty.get(url, query: parameters) + # end + + def self.list + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + parameters = {'token': key} - def get(url, parameters) response = HTTParty.get(url, query: parameters) - end - def list puts "Here are a list of your users for this Workspace:" response["members"].each do |member| - puts member["name"] + puts "\nUsername: #{member["name"]}, Slack ID: #{member["id"]}, Real name: #{member["real_name"]}!" end end end From fc5769ecd62d6a68bdb2a23a2aa30d21d2b41a25 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 19 Mar 2019 16:29:26 -0700 Subject: [PATCH 04/27] added quit functionality and reprompt loop to slack.rb file --- lib/slack.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 46bbc74e..f12f8588 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -14,11 +14,18 @@ def main puts "quit" user_selection = gets.chomp - - if user_selection == "list users" - return User.list - elsif user_selection == "list channels" - return Channel.list + until user_selection == "quit" + case user_selection + when "list users" + User.list + when "list channels" + Channel.list + end + puts "\nWhat would you like to do next?" + puts "list users" + puts "list channels" + puts "quit" + user_selection = gets.chomp end puts "Thank you for using the Ada Slack CLI" From 0e2c0fa3801c581c6bd6f68fbb934612b17b2c9d Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 13:37:09 -0700 Subject: [PATCH 05/27] updated test_helper file to include VCR configuration --- specs/test_helper.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..798e2548 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,26 @@ require 'simplecov' SimpleCov.start +require 'dotenv' +Dotenv.load + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require 'webmock/minitest' require 'vcr' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" + config.cassette_library_dir = 'specs/cassettes' config.hook_into :webmock + config.default_cassette_options = { + record: :new_episodes, + match_requests_on: [:method, :uri, :body] + } + config.filter_sensitive_data("SLACK_API_TOKEN") do + ENV["SLACK_API_TOKEN"] + end end \ No newline at end of file From 607d41afb7ee52ef9b5781db3e92ab3c9b2b1f46 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Wed, 20 Mar 2019 14:05:22 -0700 Subject: [PATCH 06/27] implemented helper method to hold API information and parse JSON --- lib/channel.rb | 38 ++++++++++++++++++-------------------- lib/slack.rb | 4 +++- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 00dc82bd..2247a42b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -3,27 +3,25 @@ Dotenv.load -class Channel #< Recipient - # BASE_URL = "https://slack.com/api/channels.list" - # key = ENV["SLACK_API_TOKEN"] - - # query_parameters = {'token': key} - - # def get(url, parameters) - # response = HTTParty.get(url, query: parameters) - # end - - def self.list - url = "https://slack.com/api/channels.list" - key = ENV["SLACK_API_TOKEN"] - - parameters = {'token': key} - - response = HTTParty.get(url, query: parameters) +module SlackApi + class SlackError < StandardError; end + + class Channel #< Recipient + def self.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + parameters = { 'token': key } + response = HTTParty.get(url, query: parameters).to_s + response = JSON.parse(response) + + return response["channels"] + end - puts "Here are a list of your channels for this Workspace:" - response["channels"].each do |channel| - puts "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" + def self.list(channels_list) + puts "Here are a list of your channels for this Workspace:" + channels_list.each do |channel| + puts "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" + end end end end diff --git a/lib/slack.rb b/lib/slack.rb index f12f8588..dc2813f8 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,6 +7,8 @@ Dotenv.load def main + channels_list = SlackApi::Channel.channel_api + puts "Welcome to the Ada Slack CLI!" puts "What would you like to do?:" puts "list users" @@ -19,7 +21,7 @@ def main when "list users" User.list when "list channels" - Channel.list + SlackApi::Channel.list(channels_list) end puts "\nWhat would you like to do next?" puts "list users" From 4b89f888aaa9ac643bdcd8920701a8aa76a1995b Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 14:14:35 -0700 Subject: [PATCH 07/27] implemented helper method for user.rb to store API info and parse through JSON --- lib/slack.rb | 3 ++- lib/user.rb | 40 ++++++++++++++++++---------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index dc2813f8..d36d0eb0 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -8,6 +8,7 @@ def main channels_list = SlackApi::Channel.channel_api + users_list = SlackApi::User.user_api puts "Welcome to the Ada Slack CLI!" puts "What would you like to do?:" @@ -19,7 +20,7 @@ def main until user_selection == "quit" case user_selection when "list users" - User.list + SlackApi::User.list(users_list) when "list channels" SlackApi::Channel.list(channels_list) end diff --git a/lib/user.rb b/lib/user.rb index 31d55788..ce97f133 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -3,29 +3,25 @@ Dotenv.load -class User #< Recipient - attr_reader :url, :key - - # url = "https://slack.com/api/users.list" - # key = ENV["SLACK_API_TOKEN"] - - # query_parameters = {'token': key} - - # def get(url, parameters) - # response = HTTParty.get(url, query: parameters) - # end - - def self.list - url = "https://slack.com/api/users.list" - key = ENV["SLACK_API_TOKEN"] - - parameters = {'token': key} - - response = HTTParty.get(url, query: parameters) +module SlackApi + class SlackError < StandardError; end + + class User #< Recipient + def self.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + parameters = {'token': key} + response = HTTParty.get(url, query: parameters).to_s + response = JSON.parse(response) + + return response["members"] + end - puts "Here are a list of your users for this Workspace:" - response["members"].each do |member| - puts "\nUsername: #{member["name"]}, Slack ID: #{member["id"]}, Real name: #{member["real_name"]}!" + def self.list(users_list) + puts "Here are a list of your users for this Workspace:" + users_list.each do |user| + puts "\nUsername: #{user["name"]}, Slack ID: #{user["id"]}, Real name: #{user["real_name"]}!" + end end end end From 65f53bb46dc8a7af98eb7a009da8a0694860957d Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 14:57:00 -0700 Subject: [PATCH 08/27] wrote corresponding tests for 'list channels' --- lib/channel.rb | 2 +- specs/channel_spec.rb | 42 +++++++++++++++++++++++++++++++++++++++++- specs/test_helper.rb | 2 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 2247a42b..50456ae9 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -10,7 +10,7 @@ class Channel #< Recipient def self.channel_api url = "https://slack.com/api/channels.list" key = ENV["SLACK_API_TOKEN"] - parameters = { 'token': key } + parameters = {'token': key} response = HTTParty.get(url, query: parameters).to_s response = JSON.parse(response) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 5cc262fd..19950eb5 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,4 +1,44 @@ -#tests --> CHANNELS +require_relative "test_helper" + +describe SlackApi do + describe "list channels" do + it "returns valid channels" do + VCR.use_cassette("slack_channels_list") do + channels_list = SlackApi::Channel.channel_api + + channels_list.each do |channel| + expect(channel["is_channel"]).must_equal true + end + end + end + + it "return includes a specific channel" do + VCR.use_cassette("slack_channels_list") do + channels_list = SlackApi::Channel.channel_api + + expect(channels_list.first["name"]).must_equal "general" + end + end + + it "channels list will only include existent channels " do + VCR.use_cassette("slack_channels_list") do + channels_list = SlackApi::Channel.channel_api + + channels_list.each do |channel| + name = "Not a Channel" + expect(channel["name"]).wont_equal name + end + end + end + # it "returns info with a valid token" do + # VCR.use_cassette("slack_channels_list") do + # channels_list = SlackApi::Channel.channel_api + + # expect(channels_list["ok"]).must_equal true + # end + # end + end +end #0. make sure that what's being returned is in the format we want (i.e. a hash or array) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 798e2548..800de3db 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -11,6 +11,8 @@ require 'webmock/minitest' require 'vcr' +require_relative "../lib/channel" + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| From 3e7ac4a7ee43c9e2fa51495bdd79777f28afa23a Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Wed, 20 Mar 2019 15:05:06 -0700 Subject: [PATCH 09/27] wrote corresponding tests for list users --- specs/test_helper.rb | 25 +++++++++++++------------ specs/user_spec.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 800de3db..1789e792 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,28 +1,29 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'dotenv' +require "dotenv" Dotenv.load -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'webmock/minitest' -require 'vcr' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "webmock/minitest" +require "vcr" require_relative "../lib/channel" +require_relative "../lib/user" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = 'specs/cassettes' + config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock config.default_cassette_options = { - record: :new_episodes, - match_requests_on: [:method, :uri, :body] + record: :new_episodes, + match_requests_on: [:method, :uri, :body], } config.filter_sensitive_data("SLACK_API_TOKEN") do ENV["SLACK_API_TOKEN"] end -end \ No newline at end of file +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 71d533ef..9bd27e3c 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,3 +1,45 @@ +require_relative "test_helper" + +describe SlackApi do + describe "list users" do + it "returns valid users" do + VCR.use_cassette("slack_users_list") do + users_list = SlackApi::User.user_api + + users_list.each do |user| + expect(user["team_id"]).wont_be_nil + end + end + end + + it "includes a specific user" do + VCR.use_cassette("slack_user_list") do + users_list = SlackApi::User.user_api + + expect(users_list.first["name"]).must_equal "slackbot" + end + end + + it "users list will only include existent users" do + VCR.use_cassette("slack_users_list") do + users_list = SlackApi::User.user_api + + users_list.each do |user| + name = "Not a User" + expect(user["name"]).wont_equal name + end + end + end + # it "returns info with a valid token" do + # VCR.use_cassette("slack_channels_list") do + # channels_list = SlackApi::Channel.channel_api + + # expect(channels_list["ok"]).must_equal true + # end + # end + end +end + # users: #tests --> From 05bee2744466e4c9b86a4785590692dc007f1a3c Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 15:57:24 -0700 Subject: [PATCH 10/27] added Workspace class and select user and select channel methods --- lib/channel.rb | 5 +++-- lib/slack.rb | 27 +++++++++++++++++++++++++-- lib/user.rb | 5 +++-- lib/workspace.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 50456ae9..37d41fdd 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -19,9 +19,10 @@ def self.channel_api def self.list(channels_list) puts "Here are a list of your channels for this Workspace:" - channels_list.each do |channel| - puts "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" + list_of_channels = channels_list.map do |channel| + "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" end + return list_of_channels end end end diff --git a/lib/slack.rb b/lib/slack.rb index d36d0eb0..2245da5e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,6 +3,7 @@ # require "httparty" require_relative "user" require_relative "channel" +require_relative "workspace" Dotenv.load @@ -14,19 +15,41 @@ def main puts "What would you like to do?:" puts "list users" puts "list channels" + puts "select user" + puts "select channel" + puts "details" puts "quit" user_selection = gets.chomp until user_selection == "quit" case user_selection when "list users" - SlackApi::User.list(users_list) + puts SlackApi::User.list(users_list) when "list channels" - SlackApi::Channel.list(channels_list) + puts SlackApi::Channel.list(channels_list) + when "select user" + print "Enter user's name or user's ID: " + user_input = gets.chomp + + if SlackApi::Workspace.select_user(user_input) != true + puts "That user does not exist" + end + when "select channel" + print "Enter channel's name or channel's ID: " + user_input = gets.chomp + + if SlackApi::Workspace.select_channel(user_input) != true + puts "That channel does not exist" + end + when "details" end + puts "\nWhat would you like to do next?" puts "list users" puts "list channels" + puts "select user" + puts "select channel" + puts "details" puts "quit" user_selection = gets.chomp end diff --git a/lib/user.rb b/lib/user.rb index ce97f133..13a557fc 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -19,9 +19,10 @@ def self.user_api def self.list(users_list) puts "Here are a list of your users for this Workspace:" - users_list.each do |user| - puts "\nUsername: #{user["name"]}, Slack ID: #{user["id"]}, Real name: #{user["real_name"]}!" + list_of_users = users_list.map do |user| + "\nUsername: #{user["name"]}, Slack ID: #{user["id"]}, Real name: #{user["real_name"]}!" end + return list_of_users end end end diff --git a/lib/workspace.rb b/lib/workspace.rb index e69de29b..f8960706 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -0,0 +1,43 @@ +require "dotenv" +require "httparty" + +require_relative "channel" +require_relative "user" + +Dotenv.load + +module SlackApi + class SlackError < StandardError; end + + class Workspace + def initialize + @users = SlackApi::User.user_api + @channels = SlackApi::Channel.channel_api + # @selected = "" + end + + def self.select_channel(user_input) + #--> take user_input and put into @selected + @channels.each do |channel| + if channel["name"] == user_input || channel["id"] == user_input + @selected = channel + return true + end + end + end + + def self.select_user(user_input) + # --> take user_input and put into @selected + @users.each do |user| + if user["name"] == user_input || user["id"] == user_input + @selected = user + return true + end + end + end + + # def show_details + # --> takes @selected and returns details + # end + end +end From 6498e5662ede1d5d7504f1451faef3da8a685361 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 19:45:20 -0700 Subject: [PATCH 11/27] added show_details method and functionality for workspace --- lib/slack.rb | 1 + lib/workspace.rb | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 2245da5e..2167fa59 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -42,6 +42,7 @@ def main puts "That channel does not exist" end when "details" + puts SlackApi::Workspace.show_details end puts "\nWhat would you like to do next?" diff --git a/lib/workspace.rb b/lib/workspace.rb index f8960706..39de52f3 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -10,15 +10,17 @@ module SlackApi class SlackError < StandardError; end class Workspace + attr_reader :channels, :users + def initialize @users = SlackApi::User.user_api @channels = SlackApi::Channel.channel_api - # @selected = "" + @selected = "" end def self.select_channel(user_input) #--> take user_input and put into @selected - @channels.each do |channel| + (SlackApi::Channel.channel_api).each do |channel| if channel["name"] == user_input || channel["id"] == user_input @selected = channel return true @@ -28,7 +30,7 @@ def self.select_channel(user_input) def self.select_user(user_input) # --> take user_input and put into @selected - @users.each do |user| + (SlackApi::User.user_api).each do |user| if user["name"] == user_input || user["id"] == user_input @selected = user return true @@ -36,8 +38,19 @@ def self.select_user(user_input) end end - # def show_details - # --> takes @selected and returns details - # end + def self.show_details + # --> takes @selected and returns details + if (SlackApi::Channel.channel_api).include?(@selected) + channel_details = "\nChannel name: #{@selected["name"]}\nSlack ID: #{@selected["id"]}\nTopic: #{@selected["topic"]["value"]}\nMember count: #{@selected["num_members"]}" + + return channel_details + end + + if (SlackApi::User.user_api).include?(@selected) + user_details = "\nUsername: #{user["name"]}\nSlack ID: #{user["id"]} \nReal name: #{user["real_name"]}" + + return user_details + end + end end end From c4697ceff3d237ef2ccf7409b8a37759234c1328 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 20:14:17 -0700 Subject: [PATCH 12/27] edited cli formatting and updated self.details method to respond when no recipient was selected --- lib/channel.rb | 4 ++-- lib/slack.rb | 4 ++-- lib/user.rb | 4 ++-- lib/workspace.rb | 16 +++++++--------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 37d41fdd..9453dc0a 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -18,9 +18,9 @@ def self.channel_api end def self.list(channels_list) - puts "Here are a list of your channels for this Workspace:" + puts "\nHere are a list of your channels for this Workspace:" list_of_channels = channels_list.map do |channel| - "\nChannel name: #{channel["name"]}, Slack ID: #{channel["id"]}, Topic: #{channel["topic"]["value"]}, Member count: #{channel["num_members"]}!" + "\nChannel name: #{channel["name"]}\nSlack ID: #{channel["id"]}\nTopic: #{channel["topic"]["value"]}\nMember count: #{channel["num_members"]}\n" end return list_of_channels end diff --git a/lib/slack.rb b/lib/slack.rb index 2167fa59..5fc862d5 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -28,14 +28,14 @@ def main when "list channels" puts SlackApi::Channel.list(channels_list) when "select user" - print "Enter user's name or user's ID: " + print "Enter username or user's ID: " user_input = gets.chomp if SlackApi::Workspace.select_user(user_input) != true puts "That user does not exist" end when "select channel" - print "Enter channel's name or channel's ID: " + print "Enter channel name or channel's ID: " user_input = gets.chomp if SlackApi::Workspace.select_channel(user_input) != true diff --git a/lib/user.rb b/lib/user.rb index 13a557fc..53f8e575 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -18,9 +18,9 @@ def self.user_api end def self.list(users_list) - puts "Here are a list of your users for this Workspace:" + puts "\nHere are a list of your users for this Workspace:" list_of_users = users_list.map do |user| - "\nUsername: #{user["name"]}, Slack ID: #{user["id"]}, Real name: #{user["real_name"]}!" + "\nUsername: #{user["name"]}\nSlack ID: #{user["id"]}\nReal name: #{user["real_name"]}" end return list_of_users end diff --git a/lib/workspace.rb b/lib/workspace.rb index 39de52f3..6b009854 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -10,7 +10,7 @@ module SlackApi class SlackError < StandardError; end class Workspace - attr_reader :channels, :users + # attr_reader :channels, :users def initialize @users = SlackApi::User.user_api @@ -19,7 +19,6 @@ def initialize end def self.select_channel(user_input) - #--> take user_input and put into @selected (SlackApi::Channel.channel_api).each do |channel| if channel["name"] == user_input || channel["id"] == user_input @selected = channel @@ -29,7 +28,6 @@ def self.select_channel(user_input) end def self.select_user(user_input) - # --> take user_input and put into @selected (SlackApi::User.user_api).each do |user| if user["name"] == user_input || user["id"] == user_input @selected = user @@ -39,18 +37,18 @@ def self.select_user(user_input) end def self.show_details - # --> takes @selected and returns details if (SlackApi::Channel.channel_api).include?(@selected) channel_details = "\nChannel name: #{@selected["name"]}\nSlack ID: #{@selected["id"]}\nTopic: #{@selected["topic"]["value"]}\nMember count: #{@selected["num_members"]}" return channel_details - end - - if (SlackApi::User.user_api).include?(@selected) - user_details = "\nUsername: #{user["name"]}\nSlack ID: #{user["id"]} \nReal name: #{user["real_name"]}" + elsif (SlackApi::User.user_api).include?(@selected) + user_details = "\nUsername: #{@selected["name"]}\nSlack ID: #{@selected["id"]} \nReal name: #{@selected["real_name"]}" return user_details - end + else + error_message = "You have not selected a user or channel yet." + return error_message + end end end end From e3e34f4d11912cfe691172980e889c5e98a3f87a Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 20:24:39 -0700 Subject: [PATCH 13/27] added test outlines for workspace --- specs/channel_spec.rb | 2 +- specs/test_helper.rb | 1 + specs/user_spec.rb | 2 +- specs/workspace_spec.rb | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 19950eb5..e284e2a6 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe SlackApi do +describe SlackApi::Channel do describe "list channels" do it "returns valid channels" do VCR.use_cassette("slack_channels_list") do diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 1789e792..314b9610 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -13,6 +13,7 @@ require_relative "../lib/channel" require_relative "../lib/user" +require_relative "../lib/workspace" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 9bd27e3c..7bd755d0 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe SlackApi do +describe SlackApi::User do describe "list users" do it "returns valid users" do VCR.use_cassette("slack_users_list") do diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index e69de29b..f285da00 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -0,0 +1,18 @@ +require_relative "test_helper" + +describe SlackApi::Workspace do + describe "self.select_channel" do + it "selects a valid channel" do + end + end + + describe "self.select_user" do + it "selects a valid user" do + end + end + + describe "self.show_details" do + it "returns details for the correct recipient" do + end + end +end \ No newline at end of file From c2674043a24a2fde841d4364af36e9f04b81ede8 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Wed, 20 Mar 2019 21:02:59 -0700 Subject: [PATCH 14/27] added preliminary code for sending messages to recipients --- lib/channel.rb | 20 ++++++++++++++++++++ lib/slack.rb | 23 +++++++++-------------- lib/workspace.rb | 13 ++++++++++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 9453dc0a..9337bc56 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,6 +17,26 @@ def self.channel_api return response["channels"] end + # def self.send_message(message, recipient) + # url = "https://slack.com/api/channels.list" + # key = ENV["SLACK_API_TOKEN"] + + # response = HTTParty.post( + # "#{url}chat.postMessage", + # headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + # body: { + # token: key, + # text: message, + # channel: recipient, + # }, + # ) + # if response["ok"] + # return true + # # else + # # raise SlackApi::SlackError, "Error when posting message to #{response[:body][:channel]}, error: #{response["error"]}" + # end + # end + def self.list(channels_list) puts "\nHere are a list of your channels for this Workspace:" list_of_channels = channels_list.map do |channel| diff --git a/lib/slack.rb b/lib/slack.rb index 5fc862d5..d1cffe84 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,14 +11,11 @@ def main channels_list = SlackApi::Channel.channel_api users_list = SlackApi::User.user_api + user_options = "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" + puts "Welcome to the Ada Slack CLI!" puts "What would you like to do?:" - puts "list users" - puts "list channels" - puts "select user" - puts "select channel" - puts "details" - puts "quit" + puts user_options user_selection = gets.chomp until user_selection == "quit" @@ -30,28 +27,26 @@ def main when "select user" print "Enter username or user's ID: " user_input = gets.chomp - if SlackApi::Workspace.select_user(user_input) != true puts "That user does not exist" end when "select channel" print "Enter channel name or channel's ID: " user_input = gets.chomp - if SlackApi::Workspace.select_channel(user_input) != true puts "That channel does not exist" end when "details" puts SlackApi::Workspace.show_details + when "send message" + puts "(This will send to the recipient you have selected)" + print "What message would you like to send? " + user_message = gets.chomp + # SlackApi::Workspace.send_message(user_message) end puts "\nWhat would you like to do next?" - puts "list users" - puts "list channels" - puts "select user" - puts "select channel" - puts "details" - puts "quit" + puts user_options user_selection = gets.chomp end diff --git a/lib/workspace.rb b/lib/workspace.rb index 6b009854..0ba23313 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -45,10 +45,21 @@ def self.show_details user_details = "\nUsername: #{@selected["name"]}\nSlack ID: #{@selected["id"]} \nReal name: #{@selected["real_name"]}" return user_details - else + else error_message = "You have not selected a user or channel yet." return error_message end end + + # def self.send_message(message) + # if (SlackApi::Channel.channel_api).include?(@selected) + # SlackApi::Channel.send_message(message, @selected) + # elsif (SlackApi::User.user_api).include?(@selected) + # # SlackApi::user .method for sending message to user + # else + # error_message = "You have not selected a user or channel yet." + # return error_message + # end + # end end end From 5e5665ecd777a481494edf23a1ff7da8855d5a7c Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 21 Mar 2019 10:27:46 -0700 Subject: [PATCH 15/27] edited workspace to initialize with all channels and users --- lib/channel.rb | 36 ++++++++++++++++++------------------ lib/slack.rb | 11 ++++++----- lib/workspace.rb | 22 +++++++++++----------- specs/workspace_spec.rb | 9 +++++++++ 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 9337bc56..d153bc9d 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,25 +17,25 @@ def self.channel_api return response["channels"] end - # def self.send_message(message, recipient) - # url = "https://slack.com/api/channels.list" - # key = ENV["SLACK_API_TOKEN"] + def self.send_message(message, recipient) + url = "https://slack.com/api/" + key = ENV["SLACK_API_TOKEN"] - # response = HTTParty.post( - # "#{url}chat.postMessage", - # headers: {"Content-Type" => "application/x-www-form-urlencoded"}, - # body: { - # token: key, - # text: message, - # channel: recipient, - # }, - # ) - # if response["ok"] - # return true - # # else - # # raise SlackApi::SlackError, "Error when posting message to #{response[:body][:channel]}, error: #{response["error"]}" - # end - # end + response = HTTParty.post( + "#{url}chat.postMessage", + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + body: { + token: key, + text: message, + channel: recipient, + }, + ) + if response["ok"] + return true + else + raise SlackApi::SlackError, "Error when posting message to #{response[:body][:channel]}, error: #{response["error"]}" + end + end def self.list(channels_list) puts "\nHere are a list of your channels for this Workspace:" diff --git a/lib/slack.rb b/lib/slack.rb index d1cffe84..99a9a3d2 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -8,8 +8,9 @@ Dotenv.load def main - channels_list = SlackApi::Channel.channel_api - users_list = SlackApi::User.user_api + # channels_list = SlackApi::Channel.channel_api + # users_list = SlackApi::User.user_api + workspace = SlackApi::Workspace.new user_options = "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" @@ -21,9 +22,9 @@ def main until user_selection == "quit" case user_selection when "list users" - puts SlackApi::User.list(users_list) + puts SlackApi::User.list(workspace.users) when "list channels" - puts SlackApi::Channel.list(channels_list) + puts SlackApi::Channel.list(workspace.channels) when "select user" print "Enter username or user's ID: " user_input = gets.chomp @@ -42,7 +43,7 @@ def main puts "(This will send to the recipient you have selected)" print "What message would you like to send? " user_message = gets.chomp - # SlackApi::Workspace.send_message(user_message) + SlackApi::Workspace.send_message(user_message) end puts "\nWhat would you like to do next?" diff --git a/lib/workspace.rb b/lib/workspace.rb index 0ba23313..393431dd 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -10,7 +10,7 @@ module SlackApi class SlackError < StandardError; end class Workspace - # attr_reader :channels, :users + attr_reader :channels, :users def initialize @users = SlackApi::User.user_api @@ -51,15 +51,15 @@ def self.show_details end end - # def self.send_message(message) - # if (SlackApi::Channel.channel_api).include?(@selected) - # SlackApi::Channel.send_message(message, @selected) - # elsif (SlackApi::User.user_api).include?(@selected) - # # SlackApi::user .method for sending message to user - # else - # error_message = "You have not selected a user or channel yet." - # return error_message - # end - # end + def self.send_message(message) + if (SlackApi::Channel.channel_api).include?(@selected) + SlackApi::Channel.send_message(message, @selected) + elsif (SlackApi::User.user_api).include?(@selected) + # SlackApi::user .method for sending message to user + else + error_message = "You have not selected a user or channel yet." + return error_message + end + end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index f285da00..49abdc36 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -4,15 +4,24 @@ describe "self.select_channel" do it "selects a valid channel" do end + + it "returns false if an invalid channel is attempted to be selected" do + end end describe "self.select_user" do it "selects a valid user" do end + + it "returns a false if an invalid user is attempted to be selected" do + end end describe "self.show_details" do it "returns details for the correct recipient" do end + + it "returns an error message if no recipient has been selected" do + end end end \ No newline at end of file From f58f4f2372642f6e919c68098dda9974e04b8354 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Thu, 21 Mar 2019 14:15:03 -0700 Subject: [PATCH 16/27] changed class methods to instance methods in workspace.rb --- lib/slack.rb | 8 ++++---- lib/workspace.rb | 18 +++++++++--------- specs/user_spec.rb | 7 ++----- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 99a9a3d2..dd3df250 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -28,22 +28,22 @@ def main when "select user" print "Enter username or user's ID: " user_input = gets.chomp - if SlackApi::Workspace.select_user(user_input) != true + if workspace.select_user(user_input) != true puts "That user does not exist" end when "select channel" print "Enter channel name or channel's ID: " user_input = gets.chomp - if SlackApi::Workspace.select_channel(user_input) != true + if workspace.select_channel(user_input) != true puts "That channel does not exist" end when "details" - puts SlackApi::Workspace.show_details + puts workspace.show_details when "send message" puts "(This will send to the recipient you have selected)" print "What message would you like to send? " user_message = gets.chomp - SlackApi::Workspace.send_message(user_message) + workspace.send_message(user_message) end puts "\nWhat would you like to do next?" diff --git a/lib/workspace.rb b/lib/workspace.rb index 393431dd..c07b3435 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -18,8 +18,8 @@ def initialize @selected = "" end - def self.select_channel(user_input) - (SlackApi::Channel.channel_api).each do |channel| + def select_channel(user_input) + @channels.each do |channel| if channel["name"] == user_input || channel["id"] == user_input @selected = channel return true @@ -27,8 +27,8 @@ def self.select_channel(user_input) end end - def self.select_user(user_input) - (SlackApi::User.user_api).each do |user| + def select_user(user_input) + @users.each do |user| if user["name"] == user_input || user["id"] == user_input @selected = user return true @@ -36,12 +36,12 @@ def self.select_user(user_input) end end - def self.show_details - if (SlackApi::Channel.channel_api).include?(@selected) + def show_details + if @channels.include?(@selected) channel_details = "\nChannel name: #{@selected["name"]}\nSlack ID: #{@selected["id"]}\nTopic: #{@selected["topic"]["value"]}\nMember count: #{@selected["num_members"]}" return channel_details - elsif (SlackApi::User.user_api).include?(@selected) + elsif @users.include?(@selected) user_details = "\nUsername: #{@selected["name"]}\nSlack ID: #{@selected["id"]} \nReal name: #{@selected["real_name"]}" return user_details @@ -51,8 +51,8 @@ def self.show_details end end - def self.send_message(message) - if (SlackApi::Channel.channel_api).include?(@selected) + def send_message(message) + if @channels.include?(@selected) SlackApi::Channel.send_message(message, @selected) elsif (SlackApi::User.user_api).include?(@selected) # SlackApi::user .method for sending message to user diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 7bd755d0..00367e82 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -32,9 +32,9 @@ end # it "returns info with a valid token" do # VCR.use_cassette("slack_channels_list") do - # channels_list = SlackApi::Channel.channel_api + # channels_list = SlackApi::Channel.user_api - # expect(channels_list["ok"]).must_equal true + # expect(users_list["ok"]).must_equal true # end # end end @@ -46,9 +46,6 @@ #0. make sure that what's being returned is in the format we want (i.e. a hash or array) -#1. make sure that what is returned are actual users -# expect(users.list) includes Amy - #2. make sure that the right things are being returned for each users # expect { one component of Amy's user info is } real name # expect { one component of Amy's user info is } slack_id From 5d541ed6d91124b582c7ead848772b61aaf6a10c Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 21 Mar 2019 14:45:47 -0700 Subject: [PATCH 17/27] fixed VCR boo-boo (moved initialization of Workspace into VCR.use_cassette --- lib/workspace.rb | 20 +++++++------- specs/workspace_spec.rb | 60 ++++++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index c07b3435..562b0694 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -51,15 +51,15 @@ def show_details end end - def send_message(message) - if @channels.include?(@selected) - SlackApi::Channel.send_message(message, @selected) - elsif (SlackApi::User.user_api).include?(@selected) - # SlackApi::user .method for sending message to user - else - error_message = "You have not selected a user or channel yet." - return error_message - end - end + # def send_message(message) + # if @channels.include?(@selected) + # SlackApi::Channel.send_message(message, @selected) + # elsif (SlackApi::User.user_api).include?(@selected) + # # SlackApi::user .method for sending message to user + # else + # error_message = "You have not selected a user or channel yet." + # return error_message + # end + # end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 49abdc36..5f6af980 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,27 +1,49 @@ require_relative "test_helper" describe SlackApi::Workspace do - describe "self.select_channel" do - it "selects a valid channel" do - end + before do + end + describe "select_channel" do + it "selects a valid channel" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + valid_channel_name = "random" + expect(workspace.select_channel(valid_channel_name)).must_equal true + end + end - it "returns false if an invalid channel is attempted to be selected" do - end - end + # it "returns false if an invalid channel is attempted to be selected" do + # skip + # VCR.use_cassette("slack_workspace") do + # end + # end + end - describe "self.select_user" do - it "selects a valid user" do - end + # describe "select_user" do + # it "selects a valid user" do + # skip + # VCR.use_cassette("slack_workspace") do + # end + # end - it "returns a false if an invalid user is attempted to be selected" do - end - end + # it "returns a false if an invalid user is attempted to be selected" do + # skip + # VCR.use_cassette("slack_workspace") do + # end + # end + # end - describe "self.show_details" do - it "returns details for the correct recipient" do - end + # describe "show_details" do + # it "returns details for the correct recipient" do + # skip + # VCR.use_cassette("slack_workspace") do + # end + # end - it "returns an error message if no recipient has been selected" do - end - end -end \ No newline at end of file + # it "returns an error message if no recipient has been selected" do + # skip + # VCR.use_cassette("slack_workspace") do + # end + # end + # end +end From 3b9e6041da3ab6ff22faa09bb7275990bf6996ef Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Thu, 21 Mar 2019 15:04:01 -0700 Subject: [PATCH 18/27] select_user tests added --- lib/workspace.rb | 2 ++ specs/workspace_spec.rb | 46 +++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 562b0694..30136c04 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -25,6 +25,7 @@ def select_channel(user_input) return true end end + return false end def select_user(user_input) @@ -34,6 +35,7 @@ def select_user(user_input) return true end end + return false end def show_details diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 5f6af980..0f229592 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,8 +1,6 @@ require_relative "test_helper" describe SlackApi::Workspace do - before do - end describe "select_channel" do it "selects a valid channel" do VCR.use_cassette("slack_workspace") do @@ -12,31 +10,39 @@ end end - # it "returns false if an invalid channel is attempted to be selected" do - # skip - # VCR.use_cassette("slack_workspace") do - # end - # end + it "returns false if an invalid channel is attempted to be selected" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + invalid_channel_name = "not a channel" + expect(workspace.select_channel(invalid_channel_name)).must_equal false + end + end end - # describe "select_user" do - # it "selects a valid user" do - # skip - # VCR.use_cassette("slack_workspace") do - # end - # end + describe "select_user" do + it "selects a valid user" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + valid_user_name = "slackbot" + expect(workspace.select_user(valid_user_name)).must_equal true + end + end - # it "returns a false if an invalid user is attempted to be selected" do - # skip - # VCR.use_cassette("slack_workspace") do - # end - # end - # end + it "returns a false if an invalid user is attempted to be selected" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + invalid_user_name = "smackbot" + expect(workspace.select_user(invalid_user_name)).must_equal false + end + end + + # write tests for User ID also + end # describe "show_details" do # it "returns details for the correct recipient" do - # skip # VCR.use_cassette("slack_workspace") do + # end # end From d63ce7653ae0922590ecd6d6698057a23c68b153 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 21 Mar 2019 15:17:31 -0700 Subject: [PATCH 19/27] added tests for show_details for workspace class --- specs/workspace_spec.rb | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 0f229592..77d0dd52 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -39,17 +39,31 @@ # write tests for User ID also end - # describe "show_details" do - # it "returns details for the correct recipient" do - # VCR.use_cassette("slack_workspace") do - - # end - # end - - # it "returns an error message if no recipient has been selected" do - # skip - # VCR.use_cassette("slack_workspace") do - # end - # end - # end + describe "show_details" do + it "returns details for the correct channel" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + workspace.select_channel("random") + + expect(workspace.show_details).must_include "CH3UGLBHV" + end + end + + it "returns details for the correct user" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + workspace.select_user("slackbot") + + expect(workspace.show_details).must_include "USLACKBOT" + end + end + + it "returns an error message if no recipient has been selected" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + + expect(workspace.show_details).must_include "You have not selected a user or channel yet." + end + end + end end From 4379d225028ba3d051224bff0b52c307e3af51dd Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 21 Mar 2019 15:27:31 -0700 Subject: [PATCH 20/27] added more tests for each workspace method --- specs/workspace_spec.rb | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 77d0dd52..23a6a7b3 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -2,7 +2,7 @@ describe SlackApi::Workspace do describe "select_channel" do - it "selects a valid channel" do + it "selects a valid channel name" do VCR.use_cassette("slack_workspace") do workspace = SlackApi::Workspace.new valid_channel_name = "random" @@ -10,6 +10,14 @@ end end + it "selects a valid channel ID" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + valid_channel_id = "CH3UGLBHV" + expect(workspace.select_channel(valid_channel_id)).must_equal true + end + end + it "returns false if an invalid channel is attempted to be selected" do VCR.use_cassette("slack_workspace") do workspace = SlackApi::Workspace.new @@ -17,10 +25,17 @@ expect(workspace.select_channel(invalid_channel_name)).must_equal false end end + + it "returns a false if select_channel is not given an input" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + expect(workspace.select_channel("")).must_equal false + end + end end describe "select_user" do - it "selects a valid user" do + it "selects a valid username" do VCR.use_cassette("slack_workspace") do workspace = SlackApi::Workspace.new valid_user_name = "slackbot" @@ -28,6 +43,14 @@ end end + it "selects a valid user ID" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + valid_user_id = "USLACKBOT" + expect(workspace.select_user(valid_user_id)).must_equal true + end + end + it "returns a false if an invalid user is attempted to be selected" do VCR.use_cassette("slack_workspace") do workspace = SlackApi::Workspace.new @@ -36,7 +59,12 @@ end end - # write tests for User ID also + it "returns a false if select_user is not given an input" do + VCR.use_cassette("slack_workspace") do + workspace = SlackApi::Workspace.new + expect(workspace.select_user("")).must_equal false + end + end end describe "show_details" do From 1af022711c6cc0c7d478fab0db4e323c0c68f0c5 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 21 Mar 2019 15:51:51 -0700 Subject: [PATCH 21/27] edited UI for cleaner output --- lib/slack.rb | 13 +++++++------ lib/workspace.rb | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index dd3df250..02eeda2b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,7 +15,8 @@ def main user_options = "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" puts "Welcome to the Ada Slack CLI!" - puts "What would you like to do?:" + puts "\nThere are #{workspace.channels.length} channels and #{workspace.users.length} users in this Workspace." + puts "\nWhat would you like to do?:" puts user_options user_selection = gets.chomp @@ -26,21 +27,21 @@ def main when "list channels" puts SlackApi::Channel.list(workspace.channels) when "select user" - print "Enter username or user's ID: " + print "\nEnter username or user's ID: " user_input = gets.chomp if workspace.select_user(user_input) != true - puts "That user does not exist" + puts "\n~That user does not exist~" end when "select channel" - print "Enter channel name or channel's ID: " + print "\nEnter channel name or channel's ID: " user_input = gets.chomp if workspace.select_channel(user_input) != true - puts "That channel does not exist" + puts "\n~That channel does not exist~" end when "details" puts workspace.show_details when "send message" - puts "(This will send to the recipient you have selected)" + puts "\n(This will send to the recipient you have selected)" print "What message would you like to send? " user_message = gets.chomp workspace.send_message(user_message) diff --git a/lib/workspace.rb b/lib/workspace.rb index 30136c04..e54fe355 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -48,20 +48,20 @@ def show_details return user_details else - error_message = "You have not selected a user or channel yet." + error_message = "\n~You have not selected a user or channel yet.~" return error_message end end - # def send_message(message) - # if @channels.include?(@selected) - # SlackApi::Channel.send_message(message, @selected) - # elsif (SlackApi::User.user_api).include?(@selected) - # # SlackApi::user .method for sending message to user - # else - # error_message = "You have not selected a user or channel yet." - # return error_message - # end - # end + def send_message(message) + if @channels.include?(@selected) + SlackApi::Channel.send_message(message, @selected) + elsif (SlackApi::User.user_api).include?(@selected) + # SlackApi::user .method for sending message to user + else + error_message = "You have not selected a user or channel yet." + return error_message + end + end end end From 960edc39294830811d21561db239b375fc2efce8 Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Fri, 22 Mar 2019 11:00:02 -0700 Subject: [PATCH 22/27] edited and completed send_message functionality with corresponding tests --- lib/channel.rb | 26 ++++++-------------------- lib/slack.rb | 2 +- lib/workspace.rb | 24 +++++++++++++++++++----- specs/workspace_spec.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index d153bc9d..18516c21 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -7,6 +7,9 @@ module SlackApi class SlackError < StandardError; end class Channel #< Recipient + def initialize(raw_channel) + end + def self.channel_api url = "https://slack.com/api/channels.list" key = ENV["SLACK_API_TOKEN"] @@ -15,26 +18,9 @@ def self.channel_api response = JSON.parse(response) return response["channels"] - end - - def self.send_message(message, recipient) - url = "https://slack.com/api/" - key = ENV["SLACK_API_TOKEN"] - - response = HTTParty.post( - "#{url}chat.postMessage", - headers: {"Content-Type" => "application/x-www-form-urlencoded"}, - body: { - token: key, - text: message, - channel: recipient, - }, - ) - if response["ok"] - return true - else - raise SlackApi::SlackError, "Error when posting message to #{response[:body][:channel]}, error: #{response["error"]}" - end + # return response["channels"].map do |channel| + # self.new(channel) + # end end def self.list(channels_list) diff --git a/lib/slack.rb b/lib/slack.rb index 02eeda2b..0cb19ad9 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -44,7 +44,7 @@ def main puts "\n(This will send to the recipient you have selected)" print "What message would you like to send? " user_message = gets.chomp - workspace.send_message(user_message) + puts workspace.send_message(user_message) end puts "\nWhat would you like to do next?" diff --git a/lib/workspace.rb b/lib/workspace.rb index e54fe355..d4cb3217 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -54,12 +54,26 @@ def show_details end def send_message(message) - if @channels.include?(@selected) - SlackApi::Channel.send_message(message, @selected) - elsif (SlackApi::User.user_api).include?(@selected) - # SlackApi::user .method for sending message to user + url = "https://slack.com/api/chat.postMessage" + key = ENV["SLACK_API_TOKEN"] + + if @channels.include?(@selected) || @users.include?(@selected) + response = HTTParty.post( + url, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + body: { + token: key, + text: message, + channel: @selected["id"], + }, + ) + if response["ok"] + return true + else + raise SlackApi::SlackError, "Error when posting message to #{@selected["name"]}, error: #{response["error"]}" + end else - error_message = "You have not selected a user or channel yet." + error_message = "\n~You have not selected a user or channel yet.~" return error_message end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 23a6a7b3..65a2ed0e 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -94,4 +94,35 @@ end end end + + describe "send_message" do + it "can send a valid message to a channel" do + VCR.use_cassette("slack_message") do + workspace = SlackApi::Workspace.new + workspace.select_channel("general") + return_value = workspace.send_message("This is a message!") + + expect(return_value).must_equal true + end + end + + it "returns error message if no recipient has been selected yet" do + VCR.use_cassette("slack_message") do + workspace = SlackApi::Workspace.new + return_value = workspace.send_message("This is a message!") + + expect(return_value).must_include "You have not selected a user or channel yet." + end + end + + it "will raise an error if given an empty message" do + VCR.use_cassette("slack_message") do + workspace = SlackApi::Workspace.new + workspace.select_channel("general") + expect { + workspace.send_message("") + }.must_raise SlackApi::SlackError + end + end + end end From 2d6a4f592a5018da78dbce201143fd5005c23844 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Fri, 22 Mar 2019 13:31:11 -0700 Subject: [PATCH 23/27] added tests for listing users and channels --- lib/channel.rb | 5 +---- specs/channel_spec.rb | 16 ++++++++++++---- specs/test_helper.rb | 4 +++- specs/user_spec.rb | 28 ++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 18516c21..769fc5f7 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -13,14 +13,11 @@ def initialize(raw_channel) def self.channel_api url = "https://slack.com/api/channels.list" key = ENV["SLACK_API_TOKEN"] - parameters = {'token': key} + parameters = { 'token': key } response = HTTParty.get(url, query: parameters).to_s response = JSON.parse(response) return response["channels"] - # return response["channels"].map do |channel| - # self.new(channel) - # end end def self.list(channels_list) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e284e2a6..a21546e8 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,9 +1,9 @@ require_relative "test_helper" describe SlackApi::Channel do - describe "list channels" do + describe "json channels" do it "returns valid channels" do - VCR.use_cassette("slack_channels_list") do + VCR.use_cassette("slack_channels_json") do channels_list = SlackApi::Channel.channel_api channels_list.each do |channel| @@ -13,7 +13,7 @@ end it "return includes a specific channel" do - VCR.use_cassette("slack_channels_list") do + VCR.use_cassette("slack_channels_json") do channels_list = SlackApi::Channel.channel_api expect(channels_list.first["name"]).must_equal "general" @@ -21,7 +21,7 @@ end it "channels list will only include existent channels " do - VCR.use_cassette("slack_channels_list") do + VCR.use_cassette("slack_channels_json") do channels_list = SlackApi::Channel.channel_api channels_list.each do |channel| @@ -38,6 +38,14 @@ # end # end end + describe "list channels" do + it "returns an array" do + VCR.use_cassette("slack_channels_list") do + channels_list = SlackApi::Channel.channel_api + expect(SlackApi::Channel.list(channels_list)).must_be_instance_of Array + end + end + end end #0. make sure that what's being returned is in the format we want (i.e. a hash or array) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 314b9610..24c06e39 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,5 +1,7 @@ require "simplecov" -SimpleCov.start +SimpleCov.start do + add_filter %r{^/specs?/} +end require "dotenv" Dotenv.load diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 00367e82..224a9d61 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,9 +1,9 @@ require_relative "test_helper" describe SlackApi::User do - describe "list users" do + describe "json users" do it "returns valid users" do - VCR.use_cassette("slack_users_list") do + VCR.use_cassette("slack_users_json") do users_list = SlackApi::User.user_api users_list.each do |user| @@ -13,7 +13,7 @@ end it "includes a specific user" do - VCR.use_cassette("slack_user_list") do + VCR.use_cassette("slack_users_json") do users_list = SlackApi::User.user_api expect(users_list.first["name"]).must_equal "slackbot" @@ -21,7 +21,7 @@ end it "users list will only include existent users" do - VCR.use_cassette("slack_users_list") do + VCR.use_cassette("slack_users_json") do users_list = SlackApi::User.user_api users_list.each do |user| @@ -38,6 +38,26 @@ # end # end end + + describe "list users" do + it "contains slackbot as a user" do + skip + VCR.use_cassette("slack_users_list") do + users_list = SlackApi::User.user_api + # users_list.each do |user| + + # end + expect(SlackApi::User.list(users_list)).must_include "slackbot" + end + end + + it "returns an array" do + VCR.use_cassette("slack_users_list") do + users_list = SlackApi::User.user_api + expect(SlackApi::User.list(users_list)).must_be_instance_of Array + end + end + end end # users: From e429dfc222006d1d0c18407a3b1865446588ff6d Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Fri, 22 Mar 2019 14:29:50 -0700 Subject: [PATCH 24/27] added tests to user and channel. updated CLI to get rid of extra puts --- lib/slack.rb | 2 +- specs/channel_spec.rb | 37 +++++++++++++++++++++++------------- specs/user_spec.rb | 44 ++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 0cb19ad9..02eeda2b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -44,7 +44,7 @@ def main puts "\n(This will send to the recipient you have selected)" print "What message would you like to send? " user_message = gets.chomp - puts workspace.send_message(user_message) + workspace.send_message(user_message) end puts "\nWhat would you like to do next?" diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index a21546e8..f9151f5f 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -20,6 +20,30 @@ end end + it "return includes a specific channel" do + VCR.use_cassette("slack_channels_json") do + channels_list = SlackApi::Channel.channel_api + + expect(channels_list.first["id"]).must_equal "CH2RC3CNQ" + end + end + + it "return includes a specific channel" do + VCR.use_cassette("slack_channels_json") do + channels_list = SlackApi::Channel.channel_api + + expect(channels_list.first["topic"]["value"]).must_equal "Company-wide announcements and work-based matters" + end + end + + it "return includes a specific channel" do + VCR.use_cassette("slack_channels_json") do + channels_list = SlackApi::Channel.channel_api + + expect(channels_list.first["num_members"]).must_equal 2 + end + end + it "channels list will only include existent channels " do VCR.use_cassette("slack_channels_json") do channels_list = SlackApi::Channel.channel_api @@ -47,16 +71,3 @@ end end end - -#0. make sure that what's being returned is in the format we want (i.e. a hash or array) - -#1. make sure that what is returned are actual channels -# expect(channels.list) includes "general" - -#2. make sure that the right things are being returned for each users -# expect { one component of generals's user info is } channel name -# expect { one component of generals's user info is } slack_id -# expect { one component of generals's user info is } topic -# expect { one component of generals's user info is } user count - -#3. expect if there are NO channels -> return empty array diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 224a9d61..610c1af5 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -12,14 +12,30 @@ end end - it "includes a specific user" do - VCR.use_cassette("slack_users_json") do + it "includes a specific username" do + VCR.use_cassette("slack_user_list") do users_list = SlackApi::User.user_api expect(users_list.first["name"]).must_equal "slackbot" end end + it "includes a specific user's real name" do + VCR.use_cassette("slack_user_list") do + users_list = SlackApi::User.user_api + + expect(users_list.first["real_name"]).must_equal "Slackbot" + end + end + + it "includes a specific user's slack id" do + VCR.use_cassette("slack_user_list") do + users_list = SlackApi::User.user_api + + expect(users_list.first["id"]).must_equal "USLACKBOT" + end + end + it "users list will only include existent users" do VCR.use_cassette("slack_users_json") do users_list = SlackApi::User.user_api @@ -40,17 +56,6 @@ end describe "list users" do - it "contains slackbot as a user" do - skip - VCR.use_cassette("slack_users_list") do - users_list = SlackApi::User.user_api - # users_list.each do |user| - - # end - expect(SlackApi::User.list(users_list)).must_include "slackbot" - end - end - it "returns an array" do VCR.use_cassette("slack_users_list") do users_list = SlackApi::User.user_api @@ -59,16 +64,3 @@ end end end - -# users: - -#tests --> - -#0. make sure that what's being returned is in the format we want (i.e. a hash or array) - -#2. make sure that the right things are being returned for each users -# expect { one component of Amy's user info is } real name -# expect { one component of Amy's user info is } slack_id -# expect { one component of Amy's user info is } username - -#3. expect if there are NO users -> return empty array From 8e24b975c9b5d890e440b474134c796da73e6ccd Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Fri, 22 Mar 2019 14:56:00 -0700 Subject: [PATCH 25/27] added API testing --- lib/channel.rb | 13 +++++++++---- lib/slack.rb | 5 +---- lib/user.rb | 15 ++++++++++----- lib/workspace.rb | 6 +++--- specs/channel_spec.rb | 43 ++++++++++++++++++++++++++++++------------- specs/user_spec.rb | 43 +++++++++++++++++++++++++++++++------------ 6 files changed, 84 insertions(+), 41 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 769fc5f7..30876281 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -10,14 +10,19 @@ class Channel #< Recipient def initialize(raw_channel) end - def self.channel_api - url = "https://slack.com/api/channels.list" - key = ENV["SLACK_API_TOKEN"] + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + + def self.channel_api(url, key) parameters = { 'token': key } response = HTTParty.get(url, query: parameters).to_s response = JSON.parse(response) - return response["channels"] + if response["ok"] == true + return response["channels"] + else + raise SlackApi::SlackError, "Error with Channel API: #{response["error"]}" + end end def self.list(channels_list) diff --git a/lib/slack.rb b/lib/slack.rb index 02eeda2b..192148ec 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,5 @@ #!/usr/bin/env ruby -# require "dotenv" -# require "httparty" + require_relative "user" require_relative "channel" require_relative "workspace" @@ -8,8 +7,6 @@ Dotenv.load def main - # channels_list = SlackApi::Channel.channel_api - # users_list = SlackApi::User.user_api workspace = SlackApi::Workspace.new user_options = "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" diff --git a/lib/user.rb b/lib/user.rb index 53f8e575..2644b1cb 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -7,14 +7,19 @@ module SlackApi class SlackError < StandardError; end class User #< Recipient - def self.user_api - url = "https://slack.com/api/users.list" - key = ENV["SLACK_API_TOKEN"] - parameters = {'token': key} + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + def self.user_api(url, key) + parameters = { 'token': key } response = HTTParty.get(url, query: parameters).to_s response = JSON.parse(response) - return response["members"] + if response["ok"] == true + return response["members"] + else + raise SlackApi::SlackError, "Error with User API: #{response["error"]}" + end end def self.list(users_list) diff --git a/lib/workspace.rb b/lib/workspace.rb index d4cb3217..0ad3c6cc 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -13,8 +13,8 @@ class Workspace attr_reader :channels, :users def initialize - @users = SlackApi::User.user_api - @channels = SlackApi::Channel.channel_api + @users = SlackApi::User.user_api("https://slack.com/api/users.list", ENV["SLACK_API_TOKEN"]) + @channels = SlackApi::Channel.channel_api("https://slack.com/api/channels.list", ENV["SLACK_API_TOKEN"]) @selected = "" end @@ -60,7 +60,7 @@ def send_message(message) if @channels.include?(@selected) || @users.include?(@selected) response = HTTParty.post( url, - headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + headers: { "Content-Type" => "application/x-www-form-urlencoded" }, body: { token: key, text: message, diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index f9151f5f..d331438a 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -4,7 +4,9 @@ describe "json channels" do it "returns valid channels" do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) channels_list.each do |channel| expect(channel["is_channel"]).must_equal true @@ -14,7 +16,9 @@ it "return includes a specific channel" do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) expect(channels_list.first["name"]).must_equal "general" end @@ -22,7 +26,9 @@ it "return includes a specific channel" do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) expect(channels_list.first["id"]).must_equal "CH2RC3CNQ" end @@ -30,7 +36,9 @@ it "return includes a specific channel" do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) expect(channels_list.first["topic"]["value"]).must_equal "Company-wide announcements and work-based matters" end @@ -38,7 +46,9 @@ it "return includes a specific channel" do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) expect(channels_list.first["num_members"]).must_equal 2 end @@ -46,7 +56,9 @@ it "channels list will only include existent channels " do VCR.use_cassette("slack_channels_json") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) channels_list.each do |channel| name = "Not a Channel" @@ -54,18 +66,23 @@ end end end - # it "returns info with a valid token" do - # VCR.use_cassette("slack_channels_list") do - # channels_list = SlackApi::Channel.channel_api - # expect(channels_list["ok"]).must_equal true - # end - # end + it "raises error when a bad API call is made" do + VCR.use_cassette("slack_channels_list") do + url = "https://slack.com/api/channels.list" + + expect { SlackApi::Channel.channel_api(url, "") }.must_raise SlackApi::SlackError + end + end end + describe "list channels" do it "returns an array" do VCR.use_cassette("slack_channels_list") do - channels_list = SlackApi::Channel.channel_api + url = "https://slack.com/api/channels.list" + key = ENV["SLACK_API_TOKEN"] + channels_list = SlackApi::Channel.channel_api(url, key) + expect(SlackApi::Channel.list(channels_list)).must_be_instance_of Array end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 610c1af5..e0452003 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,7 +4,10 @@ describe "json users" do it "returns valid users" do VCR.use_cassette("slack_users_json") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) users_list.each do |user| expect(user["team_id"]).wont_be_nil @@ -14,7 +17,10 @@ it "includes a specific username" do VCR.use_cassette("slack_user_list") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) expect(users_list.first["name"]).must_equal "slackbot" end @@ -22,7 +28,10 @@ it "includes a specific user's real name" do VCR.use_cassette("slack_user_list") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) expect(users_list.first["real_name"]).must_equal "Slackbot" end @@ -30,7 +39,10 @@ it "includes a specific user's slack id" do VCR.use_cassette("slack_user_list") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) expect(users_list.first["id"]).must_equal "USLACKBOT" end @@ -38,7 +50,10 @@ it "users list will only include existent users" do VCR.use_cassette("slack_users_json") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) users_list.each do |user| name = "Not a User" @@ -46,19 +61,23 @@ end end end - # it "returns info with a valid token" do - # VCR.use_cassette("slack_channels_list") do - # channels_list = SlackApi::Channel.user_api - # expect(users_list["ok"]).must_equal true - # end - # end + it "raises error when a bad API call is made" do + VCR.use_cassette("slack_users_list") do + url = "https://slack.com/api/users.list" + + expect { SlackApi::User.user_api(url, "") }.must_raise SlackApi::SlackError + end + end end describe "list users" do it "returns an array" do VCR.use_cassette("slack_users_list") do - users_list = SlackApi::User.user_api + url = "https://slack.com/api/users.list" + key = ENV["SLACK_API_TOKEN"] + + users_list = SlackApi::User.user_api(url, key) expect(SlackApi::User.list(users_list)).must_be_instance_of Array end end From 20a9e5faf360c2c53cd61962a0b2b6ea92a074fb Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Fri, 22 Mar 2019 14:57:44 -0700 Subject: [PATCH 26/27] project file clean up --- lib/recipient.rb | 15 --------------- specs/recipient_spec.rb | 3 --- 2 files changed, 18 deletions(-) delete mode 100644 lib/recipient.rb delete mode 100644 specs/recipient_spec.rb diff --git a/lib/recipient.rb b/lib/recipient.rb deleted file mode 100644 index 06b17aac..00000000 --- a/lib/recipient.rb +++ /dev/null @@ -1,15 +0,0 @@ -class Recipient - def initialize - @name = name - @slack_id = slack_id - end - - def self.list - # return all instances of the recipient (either Users or Channels) - end - - def self.get(url, parameters) - # call the API to return information - response = HTTParty.get(url, query: parameters) - end -end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb deleted file mode 100644 index 70a5de70..00000000 --- a/specs/recipient_spec.rb +++ /dev/null @@ -1,3 +0,0 @@ -# tests: - -# should have a slack_id and name From 4f0b1b915628e9f320d8c8c4a4052e25f579f1b9 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Fri, 22 Mar 2019 15:01:20 -0700 Subject: [PATCH 27/27] additional project clean up --- lib/channel.rb | 5 +---- lib/user.rb | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 30876281..f85cfb4f 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -6,10 +6,7 @@ module SlackApi class SlackError < StandardError; end - class Channel #< Recipient - def initialize(raw_channel) - end - + class Channel url = "https://slack.com/api/channels.list" key = ENV["SLACK_API_TOKEN"] diff --git a/lib/user.rb b/lib/user.rb index 2644b1cb..d0c68fd9 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -6,7 +6,7 @@ module SlackApi class SlackError < StandardError; end - class User #< Recipient + class User url = "https://slack.com/api/users.list" key = ENV["SLACK_API_TOKEN"]