From c7dfaa20f8e463937aeb5f83a623c33029e73582 Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 13:33:37 -0700 Subject: [PATCH 01/34] project verification --- lib/slack.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..ac354f91 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,22 @@ -#!/usr/bin/env ruby +require "dotenv" +require "httparty" +Dotenv.load +require "ap" +require "pry" -def main - puts "Welcome to the Ada Slack CLI!" +#def main +#puts "Welcome to the Ada Slack CLI!" - # TODO project - - puts "Thank you for using the Ada Slack CLI" +BASE_URL = "https://slack.com/api/channels.list" +query = { + token: ENV["SLACK_API_TOKEN"], +} +channel_info = HTTParty.get(BASE_URL, query: query) +channel_list = channel_info.map do |channel| + channel["name"] end +ap channel_list +#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 37abe63aa3faf5f2b83add8952dedc77240dbdcc Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 13:48:49 -0700 Subject: [PATCH 02/34] completed verification --- lib/slack.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index ac354f91..44ca4487 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -12,10 +12,11 @@ token: ENV["SLACK_API_TOKEN"], } channel_info = HTTParty.get(BASE_URL, query: query) -channel_list = channel_info.map do |channel| +channel_list = channel_info["channels"] +channel_names = channel_list.map do |channel| channel["name"] end -ap channel_list +ap channel_names #puts "Thank you for using the Ada Slack CLI" #end From a805089149a0a267f023187249193f98493cda34 Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 13:54:15 -0700 Subject: [PATCH 03/34] updated files structure --- .gitignore | 1 + lib/slack.rb | 19 ++++--------------- lib/verification.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 lib/verification.rb diff --git a/.gitignore b/.gitignore index 8d6a243f..451dd5dc 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ build-iPhoneSimulator/ # Ignore environemnt variables .env +.DS_Store # Ignore cassette files /specs/cassettes/ diff --git a/lib/slack.rb b/lib/slack.rb index 44ca4487..72bea7b6 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,22 +2,11 @@ require "httparty" Dotenv.load require "ap" -require "pry" -#def main -#puts "Welcome to the Ada Slack CLI!" +def main + puts "Welcome to the Ada Slack CLI!" -BASE_URL = "https://slack.com/api/channels.list" -query = { - token: ENV["SLACK_API_TOKEN"], -} -channel_info = HTTParty.get(BASE_URL, query: query) -channel_list = channel_info["channels"] -channel_names = channel_list.map do |channel| - channel["name"] + puts "Thank you for using the Ada Slack CLI" end -ap channel_names -#puts "Thank you for using the Ada Slack CLI" -#end -#main if __FILE__ == $PROGRAM_NAME +main if __FILE__ == $PROGRAM_NAME diff --git a/lib/verification.rb b/lib/verification.rb new file mode 100644 index 00000000..3061338a --- /dev/null +++ b/lib/verification.rb @@ -0,0 +1,15 @@ +require "dotenv" +require "httparty" +Dotenv.load +require "ap" + +BASE_URL = "https://slack.com/api/channels.list" +query = { + token: ENV["SLACK_API_TOKEN"], +} +channel_info = HTTParty.get(BASE_URL, query: query) +channel_list = channel_info["channels"] +channel_names = channel_list.map do |channel| + channel["name"] +end +ap channel_names From 2c55f9ad8ff6cbb5dae18580759229e2ec0c44f4 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Tue, 19 Mar 2019 14:06:21 -0700 Subject: [PATCH 04/34] added ruby files for different classes --- lib/channel.rb | 0 lib/recipient.rb | 0 lib/user.rb | 0 specs/channel_spec.rb | 0 specs/recipient_spec.rb | 0 specs/slack_spec.rb | 0 specs/user_spec.rb | 0 7 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/channel.rb create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 specs/channel_spec.rb create mode 100644 specs/recipient_spec.rb create mode 100644 specs/slack_spec.rb create mode 100644 specs/user_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e69de29b From 988df5766c5f9f5a743e6ef7e4235346fc305845 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Tue, 19 Mar 2019 15:17:39 -0700 Subject: [PATCH 05/34] create list method for channel --- lib/channel.rb | 32 ++++++++++++++++++++++++++++++++ lib/recipient.rb | 22 ++++++++++++++++++++++ lib/slack.rb | 4 ++++ lib/user.rb | 1 + 4 files changed, 59 insertions(+) diff --git a/lib/channel.rb b/lib/channel.rb index e69de29b..40b87711 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -0,0 +1,32 @@ +# require_relative "recipient" +require "pry" + +class Channel + attr_reader :topic, :member_count + attr_accessor :channel_names + BASE_URL = "https://slack.com/api/channels.list" + TOKEN = ENV["SLACK_API_TOKEN"] + + def initialize + @topic = topic + @member_count = member_count + @channel_names = [] + end + + def list + query = { + token: TOKEN, + } + channel_info = HTTParty.get(BASE_URL, query: query) + channel_list = channel_info["channels"] + @channel_names = channel_list.map do |channel| + channel["name"] + end + return @channel_names + end +end + +# binding.pry +channel = Channel.new +channel.list +puts channel.channel_names diff --git a/lib/recipient.rb b/lib/recipient.rb index e69de29b..a593f685 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -0,0 +1,22 @@ + +class Recipient + # attr_reader :slack_id, :name + + # def initialize + # @slack_id = slack_id + # @name = name + # end + + # def send_message(message) + # end + + # # def self.get(url, params) + # # info = HTTParty.get(url, params) + # # end + + # def details + # end + + # # def self.list + # # end +end diff --git a/lib/slack.rb b/lib/slack.rb index 72bea7b6..632e0f72 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,3 +1,7 @@ +require_relative "recipient" +require_relative "user" +require_relative "channel" + require "dotenv" require "httparty" Dotenv.load diff --git a/lib/user.rb b/lib/user.rb index e69de29b..25f964f8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -0,0 +1 @@ +require_relative "recipient" From 50fc8b50ae10fcf48b18433bfdd4ece35c0ee6ee Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 15:34:20 -0700 Subject: [PATCH 06/34] working through CLI for list channels --- lib/channel.rb | 51 ++++++++++++++++++++++++++------------------------ lib/slack.rb | 9 ++++++++- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 40b87711..a92e2eaa 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,32 +1,35 @@ -# require_relative "recipient" +require "dotenv" +require "httparty" +Dotenv.load require "pry" -class Channel - attr_reader :topic, :member_count - attr_accessor :channel_names - BASE_URL = "https://slack.com/api/channels.list" - TOKEN = ENV["SLACK_API_TOKEN"] +module Slack + class Channel + attr_reader :topic, :member_count + attr_accessor :channel_names + BASE_URL = "https://slack.com/api/channels.list" + TOKEN = ENV["SLACK_API_TOKEN"] - def initialize - @topic = topic - @member_count = member_count - @channel_names = [] - end + def initialize + @topic = topic + @member_count = member_count + @channel_names = [] + end - def list - query = { - token: TOKEN, - } - channel_info = HTTParty.get(BASE_URL, query: query) - channel_list = channel_info["channels"] - @channel_names = channel_list.map do |channel| - channel["name"] + def list + query = { + token: TOKEN, + } + channel_info = HTTParty.get(BASE_URL, query: query) + channel_list = channel_info["channels"] + @channel_names = channel_list.map do |channel| + channel["name"] + end + return @channel_names end - return @channel_names end end -# binding.pry -channel = Channel.new -channel.list -puts channel.channel_names +# channel = Channel.new +# channel.list +# puts channel.channel_names diff --git a/lib/slack.rb b/lib/slack.rb index 632e0f72..14a5ada0 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,7 @@ require_relative "recipient" require_relative "user" require_relative "channel" +require "pry" require "dotenv" require "httparty" @@ -9,7 +10,13 @@ def main puts "Welcome to the Ada Slack CLI!" - + puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Quit" + choice = gets.chomp + if choice == 1 + channel = Slack::Channel.new + channel.list + return channel.channel_names + end puts "Thank you for using the Ada Slack CLI" end From 4dbbb71ea379f4a0c343f912dc45a0ab228c8795 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Tue, 19 Mar 2019 15:35:05 -0700 Subject: [PATCH 07/34] minor changes --- lib/channel.rb | 3 +++ specs/channel_spec.rb | 1 + specs/recipient_spec.rb | 1 + specs/slack_spec.rb | 1 + specs/test_helper.rb | 20 +++++++++++++------- specs/user_spec.rb | 1 + 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 40b87711..c4cf73ca 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,5 +1,8 @@ # require_relative "recipient" require "pry" +require "httparty" +require "dotenv" +Dotenv.load class Channel attr_reader :topic, :member_count diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e69de29b..b5cae113 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e69de29b..b5cae113 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb index e69de29b..b5cae113 100644 --- a/specs/slack_spec.rb +++ b/specs/slack_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..2557ffaf 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,21 @@ -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" 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 +end + +#require_relative lib files +require_relative "../lib/recipient.rb" +require_relative "../lib/user.rb" +require_relative "../lib/channel.rb" +require_relative "../lib/slack.rb" diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e69de29b..b5cae113 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -0,0 +1 @@ +require_relative "test_helper" From e51b54a9b8dbbc033886bc1d444b725672b52094 Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 15:58:08 -0700 Subject: [PATCH 08/34] CLI prints channel lists --- lib/channel.rb | 16 ++++++++-------- lib/slack.rb | 5 ++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index a92e2eaa..5287b378 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -21,15 +21,15 @@ def list token: TOKEN, } channel_info = HTTParty.get(BASE_URL, query: query) - channel_list = channel_info["channels"] - @channel_names = channel_list.map do |channel| - channel["name"] + if channel_info.code != 200 + raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" + else + channel_list = channel_info["channels"] + @channel_names = channel_list.map do |channel| + channel["name"] + end + return @channel_names end - return @channel_names end end end - -# channel = Channel.new -# channel.list -# puts channel.channel_names diff --git a/lib/slack.rb b/lib/slack.rb index 14a5ada0..6838243f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -12,10 +12,9 @@ def main puts "Welcome to the Ada Slack CLI!" puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Quit" choice = gets.chomp - if choice == 1 + if choice == "2" channel = Slack::Channel.new - channel.list - return channel.channel_names + puts channel.list end puts "Thank you for using the Ada Slack CLI" end From f4e85880b696c0290f29b0a00df3c22248d69c97 Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 16:03:50 -0700 Subject: [PATCH 09/34] removed slack_spec for CLI and added workspace files --- lib/workspace.rb | 1 + specs/slack_spec.rb | 0 specs/workspace_spec.rb | 1 + 3 files changed, 2 insertions(+) create mode 100644 lib/workspace.rb delete mode 100644 specs/slack_spec.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1 @@ + diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1 @@ + From 4d124142cd0e8baf75a462b19eacc65cbfd4410e Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 16:07:00 -0700 Subject: [PATCH 10/34] added files to test_helper --- lib/workspace.rb | 9 +++++++++ specs/test_helper.rb | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 8b137891..2e81b18c 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1 +1,10 @@ +require_relative "recipient" +require_relative "user" +require_relative "channel" +require "pry" + +require "dotenv" +require "httparty" +Dotenv.load +require "ap" diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..09d8ca29 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,21 @@ -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" 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 +end + +require_relative "../lib/recipient.rb" +require_relative "../lib/user.rb" +require_relative "../lib/channel.rb" +require_relative "../lib/slack.rb" +require_relative "../lib/workspace.rb" From fb61b11586278c3c4324ec484a7ccbb159320108 Mon Sep 17 00:00:00 2001 From: amyesh Date: Tue, 19 Mar 2019 16:48:12 -0700 Subject: [PATCH 11/34] channel tests --- lib/channel.rb | 46 +++++++++++++++++++++---------------------- lib/slack.rb | 7 ++++--- lib/user.rb | 32 ++++++++++++++++++++++++++++++ specs/channel_spec.rb | 25 +++++++++++++++++++++++ specs/test_helper.rb | 7 +++++++ 5 files changed, 90 insertions(+), 27 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 5287b378..e16de494 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -3,33 +3,31 @@ Dotenv.load require "pry" -module Slack - class Channel - attr_reader :topic, :member_count - attr_accessor :channel_names - BASE_URL = "https://slack.com/api/channels.list" - TOKEN = ENV["SLACK_API_TOKEN"] +class Channel + attr_reader :topic, :member_count + attr_accessor :channel_names + BASE_URL = "https://slack.com/api/channels.list" + TOKEN = ENV["SLACK_API_TOKEN"] - def initialize - @topic = topic - @member_count = member_count - @channel_names = [] - end + def initialize + @topic = topic + @member_count = member_count + @channel_names = [] + end - def list - query = { - token: TOKEN, - } - channel_info = HTTParty.get(BASE_URL, query: query) - if channel_info.code != 200 - raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" - else - channel_list = channel_info["channels"] - @channel_names = channel_list.map do |channel| - channel["name"] - end - return @channel_names + def self.list + query = { + token: TOKEN, + } + channel_info = HTTParty.get(BASE_URL, query: query) + if channel_info.code != 200 + raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" + else + channel_list = channel_info["channels"] + @channel_names = channel_list.map do |channel| + channel["name"] end + return @channel_names end end end diff --git a/lib/slack.rb b/lib/slack.rb index 6838243f..48acd385 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -12,9 +12,10 @@ def main puts "Welcome to the Ada Slack CLI!" puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Quit" choice = gets.chomp - if choice == "2" - channel = Slack::Channel.new - puts channel.list + if choice == "1" + puts User.list + elsif choice == "2" + puts Channel.list end puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index 25f964f8..4ba2a160 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1 +1,33 @@ require_relative "recipient" +require "dotenv" +require "httparty" +Dotenv.load +require "pry" + +class User + attr_reader :topic, :member_count + attr_accessor :user_names + BASE_URL = "https://slack.com/api/users.list" + TOKEN = ENV["SLACK_API_TOKEN"] + + def initialize + @real_name = real_name + @user_names = [] + end + + def self.list + query = { + token: TOKEN, + } + user_info = HTTParty.get(BASE_URL, query: query) + if user_info.code != 200 + raise ArgumentError, "The error code is #{user_info.code} and the reason is: #{user_info.message}" + else + user_list = user_info["members"] + @user_names = user_list.map do |user| + user["name"] + end + return @user_names + end + end +end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e69de29b..627e9084 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -0,0 +1,25 @@ +require_relative "test_helper" + +describe "Channel class" do + describe "Channel#list" do + it "finds channels" do + VCR.use_cassette("Channel") do + location = "Seattle" + response = get_location(location) + + expect(response["Seattle"]).wont_be_nil + expect(response["Seattle"][:lon]).must_equal "-122.3300624" + expect(response["Seattle"][:lat]).must_equal "47.6038321" + end + end + + it "will raise an exception if the search fails" do + VCR.use_cassette("Channel") do + location = "" + expect { + response = get_location(location) + }.must_raise SearchError + end + end + end +end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 09d8ca29..f1dc124a 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -12,6 +12,13 @@ VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock + config.default_cassette_options = { + :record => :new_episodes, + :match_requests_on => [:method, :uri, :body], + } + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end end require_relative "../lib/recipient.rb" From d157d554b0e94e965691f5bf26783769cc244c21 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Wed, 20 Mar 2019 09:25:50 -0700 Subject: [PATCH 12/34] added test to channel_spec --- specs/channel_spec.rb | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 46b2d754..abe7a179 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,28 +1,12 @@ require_relative "test_helper" -<<<<<<< HEAD -======= describe "Channel class" do describe "Channel#list" do - it "finds channels" do - VCR.use_cassette("Channel") do - location = "Seattle" - response = get_location(location) - - expect(response["Seattle"]).wont_be_nil - expect(response["Seattle"][:lon]).must_equal "-122.3300624" - expect(response["Seattle"][:lat]).must_equal "47.6038321" - end - end - it "will raise an exception if the search fails" do VCR.use_cassette("Channel") do - location = "" - expect { - response = get_location(location) - }.must_raise SearchError + response = Channel.list + expect { response }.must_include "general" end end end end ->>>>>>> fb61b11586278c3c4324ec484a7ccbb159320108 From 5cdf34f4a6b1b876d74daf6115cc624a661ab953 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 10:23:09 -0700 Subject: [PATCH 13/34] added requires to test_helper --- lib/slack.rb | 1 - lib/user.rb | 2 ++ lib/workspace.rb | 2 -- specs/test_helper.rb | 9 +++++++++ specs/workspace_spec.rb | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 48acd385..aeef05be 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,7 +2,6 @@ require_relative "user" require_relative "channel" require "pry" - require "dotenv" require "httparty" Dotenv.load diff --git a/lib/user.rb b/lib/user.rb index 4ba2a160..61d02ae3 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -15,6 +15,8 @@ def initialize @user_names = [] end + # self.get method to assign User object a @real_name + def self.list query = { token: TOKEN, diff --git a/lib/workspace.rb b/lib/workspace.rb index 2e81b18c..354dcad1 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,9 +1,7 @@ - require_relative "recipient" require_relative "user" require_relative "channel" require "pry" - require "dotenv" require "httparty" Dotenv.load diff --git a/specs/test_helper.rb b/specs/test_helper.rb index f1dc124a..21aab23b 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -6,6 +6,15 @@ require "minitest/reporters" require "minitest/skip_dsl" require "vcr" +require "webmock/minitest" +require "httparty" +require "dotenv" +Dotenv.load + +require_relative "../lib/slack.rb" +require_relative "../lib/channel.rb" +require_relative "../lib/user.rb" +require_relative "../lib/workspace.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8b137891..b5cae113 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1 +1 @@ - +require_relative "test_helper" From 4d5c5c6c2993fdce56ac5fcaa8154680e145ae7c Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Wed, 20 Mar 2019 10:24:07 -0700 Subject: [PATCH 14/34] added test --- specs/test_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index f1dc124a..37bcae2d 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -6,6 +6,7 @@ require "minitest/reporters" require "minitest/skip_dsl" require "vcr" +require "webmock/minitest" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 17a0657a9dfffd9607a343f561b3c82de60af7d6 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 10:37:22 -0700 Subject: [PATCH 15/34] added channel_spec tests --- specs/channel_spec.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index abe7a179..bbcc722a 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,12 +1,34 @@ require_relative "test_helper" describe "Channel class" do + describe "Channel#initialize" do + it "initializes a Channel object" do + VCR.use_cassette("Channel") do + response = Channel.new + expect(response).must_be_instance_of Channel + end + end + end describe "Channel#list" do - it "will raise an exception if the search fails" do + it "includes a known channel" do VCR.use_cassette("Channel") do response = Channel.list - expect { response }.must_include "general" + expect(response).must_include "general" end end + + it "returns correct number of channels" do + VCR.use_cassette("Channel") do + response = Channel.list.length + expect(response).must_equal 3 + end + end + + # it "will raise an exception if the search fails" do + # VCR.use_cassette("Channel") do + # response = Channel.list + # expect { response }.must_raise ArgumentError + # end + # end end end From 3ff2cb35866232e754c6df5d42c17563bb4d921d Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 13:40:16 -0700 Subject: [PATCH 16/34] added tests for list users --- lib/channel.rb | 2 +- lib/user.rb | 4 ++-- specs/user_spec.rb | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 93d2ef8d..c4a24fae 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -23,7 +23,7 @@ def self.list token: TOKEN, } channel_info = HTTParty.get(BASE_URL, query: query) - if channel_info.code != 200 + if channel_info["ok"] == false raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" else channel_list = channel_info["channels"] diff --git a/lib/user.rb b/lib/user.rb index 61d02ae3..1bee6a49 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -6,7 +6,7 @@ class User attr_reader :topic, :member_count - attr_accessor :user_names + attr_accessor :user_names, :real_name BASE_URL = "https://slack.com/api/users.list" TOKEN = ENV["SLACK_API_TOKEN"] @@ -22,7 +22,7 @@ def self.list token: TOKEN, } user_info = HTTParty.get(BASE_URL, query: query) - if user_info.code != 200 + if user_info["ok"] == false raise ArgumentError, "The error code is #{user_info.code} and the reason is: #{user_info.message}" else user_list = user_info["members"] diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b5cae113..e0aa0812 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1 +1,34 @@ require_relative "test_helper" + +describe "User class" do + describe "User#initialize" do + it "initializes a User object" do + VCR.use_cassette("User") do + response = User.new + expect(response).must_be_instance_of User + end + end + end + describe "User#list" do + it "includes a known User" do + VCR.use_cassette("User") do + response = User.list + expect(response).must_include "ngocle" + end + end + + it "returns correct number of Users" do + VCR.use_cassette("User") do + response = User.list.length + expect(response).must_equal 3 + end + end + + # it "will raise an exception if the search fails" do + # VCR.use_cassette("User") do + # response = User.list + # expect { response }.must_raise ArgumentError + # end + # end + end +end From 0d8ecc540d400bcb1a2ad72917386b4889696ce1 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 13:50:00 -0700 Subject: [PATCH 17/34] added quit function --- lib/slack.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index aeef05be..b9442ec7 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,10 +11,14 @@ def main puts "Welcome to the Ada Slack CLI!" puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Quit" choice = gets.chomp - if choice == "1" - puts User.list - elsif choice == "2" - puts Channel.list + until choice == "3" + if choice == "1" + puts User.list + elsif choice == "2" + puts Channel.list + end + puts "\nChoose from the following options:\n1. List users\n2. List Channels\n3. Quit" + choice = gets.chomp end puts "Thank you for using the Ada Slack CLI" end From 204e4d3755746b2fef90dd3a55eae71411a43a42 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 14:38:53 -0700 Subject: [PATCH 18/34] added workspace pseudocode --- lib/channel.rb | 4 ---- lib/slack.rb | 24 ++++++++++++++++------- lib/workspace.rb | 50 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c4a24fae..89e3b648 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,7 +1,3 @@ -require "dotenv" -require "httparty" -Dotenv.load -require "pry" require "httparty" require "dotenv" Dotenv.load diff --git a/lib/slack.rb b/lib/slack.rb index b9442ec7..2903bd25 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -9,16 +9,26 @@ def main puts "Welcome to the Ada Slack CLI!" - puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Quit" - choice = gets.chomp - until choice == "3" - if choice == "1" + puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" + choice = gets.chomp.upcase + until choice == "QUIT" + if choice == "LIST USERS" + puts "\n" puts User.list - elsif choice == "2" + elsif choice == "LIST CHANNELS" + puts "\n" puts Channel.list + elsif choice == "SELECT USER" + puts "Please enter a username or Slack ID:" + user_input = gets.chomp.upcase + elsif choice == "SELECT CHANNEL" + puts "Please enter channel name or Slack ID:" + channel_input = gets.chomp.upcase + elsif choice == "DETAILS" + #call detail method end - puts "\nChoose from the following options:\n1. List users\n2. List Channels\n3. Quit" - choice = gets.chomp + puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" + choice = gets.chomp.upcase end puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/workspace.rb b/lib/workspace.rb index 354dcad1..5f99332c 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,8 +1,52 @@ -require_relative "recipient" require_relative "user" require_relative "channel" -require "pry" +require_relative "slack" require "dotenv" require "httparty" Dotenv.load -require "ap" + +class Workspace + attr_reader :users, :channels + attr_accessor :selected + # BASE_URL = "https://slack.com/api/channels.list" + # TOKEN = ENV["SLACK_API_TOKEN"] + + def initialize + @users = users #User.list + @channels = channels #Channel.list + @selected = selected + end + + def self.select_user(user) + #selected_user = @users.find(user) + #@selected = selected_user + end + + def self.select_channel(channel) + #selected_channel = @channels.find(channel) + #@selected = channel + end + + def self.show_details + #format details for selected user/channel w/ table gem + end + + def self.send_message + # do a post request taking selected user as parameter + end +end + +# query = { +# token: TOKEN, +# } +# channel_info = HTTParty.get(BASE_URL, query: query) +# if channel_info["ok"] == false +# raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" +# else +# channel_list = channel_info["channels"] +# @channel_names = channel_list.map do |channel| +# channel["name"] +# end +# return @channel_names +# end +# end From 3e47d87147f646c9ea538e84ebb0f3d58527d3f1 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 14:50:58 -0700 Subject: [PATCH 19/34] refactoring list methods --- lib/channel.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 89e3b648..1b022602 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -14,18 +14,30 @@ def initialize @channel_names = [] end - def self.list + def self.get query = { token: TOKEN, } channel_info = HTTParty.get(BASE_URL, query: query) if channel_info["ok"] == false raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" - else - channel_list = channel_info["channels"] - @channel_names = channel_list.map do |channel| - channel["name"] - end + end + return channel_info + end + + def self.list + # query = { + # token: TOKEN, + # } + # channel_info = HTTParty.get(BASE_URL, query: query) + # if channel_info["ok"] == false + # raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" + # else + channel_info = Channel.get + channel_list = channel_info["channels"] + @channel_names = channel_list.map do |channel| + channel["name"] + # end return @channel_names end end From 2432233f1fb9ff6b75444a5daf23cb6fccf9289a Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 14:56:43 -0700 Subject: [PATCH 20/34] refactored list users method --- lib/channel.rb | 11 ++--------- lib/user.rb | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 1b022602..e7db0322 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,6 +1,7 @@ require "httparty" require "dotenv" Dotenv.load +require "pry" class Channel attr_reader :topic, :member_count @@ -26,19 +27,11 @@ def self.get end def self.list - # query = { - # token: TOKEN, - # } - # channel_info = HTTParty.get(BASE_URL, query: query) - # if channel_info["ok"] == false - # raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" - # else channel_info = Channel.get channel_list = channel_info["channels"] @channel_names = channel_list.map do |channel| channel["name"] - # end - return @channel_names end + return @channel_names end end diff --git a/lib/user.rb b/lib/user.rb index 1bee6a49..bfbd7681 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -15,21 +15,23 @@ def initialize @user_names = [] end - # self.get method to assign User object a @real_name - - def self.list + def self.get query = { token: TOKEN, } user_info = HTTParty.get(BASE_URL, query: query) if user_info["ok"] == false raise ArgumentError, "The error code is #{user_info.code} and the reason is: #{user_info.message}" - else - user_list = user_info["members"] - @user_names = user_list.map do |user| - user["name"] - end - return @user_names end + return user_info + end + + def self.list + user_info = User.get + user_list = user_info["members"] + @user_names = user_list.map do |user| + user["name"] + end + return @user_names end end From 0a02d515bd1108317ce52848ba77489c118dea04 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 21:18:13 -0700 Subject: [PATCH 21/34] select_user code passes tests --- lib/slack.rb | 2 +- lib/user.rb | 11 +++++------ lib/workspace.rb | 40 +++++++++++----------------------------- specs/test_helper.rb | 6 ------ specs/workspace_spec.rb | 30 ++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 2903bd25..0a541f19 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -25,7 +25,7 @@ def main puts "Please enter channel name or Slack ID:" channel_input = gets.chomp.upcase elsif choice == "DETAILS" - #call detail method + #call get method end puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" choice = gets.chomp.upcase diff --git a/lib/user.rb b/lib/user.rb index bfbd7681..bab82cdf 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,29 +1,28 @@ -require_relative "recipient" require "dotenv" require "httparty" Dotenv.load -require "pry" class User attr_reader :topic, :member_count - attr_accessor :user_names, :real_name + attr_accessor :user_names, :real_name, :user_info BASE_URL = "https://slack.com/api/users.list" TOKEN = ENV["SLACK_API_TOKEN"] def initialize @real_name = real_name @user_names = [] + @user_info = User.get end def self.get query = { token: TOKEN, } - user_info = HTTParty.get(BASE_URL, query: query) - if user_info["ok"] == false + @user_info = HTTParty.get(BASE_URL, query: query) + if @user_info["ok"] == false raise ArgumentError, "The error code is #{user_info.code} and the reason is: #{user_info.message}" end - return user_info + return @user_info end def self.list diff --git a/lib/workspace.rb b/lib/workspace.rb index 5f99332c..d9ff8bfd 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -6,25 +6,22 @@ Dotenv.load class Workspace - attr_reader :users, :channels - attr_accessor :selected - # BASE_URL = "https://slack.com/api/channels.list" - # TOKEN = ENV["SLACK_API_TOKEN"] + attr_accessor :selected, :users, :channels def initialize - @users = users #User.list - @channels = channels #Channel.list + @users = User.get + @channels = Channel.get @selected = selected end - def self.select_user(user) - #selected_user = @users.find(user) - #@selected = selected_user - end - - def self.select_channel(channel) - #selected_channel = @channels.find(channel) - #@selected = channel + def select_user(user) + selected_user = @users["members"].select { |user_info| user_info["name"] == user } + if selected_user.empty? + raise ArgumentError, "That user is invalid" + else + @selected = selected_user + return @selected + end end def self.show_details @@ -35,18 +32,3 @@ def self.send_message # do a post request taking selected user as parameter end end - -# query = { -# token: TOKEN, -# } -# channel_info = HTTParty.get(BASE_URL, query: query) -# if channel_info["ok"] == false -# raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}" -# else -# channel_list = channel_info["channels"] -# @channel_names = channel_list.map do |channel| -# channel["name"] -# end -# return @channel_names -# end -# end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 21aab23b..efe23561 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -29,9 +29,3 @@ ENV["SLACK_API_TOKEN"] end end - -require_relative "../lib/recipient.rb" -require_relative "../lib/user.rb" -require_relative "../lib/channel.rb" -require_relative "../lib/slack.rb" -require_relative "../lib/workspace.rb" diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b5cae113..cf09b25f 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1 +1,31 @@ require_relative "test_helper" +require "pry" + +describe "Workspace class" do + describe "Workspace#initialize" do + it "initializes a Workspace object" do + VCR.use_cassette("Workspace") do + response = Workspace.new + #binding.pry + expect(response).must_be_instance_of Workspace + end + end + end + + describe "select_user" do + it "selects a known user" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_user("ngocle") + expect(response.selected[0]["name"]).must_equal "ngocle" + end + end + + it "raises an Argument if user info is invalid" do + VCR.use_cassette("Channel") do + response = Workspace.new + expect { response.select_user("tildee") }.must_raise ArgumentError + end + end + end +end From 0433674cf7620976dba6745c2be70671588b6044 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 20 Mar 2019 21:23:36 -0700 Subject: [PATCH 22/34] add select_channel tests and code --- lib/workspace.rb | 10 ++++++++++ specs/workspace_spec.rb | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index d9ff8bfd..22ae1cfa 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -24,6 +24,16 @@ def select_user(user) end end + def select_channel(channel) + selected_channel = @channels["channels"].select { |channel_info| channel_info["name"] == channel } + if selected_channel.empty? + raise ArgumentError, "That channel is invalid" + else + @selected = selected_channel + return @selected + end + end + def self.show_details #format details for selected user/channel w/ table gem end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index cf09b25f..4cbe2023 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -6,7 +6,6 @@ it "initializes a Workspace object" do VCR.use_cassette("Workspace") do response = Workspace.new - #binding.pry expect(response).must_be_instance_of Workspace end end @@ -28,4 +27,21 @@ end end end + + describe "select_channel" do + it "selects a known channel" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_channel("general") + expect(response.selected[0]["name"]).must_equal "general" + end + end + + it "raises an Argument if channel info is invalid" do + VCR.use_cassette("Channel") do + response = Workspace.new + expect { response.select_channel("fur_babes") }.must_raise ArgumentError + end + end + end end From 57eb25ac70427b98dc164bc842f0ae4a39a1ef38 Mon Sep 17 00:00:00 2001 From: amyesh Date: Thu, 21 Mar 2019 10:01:39 -0700 Subject: [PATCH 23/34] added test for get method --- lib/channel.rb | 1 - lib/slack.rb | 6 ++++-- lib/workspace.rb | 4 ++-- specs/channel_spec.rb | 17 ++++++++++------- specs/user_spec.rb | 17 ++++++++++------- specs/workspace_spec.rb | 5 ++++- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index e7db0322..88b3cba8 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,7 +1,6 @@ require "httparty" require "dotenv" Dotenv.load -require "pry" class Channel attr_reader :topic, :member_count diff --git a/lib/slack.rb b/lib/slack.rb index 0a541f19..4e08d448 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,7 +1,6 @@ require_relative "recipient" require_relative "user" require_relative "channel" -require "pry" require "dotenv" require "httparty" Dotenv.load @@ -25,7 +24,10 @@ def main puts "Please enter channel name or Slack ID:" channel_input = gets.chomp.upcase elsif choice == "DETAILS" - #call get method + #call workspace.selected + #if that's nil or empty, raise error + #else call details method on selected + #return details end puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" choice = gets.chomp.upcase diff --git a/lib/workspace.rb b/lib/workspace.rb index 22ae1cfa..10bfc43d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -15,7 +15,7 @@ def initialize end def select_user(user) - selected_user = @users["members"].select { |user_info| user_info["name"] == user } + selected_user = @users["members"].select { |user_info| user_info.has_value?(user) } if selected_user.empty? raise ArgumentError, "That user is invalid" else @@ -25,7 +25,7 @@ def select_user(user) end def select_channel(channel) - selected_channel = @channels["channels"].select { |channel_info| channel_info["name"] == channel } + selected_channel = @channels["channels"].select { |channel_info| channel_info.has_value?(channel) } if selected_channel.empty? raise ArgumentError, "That channel is invalid" else diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index bbcc722a..7b249ad6 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -9,6 +9,16 @@ end end end + + describe "Channel#get" do + it "returns information from the API" do + VCR.use_cassette("Channel") do + response = Channel.get + expect(response["ok"]).must_equal true + end + end + end + describe "Channel#list" do it "includes a known channel" do VCR.use_cassette("Channel") do @@ -23,12 +33,5 @@ expect(response).must_equal 3 end end - - # it "will raise an exception if the search fails" do - # VCR.use_cassette("Channel") do - # response = Channel.list - # expect { response }.must_raise ArgumentError - # end - # end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e0aa0812..96d70ca2 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -9,6 +9,16 @@ end end end + + describe "User#get" do + it "returns information from the API" do + VCR.use_cassette("User") do + response = User.get + expect(response["ok"]).must_equal true + end + end + end + describe "User#list" do it "includes a known User" do VCR.use_cassette("User") do @@ -23,12 +33,5 @@ expect(response).must_equal 3 end end - - # it "will raise an exception if the search fails" do - # VCR.use_cassette("User") do - # response = User.list - # expect { response }.must_raise ArgumentError - # end - # end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 4cbe2023..f9f078b9 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,5 +1,4 @@ require_relative "test_helper" -require "pry" describe "Workspace class" do describe "Workspace#initialize" do @@ -20,6 +19,8 @@ end end + #ADD SLACK ID SEARCH TEST HERE + it "raises an Argument if user info is invalid" do VCR.use_cassette("Channel") do response = Workspace.new @@ -37,6 +38,8 @@ end end + #ADD SLACK ID SEARCH TEST HERE + it "raises an Argument if channel info is invalid" do VCR.use_cassette("Channel") do response = Workspace.new From f24cbe3c7d3e19a54c87bbd135fa2d61539f2b91 Mon Sep 17 00:00:00 2001 From: amyesh Date: Thu, 21 Mar 2019 13:29:12 -0700 Subject: [PATCH 24/34] refactored model classes to return class objects --- lib/channel.rb | 18 ++++++++++-------- lib/user.rb | 14 +++++++------- lib/workspace.rb | 27 +++++++++++++++++++-------- specs/channel_spec.rb | 6 ++++-- specs/user_spec.rb | 11 +++++++++-- specs/workspace_spec.rb | 28 ++++++++++++++++++++-------- 6 files changed, 69 insertions(+), 35 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 88b3cba8..f7d1348c 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,17 +1,19 @@ require "httparty" require "dotenv" Dotenv.load +require "pry" class Channel - attr_reader :topic, :member_count - attr_accessor :channel_names + attr_accessor :channel_name, :id, :topic, :members, :member_count BASE_URL = "https://slack.com/api/channels.list" TOKEN = ENV["SLACK_API_TOKEN"] - def initialize + def initialize(channel_name, id, topic, members) + @channel_name = channel_name + @id = id @topic = topic - @member_count = member_count - @channel_names = [] + @members = members + @member_count = members.length end def self.get @@ -28,9 +30,9 @@ def self.get def self.list channel_info = Channel.get channel_list = channel_info["channels"] - @channel_names = channel_list.map do |channel| - channel["name"] + channels = channel_list.map do |channel| + Channel.new(channel["name"], channel["id"], channel["topic"], channel["members"]) end - return @channel_names + return channels end end diff --git a/lib/user.rb b/lib/user.rb index bab82cdf..b64fffbc 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -3,14 +3,14 @@ Dotenv.load class User - attr_reader :topic, :member_count - attr_accessor :user_names, :real_name, :user_info + attr_accessor :username, :real_name, :user_info, :id BASE_URL = "https://slack.com/api/users.list" TOKEN = ENV["SLACK_API_TOKEN"] - def initialize + def initialize(real_name, username, id) + @username = username @real_name = real_name - @user_names = [] + @id = id @user_info = User.get end @@ -28,9 +28,9 @@ def self.get def self.list user_info = User.get user_list = user_info["members"] - @user_names = user_list.map do |user| - user["name"] + users = user_list.map do |user| + User.new(user["real_name"], user["name"], user["id"]) end - return @user_names + return users end end diff --git a/lib/workspace.rb b/lib/workspace.rb index 10bfc43d..4430908d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -3,20 +3,26 @@ require_relative "slack" require "dotenv" require "httparty" +require "pry" Dotenv.load class Workspace attr_accessor :selected, :users, :channels def initialize - @users = User.get - @channels = Channel.get + @users = User.list + @channels = Channel.list @selected = selected end - def select_user(user) - selected_user = @users["members"].select { |user_info| user_info.has_value?(user) } - if selected_user.empty? + def select_user(user_identifier) + selected_user = nil + @users.each do |user| + if user.username == user_identifier || user.id == user_identifier + selected_user = user + end + end + if selected_user == nil raise ArgumentError, "That user is invalid" else @selected = selected_user @@ -24,9 +30,14 @@ def select_user(user) end end - def select_channel(channel) - selected_channel = @channels["channels"].select { |channel_info| channel_info.has_value?(channel) } - if selected_channel.empty? + def select_channel(channel_identifier) + selected_channel = nil + @channels.each do |channel| + if channel.channel_name == channel_identifier || channel.id == channel_identifier + selected_channel = channel + end + end + if selected_channel == nil raise ArgumentError, "That channel is invalid" else @selected = selected_channel diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 7b249ad6..26274b33 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -1,10 +1,11 @@ require_relative "test_helper" +require "pry" describe "Channel class" do describe "Channel#initialize" do it "initializes a Channel object" do VCR.use_cassette("Channel") do - response = Channel.new + response = Channel.new("fur_babes", "1234", "pets", ["AUAIWFS"]) expect(response).must_be_instance_of Channel end end @@ -23,7 +24,8 @@ it "includes a known channel" do VCR.use_cassette("Channel") do response = Channel.list - expect(response).must_include "general" + expect(response[0]).must_be_instance_of Channel + expect(response[0].member_count).must_be_instance_of Integer end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 96d70ca2..b55f3a63 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,7 +4,7 @@ describe "User#initialize" do it "initializes a User object" do VCR.use_cassette("User") do - response = User.new + response = User.new("Larry", "Truck", "1234") expect(response).must_be_instance_of User end end @@ -23,7 +23,14 @@ it "includes a known User" do VCR.use_cassette("User") do response = User.list - expect(response).must_include "ngocle" + expect(response[0]).must_be_instance_of User + end + end + + it "User details are correct" do + VCR.use_cassette("User") do + response = User.list + expect(response[1].username).must_equal "amyesh08" end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index f9f078b9..af3cb9ab 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -11,18 +11,24 @@ end describe "select_user" do - it "selects a known user" do + it "selects a known user by username" do VCR.use_cassette("Workspace") do response = Workspace.new response.select_user("ngocle") - expect(response.selected[0]["name"]).must_equal "ngocle" + expect(response.selected.username).must_equal "ngocle" end end - #ADD SLACK ID SEARCH TEST HERE + it "selects a known user by slack id" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_user("UH553UM7G") + expect(response.selected.username).must_equal "ngocle" + end + end it "raises an Argument if user info is invalid" do - VCR.use_cassette("Channel") do + VCR.use_cassette("Workspace") do response = Workspace.new expect { response.select_user("tildee") }.must_raise ArgumentError end @@ -34,16 +40,22 @@ VCR.use_cassette("Workspace") do response = Workspace.new response.select_channel("general") - expect(response.selected[0]["name"]).must_equal "general" + expect(response.selected.channel_name).must_equal "general" end end - #ADD SLACK ID SEARCH TEST HERE + it "selects a known channel by slack id" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_channel("CH41W659D") + expect(response.selected.channel_name).must_equal "general" + end + end it "raises an Argument if channel info is invalid" do - VCR.use_cassette("Channel") do + VCR.use_cassette("Workspace") do response = Workspace.new - expect { response.select_channel("fur_babes") }.must_raise ArgumentError + expect { response.select_channel("starburst") }.must_raise ArgumentError end end end From 6733aa60d702e0eb003afc10f1ba20b29e827684 Mon Sep 17 00:00:00 2001 From: amyesh Date: Fri, 22 Mar 2019 10:09:10 -0700 Subject: [PATCH 25/34] added table_print functionality to Slack CLI --- lib/channel.rb | 2 +- lib/slack.rb | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index f7d1348c..4e057032 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -31,7 +31,7 @@ def self.list channel_info = Channel.get channel_list = channel_info["channels"] channels = channel_list.map do |channel| - Channel.new(channel["name"], channel["id"], channel["topic"], channel["members"]) + Channel.new(channel["name"], channel["id"], channel["topic"]["value"], channel["members"]) end return channels end diff --git a/lib/slack.rb b/lib/slack.rb index 4e08d448..d762fef5 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,37 +1,43 @@ require_relative "recipient" require_relative "user" require_relative "channel" +require_relative "workspace" require "dotenv" require "httparty" +require "table_print" Dotenv.load -require "ap" def main + slack = Workspace.new puts "Welcome to the Ada Slack CLI!" puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" choice = gets.chomp.upcase + until choice == "QUIT" if choice == "LIST USERS" puts "\n" - puts User.list + tp slack.users + puts "\n" elsif choice == "LIST CHANNELS" puts "\n" - puts Channel.list + tp slack.channels + puts "\n" elsif choice == "SELECT USER" puts "Please enter a username or Slack ID:" - user_input = gets.chomp.upcase + user_input = gets.chomp + slack.select_user(user_input) elsif choice == "SELECT CHANNEL" puts "Please enter channel name or Slack ID:" - channel_input = gets.chomp.upcase + channel_input = gets.chomp + slack.select_channel(channel_input) elsif choice == "DETAILS" - #call workspace.selected - #if that's nil or empty, raise error - #else call details method on selected - #return details + slack.show_details end + puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" choice = gets.chomp.upcase end + puts "Thank you for using the Ada Slack CLI" end From e213d765c552bcec191dc4787f81369dde51e2c3 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Fri, 22 Mar 2019 10:14:16 -0700 Subject: [PATCH 26/34] added select channel/user and details functions --- lib/channel.rb | 2 +- lib/slack.rb | 40 +++++++++++++++++++++++++--------------- lib/user.rb | 9 ++++++--- lib/workspace.rb | 16 ++++++---------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index f7d1348c..cffd0f65 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,6 +1,6 @@ -require "httparty" require "dotenv" Dotenv.load +require "httparty" require "pry" class Channel diff --git a/lib/slack.rb b/lib/slack.rb index 4e08d448..035992f2 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,38 +1,48 @@ -require_relative "recipient" -require_relative "user" -require_relative "channel" require "dotenv" -require "httparty" Dotenv.load -require "ap" +require "table_print" +require "httparty" +require "colorize" +require_relative "workspace" +require_relative "user" +require_relative "channel" def main - puts "Welcome to the Ada Slack CLI!" - puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" + work_space = Workspace.new + puts "Welcome to the Ada Slack CLI!".colorize(:color => :blue, :mode => :bold) + puts "\nPlease Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase + until choice == "QUIT" if choice == "LIST USERS" puts "\n" - puts User.list + tp work_space.users, "username", "real_name", "id" elsif choice == "LIST CHANNELS" puts "\n" - puts Channel.list + tp work_space.channels, "channel_name", "id", "topic", "members" elsif choice == "SELECT USER" - puts "Please enter a username or Slack ID:" - user_input = gets.chomp.upcase + puts "Here is a list of users:".colorize(:color => :magenta) + tp work_space.users, "username", "id" + puts "Please enter an username or Slack ID:".colorize(:color => :blue, :mode => :bold) + user_input = gets.chomp + user_recipient = work_space.select_user(user_input) elsif choice == "SELECT CHANNEL" - puts "Please enter channel name or Slack ID:" - channel_input = gets.chomp.upcase + puts "Here is a list of channels:".colorize(:color => :magenta) + tp work_space.channels, "channel_name", "id", "topic", "members" + puts "Please enter channel name or Slack ID:".colorize(:color => :blue, :mode => :bold) + channel_input = gets.chomp + channel_recipient = work_space.select_channel(channel_input) elsif choice == "DETAILS" + puts work_space.selected #call workspace.selected #if that's nil or empty, raise error #else call details method on selected #return details end - puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" + puts "\nWhat would you like to do next?\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase end - puts "Thank you for using the Ada Slack CLI" + puts "Thank you for using the Ada Slack CLI".colorize(:color => :green, :mode => :bold) end main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index b64fffbc..4bad1330 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,9 +1,10 @@ require "dotenv" -require "httparty" Dotenv.load +require "httparty" +require "pry" class User - attr_accessor :username, :real_name, :user_info, :id + attr_accessor :username, :real_name, :id BASE_URL = "https://slack.com/api/users.list" TOKEN = ENV["SLACK_API_TOKEN"] @@ -20,7 +21,7 @@ def self.get } @user_info = HTTParty.get(BASE_URL, query: query) if @user_info["ok"] == false - raise ArgumentError, "The error code is #{user_info.code} and the reason is: #{user_info.message}" + raise ArgumentError, "The error code is #{@user_info.code} and the reason is: #{@user_info["error"]}" end return @user_info end @@ -34,3 +35,5 @@ def self.list return users end end + +# binding.pry diff --git a/lib/workspace.rb b/lib/workspace.rb index 4430908d..5d867b33 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -7,40 +7,36 @@ Dotenv.load class Workspace - attr_accessor :selected, :users, :channels + attr_accessor :selected, :selected, :users, :channels def initialize @users = User.list @channels = Channel.list - @selected = selected + @selected = nil end def select_user(user_identifier) - selected_user = nil @users.each do |user| if user.username == user_identifier || user.id == user_identifier - selected_user = user + @selected = user end end - if selected_user == nil + if @selected == nil raise ArgumentError, "That user is invalid" else - @selected = selected_user return @selected end end def select_channel(channel_identifier) - selected_channel = nil @channels.each do |channel| if channel.channel_name == channel_identifier || channel.id == channel_identifier - selected_channel = channel + @selected = channel end end - if selected_channel == nil + if @selected == nil raise ArgumentError, "That channel is invalid" else - @selected = selected_channel return @selected end end From c2626754f6b97fc8a1eb45ba38834eda15b3b6d7 Mon Sep 17 00:00:00 2001 From: amyesh Date: Fri, 22 Mar 2019 10:18:14 -0700 Subject: [PATCH 27/34] added show details method and test --- lib/workspace.rb | 8 +++++--- specs/workspace_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 4430908d..73c162e5 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -3,7 +3,9 @@ require_relative "slack" require "dotenv" require "httparty" -require "pry" +require "table_print" +tp.set User, :username, :id +tp.set Channel, :channel_name, :id, :members, :topic Dotenv.load class Workspace @@ -45,8 +47,8 @@ def select_channel(channel_identifier) end end - def self.show_details - #format details for selected user/channel w/ table gem + def show_details + tp @selected end def self.send_message diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index af3cb9ab..b619145a 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -59,4 +59,14 @@ end end end + + describe "show_details" do + it "shows details of the selected channel" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_channel("general") + expect(response.show_details).must_be_instance_of TablePrint::Returnable + end + end + end end From 7d9e5e93158c6265a517bf69c6783380dfe70608 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Fri, 22 Mar 2019 10:23:55 -0700 Subject: [PATCH 28/34] merged changes on show details method in workspace --- lib/slack.rb | 46 ++-------------------------------------------- 1 file changed, 2 insertions(+), 44 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 5c5f4c9e..b77a9c44 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,4 +1,3 @@ -<<<<<<< HEAD require "dotenv" Dotenv.load require "table_print" @@ -12,27 +11,11 @@ def main work_space = Workspace.new puts "Welcome to the Ada Slack CLI!".colorize(:color => :blue, :mode => :bold) puts "\nPlease Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) -======= -require_relative "recipient" -require_relative "user" -require_relative "channel" -require_relative "workspace" -require "dotenv" -require "httparty" -require "table_print" -Dotenv.load - -def main - slack = Workspace.new - puts "Welcome to the Ada Slack CLI!" - puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" ->>>>>>> 6733aa60d702e0eb003afc10f1ba20b29e827684 choice = gets.chomp.upcase until choice == "QUIT" if choice == "LIST USERS" puts "\n" -<<<<<<< HEAD tp work_space.users, "username", "real_name", "id" elsif choice == "LIST CHANNELS" puts "\n" @@ -50,7 +33,7 @@ def main channel_input = gets.chomp channel_recipient = work_space.select_channel(channel_input) elsif choice == "DETAILS" - puts work_space.selected + puts work_space.show_details #call workspace.selected #if that's nil or empty, raise error #else call details method on selected @@ -59,32 +42,7 @@ def main puts "\nWhat would you like to do next?\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase end - puts "Thank you for using the Ada Slack CLI".colorize(:color => :green, :mode => :bold) -======= - tp slack.users - puts "\n" - elsif choice == "LIST CHANNELS" - puts "\n" - tp slack.channels - puts "\n" - elsif choice == "SELECT USER" - puts "Please enter a username or Slack ID:" - user_input = gets.chomp - slack.select_user(user_input) - elsif choice == "SELECT CHANNEL" - puts "Please enter channel name or Slack ID:" - channel_input = gets.chomp - slack.select_channel(channel_input) - elsif choice == "DETAILS" - slack.show_details - end - - puts "Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit" - choice = gets.chomp.upcase - end - - puts "Thank you for using the Ada Slack CLI" ->>>>>>> 6733aa60d702e0eb003afc10f1ba20b29e827684 + puts "Thank you for using the Ada Slack CLI!".colorize(:color => :green, :mode => :bold) end main if __FILE__ == $PROGRAM_NAME From 0eb802715a4a073efc80d47620753b6446caa1b6 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Fri, 22 Mar 2019 13:02:01 -0700 Subject: [PATCH 29/34] added test for workspace details and message function --- lib/workspace.rb | 17 ++++++++++++++--- specs/workspace_spec.rb | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index c9e595ae..d3ad1497 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,7 +9,7 @@ Dotenv.load class Workspace - attr_accessor :selected, :selected, :users, :channels + attr_accessor :selected, :users, :channels def initialize @users = User.list @@ -47,7 +47,18 @@ def show_details tp @selected end - def self.send_message - # do a post request taking selected user as parameter + def send_message(message) + post_url = "https://slack.com/api/" + token = ENV["SLACK_API_TOKEN"] + response = HTTParty.post( + "#{post_url}/chat.postMessage", + body: { + token: token, + text: message, + channel: @selected.id, + }, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + ) + return response.code == 200 && response.parsed_response["ok"] end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b619145a..7d3ec2a8 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -10,7 +10,7 @@ end end - describe "select_user" do + describe "Workspace#select_user" do it "selects a known user by username" do VCR.use_cassette("Workspace") do response = Workspace.new @@ -35,7 +35,7 @@ end end - describe "select_channel" do + describe "Workspace#select_channel" do it "selects a known channel" do VCR.use_cassette("Workspace") do response = Workspace.new @@ -60,7 +60,7 @@ end end - describe "show_details" do + describe "Workspace#show_details" do it "shows details of the selected channel" do VCR.use_cassette("Workspace") do response = Workspace.new @@ -68,5 +68,32 @@ expect(response.show_details).must_be_instance_of TablePrint::Returnable end end + + it "shows details of the selected user" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_user("ngocle") + expect(response.show_details).must_be_instance_of TablePrint::Returnable + end + end + end + describe "Workspace#send_message" do + it "can send a valid message to a channel" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_channel("general") + status = response.send_message("Hey I can post messages!") + expect(status).must_equal true + end + end + + it "can send a valid message to an user" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_user("ngocle") + status = response.send_message("Hey I can post messages!") + expect(status).must_equal true + end + end end end From 5fe7ccebf1d7fd08ad60229f681df7bd704c2e8d Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Fri, 22 Mar 2019 13:43:17 -0700 Subject: [PATCH 30/34] added rescue block to slack.rb --- lib/slack.rb | 36 ++++++++++++++++++++++-------------- lib/workspace.rb | 6 +++++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index b77a9c44..8905776b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -10,7 +10,7 @@ def main work_space = Workspace.new puts "Welcome to the Ada Slack CLI!".colorize(:color => :blue, :mode => :bold) - puts "\nPlease Choose from the following options:\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) + puts "\nPlease Choose from the following options:\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase until choice == "QUIT" @@ -19,27 +19,35 @@ def main tp work_space.users, "username", "real_name", "id" elsif choice == "LIST CHANNELS" puts "\n" - tp work_space.channels, "channel_name", "id", "topic", "members" + tp work_space.channels, "channel_name", "id", "topic", :members => {:width => 60} elsif choice == "SELECT USER" puts "Here is a list of users:".colorize(:color => :magenta) tp work_space.users, "username", "id" - puts "Please enter an username or Slack ID:".colorize(:color => :blue, :mode => :bold) - user_input = gets.chomp - user_recipient = work_space.select_user(user_input) + begin + puts "Please enter a username or Slack ID:".colorize(:color => :blue, :mode => :bold) + user_input = gets.chomp + user_recipient = work_space.select_user(user_input) + rescue ArgumentError + puts "The username or SlackID is invalid." + end elsif choice == "SELECT CHANNEL" puts "Here is a list of channels:".colorize(:color => :magenta) tp work_space.channels, "channel_name", "id", "topic", "members" - puts "Please enter channel name or Slack ID:".colorize(:color => :blue, :mode => :bold) - channel_input = gets.chomp - channel_recipient = work_space.select_channel(channel_input) + begin + puts "Please enter channel name or Slack ID:".colorize(:color => :blue, :mode => :bold) + channel_input = gets.chomp + channel_recipient = work_space.select_channel(channel_input) + rescue ArgumentError + puts "The channel name or SlackID is invalid." + end elsif choice == "DETAILS" - puts work_space.show_details - #call workspace.selected - #if that's nil or empty, raise error - #else call details method on selected - #return details + begin + tp work_space.show_details + rescue ArgumentError + puts "There is no recipient selected. Please select a user or a channel" + end end - puts "\nWhat would you like to do next?\n1. List users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) + puts "\nWhat would you like to do next?\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase end puts "Thank you for using the Ada Slack CLI!".colorize(:color => :green, :mode => :bold) diff --git a/lib/workspace.rb b/lib/workspace.rb index d3ad1497..514240f7 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -44,7 +44,11 @@ def select_channel(channel_identifier) end def show_details - tp @selected + if @selected == nil + raise ArgumentError + else + return @selected + end end def send_message(message) From 91b31b2be256f81e5e218c0c9b4e57af25ea7a70 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Fri, 22 Mar 2019 15:20:53 -0700 Subject: [PATCH 31/34] Added test on empty message, debugged --- lib/channel.rb | 3 +-- lib/slack.rb | 17 ++++++++++++++--- lib/user.rb | 5 +---- lib/workspace.rb | 20 ++++++++++++++------ specs/workspace_spec.rb | 11 +++++++++-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 51adf2bc..8bb8988d 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,10 +1,9 @@ require "dotenv" Dotenv.load require "httparty" -require "pry" class Channel - attr_accessor :channel_name, :id, :topic, :members, :member_count + attr_reader :channel_name, :id, :topic, :members, :member_count BASE_URL = "https://slack.com/api/channels.list" TOKEN = ENV["SLACK_API_TOKEN"] diff --git a/lib/slack.rb b/lib/slack.rb index 8905776b..a91038b9 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -10,7 +10,7 @@ def main work_space = Workspace.new puts "Welcome to the Ada Slack CLI!".colorize(:color => :blue, :mode => :bold) - puts "\nPlease Choose from the following options:\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) + puts "\nPlease Choose from the following options:\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Send Message\n7. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase until choice == "QUIT" @@ -44,10 +44,21 @@ def main begin tp work_space.show_details rescue ArgumentError - puts "There is no recipient selected. Please select a user or a channel" + puts "There is no recipient selected. Please select a user or a channel." end + elsif choice == "SEND MESSAGE" + puts "Please enter your message below:" + user_message = gets.chomp + begin + work_space.send_message(user_message) + rescue ArgumentError + puts "The message is invalid. Please try again." + rescue NoMethodError + puts "Message failed to send. Please select a recipient and try again." + end + puts "Your message was successfully sent to the recipient!" end - puts "\nWhat would you like to do next?\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Quit".colorize(:color => :blue, :mode => :bold) + puts "\nWhat would you like to do next?\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Send Message\n7. Quit".colorize(:color => :blue, :mode => :bold) choice = gets.chomp.upcase end puts "Thank you for using the Ada Slack CLI!".colorize(:color => :green, :mode => :bold) diff --git a/lib/user.rb b/lib/user.rb index 4bad1330..70fa2971 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,10 +1,9 @@ require "dotenv" Dotenv.load require "httparty" -require "pry" class User - attr_accessor :username, :real_name, :id + attr_reader :username, :real_name, :id BASE_URL = "https://slack.com/api/users.list" TOKEN = ENV["SLACK_API_TOKEN"] @@ -35,5 +34,3 @@ def self.list return users end end - -# binding.pry diff --git a/lib/workspace.rb b/lib/workspace.rb index 514240f7..575951f6 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,7 +9,7 @@ Dotenv.load class Workspace - attr_accessor :selected, :users, :channels + attr_reader :selected, :users, :channels def initialize @users = User.list @@ -18,27 +18,31 @@ def initialize end def select_user(user_identifier) + selected_user = nil @users.each do |user| if user.username == user_identifier || user.id == user_identifier - @selected = user + selected_user = user end end - if @selected == nil + if selected_user == nil raise ArgumentError, "That user is invalid" else + @selected = selected_user return @selected end end def select_channel(channel_identifier) + selected_channel = nil @channels.each do |channel| if channel.channel_name == channel_identifier || channel.id == channel_identifier - @selected = channel + selected_channel = channel end end - if @selected == nil + if selected_channel == nil raise ArgumentError, "That channel is invalid" else + @selected = selected_channel return @selected end end @@ -63,6 +67,10 @@ def send_message(message) }, headers: {"Content-Type" => "application/x-www-form-urlencoded"}, ) - return response.code == 200 && response.parsed_response["ok"] + if response["ok"] + return response["ok"] + else + raise ArgumentError + end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 7d3ec2a8..937cbf39 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -65,7 +65,7 @@ VCR.use_cassette("Workspace") do response = Workspace.new response.select_channel("general") - expect(response.show_details).must_be_instance_of TablePrint::Returnable + expect(response.show_details).must_be_instance_of Channel end end @@ -73,7 +73,7 @@ VCR.use_cassette("Workspace") do response = Workspace.new response.select_user("ngocle") - expect(response.show_details).must_be_instance_of TablePrint::Returnable + expect(response.show_details).must_be_instance_of User end end end @@ -86,6 +86,13 @@ expect(status).must_equal true end end + it "raise an argument error when a message is invalid(empty,nil)" do + VCR.use_cassette("Workspace") do + response = Workspace.new + response.select_channel("general") + expect { response.send_message("") }.must_raise ArgumentError + end + end it "can send a valid message to an user" do VCR.use_cassette("Workspace") do From 5c8a3132c49a58415e94686e557449f498dc9090 Mon Sep 17 00:00:00 2001 From: lebaongoc <37170363+lebaongoc@users.noreply.github.com> Date: Sat, 23 Mar 2019 22:12:24 -0700 Subject: [PATCH 32/34] Delete recipient.rb --- lib/recipient.rb | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 lib/recipient.rb diff --git a/lib/recipient.rb b/lib/recipient.rb deleted file mode 100644 index a593f685..00000000 --- a/lib/recipient.rb +++ /dev/null @@ -1,22 +0,0 @@ - -class Recipient - # attr_reader :slack_id, :name - - # def initialize - # @slack_id = slack_id - # @name = name - # end - - # def send_message(message) - # end - - # # def self.get(url, params) - # # info = HTTParty.get(url, params) - # # end - - # def details - # end - - # # def self.list - # # end -end From 388e9ad24f4d99717f7eb3ff164e6fbe192d20e9 Mon Sep 17 00:00:00 2001 From: lebaongoc <37170363+lebaongoc@users.noreply.github.com> Date: Sat, 23 Mar 2019 22:13:24 -0700 Subject: [PATCH 33/34] Delete recipient_spec.rb --- specs/recipient_spec.rb | 1 - 1 file changed, 1 deletion(-) delete mode 100644 specs/recipient_spec.rb diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb deleted file mode 100644 index b5cae113..00000000 --- a/specs/recipient_spec.rb +++ /dev/null @@ -1 +0,0 @@ -require_relative "test_helper" From 1d18267efdf363714396a7ffdead0dede980c189 Mon Sep 17 00:00:00 2001 From: lebaongoc <37170363+lebaongoc@users.noreply.github.com> Date: Sat, 23 Mar 2019 22:14:05 -0700 Subject: [PATCH 34/34] Update slack.rb --- lib/slack.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index a91038b9..8b9af819 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -19,7 +19,7 @@ def main tp work_space.users, "username", "real_name", "id" elsif choice == "LIST CHANNELS" puts "\n" - tp work_space.channels, "channel_name", "id", "topic", :members => {:width => 60} + tp work_space.channels, "channel_name", "id", "topic" elsif choice == "SELECT USER" puts "Here is a list of users:".colorize(:color => :magenta) tp work_space.users, "username", "id"