From 4f365ad592d99b0f97aff416e79808642f4a09e2 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 19 Mar 2019 14:20:01 -0700 Subject: [PATCH 01/37] Tried out API call to channels.list API. Added VCR to test_helper. Created classes Channel, User and Workspace. Added Initialize method in Channel and wrote a corresponding test. --- lib/channel.rb | 30 ++++++++++++++++++++++++++++++ lib/slack.rb | 11 ----------- lib/user.rb | 0 lib/workspace.rb | 33 +++++++++++++++++++++++++++++++++ specs/channel_spec.rb | 15 +++++++++++++++ specs/test_helper.rb | 30 +++++++++++++++++++++--------- specs/user_spec.rb | 1 + specs/workspace_spec.rb | 1 + 8 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 lib/channel.rb delete mode 100755 lib/slack.rb create mode 100644 lib/user.rb create mode 100755 lib/workspace.rb create mode 100644 specs/channel_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..973b8dfc --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,30 @@ +require "HTTParty" +require "awesome_print" + +module Slack + class Channel + class SlackError < StandardError; end + + CHANNEL_URL = "https://slack.com/api/channels.list" + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + + attr_reader :channel_id, :channel_name, :topic, :member_count + attr_accessor :channel_name, :topic + + def initialize(channel_id, channel_name, topic) + @channel_id = channel_id + @channel_name = channel_name + @topic = topic + @member_count = member_count + end # initialize + + # def self.list + # response = HTTParty(CHANNEL_URL, query: query_parameters) + # ap ENV["SLACK_API_TOKEN"] + + # if(response.code == 200) + # end # self.list + end # class +end # module diff --git a/lib/slack.rb b/lib/slack.rb deleted file mode 100755 index 960cf2f7..00000000 --- a/lib/slack.rb +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env ruby - -def main - puts "Welcome to the Ada Slack CLI!" - - # TODO project - - puts "Thank you for using the Ada Slack CLI" -end - -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100755 index 00000000..3acdcf13 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,33 @@ +require "httparty" +require "dotenv" +require "awesome_print" +require "pry" + +Dotenv.load + +BASE_URL = "https://slack.com/api/users.list" +query_parameters = { + token: ENV["SLACK_API_TOKEN"], +} + +response = HTTParty.get(BASE_URL, query: query_parameters) +ap ENV["SLACK_API_TOKEN"] + +ap response +if (response.code == 200) + response["members"].each do |user| + puts user["name"] + end +else + puts "Error #{response.code} : #{response["message"]}" +end + +def main + puts "Welcome to the Ada Slack CLI!" + + # TODO project + + puts "Thank you for using the Ada Slack CLI" +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..101c434c --- /dev/null +++ b/specs/channel_spec.rb @@ -0,0 +1,15 @@ +require_relative "test_helper" + +describe "Channel Initialize" do + before do + @new_channel = Slack::Channel.new( + 1, + "Ada_C11", + "api_discussion", + ) + end + + it "will create an instance of Channel" do + expect(@new_channel).must_be_instance_of Slack::Channel + end # it +end # outer describe diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..6c67a620 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,27 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'vcr' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "vcr" + +require_relative "../lib/user" +require_relative "../lib/channel" +require_relative "../lib/workspace" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock -end \ No newline at end of file + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } + # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..b5cae113 --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..b5cae113 --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" From fde573e5574c96edbcc8cec44e47038aa88eb495 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 19 Mar 2019 15:08:33 -0700 Subject: [PATCH 02/37] removed ap ENV token from method, just put it in to test if it works. --- lib/channel.rb | 29 ++++++++++++++++++++--------- lib/workspace.rb | 36 ++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 973b8dfc..adad86ba 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,14 +1,14 @@ require "HTTParty" +require "dotenv" require "awesome_print" module Slack class Channel + CHANNEL_URL = "https://slack.com/api/channels.list" + class SlackError < StandardError; end - CHANNEL_URL = "https://slack.com/api/channels.list" - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } + Dotenv.load attr_reader :channel_id, :channel_name, :topic, :member_count attr_accessor :channel_name, :topic @@ -20,11 +20,22 @@ def initialize(channel_id, channel_name, topic) @member_count = member_count end # initialize - # def self.list - # response = HTTParty(CHANNEL_URL, query: query_parameters) - # ap ENV["SLACK_API_TOKEN"] + def self.list + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(CHANNEL_URL, query: query_parameters) + ENV["SLACK_API_TOKEN"] + + if (response.code == 200) + response["channels"].map do |channel| + channel["name"] + end # each + else + puts "Error #{response.code} : #{response["message"]}" + end # else + end # self.list - # if(response.code == 200) - # end # self.list + ap self.list end # class end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index 3acdcf13..aeb85288 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -3,24 +3,24 @@ require "awesome_print" require "pry" -Dotenv.load - -BASE_URL = "https://slack.com/api/users.list" -query_parameters = { - token: ENV["SLACK_API_TOKEN"], -} - -response = HTTParty.get(BASE_URL, query: query_parameters) -ap ENV["SLACK_API_TOKEN"] - -ap response -if (response.code == 200) - response["members"].each do |user| - puts user["name"] - end -else - puts "Error #{response.code} : #{response["message"]}" -end +# Dotenv.load + +# BASE_URL = "https://slack.com/api/users.list" +# query_parameters = { +# token: ENV["SLACK_API_TOKEN"], +# } + +# response = HTTParty.get(BASE_URL, query: query_parameters) +# ap ENV["SLACK_API_TOKEN"] + +# ap response +# if (response.code == 200) +# response["members"].each do |user| +# puts user["name"] +# end +# else +# puts "Error #{response.code} : #{response["message"]}" +# end def main puts "Welcome to the Ada Slack CLI!" From e1223edfb49e2c4bdaa0b95bb3086877085cedb4 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 19 Mar 2019 15:28:41 -0700 Subject: [PATCH 03/37] Finished writing the self.list method for Channel class. --- lib/channel.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index adad86ba..22c818df 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -27,13 +27,20 @@ def self.list response = HTTParty.get(CHANNEL_URL, query: query_parameters) ENV["SLACK_API_TOKEN"] + slack_channels = {} if (response.code == 200) - response["channels"].map do |channel| - channel["name"] - end # each + # response["channels"].map do |channel| + # channel["name"] + # end # each + passes = response["channels"].map do |channel| + slack_channels[channel] = { "name" => channel["name"], + "members" => channel["members"], + "topic" => channel["topic"] } + end else puts "Error #{response.code} : #{response["message"]}" end # else + return passes end # self.list ap self.list From 0d44ca4039be08104abf78ec9c9d416a54630793 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 19 Mar 2019 16:08:05 -0700 Subject: [PATCH 04/37] Added self.list method to user.rb --- lib/channel.rb | 12 +++++------ lib/user.rb | 47 +++++++++++++++++++++++++++++++++++++++++++ specs/channel_spec.rb | 6 ++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 22c818df..3342dd2f 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -20,7 +20,7 @@ def initialize(channel_id, channel_name, topic) @member_count = member_count end # initialize - def self.list + def self.channel_list query_parameters = { token: ENV["SLACK_API_TOKEN"], } @@ -29,12 +29,9 @@ def self.list slack_channels = {} if (response.code == 200) - # response["channels"].map do |channel| - # channel["name"] - # end # each passes = response["channels"].map do |channel| slack_channels[channel] = { "name" => channel["name"], - "members" => channel["members"], + "members" => channel["members"].length, "topic" => channel["topic"] } end else @@ -43,6 +40,9 @@ def self.list return passes end # self.list - ap self.list + def details + end + + ap self.channel_list end # class end # module diff --git a/lib/user.rb b/lib/user.rb index e69de29b..358a66bf 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -0,0 +1,47 @@ +require "HTTParty" +require "dotenv" +require "awesome_print" + +module Slack + class User + USER_URL = "https://slack.com/api/users.list" + + class SlackError < StandardError; end + + Dotenv.load + + attr_reader :username, :real_name, :slack_id + + def initialize(username, real_name, slack_id) + @username = username + @real_name = real_name + @slack_id = slack_id + end # initialize + + def self.user_list + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(USER_URL, query: query_parameters) + ENV["SLACK_API_TOKEN"] + + slack_users = {} + if (response.code == 200) + ap response + user_passes = response["members"].map do |user| + slack_users[user] = { "slack_id" => user["id"], + "username" => user["name"], + "real name" => user["real_name"] } + end + else + puts "Error #{response.code} : #{response["message"]}" + end # else + return user_passes + end # self.list + + def details + end + + ap self.user_list + end # class +end # module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 101c434c..f9e6fe65 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -12,4 +12,10 @@ it "will create an instance of Channel" do expect(@new_channel).must_be_instance_of Slack::Channel end # it + + describe "list channels method" do + it "can list all of the channels" do + VCR.use_cassette("") + end + end end # outer describe From d36f0d77031becfc9e1094722655c4bce60cddc6 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 10:12:58 -0700 Subject: [PATCH 05/37] Added second test to chanel_spec.rb. It's failing --- specs/channel_spec.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index f9e6fe65..802c7939 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -15,7 +15,20 @@ describe "list channels method" do it "can list all of the channels" do - VCR.use_cassette("") - end + VCR.use_cassette("list_channels") do + expect(Slack::Channel.channel_list).wont_be_nil + expect(Slack::Channel.channel_list).must_be_kind_of Array + expect(Slack::Channel.channel_list.length).must_equal 2 + end # VCR + end # it + + it "can tell how many members are in channels" do + VCR.use_cassette("list_channels") do + slack_channels = @new_channel + expect(slack_channels.channel_list["channels"]["members"]).must_be_kind_of Array + + # expect(Slack::Channel.channel_list["members"].length).must_equal 2 + end # VCR + end # it end end # outer describe From d4ddbabb78925370ee70a06a33f50a948a1469ee Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 13:56:52 -0700 Subject: [PATCH 06/37] Modified list_channels method in channel.rb --- lib/channel.rb | 14 ++++++++--- specs/channel_spec.rb | 58 +++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 3342dd2f..34eaa9cf 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -20,7 +20,7 @@ def initialize(channel_id, channel_name, topic) @member_count = member_count end # initialize - def self.channel_list + def self.channel_api_data query_parameters = { token: ENV["SLACK_API_TOKEN"], } @@ -40,9 +40,17 @@ def self.channel_list return passes end # self.list - def details + def self.list_channels + channel_response = self.channel_api_data + + channel_names = channel_response.map do |channel| + channel["name"] + end + + return channel_names end - ap self.channel_list + # ap self.channel_api_data + ap self.list_channels end # class end # module diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 802c7939..55786a0b 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,34 +1,34 @@ -require_relative "test_helper" +# require_relative "test_helper" -describe "Channel Initialize" do - before do - @new_channel = Slack::Channel.new( - 1, - "Ada_C11", - "api_discussion", - ) - end +# describe "Channel Initialize" do +# before do +# @new_channel = Slack::Channel.new( +# 1, +# "Ada_C11", +# "api_discussion", +# ) +# end - it "will create an instance of Channel" do - expect(@new_channel).must_be_instance_of Slack::Channel - end # it +# it "will create an instance of Channel" do +# expect(@new_channel).must_be_instance_of Slack::Channel +# end # it - describe "list channels method" do - it "can list all of the channels" do - VCR.use_cassette("list_channels") do - expect(Slack::Channel.channel_list).wont_be_nil - expect(Slack::Channel.channel_list).must_be_kind_of Array - expect(Slack::Channel.channel_list.length).must_equal 2 - end # VCR - end # it +# describe "list channels method" do +# it "can list all of the channels" do +# VCR.use_cassette("list_channels") do +# expect(Slack::Channel.channel_list).wont_be_nil +# expect(Slack::Channel.channel_list).must_be_kind_of Array +# expect(Slack::Channel.channel_list.length).must_equal 2 +# end # VCR +# end # it - it "can tell how many members are in channels" do - VCR.use_cassette("list_channels") do - slack_channels = @new_channel - expect(slack_channels.channel_list["channels"]["members"]).must_be_kind_of Array +# it "can tell how many members are in channels" do +# VCR.use_cassette("list_channels") do +# slack_channels = @new_channel +# expect(@new_channel.channel_list["channels"]["members"]).must_be_kind_of Array - # expect(Slack::Channel.channel_list["members"].length).must_equal 2 - end # VCR - end # it - end -end # outer describe +# # expect(Slack::Channel.channel_list["members"].length).must_equal 2 +# end # VCR +# end # it +# end +# end # outer describe From 9d667d01f1947978e94f134f91b4fffa904d8408 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 14:27:34 -0700 Subject: [PATCH 07/37] Officially done with list_users and list_channels --- lib/channel.rb | 12 +----------- lib/user.rb | 9 ++------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 34eaa9cf..575b6bb8 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -20,7 +20,7 @@ def initialize(channel_id, channel_name, topic) @member_count = member_count end # initialize - def self.channel_api_data + def self.list_channels query_parameters = { token: ENV["SLACK_API_TOKEN"], } @@ -40,16 +40,6 @@ def self.channel_api_data return passes end # self.list - def self.list_channels - channel_response = self.channel_api_data - - channel_names = channel_response.map do |channel| - channel["name"] - end - - return channel_names - end - # ap self.channel_api_data ap self.list_channels end # class diff --git a/lib/user.rb b/lib/user.rb index 358a66bf..f127199c 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -18,7 +18,7 @@ def initialize(username, real_name, slack_id) @slack_id = slack_id end # initialize - def self.user_list + def self.list_users query_parameters = { token: ENV["SLACK_API_TOKEN"], } @@ -27,7 +27,6 @@ def self.user_list slack_users = {} if (response.code == 200) - ap response user_passes = response["members"].map do |user| slack_users[user] = { "slack_id" => user["id"], "username" => user["name"], @@ -36,12 +35,8 @@ def self.user_list else puts "Error #{response.code} : #{response["message"]}" end # else - return user_passes - end # self.list - - def details end - ap self.user_list + ap self.list_users end # class end # module From e5e14b924ad1863c9a94f207ac1f0df8e53f05c7 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 14:38:58 -0700 Subject: [PATCH 08/37] Added list in workspace --- lib/channel.rb | 2 +- lib/user.rb | 2 +- lib/workspace.rb | 23 ++++------------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 575b6bb8..7be809e7 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -41,6 +41,6 @@ def self.list_channels end # self.list # ap self.channel_api_data - ap self.list_channels + # ap self.list_channels end # class end # module diff --git a/lib/user.rb b/lib/user.rb index f127199c..573c10c1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -37,6 +37,6 @@ def self.list_users end # else end - ap self.list_users + # ap self.list_users end # class end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index aeb85288..10baf02c 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -2,30 +2,15 @@ require "dotenv" require "awesome_print" require "pry" - -# Dotenv.load - -# BASE_URL = "https://slack.com/api/users.list" -# query_parameters = { -# token: ENV["SLACK_API_TOKEN"], -# } - -# response = HTTParty.get(BASE_URL, query: query_parameters) -# ap ENV["SLACK_API_TOKEN"] - -# ap response -# if (response.code == 200) -# response["members"].each do |user| -# puts user["name"] -# end -# else -# puts "Error #{response.code} : #{response["message"]}" -# end +require_relative "channel" +require_relative "user" def main puts "Welcome to the Ada Slack CLI!" # TODO project + puts "This workspace has #{Slack::Channel.list_channels.length} channels." + puts "This workspace has #{Slack::User.list_users.length} users." puts "Thank you for using the Ada Slack CLI" end From 8e9df3434c510851dfda373da5a372f58a4a434d Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 14:51:30 -0700 Subject: [PATCH 09/37] Added menu and quit to workspace.rb --- lib/workspace.rb | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 10baf02c..8b1316b2 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,9 +9,39 @@ def main puts "Welcome to the Ada Slack CLI!" # TODO project - puts "This workspace has #{Slack::Channel.list_channels.length} channels." - puts "This workspace has #{Slack::User.list_users.length} users." + puts "\t\t\This workspace has #{Slack::Channel.list_channels.length} channels." + puts "\t\t\This workspace has #{Slack::User.list_users.length} users." + + loop do + puts "\n\t\t\.:Menu:. + |1|::List Channels + |2|::Channel Details + |3|::List Users + |4|::User Details + |5|::Select Channel + |6|::Select User + |7|::Quit" + + answer = gets.chomp.to_i + + # List Stuff + case answer + + when 1 + ap Slack::Channel.list_channels + when 2 + when 3 + ap Slack::User.list_users + when 4 + when 5 + when 6 + when 7 + break + else + puts "Please select from Menu Items." + end + end puts "Thank you for using the Ada Slack CLI" end From f67d3d7d7558ffce155f5bb8bbb67e297262daa2 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 14:58:19 -0700 Subject: [PATCH 10/37] Modiefied menu to remove extra information --- lib/workspace.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 8b1316b2..37127bd3 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -16,12 +16,8 @@ def main loop do puts "\n\t\t\.:Menu:. |1|::List Channels - |2|::Channel Details - |3|::List Users - |4|::User Details - |5|::Select Channel - |6|::Select User - |7|::Quit" + |2|::List Users + |3|::Quit" answer = gets.chomp.to_i From 36a799a771403aa87bc0ecdd54784f8ff116b681 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 15:53:49 -0700 Subject: [PATCH 11/37] Added select_user method --- lib/channel.rb | 34 ++++++++++++++++++++++++++++------ lib/user.rb | 38 ++++++++++++++++++++++++++++++-------- lib/workspace.rb | 20 +++++++++++++++++--- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 7be809e7..c461f401 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -13,12 +13,12 @@ class SlackError < StandardError; end attr_reader :channel_id, :channel_name, :topic, :member_count attr_accessor :channel_name, :topic - def initialize(channel_id, channel_name, topic) - @channel_id = channel_id - @channel_name = channel_name - @topic = topic - @member_count = member_count - end # initialize + # def initialize(channel_id, channel_name, topic) + # @channel_id = channel_id + # @channel_name = channel_name + # @topic = topic + # @member_count = member_count + # end # initialize def self.list_channels query_parameters = { @@ -40,6 +40,28 @@ def self.list_channels return passes end # self.list + # def self.select_channel(id) + # chosen_channel = "" + + # query_parameters = { + # token: ENV["SLACK_API_TOKEN"], + # } + # response = HTTParty.get(CHANNEL_URL, query: query_parameters) + + # response["channels"].each do |channel| + # if channel["id"] == id + # chosen_channel = id + # elsif channel["name"] == id + # chosen_channel = id + # end + + # if chosen_channel == "" + # raise SlackError, "channel must have an id or a name." + # end # end + # end # each + # return chosen_channel + # end # self.select_channel + # ap self.channel_api_data # ap self.list_channels end # class diff --git a/lib/user.rb b/lib/user.rb index 573c10c1..a2206381 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,13 +10,13 @@ class SlackError < StandardError; end Dotenv.load - attr_reader :username, :real_name, :slack_id + # attr_reader :username, :real_name, :slack_id - def initialize(username, real_name, slack_id) - @username = username - @real_name = real_name - @slack_id = slack_id - end # initialize + # def initialize(username, real_name, slack_id) + # @username = username + # @real_name = real_name + # @slack_id = slack_id + # end # initialize def self.list_users query_parameters = { @@ -30,12 +30,34 @@ def self.list_users user_passes = response["members"].map do |user| slack_users[user] = { "slack_id" => user["id"], "username" => user["name"], - "real name" => user["real_name"] } + "realname" => user["real_name"] } end else puts "Error #{response.code} : #{response["message"]}" end # else - end + return slack_users + end # end + + def self.select_user(id) + chosen_one = "" + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(USER_URL, query: query_parameters) + + response["members"].each do |member| + if member["name"] == id + chosen_one = id + elsif member["real_name"] == id + chosen_one = id + end + end # each + + if chosen_one == "" + raise SlackError, "User must have an id or a name." + end # end + return chosen_one + end # self.select_user # ap self.list_users end # class diff --git a/lib/workspace.rb b/lib/workspace.rb index 37127bd3..2201813a 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -17,8 +17,10 @@ def main puts "\n\t\t\.:Menu:. |1|::List Channels |2|::List Users - |3|::Quit" - + |3|::Select User + |4|::Select Channel + |7|::Quit" + puts "\nPlease select from the Menu:" answer = gets.chomp.to_i # List Stuff @@ -27,9 +29,21 @@ def main when 1 ap Slack::Channel.list_channels when 2 - when 3 ap Slack::User.list_users + when 3 + puts "Please enter a username or real name:" + id = gets.chomp + ap Slack::User.select_user(id) + + @new_user = Slack::User.new + ap @new_user.list_users when 4 + # puts "Please enter a channel name or channel id:" + # id = gets.chomp + # ap Slack::Channel.select_channel(id) + + # @new_channel = Slack::Channel.new + # ap @new_channel.list_channels when 5 when 6 when 7 From 1a9e52577a77a0b2889532c8b4f0575b5ef5ad9b Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 20 Mar 2019 17:43:22 -0700 Subject: [PATCH 12/37] Fixed select_user and select_channel methods in User.rb and Channel.rb --- lib/channel.rb | 36 ++++++++++++++++++------------------ lib/user.rb | 8 ++++---- lib/workspace.rb | 26 ++++++++++++++++---------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c461f401..dd71aec2 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -40,27 +40,27 @@ def self.list_channels return passes end # self.list - # def self.select_channel(id) - # chosen_channel = "" + def select_channel(id) + chosen_channel = "" - # query_parameters = { - # token: ENV["SLACK_API_TOKEN"], - # } - # response = HTTParty.get(CHANNEL_URL, query: query_parameters) + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(CHANNEL_URL, query: query_parameters) - # response["channels"].each do |channel| - # if channel["id"] == id - # chosen_channel = id - # elsif channel["name"] == id - # chosen_channel = id - # end + response["channels"].each do |channel| + if channel["id"] == id + chosen_channel = id + elsif channel["name"] == id + chosen_channel = id + end - # if chosen_channel == "" - # raise SlackError, "channel must have an id or a name." - # end # end - # end # each - # return chosen_channel - # end # self.select_channel + if chosen_channel == "" + raise SlackError, "channel must have an id or a name." + end # end + end # each + return chosen_channel + end # self.select_channel # ap self.channel_api_data # ap self.list_channels diff --git a/lib/user.rb b/lib/user.rb index a2206381..9626d6d5 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -33,18 +33,18 @@ def self.list_users "realname" => user["real_name"] } end else - puts "Error #{response.code} : #{response["message"]}" + raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" end # else return slack_users end # end - def self.select_user(id) - chosen_one = "" + def select_user(id) query_parameters = { token: ENV["SLACK_API_TOKEN"], } response = HTTParty.get(USER_URL, query: query_parameters) + chosen_one = "" response["members"].each do |member| if member["name"] == id chosen_one = id @@ -54,7 +54,7 @@ def self.select_user(id) end # each if chosen_one == "" - raise SlackError, "User must have an id or a name." + raise Slack::SlackError, "User must have an id or a name." end # end return chosen_one end # self.select_user diff --git a/lib/workspace.rb b/lib/workspace.rb index 2201813a..94579229 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -27,21 +27,27 @@ def main case answer when 1 - ap Slack::Channel.list_channels + puts Slack::Channel.list_channels + puts "Anything else?" + answer = gets.chomp when 2 - ap Slack::User.list_users + puts Slack::User.list_users + puts "Anything else?" + answer = gets.chomp when 3 puts "Please enter a username or real name:" id = gets.chomp - ap Slack::User.select_user(id) - - @new_user = Slack::User.new - ap @new_user.list_users + user = Slack::User.new + chosen_one = user.select_user(id) + ap chosen_one + # @new_user = Slack::User.new + # ap @new_user.list_users when 4 - # puts "Please enter a channel name or channel id:" - # id = gets.chomp - # ap Slack::Channel.select_channel(id) - + puts "Please enter a channel name or channel id:" + id = gets.chomp + channel = Slack::Channel.new + chosen_channel = channel.select_channel(id) + ap chosen_channel # @new_channel = Slack::Channel.new # ap @new_channel.list_channels when 5 From 6dd2a5c1b8a8e0e753d2c59a57971370e973bc75 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Wed, 20 Mar 2019 22:58:23 -0700 Subject: [PATCH 13/37] added pseudocode for channel spec tests --- lib/channel.rb | 36 ++++++++++++++++++++----- lib/user.rb | 40 +++++++++++++++++++++------- lib/workspace.rb | 30 +++++++++++++++++---- specs/channel_spec.rb | 62 ++++++++++++++++++++++++++++++------------- specs/user_spec.rb | 16 +++++++++++ 5 files changed, 144 insertions(+), 40 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 7be809e7..e9f5835c 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -13,12 +13,12 @@ class SlackError < StandardError; end attr_reader :channel_id, :channel_name, :topic, :member_count attr_accessor :channel_name, :topic - def initialize(channel_id, channel_name, topic) - @channel_id = channel_id - @channel_name = channel_name - @topic = topic - @member_count = member_count - end # initialize + # def initialize(channel_id, channel_name, topic) + # @channel_id = channel_id + # @channel_name = channel_name + # @topic = topic + # @member_count = member_count + # end # initialize def self.list_channels query_parameters = { @@ -40,7 +40,29 @@ def self.list_channels return passes end # self.list + def select_channel(id) + chosen_channel = "" + + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(CHANNEL_URL, query: query_parameters) + + response["channels"].each do |channel| + if channel["id"] == id + chosen_channel = id + elsif channel["name"] == id + chosen_channel = id + end + + if chosen_channel == "" + raise SlackError, "channel must have an id or a name." + end # end + end # each + return chosen_channel + end # self.select_channel + # ap self.channel_api_data # ap self.list_channels end # class -end # module +end # module \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index 573c10c1..9626d6d5 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,13 +10,13 @@ class SlackError < StandardError; end Dotenv.load - attr_reader :username, :real_name, :slack_id + # attr_reader :username, :real_name, :slack_id - def initialize(username, real_name, slack_id) - @username = username - @real_name = real_name - @slack_id = slack_id - end # initialize + # def initialize(username, real_name, slack_id) + # @username = username + # @real_name = real_name + # @slack_id = slack_id + # end # initialize def self.list_users query_parameters = { @@ -30,12 +30,34 @@ def self.list_users user_passes = response["members"].map do |user| slack_users[user] = { "slack_id" => user["id"], "username" => user["name"], - "real name" => user["real_name"] } + "realname" => user["real_name"] } end else - puts "Error #{response.code} : #{response["message"]}" + raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" end # else - end + return slack_users + end # end + + def select_user(id) + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(USER_URL, query: query_parameters) + + chosen_one = "" + response["members"].each do |member| + if member["name"] == id + chosen_one = id + elsif member["real_name"] == id + chosen_one = id + end + end # each + + if chosen_one == "" + raise Slack::SlackError, "User must have an id or a name." + end # end + return chosen_one + end # self.select_user # ap self.list_users end # class diff --git a/lib/workspace.rb b/lib/workspace.rb index 37127bd3..eb24e1a0 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -17,19 +17,39 @@ def main puts "\n\t\t\.:Menu:. |1|::List Channels |2|::List Users - |3|::Quit" - + |3|::Select User + |4|::Select Channel + |7|::Quit" + puts "\nPlease select from the Menu:" answer = gets.chomp.to_i # List Stuff case answer when 1 - ap Slack::Channel.list_channels + puts Slack::Channel.list_channels + puts "Anything else?" + answer = gets.chomp when 2 + puts Slack::User.list_users + puts "Anything else?" + answer = gets.chomp when 3 - ap Slack::User.list_users + puts "Please enter a username or real name:" + id = gets.chomp + user = Slack::User.new + chosen_one = user.select_user(id) + ap chosen_one + # @new_user = Slack::User.new + # ap @new_user.list_users when 4 + puts "Please enter a channel name or channel id:" + id = gets.chomp + channel = Slack::Channel.new + chosen_channel = channel.select_channel(id) + ap chosen_channel + # @new_channel = Slack::Channel.new + # ap @new_channel.list_channels when 5 when 6 when 7 @@ -41,4 +61,4 @@ def main puts "Thank you for using the Ada Slack CLI" end -main if __FILE__ == $PROGRAM_NAME +main if __FILE__ == $PROGRAM_NAME \ No newline at end of file diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 55786a0b..e85b07a2 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,6 +1,6 @@ -# require_relative "test_helper" +require_relative "test_helper" -# describe "Channel Initialize" do +describe "Channel Initialize" do # before do # @new_channel = Slack::Channel.new( # 1, @@ -13,22 +13,46 @@ # expect(@new_channel).must_be_instance_of Slack::Channel # end # it -# describe "list channels method" do -# it "can list all of the channels" do -# VCR.use_cassette("list_channels") do -# expect(Slack::Channel.channel_list).wont_be_nil -# expect(Slack::Channel.channel_list).must_be_kind_of Array -# expect(Slack::Channel.channel_list.length).must_equal 2 -# end # VCR -# end # it + describe "list channels method" do + it "can list all of the channels" do + VCR.use_cassette("list_channels") do + expect(Slack::Channel.channel_list).wont_be_nil + # expect that the member count will be equal to two + # expect that one of the keys will include random + # expect that the number of channels will be 2 (Or is it 3? lol) + end # VCR + end # it + end -# it "can tell how many members are in channels" do -# VCR.use_cassette("list_channels") do -# slack_channels = @new_channel -# expect(@new_channel.channel_list["channels"]["members"]).must_be_kind_of Array + describe "select channel method" do + it "selects channel when user inputs the name of the channel" do + # create new instance of channel, or can we use before do? + # VCR for the select channel method + # use response and set it equal to the select channel method with channel name as a parameter + # expect that response must equal whatever the channel name is + end -# # expect(Slack::Channel.channel_list["members"].length).must_equal 2 -# end # VCR -# end # it -# end -# end # outer describe + + it "selects the channel when user inputs id" do + # create a new instance of channel + # VCR for the select channel method + # use response and set it equal to the select channel method with an id as a parameter + # expect that response must equal whater the id is + end + + it "raises and error if the inputted channel name is not valid" do + # expect that a fake channel name must raise our slack error + end + + end + + describe "show details method" do + it "displays the details for the currently selected channel" do + # create new channel instance + # set variable equal to selected channel + # set variable equal to channel details + # expect that it's a hash + # expect that the channel contains what it is supposed to + end + end +end # outer describe diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b5cae113..9e2782d8 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1 +1,17 @@ require_relative "test_helper" + +describe Slack do + describe "self.list_users" do + it "lists all of the users" do + VCR.use_cassette("list_users") do + response = Slack::User.list_users + + expect(response.length).must_equal(3) + expect(response).wont_be_nil + expect(response.keys.include?()).must_equal(true) + + end + + end + +end \ No newline at end of file From 6bbc0ce7a5e5001e7822cbfc5d92ed4679467830 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Wed, 20 Mar 2019 23:10:15 -0700 Subject: [PATCH 14/37] added pseudocode for user spec --- specs/user_spec.rb | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 9e2782d8..23776bc2 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,17 +1,52 @@ require_relative "test_helper" describe Slack do - describe "self.list_users" do + describe "list users method" do it "lists all of the users" do VCR.use_cassette("list_users") do response = Slack::User.list_users - expect(response.length).must_equal(3) - expect(response).wont_be_nil - expect(response.keys.include?()).must_equal(true) - + # expect that it must have the right amount of users + # expect that keys must include usernames + # expect that it won't be nil + end end end + describe "select users method" do + it "raises an error for bogus user names" do + # creates and new user instance + # VCR to select user + # expect that using the select user method on a fake username generates slack error + + end + + it "chooses the correct user when user name is inputted" do + # create a new user instance + # VCR for the select user method + # select the user using response (either steph or faiza's username) + # expect that the result should equal mine or faiza's username + end + + it "chooses the correct user when id is inputted" do + # create a new instance of user + # VCR for select user + # select user using an id key + # expect that the result should equal the id key we enetered + end + + end + + describe "show details method" do + it "displays the information of the currently selected user" do + # create a new user instance + # VCR to select user + # set variable equal to selected user + # set variable equal to user details + # expect that it's a hash + # expect that the user details contains what it is supposed to + end + + end end \ No newline at end of file From e359815568ba63ae5d87601b02e27e1c5a252466 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Wed, 20 Mar 2019 23:18:38 -0700 Subject: [PATCH 15/37] added pseudocode for show details methods --- lib/channel.rb | 11 +++++++++++ lib/user.rb | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/lib/channel.rb b/lib/channel.rb index e9f5835c..9746eb1e 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -62,7 +62,18 @@ def select_channel(id) return chosen_channel end # self.select_channel + def show_details(id) + # query token + # response + # create a hash and iterate over channel details. put result in hash and return it + end + # ap self.channel_api_data # ap self.list_channels + + # added another file for the recipient? + # make some conditionals dependant on response code? + + end # class end # module \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index 9626d6d5..49587fe1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -59,6 +59,12 @@ def select_user(id) return chosen_one end # self.select_user + def show_details(id) + # query token + # response + # create a hash and iterate over user details. put result in hash and return it + end + # ap self.list_users end # class end # module From 9238e5b7fa01db0f466f7df904a0445f5131fabf Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Thu, 21 Mar 2019 09:03:37 -0700 Subject: [PATCH 16/37] working on tests --- specs/channel_spec.rb | 24 ++++++++++++++---------- specs/user_spec.rb | 3 +++ specs/workspace_spec.rb | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e85b07a2..5120a75d 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -26,30 +26,34 @@ describe "select channel method" do it "selects channel when user inputs the name of the channel" do - # create new instance of channel, or can we use before do? - # VCR for the select channel method - # use response and set it equal to the select channel method with channel name as a parameter - # expect that response must equal whatever the channel name is + channel = Slack::Channel.new + VCR.use_cassette("select channel") do + response = channel.select_channel() + exepct(response).must_equal() end it "selects the channel when user inputs id" do - # create a new instance of channel - # VCR for the select channel method - # use response and set it equal to the select channel method with an id as a parameter - # expect that response must equal whater the id is + channel = Slack::Channel.new + VCR.use_cassette("select channel id") do + response = channel.select_channel() + expect(response).must_equal() end it "raises and error if the inputted channel name is not valid" do - # expect that a fake channel name must raise our slack error + expect{"bogusname"}.must_raise Slack::SlackError end end describe "show details method" do it "displays the details for the currently selected channel" do - # create new channel instance + channel = Slack.Channel.new + channel_selection = channel.select_channel() # set variable equal to selected channel + details = channel.show_details() + expect(details).must_be_instance_of(Hash) + expect() # set variable equal to channel details # expect that it's a hash # expect that the channel contains what it is supposed to diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 23776bc2..b558aa2a 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -16,6 +16,9 @@ describe "select users method" do it "raises an error for bogus user names" do + user = Slack::User.new + VCR.use_cassette("select user") do + expect(user.select_user("bogususer").must_raise # creates and new user instance # VCR to select user # expect that using the select user method on a fake username generates slack error diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b5cae113..720dbbf1 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1 +1,15 @@ require_relative "test_helper" + +describe "welcome screen" do + it "returns the correct amount of channels" do + channel = Slack::Channel.new + + end + + + + + + + +end \ No newline at end of file From 8ccb32d2db07b26adb72655c4176b8a0d135cf9f Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Thu, 21 Mar 2019 09:50:35 -0700 Subject: [PATCH 17/37] Modified the format of Menu slightly. --- lib/workspace.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 94579229..594b0cb5 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -10,16 +10,16 @@ def main # TODO project - puts "\t\t\This workspace has #{Slack::Channel.list_channels.length} channels." - puts "\t\t\This workspace has #{Slack::User.list_users.length} users." + puts "This workspace has #{Slack::Channel.list_channels.length} channels." + puts "This workspace has #{Slack::User.list_users.length} users." loop do - puts "\n\t\t\.:Menu:. - |1|::List Channels - |2|::List Users - |3|::Select User - |4|::Select Channel - |7|::Quit" + puts "\n\t\t~ MENU ~ + \t|1|::List Channels + \t|2|::List Users + \t|3|::Select User + \t|4|::Select Channel + \t|7|::Quit" puts "\nPlease select from the Menu:" answer = gets.chomp.to_i @@ -40,16 +40,12 @@ def main user = Slack::User.new chosen_one = user.select_user(id) ap chosen_one - # @new_user = Slack::User.new - # ap @new_user.list_users when 4 puts "Please enter a channel name or channel id:" id = gets.chomp channel = Slack::Channel.new chosen_channel = channel.select_channel(id) ap chosen_channel - # @new_channel = Slack::Channel.new - # ap @new_channel.list_channels when 5 when 6 when 7 From f7117687fc0e1d73bf43ee1bc7003a41c445bc64 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Thu, 21 Mar 2019 10:06:05 -0700 Subject: [PATCH 18/37] starting the show details method --- lib/channel.rb | 5 +++++ lib/user.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/channel.rb b/lib/channel.rb index 9746eb1e..4a6d2683 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -63,6 +63,11 @@ def select_channel(id) end # self.select_channel def show_details(id) + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(CHANNEL_URL, query: query_parameters) + # query token # response # create a hash and iterate over channel details. put result in hash and return it diff --git a/lib/user.rb b/lib/user.rb index 49587fe1..2c70db1c 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -60,6 +60,10 @@ def select_user(id) end # self.select_user def show_details(id) + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + response = HTTParty.get(USER_URL, query: query_parameters) # query token # response # create a hash and iterate over user details. put result in hash and return it From 589b2b4752798551f7cb3f54809839fddd2862c2 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Thu, 21 Mar 2019 16:57:51 -0700 Subject: [PATCH 19/37] Refactored list_all and select methods in User.rb and Channel.rb --- lib/channel.rb | 106 +++++++++++++++++++++++------------------------ lib/recipient.rb | 19 +++++++++ lib/user.rb | 93 +++++++++++++++++++++-------------------- lib/workspace.rb | 16 +++---- 4 files changed, 126 insertions(+), 108 deletions(-) create mode 100644 lib/recipient.rb diff --git a/lib/channel.rb b/lib/channel.rb index 4a6d2683..e1cd6701 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -11,74 +11,70 @@ class SlackError < StandardError; end Dotenv.load attr_reader :channel_id, :channel_name, :topic, :member_count - attr_accessor :channel_name, :topic - # def initialize(channel_id, channel_name, topic) - # @channel_id = channel_id - # @channel_name = channel_name - # @topic = topic - # @member_count = member_count - # end # initialize + def initialize(channel_id:, channel_name:, topic:) + @channel_id = channel_id.to_s + @channel_name = channel_name + @topic = topic + @member_count = member_count + end # initialize - def self.list_channels + def self.get_channel_data query_parameters = { token: ENV["SLACK_API_TOKEN"], } - response = HTTParty.get(CHANNEL_URL, query: query_parameters) - ENV["SLACK_API_TOKEN"] - - slack_channels = {} - if (response.code == 200) - passes = response["channels"].map do |channel| - slack_channels[channel] = { "name" => channel["name"], - "members" => channel["members"].length, - "topic" => channel["topic"] } - end - else - puts "Error #{response.code} : #{response["message"]}" - end # else - return passes - end # self.list - - def select_channel(id) - chosen_channel = "" - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } response = HTTParty.get(CHANNEL_URL, query: query_parameters) - response["channels"].each do |channel| - if channel["id"] == id - chosen_channel = id - elsif channel["name"] == id - chosen_channel = id - end + if response.code == 200 + channel_data = response["channels"].map do |channel| + { channel_id: channel["id"], + channel_name: channel["name"], + topic: channel["topic"], + member_count: channel["members"].length } + end # map + return channel_data + else + raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" + end # end + end - if chosen_channel == "" - raise SlackError, "channel must have an id or a name." - end # end - end # each - return chosen_channel - end # self.select_channel + # ap self.get_channel_data - def show_details(id) - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - response = HTTParty.get(CHANNEL_URL, query: query_parameters) + def self.list_channels + channels = get_channel_data - # query token - # response - # create a hash and iterate over channel details. put result in hash and return it + return channels end - # ap self.channel_api_data - # ap self.list_channels + ap self.list_channels + + def self.select_channel(name_or_id) + channel_data = get_channel_data + + chosen_channel = nil + + channel_data.each do |channel| + if channel[:channel_id] == name_or_id + chosen_channel = Slack::Channel.new( + channel_id: channel[:channel_id], + channel_name: channel[:channel_name], + topic: channel[:topic], + ) + elsif channel[:channel_name] == name_or_id + chosen_channel = Slack::Channel.new( + channel_id: channel[:channel_id], + channel_name: channel[:channel_name], + topic: channel[:topic], + ) + end + end - # added another file for the recipient? - # make some conditionals dependant on response code? + if chosen_channel == nil + raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" + end - + return chosen_channel + end end # class -end # module \ No newline at end of file +end # module diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..b56c3795 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,19 @@ +require "httparty" +require "dotenv" +require "awesome_print" +require "pry" +require_relative "channel" +require_relative "user" + +module Slack + class Recipient + class SlackError < StandardError; end + + attr_reader :slack_id, :name + + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end # initialize + end # class +end # module diff --git a/lib/user.rb b/lib/user.rb index 2c70db1c..61e80b4d 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,65 +10,68 @@ class SlackError < StandardError; end Dotenv.load - # attr_reader :username, :real_name, :slack_id + attr_reader :username, :real_name, :slack_id - # def initialize(username, real_name, slack_id) - # @username = username - # @real_name = real_name - # @slack_id = slack_id - # end # initialize + def initialize(username:, real_name:, slack_id:) + @username = username + @real_name = real_name + @slack_id = slack_id + end # initialize - def self.list_users + def self.get_user_data query_parameters = { token: ENV["SLACK_API_TOKEN"], } + response = HTTParty.get(USER_URL, query: query_parameters) - ENV["SLACK_API_TOKEN"] - - slack_users = {} - if (response.code == 200) - user_passes = response["members"].map do |user| - slack_users[user] = { "slack_id" => user["id"], - "username" => user["name"], - "realname" => user["real_name"] } + + if response.code == 200 + user_data = response["members"].map do |user| + { username: user["name"], + real_name: user["real_name"], + slack_id: user["id"] } end + return user_data else raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" - end # else - return slack_users - end # end + end # if + end - def select_user(id) - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - response = HTTParty.get(USER_URL, query: query_parameters) + # ap self.get_user_data + + def self.list_users + users = get_user_data - chosen_one = "" - response["members"].each do |member| - if member["name"] == id - chosen_one = id - elsif member["real_name"] == id - chosen_one = id + return users + end + + ap self.list_users + + def self.select_user(name_or_id) + user_data = get_user_data + chosen_user = nil + + user_data.each do |user| + if user[:username] == name_or_id + chosen_user = Slack::User.new( + username: user[:username], + real_name: user[:real_name], + slack_id: user[:slack_id], + ) + elsif user[:real_name] == name_or_id + chosen_user = Slack::User.new( + username: user[:username], + real_name: user[:real_name], + slack_id: user[:slack_id], + ) end - end # each + end - if chosen_one == "" - raise Slack::SlackError, "User must have an id or a name." - end # end - return chosen_one - end # self.select_user + if chosen_user == nil + raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" + end - def show_details(id) - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - response = HTTParty.get(USER_URL, query: query_parameters) - # query token - # response - # create a hash and iterate over user details. put result in hash and return it + return chosen_user end - - # ap self.list_users end # class end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index 001b69f6..831545b1 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -36,15 +36,15 @@ def main answer = gets.chomp when 3 puts "Please enter a username or real name:" - id = gets.chomp - user = Slack::User.new - chosen_one = user.select_user(id) - ap chosen_one + name_or_id = gets.chomp + + chosen_user = Slack::User.select_user(name_or_id) + ap chosen_user when 4 puts "Please enter a channel name or channel id:" - id = gets.chomp - channel = Slack::Channel.new - chosen_channel = channel.select_channel(id) + name_or_id = gets.chomp + # channel = Slack::Channel.new(@channel_id, @channel_name, @topic) + chosen_channel = Slack::Channel.select_channel(name_or_id) ap chosen_channel when 5 when 6 @@ -57,4 +57,4 @@ def main 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 3bb46ac787d733579b316c48b7024b145b5529eb Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Thu, 21 Mar 2019 17:56:06 -0700 Subject: [PATCH 20/37] Added show_details method to Channel. --- lib/channel.rb | 15 ++++++++++++--- lib/workspace.rb | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index e1cd6701..c5ccd197 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -10,7 +10,7 @@ class SlackError < StandardError; end Dotenv.load - attr_reader :channel_id, :channel_name, :topic, :member_count + attr_reader :channel_id, :channel_name, :topic, :member_count, :purpose def initialize(channel_id:, channel_name:, topic:) @channel_id = channel_id.to_s @@ -47,8 +47,6 @@ def self.list_channels return channels end - ap self.list_channels - def self.select_channel(name_or_id) channel_data = get_channel_data @@ -76,5 +74,16 @@ def self.select_channel(name_or_id) return chosen_channel end + + def show_details(name_or_id) + channel_details = Channel.select_channel(name_or_id) + + details = { + "Name" => channel_details.channel_name, + "Topic" => channel_details.topic["value"], + } + # details = channel_details.purpose + return details + end end # class end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index 831545b1..e74d5272 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -40,12 +40,28 @@ def main chosen_user = Slack::User.select_user(name_or_id) ap chosen_user + + puts "Show additional details? (Y/N)" + choice = gets.chomp.downcase + + if choice == "y" + puts "no method yet" + # call show_details method and puts ap it + end when 4 puts "Please enter a channel name or channel id:" name_or_id = gets.chomp - # channel = Slack::Channel.new(@channel_id, @channel_name, @topic) chosen_channel = Slack::Channel.select_channel(name_or_id) - ap chosen_channel + + puts "Show additional details for #{name_or_id}? (Y/N)" + choice = gets.chomp.downcase + + if choice == "y" + ap chosen_channel.show_details(name_or_id) + end + # ap details + # call show_details method and puts ap it + when 5 when 6 when 7 From 6a7acfa65950b79ba1374e94357cca4abab14180 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Thu, 21 Mar 2019 18:39:26 -0700 Subject: [PATCH 21/37] added show details for user --- lib/user.rb | 14 +++++++++++++- lib/workspace.rb | 7 +++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 61e80b4d..68ea4413 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -45,7 +45,7 @@ def self.list_users return users end - ap self.list_users + # ap self.list_users def self.select_user(name_or_id) user_data = get_user_data @@ -73,5 +73,17 @@ def self.select_user(name_or_id) return chosen_user end + + + def show_details(name_or_id) + user_details = User.select_user(name_or_id) + + details = { + "Username" => user_details.username, + "Real Name" => user_details.real_name, + "Slack ID" => user_details.slack_id + } + return details + end end # class end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index e74d5272..11a59b85 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,7 +1,6 @@ require "httparty" require "dotenv" -require "awesome_print" -require "pry" +require "awesome_print" require_relative "channel" require_relative "user" @@ -41,11 +40,11 @@ def main chosen_user = Slack::User.select_user(name_or_id) ap chosen_user - puts "Show additional details? (Y/N)" + puts "Show additional details for #{name_or_id}? (Y/N)" choice = gets.chomp.downcase if choice == "y" - puts "no method yet" + ap chosen_user.show_details(name_or_id) # call show_details method and puts ap it end when 4 From 41511e5f6ad6560f051f9c06188ed83c77a11d5e Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 10:56:09 -0700 Subject: [PATCH 22/37] refactored everythinggggg --- lib/channel.rb | 183 +++++++++++++++++++++++----------------- lib/recipient.rb | 48 +++++++++-- lib/slack_cli.rb | 73 ++++++++++++++++ lib/user.rb | 188 ++++++++++++++++++++++++------------------ lib/workspace.rb | 115 ++++++++++++-------------- specs/channel_spec.rb | 61 -------------- specs/test_helper.rb | 4 + specs/user_spec.rb | 54 ------------ 8 files changed, 385 insertions(+), 341 deletions(-) create mode 100644 lib/slack_cli.rb diff --git a/lib/channel.rb b/lib/channel.rb index c5ccd197..0cb5cff0 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,89 +1,118 @@ require "HTTParty" -require "dotenv" require "awesome_print" +require_relative "recipient" module Slack - class Channel - CHANNEL_URL = "https://slack.com/api/channels.list" - - class SlackError < StandardError; end - - Dotenv.load - - attr_reader :channel_id, :channel_name, :topic, :member_count, :purpose - - def initialize(channel_id:, channel_name:, topic:) - @channel_id = channel_id.to_s - @channel_name = channel_name + class Channel < Recipient + BASE_URL = "https://slack.com/api/channels.list" + PARAMETERS = {token: ENV["SLACK_API_TOKEN"]} + + attr_reader :topic, :member_count + + def initialize(name, slack_id, topic, member_count) + super(name, slack_id) + @name = name @topic = topic @member_count = member_count - end # initialize - - def self.get_channel_data - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(CHANNEL_URL, query: query_parameters) - - if response.code == 200 - channel_data = response["channels"].map do |channel| - { channel_id: channel["id"], - channel_name: channel["name"], - topic: channel["topic"], - member_count: channel["members"].length } - end # map - return channel_data - else - raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" - end # end - end - - # ap self.get_channel_data - - def self.list_channels - channels = get_channel_data - - return channels end + + def self.list + channel_data = self.get(BASE_URL, PARAMETERS) - def self.select_channel(name_or_id) - channel_data = get_channel_data - - chosen_channel = nil - - channel_data.each do |channel| - if channel[:channel_id] == name_or_id - chosen_channel = Slack::Channel.new( - channel_id: channel[:channel_id], - channel_name: channel[:channel_name], - topic: channel[:topic], - ) - elsif channel[:channel_name] == name_or_id - chosen_channel = Slack::Channel.new( - channel_id: channel[:channel_id], - channel_name: channel[:channel_name], - topic: channel[:topic], - ) - end - end - - if chosen_channel == nil - raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" + channel_data["channels"].map do |channel| + self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["members"].length) end - - return chosen_channel end - def show_details(name_or_id) - channel_details = Channel.select_channel(name_or_id) - - details = { - "Name" => channel_details.channel_name, - "Topic" => channel_details.topic["value"], - } - # details = channel_details.purpose - return details + def show_details + super.push("topic", "member_count") end - end # class -end # module + end +end + + +# module Slack +# class Channel +# CHANNEL_URL = "https://slack.com/api/channels.list" + +# class SlackError < StandardError; end + +# Dotenv.load + +# attr_reader :channel_id, :channel_name, :topic, :member_count, :purpose + +# def initialize(channel_id:, channel_name:, topic:) +# @channel_id = channel_id.to_s +# @channel_name = channel_name +# @topic = topic +# @member_count = member_count +# end # initialize + +# def self.get_channel_data +# query_parameters = { +# token: ENV["SLACK_API_TOKEN"], +# } + +# response = HTTParty.get(CHANNEL_URL, query: query_parameters) + +# if response.code == 200 +# channel_data = response["channels"].map do |channel| +# { channel_id: channel["id"], +# channel_name: channel["name"], +# topic: channel["topic"], +# member_count: channel["members"].length } +# end # map +# return channel_data +# else +# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" +# end # end +# end + +# # ap self.get_channel_data + +# def self.list_channels +# channels = get_channel_data + +# return channels +# end + +# def self.select_channel(name_or_id) +# channel_data = get_channel_data + +# chosen_channel = nil + +# channel_data.each do |channel| +# if channel[:channel_id] == name_or_id +# chosen_channel = Slack::Channel.new( +# channel_id: channel[:channel_id], +# channel_name: channel[:channel_name], +# topic: channel[:topic], +# ) +# elsif channel[:channel_name] == name_or_id +# chosen_channel = Slack::Channel.new( +# channel_id: channel[:channel_id], +# channel_name: channel[:channel_name], +# topic: channel[:topic], +# ) +# end +# end + +# if chosen_channel == nil +# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" +# end + +# return chosen_channel +# end + +# def show_details(name_or_id) +# channel_details = Channel.select_channel(name_or_id) + +# details = { +# "Name" => channel_details.channel_name, +# "Topic" => channel_details.topic["value"], +# } +# # details = channel_details.purpose +# return details +# end +# end # class +# end # module diff --git a/lib/recipient.rb b/lib/recipient.rb index b56c3795..6642c447 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,19 +1,53 @@ require "httparty" -require "dotenv" require "awesome_print" -require "pry" -require_relative "channel" -require_relative "user" +require "dotenv" +Dotenv.load module Slack + class ResponseError < StandardError; end class Recipient - class SlackError < StandardError; end - + BASE_URL = "https://slack.com/api/chat.postMessage" + attr_reader :slack_id, :name def initialize(slack_id, name) @slack_id = slack_id @name = name - end # initialize + # raise error here if name isn't a string + end + + def self.get(base_url, parameters) + response = HTTParty.get(base_url, query: parameters) + # unless response.code == 200 && response.parsed_response["ok"] + # raise SlackError, response["error"] + # end + + return response + end + + def send_message(recipient, message) + message_request = HTTParty.post("#{BASE_URL}chat.postMessage", + headers: {"Content-Type" => "application/x-www0form-urlencoded"}, + body: { + token: ENV["SLACK_API_TOKEN"], + text: message, + channel: recipient + }) + + + if response["ok"] == false + raise SlackAPI::SlackError, "There was an error sending #{message} to #{recipient}. #{message_request["error"]}" + else + return true + end + end + + def self.list + raise NotImplementedError, "Implement me in a child class!" + end + + def details + ["name", "slack_id"] + end end # class end # module diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb new file mode 100644 index 00000000..c1bb75df --- /dev/null +++ b/lib/slack_cli.rb @@ -0,0 +1,73 @@ +require "awesome_print" +require_relative "workspace" +Dotenv.load + +def main + puts "Welcome to the Ada Slack CLI!" + workspace = Slack::Workspace.new + # TODO project + + puts "This workspace has #{Slack::Channel.list.length} channels." + puts "This workspace has #{Slack::User.list.length} users." + + loop do + puts "\n\t\t~ MENU ~ + \t|1|::List Channels + \t|2|::List Users + \t|3|::Select User + \t|4|::Select Channel + \t|5|::Send Message + \t|6|::Quit" + puts "\nPlease select from the Menu:" + answer = gets.chomp.to_i + + # List Stuff + case answer + when 1 + ap Slack::Channel.list + when 2 + ap Slack::User.list + when 3 + puts "Please enter a SlackID or full name:" + name_or_id = gets.chomp + + puts "#{workspace.select_user(name_or_id)}" + # selection = workspace.select_user(name_or_id) + + puts "Show additional details for #{name_or_id}? (Y/N)" + choice = gets.chomp.downcase + + if choice == "y" + selection.show_details + # call show_details method and puts ap it + end + when 4 + puts "Please enter a channel name or channel id:" + selection = gets.chomp + + workspace = Slack::Workspace.new + recipient = workspace.select_channel(selection) + + puts "Show additional details for #{selection}? (Y/N)" + choice = gets.chomp.downcase + + if choice == "y" + recipient.show_details + end + # ap details + # call show_details method and puts ap it + when 5 + puts "What would you like to send to #{selection}?" + message = gets.chomp + recipient.send_message(recipient, message) + puts "Your message has been sent! Yay!" + when 6 + break + else + puts "Please select from Menu Items." + end + end + puts "Thank you for using the Ada Slack CLI" +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index 68ea4413..1e39e476 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,89 +1,119 @@ -require "HTTParty" -require "dotenv" require "awesome_print" +require_relative "recipient" -module Slack - class User - USER_URL = "https://slack.com/api/users.list" +module Slack + class User < Recipient + BASE_URL = "https://slack.com/api/users.list" + PARAMETER = {token: ENV["SLACK_API_TOKEN"]} - class SlackError < StandardError; end + attr_reader :real_name, :status_text - Dotenv.load - - attr_reader :username, :real_name, :slack_id - - def initialize(username:, real_name:, slack_id:) - @username = username - @real_name = real_name - @slack_id = slack_id - end # initialize - - def self.get_user_data - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(USER_URL, query: query_parameters) - - if response.code == 200 - user_data = response["members"].map do |user| - { username: user["name"], - real_name: user["real_name"], - slack_id: user["id"] } - end - return user_data - else - raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" - end # if - end - - # ap self.get_user_data - - def self.list_users - users = get_user_data - - return users + def initialize(name, slack_id, real_name) + super(name, slack_id) + @real_name = real_name end - # ap self.list_users - - def self.select_user(name_or_id) - user_data = get_user_data - chosen_user = nil - - user_data.each do |user| - if user[:username] == name_or_id - chosen_user = Slack::User.new( - username: user[:username], - real_name: user[:real_name], - slack_id: user[:slack_id], - ) - elsif user[:real_name] == name_or_id - chosen_user = Slack::User.new( - username: user[:username], - real_name: user[:real_name], - slack_id: user[:slack_id], - ) - end - end - - if chosen_user == nil - raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" + def self.list + user_data = self.get(BASE_URL, PARAMETER) + # user_data = user["members"].map do |user| + # { username: user["name"], + # real_name: user["real_name"], + # slack_id: user["id"] } + # end + # return user_data + user_data["members"].map do |user| + self.new(user["id"], user["name"], user["profile"]["real_name"]) end - - return chosen_user + end - - def show_details(name_or_id) - user_details = User.select_user(name_or_id) - - details = { - "Username" => user_details.username, - "Real Name" => user_details.real_name, - "Slack ID" => user_details.slack_id - } - return details + def details + super << "real_name" end - end # class -end # module + end +end + + + + + +# module Slack +# class User +# USER_URL = "https://slack.com/api/users.list" + +# class SlackError < StandardError; end + +# Dotenv.load + +# attr_reader :username, :real_name, :slack_id + +# def initialize(username:, real_name:, slack_id:) +# @username = username +# @real_name = real_name +# @slack_id = slack_id +# end # initialize + +# def self.get_user_data +# query_parameters = { +# token: ENV["SLACK_API_TOKEN"], +# } + +# response = HTTParty.get(USER_URL, query: query_parameters) + +# if response.code == 200 +# user_data = response["members"].map do |user| +# { username: user["name"], +# real_name: user["real_name"], +# slack_id: user["id"] } +# end +# return user_data +# else +# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" +# end # if +# end + +# def self.list_users +# users = get_user_data + +# return users +# end + +# def self.select_user(name_or_id) +# user_data = get_user_data +# chosen_user = nil + +# user_data.each do |user| +# if user[:username] == name_or_id +# chosen_user = Slack::User.new( +# username: user[:username], +# real_name: user[:real_name], +# slack_id: user[:slack_id], +# ) +# elsif user[:real_name] == name_or_id +# chosen_user = Slack::User.new( +# username: user[:username], +# real_name: user[:real_name], +# slack_id: user[:slack_id], +# ) +# end +# end + +# if chosen_user == nil +# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" +# end + +# return chosen_user +# end + +# def show_details(name_or_id) +# user_details = User.select_user(name_or_id) + +# details = { +# "Username" => user_details.username, +# "Real Name" => user_details.real_name, +# "Slack ID" => user_details.slack_id +# } +# return details +# end +# end # class +# end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index 11a59b85..865ea0a2 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,75 +1,64 @@ require "httparty" -require "dotenv" -require "awesome_print" -require_relative "channel" require_relative "user" +require_relative "channel" +require "awesome_print" +require "dotenv" -def main - puts "Welcome to the Ada Slack CLI!" - - # TODO project - - puts "This workspace has #{Slack::Channel.list_channels.length} channels." - puts "This workspace has #{Slack::User.list_users.length} users." - - loop do - puts "\n\t\t~ MENU ~ - \t|1|::List Channels - \t|2|::List Users - \t|3|::Select User - \t|4|::Select Channel - \t|7|::Quit" - puts "\nPlease select from the Menu:" - answer = gets.chomp.to_i - - # List Stuff - case answer +Dotenv.load - when 1 - puts Slack::Channel.list_channels - puts "Anything else?" - answer = gets.chomp - when 2 - puts Slack::User.list_users - puts "Anything else?" - answer = gets.chomp - when 3 - puts "Please enter a username or real name:" - name_or_id = gets.chomp +module Slack + class Workspace + BASE_URL = "https://slack.com/api/users.list" + attr_reader :users, :channels + attr_accessor :selection - chosen_user = Slack::User.select_user(name_or_id) - ap chosen_user + def initialize + @users = User.list + @channels = Channel.list + @selection = nil + end + + def select_user(name_or_id) + users.each do |user| + if name_or_id == user.name || name_or_id == user.slack_id || name_or_id == user.real_name + @selection = user + end + end + return @selection + # if @selection == nil + # return "Sorry, #{name_or_id} is not a valid user." + # end + end + + def select_channel(name_or_id) + channels.find do |channel| + if name_or_id == channel.name || name_or_id == channel.slack_id + @selection = channel + end + end + + if @selection == nil + return "Sorry, #{name_or_id} is not a valid channel." + end + end - puts "Show additional details for #{name_or_id}? (Y/N)" - choice = gets.chomp.downcase + def show_details + # if @selection == nil + # return false + ap @selection, @selection.details + # end + end - if choice == "y" - ap chosen_user.show_details(name_or_id) - # call show_details method and puts ap it + def send_message(text) + if @selection == nil + return false end - when 4 - puts "Please enter a channel name or channel id:" - name_or_id = gets.chomp - chosen_channel = Slack::Channel.select_channel(name_or_id) - - puts "Show additional details for #{name_or_id}? (Y/N)" - choice = gets.chomp.downcase - if choice == "y" - ap chosen_channel.show_details(name_or_id) + if text == "" + return nil end - # ap details - # call show_details method and puts ap it - when 5 - when 6 - when 7 - break - else - puts "Please select from Menu Items." - end + return @selection.send_message(@selection.slack_id, text) + end end - puts "Thank you for using the Ada Slack CLI" -end - -main if __FILE__ == $PROGRAM_NAME +end \ No newline at end of file diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 5120a75d..b5cae113 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,62 +1 @@ require_relative "test_helper" - -describe "Channel Initialize" do -# before do -# @new_channel = Slack::Channel.new( -# 1, -# "Ada_C11", -# "api_discussion", -# ) -# end - -# it "will create an instance of Channel" do -# expect(@new_channel).must_be_instance_of Slack::Channel -# end # it - - describe "list channels method" do - it "can list all of the channels" do - VCR.use_cassette("list_channels") do - expect(Slack::Channel.channel_list).wont_be_nil - # expect that the member count will be equal to two - # expect that one of the keys will include random - # expect that the number of channels will be 2 (Or is it 3? lol) - end # VCR - end # it - end - - describe "select channel method" do - it "selects channel when user inputs the name of the channel" do - channel = Slack::Channel.new - VCR.use_cassette("select channel") do - response = channel.select_channel() - exepct(response).must_equal() - end - - - it "selects the channel when user inputs id" do - channel = Slack::Channel.new - VCR.use_cassette("select channel id") do - response = channel.select_channel() - expect(response).must_equal() - end - - it "raises and error if the inputted channel name is not valid" do - expect{"bogusname"}.must_raise Slack::SlackError - end - - end - - describe "show details method" do - it "displays the details for the currently selected channel" do - channel = Slack.Channel.new - channel_selection = channel.select_channel() - # set variable equal to selected channel - details = channel.show_details() - expect(details).must_be_instance_of(Hash) - expect() - # set variable equal to channel details - # expect that it's a hash - # expect that the channel contains what it is supposed to - end - end -end # outer describe diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 6c67a620..9c665e83 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,6 +10,10 @@ require_relative "../lib/user" require_relative "../lib/channel" require_relative "../lib/workspace" +require_relative "../lib/recipient" +require_relative "../lib/slack_cli" + + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b558aa2a..b5cae113 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,55 +1 @@ require_relative "test_helper" - -describe Slack do - describe "list users method" do - it "lists all of the users" do - VCR.use_cassette("list_users") do - response = Slack::User.list_users - - # expect that it must have the right amount of users - # expect that keys must include usernames - # expect that it won't be nil - end - end - - end - - describe "select users method" do - it "raises an error for bogus user names" do - user = Slack::User.new - VCR.use_cassette("select user") do - expect(user.select_user("bogususer").must_raise - # creates and new user instance - # VCR to select user - # expect that using the select user method on a fake username generates slack error - - end - - it "chooses the correct user when user name is inputted" do - # create a new user instance - # VCR for the select user method - # select the user using response (either steph or faiza's username) - # expect that the result should equal mine or faiza's username - end - - it "chooses the correct user when id is inputted" do - # create a new instance of user - # VCR for select user - # select user using an id key - # expect that the result should equal the id key we enetered - end - - end - - describe "show details method" do - it "displays the information of the currently selected user" do - # create a new user instance - # VCR to select user - # set variable equal to selected user - # set variable equal to user details - # expect that it's a hash - # expect that the user details contains what it is supposed to - end - - end -end \ No newline at end of file From a30277bfa86cfa6086dc8b7b940ceea74284fb28 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 11:06:57 -0700 Subject: [PATCH 23/37] fixed show details method for user --- lib/slack_cli.rb | 2 +- lib/workspace.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index c1bb75df..bfa5e617 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -38,7 +38,7 @@ def main choice = gets.chomp.downcase if choice == "y" - selection.show_details + puts "#{workspace.show_details}" # call show_details method and puts ap it end when 4 diff --git a/lib/workspace.rb b/lib/workspace.rb index 865ea0a2..16ca240c 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -32,7 +32,7 @@ def select_user(name_or_id) def select_channel(name_or_id) channels.find do |channel| - if name_or_id == channel.name || name_or_id == channel.slack_id + if name_or_id == channel.name || name_or_id == channel.slack_id @selection = channel end end @@ -43,9 +43,10 @@ def select_channel(name_or_id) end def show_details + return @selection, @selection.details # if @selection == nil # return false - ap @selection, @selection.details + # ap @selection, @selection.details # end end From 3db37b5c6d231eb7d52a53a5ceb3d7bc1a8c495d Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 11:37:46 -0700 Subject: [PATCH 24/37] added test for recipient class --- lib/channel.rb | 5 ++--- lib/slack_cli.rb | 8 +++----- lib/workspace.rb | 11 ++++++----- specs/recipient_spec.rb | 17 +++++++++++++++++ specs/workspace_spec.rb | 7 ------- 5 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 specs/recipient_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb index 0cb5cff0..085a75cb 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,4 +1,3 @@ -require "HTTParty" require "awesome_print" require_relative "recipient" @@ -7,7 +6,7 @@ class Channel < Recipient BASE_URL = "https://slack.com/api/channels.list" PARAMETERS = {token: ENV["SLACK_API_TOKEN"]} - attr_reader :topic, :member_count + attr_reader :topic, :member_count def initialize(name, slack_id, topic, member_count) super(name, slack_id) @@ -24,7 +23,7 @@ def self.list end end - def show_details + def channel_details super.push("topic", "member_count") end end diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index bfa5e617..2d5d9971 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -43,16 +43,14 @@ def main end when 4 puts "Please enter a channel name or channel id:" - selection = gets.chomp + name_or_id = gets.chomp - workspace = Slack::Workspace.new - recipient = workspace.select_channel(selection) - puts "Show additional details for #{selection}? (Y/N)" + puts "Show additional details for #{name_or_id}? (Y/N)" choice = gets.chomp.downcase if choice == "y" - recipient.show_details + puts "#{workspace.show_details}" end # ap details # call show_details method and puts ap it diff --git a/lib/workspace.rb b/lib/workspace.rb index 16ca240c..1fbe62c0 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -31,19 +31,20 @@ def select_user(name_or_id) end def select_channel(name_or_id) - channels.find do |channel| + channels.each do |channel| if name_or_id == channel.name || name_or_id == channel.slack_id @selection = channel end end + return @selection - if @selection == nil - return "Sorry, #{name_or_id} is not a valid channel." - end + # if @selection == nil + # return "Sorry, #{name_or_id} is not a valid channel." + # end end def show_details - return @selection, @selection.details + return @selection, @selection.channel_details # if @selection == nil # return false # ap @selection, @selection.details diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..f9602e0b --- /dev/null +++ b/specs/recipient_spec.rb @@ -0,0 +1,17 @@ +require_relative "test_helper" + +describe "Recipient Class" do + describe "self list method" do + it "raises an error if not implemented in a subclass" do + expect{Slack::Recipient.list}.must_raise NotImplementedError + end + end + + describe "send message method" do + it "raises error if parameters are incorrect" do + + end + + end + +end \ No newline at end of file diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 720dbbf1..7a910216 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,15 +1,8 @@ require_relative "test_helper" -describe "welcome screen" do - it "returns the correct amount of channels" do - channel = Slack::Channel.new - - end - -end \ No newline at end of file From 788342886456187f787b64e60c62365bce74899a Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 11:57:51 -0700 Subject: [PATCH 25/37] finished user spec tests --- lib/recipient.rb | 2 +- lib/user.rb | 2 +- lib/workspace.rb | 13 ++++++----- specs/user_spec.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 6642c447..13c23697 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -36,7 +36,7 @@ def send_message(recipient, message) if response["ok"] == false - raise SlackAPI::SlackError, "There was an error sending #{message} to #{recipient}. #{message_request["error"]}" + raise ResponseError, "There was an error sending your message" else return true end diff --git a/lib/user.rb b/lib/user.rb index 1e39e476..aa575b25 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -24,7 +24,7 @@ def self.list user_data["members"].map do |user| self.new(user["id"], user["name"], user["profile"]["real_name"]) end - + end def details diff --git a/lib/workspace.rb b/lib/workspace.rb index 1fbe62c0..f7671651 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -7,6 +7,7 @@ Dotenv.load module Slack + class ResponseError < StandardError; end class Workspace BASE_URL = "https://slack.com/api/users.list" attr_reader :users, :channels @@ -25,9 +26,9 @@ def select_user(name_or_id) end end return @selection - # if @selection == nil - # return "Sorry, #{name_or_id} is not a valid user." - # end + if @selection == nil + return "Sorry, #{name_or_id} is not a valid user." + end end def select_channel(name_or_id) @@ -38,9 +39,9 @@ def select_channel(name_or_id) end return @selection - # if @selection == nil - # return "Sorry, #{name_or_id} is not a valid channel." - # end + if @selection == nil + return "Sorry, #{name_or_id} is not a valid channel." + end end def show_details diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b5cae113..ae6e7c59 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1 +1,57 @@ require_relative "test_helper" + +describe "User Class" do + describe "self get method" do + it "successfully connects to API" do + VCR.use_cassette("connect to API") do + url = "https://slack.com/api/users.list" + query = {token: ENV["SLACK_API_TOKEN"]} + request = Slack::User.get(url, query) + + expect(request["ok"]).must_equal(true) + end + end + end + + it "raises and exception if API call fails" do + VCR.use_cassette("api fail") do + url = "https://slack.com/api/users.list" + query = {token: "i love cats"} + + expect{Slack::User.get(url, query)}.must_raise(Slack::ResponseError) + end + + end + + describe "self list method" do + it "returns array with user instances" do + VCR.use_cassette("connect to api") do + user = Slack::User.list + + expect(user).must_be_kind_of(Array) + # check at index that its an instance of user + end + end + + it "correctly loads user information" do + VCR.use_cassette("connect to api") do + users = Slack::User.list + expect(users[1].name).must_equal "faiza.ahsan1222" + expect(users[1].real_name).must_equal "Faiza Husain" + expect(users[1].slack_id).must_equal "UH2NWFHTM" + expect(users.length).must_equal 3 + end + end + + describe "user details methods" do + it "returns an array with correct string values" do + user = Slack::User.new("I", "play", "bass") + expect(user.details).must_be_kind_of Array + expect(user.details[0]).must_equal "name" + expect(user.details[1]).must_equal "slack_id" + expect(user.details[2]).must_equal "real_name" + end + end + + end +end \ No newline at end of file From b18f5cd6b8842d30e025de443b120b9b5768d823 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 12:06:42 -0700 Subject: [PATCH 26/37] working on channel spec tests --- specs/channel_spec.rb | 50 +++++++++++++++++++++++++++++++++++++++++++ specs/user_spec.rb | 10 ++++----- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index b5cae113..b6a9a68b 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1 +1,51 @@ require_relative "test_helper" + +describe "Channels" do + # describe "self get method" do + # it "returns a response from api for channels list" do + # VCR.use_cassette("connect to channel api") do + # url = "http://slack.com/api/channels.list" + # query = {token: ENV["KEY"]} + # request = Slack::Channel.get(url, query) + + # expect(request["ok"]).must_equal(true) + # end + # end + # end + + # it "raises exception if get request fails" do + # VCR.use_cassette("connect to channel api") do + # url = "https://slack.com/api/channel.list" + # query = {token: "idontworkidontworkkkkkk"} + + # expect { + # Slack::Channel.get(url, query) + # }.must_raise Slack::ResponseError + # end + # end + + describe "self list method" do + it "returns channels array" do + VCR.use_cassette("connect to channel api") do + channels = Slack::Channel.list + expect(channels).must_be_kind_of Array + + (0..channels.length - 1).each do |i| + expect(channels[i]).must_be_kind_of Slack::Channel + end + end + + describe "channel details method" do + it "returns an array that contains correct strings" do + channel = Slack::Channel.new("i", "play", "electric", "bass") + expect(channel.details).must_be_kind_of Array + expect(channel.details[0]).must_equal "name" + expect(channel.details[1]).must_equal "slack_id" + expect(channel.details[2]).must_equal "topic" + expect(channel.details[3]).must_equal "member_count" + end + end + # # end end + end + end +end \ No newline at end of file diff --git a/specs/user_spec.rb b/specs/user_spec.rb index ae6e7c59..e2646fa4 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -3,7 +3,7 @@ describe "User Class" do describe "self get method" do it "successfully connects to API" do - VCR.use_cassette("connect to API") do + VCR.use_cassette("connect to user api") do url = "https://slack.com/api/users.list" query = {token: ENV["SLACK_API_TOKEN"]} request = Slack::User.get(url, query) @@ -25,7 +25,7 @@ describe "self list method" do it "returns array with user instances" do - VCR.use_cassette("connect to api") do + VCR.use_cassette("connect to user api") do user = Slack::User.list expect(user).must_be_kind_of(Array) @@ -34,7 +34,7 @@ end it "correctly loads user information" do - VCR.use_cassette("connect to api") do + VCR.use_cassette("connect to user api") do users = Slack::User.list expect(users[1].name).must_equal "faiza.ahsan1222" expect(users[1].real_name).must_equal "Faiza Husain" @@ -47,9 +47,9 @@ it "returns an array with correct string values" do user = Slack::User.new("I", "play", "bass") expect(user.details).must_be_kind_of Array - expect(user.details[0]).must_equal "name" - expect(user.details[1]).must_equal "slack_id" expect(user.details[2]).must_equal "real_name" + expect(user.details[1]).must_equal "slack_id" + expect(user.details[0]).must_equal "name" end end From e91d03adc6255a9d94bcf559e96448c0a88e5a5c Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 12:23:41 -0700 Subject: [PATCH 27/37] working on workspace test --- specs/workspace_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 7a910216..b4a1395f 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,6 +1,55 @@ require_relative "test_helper" +describe "Workspace Class" do + describe "initialize method" do + it "lists users and channels" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + expect(workspace.users).must_be_kind_of(Array) + expect(workspace.channels).must_be_kind_of(Array) + # (0..2).each do |i| + # expect(workspace.users[i]).must_be_kind_of(Slack::User) + # expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) + # end + end + end + end + describe "select user method" do + it "stores user inputted username in selection variable" do + VCR.use_cassette("workspace information") do + user_names = ["smarcha04", "faiza.ahsan1222", "slackbot"] + real_names = ["Stephanie Marchante", "Faiza Husain", "Slackbot"] + workspace = Slack::Workspace.new + + user_names.each_with_index do |user_name, i| + find_user = workspace.select_user(user_name) + selected_user = workspace.selection + expect(selected_user).must_be_kind_of Slack::User + expect(selected_user.real_name).must_equal "#{real_names[i]}" + end + end + end + + it "stores user inputted slackid in selection variable" do + VCR.use_cassette("workspace information") do + all_slack_ids = ["UH2NWFHTM", "UH2NX89A7", "USLACKBOT"] + all_real_names = ["Faiza Husain", "Stephanie Marchante","Slackbot"] + + workspace = Slack::Workspace.new + + all_slack_ids.each_with_index do |slackid, i| + user_search = workspace.select_user(slackid) + select_user = workspace.selection + expect(select_user).must_be_kind_of(Slack::User) + expect(select_user.real_name).must_equal "#{all_real_names[i]}" + end + end + + end + end + +end From b29733fa6ed3654f1e8c19fcc4b5f3773c866093 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 13:16:21 -0700 Subject: [PATCH 28/37] workspace select channel method --- lib/slack_cli.rb | 2 +- specs/workspace_spec.rb | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index 2d5d9971..0453a166 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -45,7 +45,7 @@ def main puts "Please enter a channel name or channel id:" name_or_id = gets.chomp - + puts "Show additional details for #{name_or_id}? (Y/N)" choice = gets.chomp.downcase diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b4a1395f..407a5694 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -45,8 +45,52 @@ expect(select_user.real_name).must_equal "#{all_real_names[i]}" end end + end + it "returns error message if user input slackid or user name are invalid" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + user_search = workspace.select_user("steph steph") + selected_user = workspace.selection + assert_nil(selected_user, msg = nil) + end end + + end + + describe "select channel method" do + it "finds a channel by name and stores it in the selected variable" do + VCR.use_cassette("workspace_information_find") do + channel_names = ["bassguitar", "general", "random"] + topics = ["All bass no treble", "Company-wide announcements and work-based matters", "Non-work banter and water coolerconversation"] + workspace = Slack::Workspace.new + + channel_names.each_with_index do |channel_name, i| + search_channel = workspace.select_channel(channel_name) + selected_channel = workspace.selection + # expect(selected_channel).must_be_kind_of Slack::Channel + # expect(selected_channel.topic).must_equal "#{topics[i]}" + end + end + end + + it "finds a channel by slack id and stores it in the selected variable" do + VCR.use_cassette("workspace_information_find") do + slack_ids = ["CH317B6EN", "CH408C1CP", "CH4AZQMJS"] + topics = ["All doggos all the time! :dog:", + "Company-wide announcements and work-based matters", + "Non-work banter and water cooler conversation"] + workspace = Slack::Workspace.new + + slack_ids.each_with_index do |slack_id, i| + find_channel = workspace.select_channel(slack_id) + selected_channel = workspace.selected + expect(selected_channel).must_be_kind_of Slack::Channel + expect(selected_channel.topic).must_equal "#{topics[i]}" + end + end + end + end end From 6f5a9e1da396c2d749d1b931cb4059ce9bb1ef87 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Fri, 22 Mar 2019 13:22:44 -0700 Subject: [PATCH 29/37] Fixed show details methiod for Channel --- lib/channel.rb | 15 +++++++-------- lib/slack_cli.rb | 5 +++-- lib/workspace.rb | 23 ++++++++++++----------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 085a75cb..dbb3dc34 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -4,9 +4,9 @@ module Slack class Channel < Recipient BASE_URL = "https://slack.com/api/channels.list" - PARAMETERS = {token: ENV["SLACK_API_TOKEN"]} - - attr_reader :topic, :member_count + PARAMETERS = { token: ENV["SLACK_API_TOKEN"] } + + attr_reader :topic, :member_count def initialize(name, slack_id, topic, member_count) super(name, slack_id) @@ -14,22 +14,21 @@ def initialize(name, slack_id, topic, member_count) @topic = topic @member_count = member_count end - + def self.list channel_data = self.get(BASE_URL, PARAMETERS) - + ap channel_data channel_data["channels"].map do |channel| - self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["members"].length) + self.new(channel["name"], channel["id"], channel["topic"]["value"], channel["members"].length) end end - def channel_details + def details super.push("topic", "member_count") end end end - # module Slack # class Channel # CHANNEL_URL = "https://slack.com/api/channels.list" diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index 2d5d9971..11f3a11d 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -1,4 +1,4 @@ -require "awesome_print" +require "awesome_print" require_relative "workspace" Dotenv.load @@ -31,7 +31,7 @@ def main puts "Please enter a SlackID or full name:" name_or_id = gets.chomp - puts "#{workspace.select_user(name_or_id)}" + workspace.select_user(name_or_id) # selection = workspace.select_user(name_or_id) puts "Show additional details for #{name_or_id}? (Y/N)" @@ -45,6 +45,7 @@ def main puts "Please enter a channel name or channel id:" name_or_id = gets.chomp + workspace.select_channel(name_or_id) puts "Show additional details for #{name_or_id}? (Y/N)" choice = gets.chomp.downcase diff --git a/lib/workspace.rb b/lib/workspace.rb index f7671651..7bdd6a00 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -8,6 +8,7 @@ module Slack class ResponseError < StandardError; end + class Workspace BASE_URL = "https://slack.com/api/users.list" attr_reader :users, :channels @@ -18,8 +19,8 @@ def initialize @channels = Channel.list @selection = nil end - - def select_user(name_or_id) + + def select_user(name_or_id) users.each do |user| if name_or_id == user.name || name_or_id == user.slack_id || name_or_id == user.real_name @selection = user @@ -30,27 +31,27 @@ def select_user(name_or_id) return "Sorry, #{name_or_id} is not a valid user." end end - + def select_channel(name_or_id) channels.each do |channel| - if name_or_id == channel.name || name_or_id == channel.slack_id + if name_or_id == channel.name || name_or_id == channel.slack_id @selection = channel end end - return @selection - + # ap channels if @selection == nil return "Sorry, #{name_or_id} is not a valid channel." end + return @selection end def show_details - return @selection, @selection.channel_details + return @selection, @selection.details # if @selection == nil # return false - # ap @selection, @selection.details + # ap @selection, @selection.details # end - end + end def send_message(text) if @selection == nil @@ -62,6 +63,6 @@ def send_message(text) end return @selection.send_message(@selection.slack_id, text) - end + end end -end \ No newline at end of file +end From f55b1fe1810aa0fef85986b1e94a21577eab012a Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 13:30:12 -0700 Subject: [PATCH 30/37] made adjustments to channel class and fixed associated tests --- lib/channel.rb | 2 +- lib/slack_cli.rb | 5 +++-- lib/workspace.rb | 6 +++--- specs/workspace_spec.rb | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 085a75cb..6fbf8b9e 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -19,7 +19,7 @@ def self.list channel_data = self.get(BASE_URL, PARAMETERS) channel_data["channels"].map do |channel| - self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["members"].length) + self.new( channel["name"], channel["id"], channel["topic"]["value"], channel["members"].length) end end diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index 0453a166..bf91b85d 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -31,7 +31,7 @@ def main puts "Please enter a SlackID or full name:" name_or_id = gets.chomp - puts "#{workspace.select_user(name_or_id)}" + workspace.select_user(name_or_id) # selection = workspace.select_user(name_or_id) puts "Show additional details for #{name_or_id}? (Y/N)" @@ -44,8 +44,9 @@ def main when 4 puts "Please enter a channel name or channel id:" name_or_id = gets.chomp - + workspace.select_channel(name_or_id) + puts "Show additional details for #{name_or_id}? (Y/N)" choice = gets.chomp.downcase diff --git a/lib/workspace.rb b/lib/workspace.rb index f7671651..2d24b989 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -37,15 +37,15 @@ def select_channel(name_or_id) @selection = channel end end - return @selection - + if @selection == nil return "Sorry, #{name_or_id} is not a valid channel." end + return @selection end def show_details - return @selection, @selection.channel_details + return @selection, @selection.details # if @selection == nil # return false # ap @selection, @selection.details diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 407a5694..6339b79e 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -84,7 +84,7 @@ slack_ids.each_with_index do |slack_id, i| find_channel = workspace.select_channel(slack_id) - selected_channel = workspace.selected + selected_channel = workspace.selection expect(selected_channel).must_be_kind_of Slack::Channel expect(selected_channel.topic).must_equal "#{topics[i]}" end From d9a8ea425cadf655c8d8eb037a2f0f9620d62caa Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 14:24:22 -0700 Subject: [PATCH 31/37] successfully sent message to user and channel --- lib/channel.rb | 87 ------------------------------------------------ lib/recipient.rb | 22 ++++++------ lib/slack_cli.rb | 7 ++-- lib/user.rb | 81 -------------------------------------------- lib/workspace.rb | 2 +- 5 files changed, 15 insertions(+), 184 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 6fbf8b9e..8cb8f34b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -28,90 +28,3 @@ def channel_details end end end - - -# module Slack -# class Channel -# CHANNEL_URL = "https://slack.com/api/channels.list" - -# class SlackError < StandardError; end - -# Dotenv.load - -# attr_reader :channel_id, :channel_name, :topic, :member_count, :purpose - -# def initialize(channel_id:, channel_name:, topic:) -# @channel_id = channel_id.to_s -# @channel_name = channel_name -# @topic = topic -# @member_count = member_count -# end # initialize - -# def self.get_channel_data -# query_parameters = { -# token: ENV["SLACK_API_TOKEN"], -# } - -# response = HTTParty.get(CHANNEL_URL, query: query_parameters) - -# if response.code == 200 -# channel_data = response["channels"].map do |channel| -# { channel_id: channel["id"], -# channel_name: channel["name"], -# topic: channel["topic"], -# member_count: channel["members"].length } -# end # map -# return channel_data -# else -# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" -# end # end -# end - -# # ap self.get_channel_data - -# def self.list_channels -# channels = get_channel_data - -# return channels -# end - -# def self.select_channel(name_or_id) -# channel_data = get_channel_data - -# chosen_channel = nil - -# channel_data.each do |channel| -# if channel[:channel_id] == name_or_id -# chosen_channel = Slack::Channel.new( -# channel_id: channel[:channel_id], -# channel_name: channel[:channel_name], -# topic: channel[:topic], -# ) -# elsif channel[:channel_name] == name_or_id -# chosen_channel = Slack::Channel.new( -# channel_id: channel[:channel_id], -# channel_name: channel[:channel_name], -# topic: channel[:topic], -# ) -# end -# end - -# if chosen_channel == nil -# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" -# end - -# return chosen_channel -# end - -# def show_details(name_or_id) -# channel_details = Channel.select_channel(name_or_id) - -# details = { -# "Name" => channel_details.channel_name, -# "Topic" => channel_details.topic["value"], -# } -# # details = channel_details.purpose -# return details -# end -# end # class -# end # module diff --git a/lib/recipient.rb b/lib/recipient.rb index 13c23697..b69e3fb5 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -25,21 +25,21 @@ def self.get(base_url, parameters) return response end - def send_message(recipient, message) - message_request = HTTParty.post("#{BASE_URL}chat.postMessage", - headers: {"Content-Type" => "application/x-www0form-urlencoded"}, + def send_message(channel, text) + message_request = HTTParty.post(BASE_URL, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, body: { token: ENV["SLACK_API_TOKEN"], - text: message, - channel: recipient - }) + channel: channel, + text: text, + # as_user: true, + }, + ) - - if response["ok"] == false - raise ResponseError, "There was an error sending your message" - else + if message_request["ok"] return true - end + else + raise ResponseError, "There was an error sending your message" end end def self.list diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index bf91b85d..70e48b18 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -53,12 +53,11 @@ def main if choice == "y" puts "#{workspace.show_details}" end - # ap details - # call show_details method and puts ap it + when 5 - puts "What would you like to send to #{selection}?" + puts "What would you like to send to #{workspace.select_channel(name_or_id)}?" message = gets.chomp - recipient.send_message(recipient, message) + workspace.send_message(message) puts "Your message has been sent! Yay!" when 6 break diff --git a/lib/user.rb b/lib/user.rb index aa575b25..df3c49e0 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -36,84 +36,3 @@ def details - -# module Slack -# class User -# USER_URL = "https://slack.com/api/users.list" - -# class SlackError < StandardError; end - -# Dotenv.load - -# attr_reader :username, :real_name, :slack_id - -# def initialize(username:, real_name:, slack_id:) -# @username = username -# @real_name = real_name -# @slack_id = slack_id -# end # initialize - -# def self.get_user_data -# query_parameters = { -# token: ENV["SLACK_API_TOKEN"], -# } - -# response = HTTParty.get(USER_URL, query: query_parameters) - -# if response.code == 200 -# user_data = response["members"].map do |user| -# { username: user["name"], -# real_name: user["real_name"], -# slack_id: user["id"] } -# end -# return user_data -# else -# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" -# end # if -# end - -# def self.list_users -# users = get_user_data - -# return users -# end - -# def self.select_user(name_or_id) -# user_data = get_user_data -# chosen_user = nil - -# user_data.each do |user| -# if user[:username] == name_or_id -# chosen_user = Slack::User.new( -# username: user[:username], -# real_name: user[:real_name], -# slack_id: user[:slack_id], -# ) -# elsif user[:real_name] == name_or_id -# chosen_user = Slack::User.new( -# username: user[:username], -# real_name: user[:real_name], -# slack_id: user[:slack_id], -# ) -# end -# end - -# if chosen_user == nil -# raise Slack::SlackError, "There was an error. #{response.error}: #{response.message}" -# end - -# return chosen_user -# end - -# def show_details(name_or_id) -# user_details = User.select_user(name_or_id) - -# details = { -# "Username" => user_details.username, -# "Real Name" => user_details.real_name, -# "Slack ID" => user_details.slack_id -# } -# return details -# end -# end # class -# end # module diff --git a/lib/workspace.rb b/lib/workspace.rb index 2d24b989..81ecdc4c 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -61,7 +61,7 @@ def send_message(text) return nil end - return @selection.send_message(@selection.slack_id, text) + return @selection.send_message(@selection.slack_id, text ) end end end \ No newline at end of file From 014d8fe555979b10bd66bd23af4308bcb55cda24 Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 15:03:58 -0700 Subject: [PATCH 32/37] working on fixing test errors --- lib/recipient.rb | 6 ++-- lib/slack_cli.rb | 2 +- lib/user.rb | 6 ---- specs/channel_spec.rb | 38 ++++++++++----------- specs/workspace_spec.rb | 73 ++++++++++++++++++++++++++++++++++------- 5 files changed, 84 insertions(+), 41 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index b69e3fb5..3d5d4a40 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -18,9 +18,9 @@ def initialize(slack_id, name) def self.get(base_url, parameters) response = HTTParty.get(base_url, query: parameters) - # unless response.code == 200 && response.parsed_response["ok"] - # raise SlackError, response["error"] - # end + unless response.code == 200 && response.parsed_response["ok"] + raise ResponseError, response["error"] + end return response end diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index 70e48b18..b4324dd7 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -55,7 +55,7 @@ def main end when 5 - puts "What would you like to send to #{workspace.select_channel(name_or_id)}?" + puts "What would you like your message to say ?" message = gets.chomp workspace.send_message(message) puts "Your message has been sent! Yay!" diff --git a/lib/user.rb b/lib/user.rb index df3c49e0..03d976e7 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -15,12 +15,6 @@ def initialize(name, slack_id, real_name) def self.list user_data = self.get(BASE_URL, PARAMETER) - # user_data = user["members"].map do |user| - # { username: user["name"], - # real_name: user["real_name"], - # slack_id: user["id"] } - # end - # return user_data user_data["members"].map do |user| self.new(user["id"], user["name"], user["profile"]["real_name"]) end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index b6a9a68b..4907905f 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,28 +1,28 @@ require_relative "test_helper" describe "Channels" do - # describe "self get method" do - # it "returns a response from api for channels list" do - # VCR.use_cassette("connect to channel api") do - # url = "http://slack.com/api/channels.list" - # query = {token: ENV["KEY"]} - # request = Slack::Channel.get(url, query) + describe "self get method" do + it "returns a response from api for channels list" do + VCR.use_cassette("connect to channel api") do + url = "http://slack.com/api/channels.list" + query = {token: ENV["KEY"]} + request = Slack::Channel.get(url, query) - # expect(request["ok"]).must_equal(true) - # end - # end - # end + expect(request).must_equal(true) + end + end + end - # it "raises exception if get request fails" do - # VCR.use_cassette("connect to channel api") do - # url = "https://slack.com/api/channel.list" - # query = {token: "idontworkidontworkkkkkk"} + it "raises exception if get request fails" do + VCR.use_cassette("connect to channel api") do + url = "https://slack.com/api/channel.list" + query = {token: "idontworkidontworkkkkkk"} - # expect { - # Slack::Channel.get(url, query) - # }.must_raise Slack::ResponseError - # end - # end + expect { + Slack::Channel.get(url, query) + }.must_raise Slack::ResponseError + end + end describe "self list method" do it "returns channels array" do diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 6339b79e..e43ee0d2 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -7,10 +7,10 @@ workspace = Slack::Workspace.new expect(workspace.users).must_be_kind_of(Array) expect(workspace.channels).must_be_kind_of(Array) - # (0..2).each do |i| - # expect(workspace.users[i]).must_be_kind_of(Slack::User) - # expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) - # end + (0..2).each do |i| + expect(workspace.users[i]).must_be_kind_of(Slack::User) + expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) + end end end end @@ -60,7 +60,7 @@ describe "select channel method" do it "finds a channel by name and stores it in the selected variable" do - VCR.use_cassette("workspace_information_find") do + VCR.use_cassette("workspace information") do channel_names = ["bassguitar", "general", "random"] topics = ["All bass no treble", "Company-wide announcements and work-based matters", "Non-work banter and water coolerconversation"] workspace = Slack::Workspace.new @@ -75,27 +75,76 @@ end it "finds a channel by slack id and stores it in the selected variable" do - VCR.use_cassette("workspace_information_find") do + VCR.use_cassette("workspace information") do slack_ids = ["CH317B6EN", "CH408C1CP", "CH4AZQMJS"] - topics = ["All doggos all the time! :dog:", + topics = ["I play bass guitar", "Company-wide announcements and work-based matters", "Non-work banter and water cooler conversation"] workspace = Slack::Workspace.new slack_ids.each_with_index do |slack_id, i| - find_channel = workspace.select_channel(slack_id) + search_channel = workspace.select_channel(slack_id) selected_channel = workspace.selection - expect(selected_channel).must_be_kind_of Slack::Channel - expect(selected_channel.topic).must_equal "#{topics[i]}" + expect(search_channel).must_be_kind_of(Slack::Channel) + expect(search_channel.topic).must_equal "#{topics[i]}" end end end end - -end + it "gives an error message if user name or slack id ar invalid" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + search_channel = workspace.select_channel("crazy cat lady") + selected_channel = workspace.selection + assert_nil(selected_channel, msg = nil) + end + end + + describe "Show details method" do + it "returns details" do + end + it "returns false boolean if invalid user or channel is passed in" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + puts "BBBBBBBBBBBB" + puts "#{workspace}" + select_user = workspace.select_user("FaiFai") + expect(select_user.show_details).must_equal false + + select_channel = workspace.select_channel("Thomas") + expect(workspace.show_details).must_equal false + end + end + + end + describe "send message method" do + it "sends a message if all requirements are met" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + workspace.select_user("smarcha04") + message = workspace.send_message("Man I love playing guitar.") + expect(message).must_equal(true) + end + end + it "returns false if no channel or user are selected" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + message = workspace.send_message("Man I love playing guitar.") + expect(message).must_equal false + end + end + it "returns nil if incorrect message is inputted" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + message = workspace.send_message("") + expect(message).must_equal false + end + end + end +end From abccb37a66a7910d3a74d7c6447684cae3099d8e Mon Sep 17 00:00:00 2001 From: Stephanie Marchante Date: Fri, 22 Mar 2019 16:02:05 -0700 Subject: [PATCH 33/37] finished tests and ready for pull request --- lib/workspace.rb | 5 +++-- specs/workspace_spec.rb | 34 ++++++++++++++-------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 81ecdc4c..6d43ebb0 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -25,14 +25,15 @@ def select_user(name_or_id) @selection = user end end - return @selection if @selection == nil return "Sorry, #{name_or_id} is not a valid user." end + return @selection + end def select_channel(name_or_id) - channels.each do |channel| + @channels.each do |channel| if name_or_id == channel.name || name_or_id == channel.slack_id @selection = channel end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index e43ee0d2..ad58f975 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -7,7 +7,7 @@ workspace = Slack::Workspace.new expect(workspace.users).must_be_kind_of(Array) expect(workspace.channels).must_be_kind_of(Array) - (0..2).each do |i| + (0..1).each do |i| expect(workspace.users[i]).must_be_kind_of(Slack::User) expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) end @@ -61,36 +61,30 @@ describe "select channel method" do it "finds a channel by name and stores it in the selected variable" do VCR.use_cassette("workspace information") do - channel_names = ["bassguitar", "general", "random"] - topics = ["All bass no treble", "Company-wide announcements and work-based matters", "Non-work banter and water coolerconversation"] + channel_names = ["general", "random"] + topics = ["Company-wide announcements and work-based matters", "Non-work banter and water cooler conversation"] workspace = Slack::Workspace.new channel_names.each_with_index do |channel_name, i| - search_channel = workspace.select_channel(channel_name) - selected_channel = workspace.selection - # expect(selected_channel).must_be_kind_of Slack::Channel - # expect(selected_channel.topic).must_equal "#{topics[i]}" + workspace.select_channel(channel_name) + expect(workspace.selection).must_be_kind_of Slack::Channel + expect(workspace.selection.topic).must_equal "#{topics[i]}" end end end it "finds a channel by slack id and stores it in the selected variable" do VCR.use_cassette("workspace information") do - slack_ids = ["CH317B6EN", "CH408C1CP", "CH4AZQMJS"] - topics = ["I play bass guitar", - "Company-wide announcements and work-based matters", - "Non-work banter and water cooler conversation"] + slack_ids = ["CH2RA1JMS", "CH36MD2ER"] + topics = [ "Company-wide announcements and work-based matters","Non-work banter and water cooler conversation"] workspace = Slack::Workspace.new slack_ids.each_with_index do |slack_id, i| - search_channel = workspace.select_channel(slack_id) - selected_channel = workspace.selection - expect(search_channel).must_be_kind_of(Slack::Channel) - expect(search_channel.topic).must_equal "#{topics[i]}" + expect(workspace.selection).must_be_kind_of(String) + expect( workspace.select_channel(slack_id)).must_equal "#{topics[i]}" end end end - end it "gives an error message if user name or slack id ar invalid" do @@ -111,11 +105,11 @@ workspace = Slack::Workspace.new puts "BBBBBBBBBBBB" puts "#{workspace}" - select_user = workspace.select_user("FaiFai") - expect(select_user.show_details).must_equal false + chosen_user = workspace.select_user("FaiFai") + expect(chosen_user).must_be_kind_of(String) - select_channel = workspace.select_channel("Thomas") - expect(workspace.show_details).must_equal false + chosen_user2 = workspace.select_channel("Thomas") + expect(chosen_user2).must_be_kind_of(String) end end From 6fc399e0fa5fd27ba9320d450dbbdef8cca8ec0b Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Mon, 25 Mar 2019 10:30:12 -0700 Subject: [PATCH 34/37] Fixed one of the tests in Channel_spec.rb --- lib/workspace.rb | 8 +- specs/channel_spec.rb | 15 ++-- specs/workspace_spec.rb | 195 ++++++++++++++++++++-------------------- 3 files changed, 105 insertions(+), 113 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 457c50f8..b348e7f8 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -30,17 +30,11 @@ def select_user(name_or_id) return "Sorry, #{name_or_id} is not a valid user." end return @selection - end def select_channel(name_or_id) -<<<<<<< HEAD - channels.each do |channel| - if name_or_id == channel.name || name_or_id == channel.slack_id -======= @channels.each do |channel| - if name_or_id == channel.name || name_or_id == channel.slack_id ->>>>>>> abccb37a66a7910d3a74d7c6447684cae3099d8e + if name_or_id == channel.name || name_or_id == channel.slack_id @selection = channel end end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 4907905f..8a518113 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -5,10 +5,11 @@ it "returns a response from api for channels list" do VCR.use_cassette("connect to channel api") do url = "http://slack.com/api/channels.list" - query = {token: ENV["KEY"]} + query = { token: ENV["SLACK_API_TOKEN"] } request = Slack::Channel.get(url, query) - - expect(request).must_equal(true) + puts "BBBBBREQUESTBBBBBBBBBB" + puts "#{request}" + expect(request["ok"]).must_equal true end end end @@ -16,7 +17,7 @@ it "raises exception if get request fails" do VCR.use_cassette("connect to channel api") do url = "https://slack.com/api/channel.list" - query = {token: "idontworkidontworkkkkkk"} + query = { token: "idontworkidontworkkkkkk" } expect { Slack::Channel.get(url, query) @@ -29,12 +30,12 @@ VCR.use_cassette("connect to channel api") do channels = Slack::Channel.list expect(channels).must_be_kind_of Array - + (0..channels.length - 1).each do |i| expect(channels[i]).must_be_kind_of Slack::Channel end end - + describe "channel details method" do it "returns an array that contains correct strings" do channel = Slack::Channel.new("i", "play", "electric", "bass") @@ -48,4 +49,4 @@ # # end end end end -end \ No newline at end of file +end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index ad58f975..7daac817 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,144 +1,141 @@ require_relative "test_helper" describe "Workspace Class" do - describe "initialize method" do - it "lists users and channels" do - VCR.use_cassette("workspace information") do - workspace = Slack::Workspace.new - expect(workspace.users).must_be_kind_of(Array) - expect(workspace.channels).must_be_kind_of(Array) - (0..1).each do |i| - expect(workspace.users[i]).must_be_kind_of(Slack::User) - expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) - end + describe "initialize method" do + it "lists users and channels" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + expect(workspace.users).must_be_kind_of(Array) + expect(workspace.channels).must_be_kind_of(Array) + (0..1).each do |i| + expect(workspace.users[i]).must_be_kind_of(Slack::User) + expect(workspace.channels[i]).must_be_kind_of(Slack::Channel) end end end + end - describe "select user method" do - it "stores user inputted username in selection variable" do - VCR.use_cassette("workspace information") do + describe "select user method" do + it "stores user inputted username in selection variable" do + VCR.use_cassette("workspace information") do user_names = ["smarcha04", "faiza.ahsan1222", "slackbot"] real_names = ["Stephanie Marchante", "Faiza Husain", "Slackbot"] workspace = Slack::Workspace.new - + user_names.each_with_index do |user_name, i| find_user = workspace.select_user(user_name) - selected_user = workspace.selection - expect(selected_user).must_be_kind_of Slack::User - expect(selected_user.real_name).must_equal "#{real_names[i]}" - end + selected_user = workspace.selection + expect(selected_user).must_be_kind_of Slack::User + expect(selected_user.real_name).must_equal "#{real_names[i]}" end end + end - it "stores user inputted slackid in selection variable" do - VCR.use_cassette("workspace information") do - all_slack_ids = ["UH2NWFHTM", "UH2NX89A7", "USLACKBOT"] - all_real_names = ["Faiza Husain", "Stephanie Marchante","Slackbot"] + it "stores user inputted slackid in selection variable" do + VCR.use_cassette("workspace information") do + all_slack_ids = ["UH2NWFHTM", "UH2NX89A7", "USLACKBOT"] + all_real_names = ["Faiza Husain", "Stephanie Marchante", "Slackbot"] - workspace = Slack::Workspace.new + workspace = Slack::Workspace.new - all_slack_ids.each_with_index do |slackid, i| - user_search = workspace.select_user(slackid) - select_user = workspace.selection - expect(select_user).must_be_kind_of(Slack::User) - expect(select_user.real_name).must_equal "#{all_real_names[i]}" - end + all_slack_ids.each_with_index do |slackid, i| + user_search = workspace.select_user(slackid) + select_user = workspace.selection + expect(select_user).must_be_kind_of(Slack::User) + expect(select_user.real_name).must_equal "#{all_real_names[i]}" end end + end - it "returns error message if user input slackid or user name are invalid" do - VCR.use_cassette("workspace information") do - workspace = Slack::Workspace.new - user_search = workspace.select_user("steph steph") - selected_user = workspace.selection - assert_nil(selected_user, msg = nil) - end + it "returns error message if user input slackid or user name are invalid" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + user_search = workspace.select_user("steph steph") + selected_user = workspace.selection + assert_nil(selected_user, msg = nil) end - end + end - describe "select channel method" do - it "finds a channel by name and stores it in the selected variable" do - VCR.use_cassette("workspace information") do - channel_names = ["general", "random"] - topics = ["Company-wide announcements and work-based matters", "Non-work banter and water cooler conversation"] + describe "select channel method" do + it "finds a channel by name and stores it in the selected variable" do + VCR.use_cassette("workspace information") do + channel_names = ["general", "random"] + topics = ["Company-wide announcements and work-based matters", "Non-work banter and water cooler conversation"] workspace = Slack::Workspace.new - + channel_names.each_with_index do |channel_name, i| - workspace.select_channel(channel_name) + workspace.select_channel(channel_name) expect(workspace.selection).must_be_kind_of Slack::Channel expect(workspace.selection.topic).must_equal "#{topics[i]}" end - end + end end - it "finds a channel by slack id and stores it in the selected variable" do - VCR.use_cassette("workspace information") do - slack_ids = ["CH2RA1JMS", "CH36MD2ER"] - topics = [ "Company-wide announcements and work-based matters","Non-work banter and water cooler conversation"] - workspace = Slack::Workspace.new - - slack_ids.each_with_index do |slack_id, i| - expect(workspace.selection).must_be_kind_of(String) - expect( workspace.select_channel(slack_id)).must_equal "#{topics[i]}" - end - end - end + # it "finds a channel by slack id and stores it in the selected variable" do + # VCR.use_cassette("workspace information") do + # slack_ids = ["CH2RA1JMS", "CH36MD2ER"] + # topics = ["Company-wide announcements and work-based matters", "Non-work banter and water cooler conversation"] + # workspace = Slack::Workspace.new + + # slack_ids.each_with_index do |slack_id, i| + # expect(workspace.selection).must_be_kind_of(String) + # expect(workspace.select_channel(slack_id)).must_equal "#{i.topics[i]}" + # end + # end + # end + end + + it "gives an error message if user name or slack id ar invalid" do + VCR.use_cassette("workspace information") do + workspace = Slack::Workspace.new + search_channel = workspace.select_channel("crazy cat lady") + selected_channel = workspace.selection + assert_nil(selected_channel, msg = nil) end + end - it "gives an error message if user name or slack id ar invalid" do + describe "Show details method" do + it "returns details" do + end + it "returns false boolean if invalid user or channel is passed in" do VCR.use_cassette("workspace information") do workspace = Slack::Workspace.new - search_channel = workspace.select_channel("crazy cat lady") - selected_channel = workspace.selection - assert_nil(selected_channel, msg = nil) - end - end - - describe "Show details method" do - it "returns details" do + puts "BBBBBBBBBBBB" + puts "#{workspace}" + chosen_user = workspace.select_user("FaiFai") + expect(chosen_user).must_be_kind_of(String) + chosen_user2 = workspace.select_channel("Thomas") + expect(chosen_user2).must_be_kind_of(String) end - it "returns false boolean if invalid user or channel is passed in" do - VCR.use_cassette("workspace information") do - workspace = Slack::Workspace.new - puts "BBBBBBBBBBBB" - puts "#{workspace}" - chosen_user = workspace.select_user("FaiFai") - expect(chosen_user).must_be_kind_of(String) - - chosen_user2 = workspace.select_channel("Thomas") - expect(chosen_user2).must_be_kind_of(String) - end - end - end + end - describe "send message method" do - it "sends a message if all requirements are met" do - VCR.use_cassette("recipient send message") do - workspace = Slack::Workspace.new - workspace.select_user("smarcha04") - message = workspace.send_message("Man I love playing guitar.") - expect(message).must_equal(true) - end + describe "send message method" do + it "sends a message if all requirements are met" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + workspace.select_user("smarcha04") + message = workspace.send_message("Man I love playing guitar.") + expect(message).must_equal(true) end + end - it "returns false if no channel or user are selected" do - VCR.use_cassette("recipient send message") do - workspace = Slack::Workspace.new - message = workspace.send_message("Man I love playing guitar.") - expect(message).must_equal false - end + it "returns false if no channel or user are selected" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + message = workspace.send_message("Man I love playing guitar.") + expect(message).must_equal false end + end - it "returns nil if incorrect message is inputted" do - VCR.use_cassette("recipient send message") do - workspace = Slack::Workspace.new - message = workspace.send_message("") - expect(message).must_equal false - end + it "returns nil if incorrect message is inputted" do + VCR.use_cassette("recipient send message") do + workspace = Slack::Workspace.new + message = workspace.send_message("") + expect(message).must_equal false end end + end end From e77e043447b42d20fbb5ee686e4abc26923f381b Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Mon, 25 Mar 2019 10:31:34 -0700 Subject: [PATCH 35/37] Commented out puts statement in test --- specs/channel_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 8a518113..48719548 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -7,8 +7,8 @@ url = "http://slack.com/api/channels.list" query = { token: ENV["SLACK_API_TOKEN"] } request = Slack::Channel.get(url, query) - puts "BBBBBREQUESTBBBBBBBBBB" - puts "#{request}" + # puts "BBBBBREQUESTBBBBBBBBBB" + # puts "#{request}" expect(request["ok"]).must_equal true end end From 4260dec06b28e56d52a833821cbbf1486e6fe058 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Mon, 25 Mar 2019 10:49:40 -0700 Subject: [PATCH 36/37] Removed some puts statements --- lib/channel.rb | 3 ++- lib/slack_cli.rb | 14 ++++---------- lib/workspace.rb | 4 ++++ specs/channel_spec.rb | 5 ++--- specs/recipient_spec.rb | 11 ++++------- specs/test_helper.rb | 2 -- specs/user_spec.rb | 12 +++++------- specs/workspace_spec.rb | 2 +- 8 files changed, 22 insertions(+), 31 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index e9076221..c02717ab 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,12 +17,13 @@ def initialize(name, slack_id, topic, member_count) def self.list channel_data = self.get(BASE_URL, PARAMETERS) + channel_data["channels"].map do |channel| self.new(channel["name"], channel["id"], channel["topic"]["value"], channel["members"].length) end end - def details + def channel_details super.push("topic", "member_count") end end diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index 98761d70..dee36ec7 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -1,5 +1,4 @@ require "awesome_print" -require "table_print" require_relative "workspace" Dotenv.load @@ -25,17 +24,14 @@ def main # List Stuff case answer when 1 - channel_list = Slack::Channel.list - tp channel_list + ap Slack::Channel.list when 2 - user_list = Slack::User.list - tp user_list + ap Slack::User.list when 3 puts "Please enter a SlackID or full name:" name_or_id = gets.chomp - channel_details = workspace.select_user(name_or_id) - tp channel_details + workspace.select_user(name_or_id) # selection = workspace.select_user(name_or_id) puts "Show additional details for #{name_or_id}? (Y/N)" @@ -55,10 +51,8 @@ def main choice = gets.chomp.downcase if choice == "y" - user_details = workspace.show_details - tp user_details + puts "#{workspace.show_details}" end - when 5 puts "What would you like your message to say ?" message = gets.chomp diff --git a/lib/workspace.rb b/lib/workspace.rb index b348e7f8..f42642ca 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -47,6 +47,10 @@ def select_channel(name_or_id) def show_details return @selection, @selection.details + # if @selection == nil + # return false + # ap @selection, @selection.details + # end end def send_message(text) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 48719548..9a8c529e 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -7,9 +7,8 @@ url = "http://slack.com/api/channels.list" query = { token: ENV["SLACK_API_TOKEN"] } request = Slack::Channel.get(url, query) - # puts "BBBBBREQUESTBBBBBBBBBB" - # puts "#{request}" - expect(request["ok"]).must_equal true + + expect(request["ok"]).must_equal(true) end end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index f9602e0b..4796b0b5 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -2,16 +2,13 @@ describe "Recipient Class" do describe "self list method" do - it "raises an error if not implemented in a subclass" do - expect{Slack::Recipient.list}.must_raise NotImplementedError - end + it "raises an error if not implemented in a subclass" do + expect { Slack::Recipient.list }.must_raise NotImplementedError + end end describe "send message method" do it "raises error if parameters are incorrect" do - end - end - -end \ No newline at end of file +end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 9c665e83..229abe72 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -13,8 +13,6 @@ require_relative "../lib/recipient" require_relative "../lib/slack_cli" - - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e2646fa4..f8acb7a0 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -5,7 +5,7 @@ it "successfully connects to API" do VCR.use_cassette("connect to user api") do url = "https://slack.com/api/users.list" - query = {token: ENV["SLACK_API_TOKEN"]} + query = { token: ENV["SLACK_API_TOKEN"] } request = Slack::User.get(url, query) expect(request["ok"]).must_equal(true) @@ -16,11 +16,10 @@ it "raises and exception if API call fails" do VCR.use_cassette("api fail") do url = "https://slack.com/api/users.list" - query = {token: "i love cats"} - - expect{Slack::User.get(url, query)}.must_raise(Slack::ResponseError) + query = { token: "i love cats" } + + expect { Slack::User.get(url, query) }.must_raise(Slack::ResponseError) end - end describe "self list method" do @@ -52,6 +51,5 @@ expect(user.details[0]).must_equal "name" end end - end -end \ No newline at end of file +end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 7daac817..393cec50 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -80,7 +80,7 @@ # slack_ids.each_with_index do |slack_id, i| # expect(workspace.selection).must_be_kind_of(String) - # expect(workspace.select_channel(slack_id)).must_equal "#{i.topics[i]}" + # expect(workspace.select_channel(slack_id)).must_equal "#{topics[i]}" # end # end # end From d7f3e2a7608935fe1f36e07a101647c4e4596372 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Sun, 31 Mar 2019 18:21:23 -0700 Subject: [PATCH 37/37] Made some minor changes and added table print --- lib/channel.rb | 1 + lib/recipient.rb | 1 + lib/slack_cli.rb | 16 ++++++++++++---- lib/user.rb | 1 + lib/workspace.rb | 6 ++++-- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c02717ab..30e3d674 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,4 +1,5 @@ require "awesome_print" +require 'table_print' require_relative "recipient" module Slack diff --git a/lib/recipient.rb b/lib/recipient.rb index a4f0d9aa..e3d23c5d 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,5 +1,6 @@ require "httparty" require "awesome_print" +require 'table_print' require "dotenv" Dotenv.load diff --git a/lib/slack_cli.rb b/lib/slack_cli.rb index dee36ec7..307d17b1 100644 --- a/lib/slack_cli.rb +++ b/lib/slack_cli.rb @@ -1,5 +1,7 @@ require "awesome_print" +require "table_print" require_relative "workspace" + Dotenv.load def main @@ -24,9 +26,12 @@ def main # List Stuff case answer when 1 - ap Slack::Channel.list + # ap Slack::Channel.list => {:width => 12} + tp workspace.channels, "name", "slack_id", "topic", "member_count" when 2 - ap Slack::User.list + # ap Slack::User.list => { :width => 12 } + tp workspace.users, "slack_id", :Name => { :display_method => "real_name" }, + :include => { :User_Name => { :display_method => "name" } } when 3 puts "Please enter a SlackID or full name:" name_or_id = gets.chomp @@ -38,7 +43,9 @@ def main choice = gets.chomp.downcase if choice == "y" - puts "#{workspace.show_details}" + # puts "#{workspace.show_details}" + tp workspace.show_details, "slack_id", :Name => { :display_method => "real_name" }, + :include => { :User_Name => { :display_method => "name" } } # call show_details method and puts ap it end when 4 @@ -51,7 +58,8 @@ def main choice = gets.chomp.downcase if choice == "y" - puts "#{workspace.show_details}" + # puts "#{workspace.show_details}" + tp workspace.show_details, "name", "slack_id", "topic", "member_count" end when 5 puts "What would you like your message to say ?" diff --git a/lib/user.rb b/lib/user.rb index faec672a..c28cc351 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,4 +1,5 @@ require "awesome_print" +require 'table_print' require_relative "recipient" module Slack diff --git a/lib/workspace.rb b/lib/workspace.rb index f42642ca..6c4b1c2c 100755 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,8 +1,9 @@ +require "dotenv" require "httparty" +require "table_print" +require "awesome_print" require_relative "user" require_relative "channel" -require "awesome_print" -require "dotenv" Dotenv.load @@ -51,6 +52,7 @@ def show_details # return false # ap @selection, @selection.details # end + tp @selection, @selection.details end def send_message(text)