From dbbcbcda44388a769de608299d0a6ad0b574efc0 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Tue, 19 Mar 2019 13:43:10 -0700 Subject: [PATCH 01/27] testing Slack API Authorization --- lib/slack.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..13442224 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,4 +1,16 @@ #!/usr/bin/env ruby +require "dotenv" +require "httparty" + +Dotenv.load + +URL = "https://slack.com/api/channels.list" + +response = HTTParty.get(URL, query: {token: ENV["KEY"]}) + +(0..response["channels"].length - 1).each do |index| + puts response["channels"][index]["name"] +end def main puts "Welcome to the Ada Slack CLI!" @@ -8,4 +20,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 7e440329d2fee08dda2ae48d06817883eb9cce5b Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Tue, 19 Mar 2019 14:13:20 -0700 Subject: [PATCH 02/27] created workspace class and tested to ensure the api request came thru --- lib/slack.rb | 3 +++ lib/workspace.rb | 25 +++++++++++++++++++++++++ specs/test_helper.rb | 12 ++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 lib/workspace.rb diff --git a/lib/slack.rb b/lib/slack.rb index 13442224..3e596c2e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -21,3 +21,6 @@ def main end main if __FILE__ == $PROGRAM_NAME + + +user_channels = [] diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..7f02b89e --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,25 @@ +# workspace file for keeping all things slack +require 'dotenv' +require 'httparty' + +Dotenv.load + +class Workspace + URL = "https://slack.com/api/users.list" + attr_reader :user, :channels, :selected + + # def initialize + # @user = user + # @channels = channels + # @selected = selected + # end + + def participants + participants = HTTParty.get(URL, query: {token: ENV["KEY"]}) + end + + end + + + slack = Workspace.new + puts slack.participants diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..88872e05 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,6 +10,14 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock + 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["LOCATIONIQ_TOKEN"] + end end \ No newline at end of file From a4c79d6185413f3ce5b320f6ca83cbeb53ce4e82 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Tue, 19 Mar 2019 16:19:30 -0700 Subject: [PATCH 03/27] added Slack module, User.list, and one test for it --- lib/recipient.rb | 32 ++++++++++++++++++++++++++++++++ lib/user.rb | 24 ++++++++++++++++++++++++ specs/recipient_spec.rb | 0 specs/test_helper.rb | 21 ++++++++++++--------- specs/user_spec.rb | 17 +++++++++++++++++ 5 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 specs/recipient_spec.rb create mode 100644 specs/user_spec.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..d0ceb986 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,32 @@ +require "httparty" +require "dotenv" +require "awesome_print" + +Dotenv.load + +module Slack + class Recipient + attr_reader :slack_id, :name + + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end + + def self.get(url, params) + response = HTTParty.get(url, query: params) + end + + def details + end + + def self.list + raise NotImplementedError, "Implement me in a child class!" + end + end +end + +# slack = Recipient.new +# slack.get("https://slack.com/api/channels.list", {token: ENV["KEY"]}) + +# puts slack diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..6ea8bff4 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,24 @@ +require_relative "recipient" +require "awesome_print" + +module Slack + class User < Recipient + attr_reader :real_name + + def initialize(real_name) + super(slack_id, name) + @real_name = real_name + # @status_text + # @status_emoji + end + + def self.list(users_json) + users_json["members"].map do |user| + self.new(user["id"], user["name"], user["real_name"]) + end + end + end +end + +ap Slack::User.get("https://slack.com/api/users.list", {token: ENV["KEY"]}) +# puts Slack::User.list(potato) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 88872e05..fcd6defd 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,14 +1,17 @@ -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 +require_relative "../lib/recipient" +require_relative "../lib/user" + VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located config.hook_into :webmock # tie into this other tool called webmock @@ -17,7 +20,7 @@ :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["LOCATIONIQ_TOKEN"] + config.filter_sensitive_data("") do + ENV["KEY"] end -end \ No newline at end of file +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..6ff576a5 --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,17 @@ +require "test_helper" + +describe "User Class" do + describe "self.get" do + it "returns an array" do + VCR.use_cassette("user_information_find") do + URL = "https://slack.com/api/users.list" + query = {token: ENV["KEY"]} + expect(Slack::User.get(URL, query)).must_be_kind_of HTTParty::Response + # expect(User.list(User.get(URL, query))).must_be_kind_of Array + end + end + + it "returns instances of Users" do + end + end +end From 29f63e0cfa8edfdd63c27dce814ff172af445fa1 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Tue, 19 Mar 2019 18:29:26 -0700 Subject: [PATCH 04/27] added testing for self.get & raising an Error for bad request --- lib/recipient.rb | 13 ++++++++++++- lib/slack.rb | 28 +++++++++++++++------------- lib/user.rb | 21 +++++++++++++-------- specs/user_spec.rb | 29 ++++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index d0ceb986..44e36d86 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -6,6 +6,8 @@ module Slack class Recipient + # class ResponseError < StandardError; end + attr_reader :slack_id, :name def initialize(slack_id, name) @@ -14,7 +16,16 @@ def initialize(slack_id, name) end def self.get(url, params) - response = HTTParty.get(url, query: params) + user_data = HTTParty.get(url, query: params) + + if user_data["ok"] == false + raise ArgumentError, "There was an error!\nCode: #{user_data.code} Message: #{user_data.message}" + end + # unless response.code == 200 + # raise SearchError, "Cannot find #{search_term}" + # end + + return user_data end def details diff --git a/lib/slack.rb b/lib/slack.rb index 3e596c2e..e7fb4b7f 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,26 +1,28 @@ #!/usr/bin/env ruby +require_relative "user" require "dotenv" require "httparty" Dotenv.load -URL = "https://slack.com/api/channels.list" +# URL = "https://slack.com/api/channels.list" -response = HTTParty.get(URL, query: {token: ENV["KEY"]}) +# response = HTTParty.get(URL, query: {token: ENV["KEY"]}) -(0..response["channels"].length - 1).each do |index| - puts response["channels"][index]["name"] -end - -def main - puts "Welcome to the Ada Slack CLI!" +# (0..response["channels"].length - 1).each do |index| +# puts response["channels"][index]["name"] +# end +module Slack + def main + puts "Welcome to the Ada Slack CLI!" - # TODO project + url = "https://slack.com/api/users.list" + query = {token: ENV["KEY"]} + potato = User.get(url, query) + puts User.list(potato) - puts "Thank you for using the Ada Slack CLI" + puts "Thank you for using the Ada Slack CLI" + end end main if __FILE__ == $PROGRAM_NAME - - -user_channels = [] diff --git a/lib/user.rb b/lib/user.rb index 6ea8bff4..6d05ced7 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -3,22 +3,27 @@ module Slack class User < Recipient + URL = "https://slack.com/api/users.list" + PARAM = {token: ENV["KEY"]} + attr_reader :real_name - def initialize(real_name) + def initialize(slack_id, name, real_name) super(slack_id, name) @real_name = real_name - # @status_text - # @status_emoji + # @slack_id = slack_id + # @name = name end - def self.list(users_json) - users_json["members"].map do |user| - self.new(user["id"], user["name"], user["real_name"]) + def self.list + json_data = self.get(URL, PARAM) + + json_data["members"].map do |user| + self.new(user["id"], user["name"], user["profile"]["real_name"]) end end end end -ap Slack::User.get("https://slack.com/api/users.list", {token: ENV["KEY"]}) -# puts Slack::User.list(potato) +# ap Slack::User.get("https://slack.com/api/users.list", {token: ENV["KEY"]}) +# # puts Slack::User.list(potato) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 6ff576a5..f4a8238f 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -2,16 +2,35 @@ describe "User Class" do describe "self.get" do - it "returns an array" do + it "sucessfully gets a response from Slack API for user list" do VCR.use_cassette("user_information_find") do - URL = "https://slack.com/api/users.list" + url = "https://slack.com/api/users.list" query = {token: ENV["KEY"]} - expect(Slack::User.get(URL, query)).must_be_kind_of HTTParty::Response - # expect(User.list(User.get(URL, query))).must_be_kind_of Array + request = Slack::User.get(url, query) + + expect(request["ok"]).must_equal true end end - it "returns instances of Users" do + it "will raise an exception if the GET request fails" do + VCR.use_cassette("user_information_find") do + url = "https://slack.com/api/users.list" + query = {token: "dkdkdkdkdkdkd"} + + expect { + Slack::User.get(url, query) + }.must_raise ArgumentError + end end + + # describe "self.list" do + # it "returns an array" do + # VCR.use_cassette("user_information_find") do + + # Slack::User.list("https://slack.com/api/users.list", {token: ENV["KEY"]}) + # end + # end + + # end end end From 1813aa0c78ad4b5fa2fce4b9c000b750722dcc9e Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Tue, 19 Mar 2019 18:35:07 -0700 Subject: [PATCH 05/27] created custom error for self.get and updated testing --- lib/recipient.rb | 6 +++--- lib/user.rb | 2 ++ specs/user_spec.rb | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 44e36d86..99e00f7d 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -6,8 +6,6 @@ module Slack class Recipient - # class ResponseError < StandardError; end - attr_reader :slack_id, :name def initialize(slack_id, name) @@ -15,11 +13,13 @@ def initialize(slack_id, name) @name = name end + class ResponseError < StandardError; end + def self.get(url, params) user_data = HTTParty.get(url, query: params) if user_data["ok"] == false - raise ArgumentError, "There was an error!\nCode: #{user_data.code} Message: #{user_data.message}" + raise ResponseError, "There was an error!\nCode: #{user_data.code} Message: #{user_data.message}" end # unless response.code == 200 # raise SearchError, "Cannot find #{search_term}" diff --git a/lib/user.rb b/lib/user.rb index 6d05ced7..b022678e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -6,6 +6,8 @@ class User < Recipient URL = "https://slack.com/api/users.list" PARAM = {token: ENV["KEY"]} + class ResponseError < StandardError; end + attr_reader :real_name def initialize(slack_id, name, real_name) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index f4a8238f..df13f010 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -19,7 +19,7 @@ expect { Slack::User.get(url, query) - }.must_raise ArgumentError + }.must_raise Slack::Recipient::ResponseError end end From 01cb46193fcff2c90b5d6ecc7a75d3a496c182d1 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Tue, 19 Mar 2019 20:38:46 -0700 Subject: [PATCH 06/27] added testing for User.list and made it pass --- specs/user_spec.rb | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index df13f010..db2e8048 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -22,15 +22,29 @@ }.must_raise Slack::Recipient::ResponseError end end + end - # describe "self.list" do - # it "returns an array" do - # VCR.use_cassette("user_information_find") do + describe "self.list" do + it "returns an array that contains instances of users" do + VCR.use_cassette("user_information_find") do + users = Slack::User.list - # Slack::User.list("https://slack.com/api/users.list", {token: ENV["KEY"]}) - # end - # end + expect(users).must_be_kind_of Array + (0..users.length - 1).each do |i| + expect(users[i]).must_be_kind_of Slack::User + end + end + end - # end + it "loads all user information correctly" do + VCR.use_cassette("user_information_find") do + users = Slack::User.list + expect(users[1].name).must_equal "kimberly.fasbender" + expect(users[1].real_name).must_equal "Kim Fasbender" + expect(users[1].slack_id).must_equal "UH408BVL7" + expect(users.length).must_equal 3 + end + end + # # end end end end From 0971306199bc66b9c3bb18aaf38cf113c8b71d29 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 13:36:58 -0700 Subject: [PATCH 07/27] updated custom error --- lib/recipient.rb | 10 +++------- lib/slack.rb | 32 +++++++++++++++----------------- lib/user.rb | 5 ----- specs/slack_spec.rb | 0 specs/test_helper.rb | 3 +++ specs/user_spec.rb | 2 +- 6 files changed, 22 insertions(+), 30 deletions(-) create mode 100644 specs/slack_spec.rb diff --git a/lib/recipient.rb b/lib/recipient.rb index 99e00f7d..9a80f1a3 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,10 +1,11 @@ require "httparty" require "dotenv" -require "awesome_print" Dotenv.load module Slack + class ResponseError < StandardError; end + class Recipient attr_reader :slack_id, :name @@ -13,17 +14,12 @@ def initialize(slack_id, name) @name = name end - class ResponseError < StandardError; end - def self.get(url, params) user_data = HTTParty.get(url, query: params) if user_data["ok"] == false - raise ResponseError, "There was an error!\nCode: #{user_data.code} Message: #{user_data.message}" + raise ResponseError, "There was an error!\nMessage: #{user_data["error"]}" end - # unless response.code == 200 - # raise SearchError, "Cannot find #{search_term}" - # end return user_data end diff --git a/lib/slack.rb b/lib/slack.rb index e7fb4b7f..12a04c9a 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,28 +1,26 @@ #!/usr/bin/env ruby require_relative "user" -require "dotenv" -require "httparty" Dotenv.load -# URL = "https://slack.com/api/channels.list" +# module Slack +def main + users = Slack::User -# response = HTTParty.get(URL, query: {token: ENV["KEY"]}) + puts "Welcome to the Ada Slack CLI!" + puts "#{users.list.length} users and #{"PUT IN CHANNELS"} channels were uploaded.\n\n" -# (0..response["channels"].length - 1).each do |index| -# puts response["channels"][index]["name"] -# end -module Slack - def main - puts "Welcome to the Ada Slack CLI!" - - url = "https://slack.com/api/users.list" - query = {token: ENV["KEY"]} - potato = User.get(url, query) - puts User.list(potato) - - puts "Thank you for using the Ada Slack CLI" + puts "Please make a selection:\n1. list users\n2. list channels\n3. quit\n\n" + selection = gets.chomp.to_i + if selection == 1 + users.list.each do |user| + puts user.real_name + end end + + puts "Thank you for using the Ada Slack CLI" end +# end + main if __FILE__ == $PROGRAM_NAME diff --git a/lib/user.rb b/lib/user.rb index b022678e..47739d02 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,20 +1,15 @@ require_relative "recipient" -require "awesome_print" module Slack class User < Recipient URL = "https://slack.com/api/users.list" PARAM = {token: ENV["KEY"]} - class ResponseError < StandardError; end - attr_reader :real_name def initialize(slack_id, name, real_name) super(slack_id, name) @real_name = real_name - # @slack_id = slack_id - # @name = name end def self.list diff --git a/specs/slack_spec.rb b/specs/slack_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/test_helper.rb b/specs/test_helper.rb index fcd6defd..e4b069d2 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -5,7 +5,10 @@ require "minitest/autorun" require "minitest/reporters" require "minitest/skip_dsl" +require "webmock/minitest" require "vcr" +require "dotenv" +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index db2e8048..1a71b067 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -19,7 +19,7 @@ expect { Slack::User.get(url, query) - }.must_raise Slack::Recipient::ResponseError + }.must_raise Slack::ResponseError end end end From 629990dacdc76770fe7944f00f0cd562c2efaa61 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 14:11:11 -0700 Subject: [PATCH 08/27] made a table for users --- lib/slack.rb | 11 ++++++++--- specs/test_helper.rb | 1 + specs/user_spec.rb | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 12a04c9a..090432ee 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,5 +1,6 @@ #!/usr/bin/env ruby require_relative "user" +require "table_print" Dotenv.load @@ -12,10 +13,14 @@ def main puts "Please make a selection:\n1. list users\n2. list channels\n3. quit\n\n" selection = gets.chomp.to_i + + # tp Author.limit(3), "name", "books.title", "books.photos.caption" if selection == 1 - users.list.each do |user| - puts user.real_name - end + # users.list.each do |user| user.real_name + tp users.list, "slack_id", "name", "real_name" + # end + elsif selection == 3 + exit end puts "Thank you for using the Ada Slack CLI" diff --git a/specs/test_helper.rb b/specs/test_helper.rb index e4b069d2..d9a71fda 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -14,6 +14,7 @@ require_relative "../lib/recipient" require_relative "../lib/user" +require_relative "../lib/slack" VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 1a71b067..c7f06460 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,4 +1,4 @@ -require "test_helper" +require_relative "test_helper" describe "User Class" do describe "self.get" do From e970cccfc169bfe7ba48dcee46506622e9e83cf9 Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 14:37:26 -0700 Subject: [PATCH 09/27] wrote tests for channels spec and passed --- lib/channels.rb | 28 ++++++++++++++++++++++++++++ specs/channels_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/channels.rb create mode 100644 specs/channels_spec.rb diff --git a/lib/channels.rb b/lib/channels.rb new file mode 100644 index 00000000..49fbb18f --- /dev/null +++ b/lib/channels.rb @@ -0,0 +1,28 @@ +require_relative "recipient" + +module Slack + class Channel < Recipient + URL = "https://slack.com/api/channels.list" + PARAM = {token: ENV["KEY"]} + + attr_reader + + def initialize(slack_id, name, topic, member_count) + super(slack_id, name) + @name = name + @topic = topic + @member_count = member_count + end + + def self.list + json_data = self.get(URL, PARAM) + + json_data["channels"].map do |channel| + self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["members"].length) + end + end + end +end + +# ap Slack::User.get("https://slack.com/api/users.list", {token: ENV["KEY"]}) +# # puts Slack::User.list(potato) diff --git a/specs/channels_spec.rb b/specs/channels_spec.rb new file mode 100644 index 00000000..b81c6241 --- /dev/null +++ b/specs/channels_spec.rb @@ -0,0 +1,25 @@ +require_relative "test_helper" + +describe "Channels" do + describe "self.get" do + it "will successfully get a response from Slack API for user list" do + VCR.use_cassette("channel_information_find") do + url = "http://slack.com/api/channels.list" + query = {token: ENV["KEY"]} + request = Slack::User.get(url, query) + + expect(request["ok"]).must_equal true + end + end + it "will raise an exception if the GET request fails" do + VCR.use_cassette("channel_information_find") do + url = "https://slack.com/api/channel.list" + query = {token: "dkdkdkdkdkdkd"} + + expect { + Slack::User.get(url, query) + }.must_raise Slack::ResponseError + end + end + end +end \ No newline at end of file From 963687f746594cbd86337a37c99b59b5e0e1a82c Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 14:50:20 -0700 Subject: [PATCH 10/27] made additional tests for channels.rb --- lib/channels.rb | 2 +- specs/channels_spec.rb | 21 ++++++++++++++++++--- specs/test_helper.rb | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/channels.rb b/lib/channels.rb index 49fbb18f..425d0834 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -5,7 +5,7 @@ class Channel < Recipient URL = "https://slack.com/api/channels.list" PARAM = {token: ENV["KEY"]} - attr_reader + attr_reader :topic, :member_count def initialize(slack_id, name, topic, member_count) super(slack_id, name) diff --git a/specs/channels_spec.rb b/specs/channels_spec.rb index b81c6241..e2ab2b75 100644 --- a/specs/channels_spec.rb +++ b/specs/channels_spec.rb @@ -6,20 +6,35 @@ VCR.use_cassette("channel_information_find") do url = "http://slack.com/api/channels.list" query = {token: ENV["KEY"]} - request = Slack::User.get(url, query) + request = Slack::Channel.get(url, query) expect(request["ok"]).must_equal true end end + it "will raise an exception if the GET request fails" do VCR.use_cassette("channel_information_find") do url = "https://slack.com/api/channel.list" query = {token: "dkdkdkdkdkdkd"} expect { - Slack::User.get(url, query) + Slack::Channel.get(url, query) }.must_raise Slack::ResponseError end end end -end \ No newline at end of file +end + +describe "Self.list" do + it "return an array of channels" do + VCR.use_cassette("channel_information_find") 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 + end +end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index d9a71fda..138257ce 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -15,6 +15,7 @@ require_relative "../lib/recipient" require_relative "../lib/user" require_relative "../lib/slack" +require_relative "../lib/channels" VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located From 1229ceb0436a23a707878e46ecfb9511938fab5d Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 15:14:48 -0700 Subject: [PATCH 11/27] fixed the key issue and created tests in channels and added code to our user interface --- lib/channels.rb | 2 ++ lib/slack.rb | 9 ++++++--- lib/user.rb | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/channels.rb b/lib/channels.rb index 425d0834..687f78d4 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -5,6 +5,8 @@ class Channel < Recipient URL = "https://slack.com/api/channels.list" PARAM = {token: ENV["KEY"]} + # puts 'ENV["KEY"]', ENV["KEY"] + attr_reader :topic, :member_count def initialize(slack_id, name, topic, member_count) diff --git a/lib/slack.rb b/lib/slack.rb index 090432ee..46030607 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,5 +1,6 @@ #!/usr/bin/env ruby require_relative "user" +require_relative "channels" require "table_print" Dotenv.load @@ -7,7 +8,8 @@ # module Slack def main users = Slack::User - + channel = Slack::Channel + puts "Welcome to the Ada Slack CLI!" puts "#{users.list.length} users and #{"PUT IN CHANNELS"} channels were uploaded.\n\n" @@ -18,10 +20,11 @@ def main if selection == 1 # users.list.each do |user| user.real_name tp users.list, "slack_id", "name", "real_name" - # end + elsif selection == 2 + tp channel.list, "slack_id", "name", "topic", "member_count" elsif selection == 3 exit - end + end # end puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index 47739d02..623443b4 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -7,6 +7,8 @@ class User < Recipient attr_reader :real_name + puts 'ENV["KEY2"]', ENV["KEY"] + def initialize(slack_id, name, real_name) super(slack_id, name) @real_name = real_name From 019ce36a30c2cb38b107beccdd3f80600522ca90 Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 15:21:33 -0700 Subject: [PATCH 12/27] erased key oops my bad --- lib/channels.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/channels.rb b/lib/channels.rb index 687f78d4..425d0834 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -5,8 +5,6 @@ class Channel < Recipient URL = "https://slack.com/api/channels.list" PARAM = {token: ENV["KEY"]} - # puts 'ENV["KEY"]', ENV["KEY"] - attr_reader :topic, :member_count def initialize(slack_id, name, topic, member_count) From 9bcd182817fa83cb5fb0062f97e3c9daa1d8bf94 Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 15:32:08 -0700 Subject: [PATCH 13/27] completed wave one implemented loop and all wave one requirement have been tested and run properly --- lib/slack.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 46030607..a50acfe1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,22 +11,23 @@ def main channel = Slack::Channel puts "Welcome to the Ada Slack CLI!" - puts "#{users.list.length} users and #{"PUT IN CHANNELS"} channels were uploaded.\n\n" + selection = 1 + while selection != 3 + puts "#{users.list.length} users and #{channel.list.length} channels were uploaded.\n\n" puts "Please make a selection:\n1. list users\n2. list channels\n3. quit\n\n" selection = gets.chomp.to_i - # tp Author.limit(3), "name", "books.title", "books.photos.caption" if selection == 1 # users.list.each do |user| user.real_name tp users.list, "slack_id", "name", "real_name" elsif selection == 2 tp channel.list, "slack_id", "name", "topic", "member_count" elsif selection == 3 + puts "Thank you for using the Ada Slack CLI" exit end # end - - puts "Thank you for using the Ada Slack CLI" + end end # end From 05f6ac3c4e6e217cb598f141adf312be9cfa8eca Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 16:01:40 -0700 Subject: [PATCH 14/27] starting wave 2, set up additional options --- lib/slack.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index a50acfe1..abe28c8b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,7 +15,14 @@ def main while selection != 3 puts "#{users.list.length} users and #{channel.list.length} channels were uploaded.\n\n" - puts "Please make a selection:\n1. list users\n2. list channels\n3. quit\n\n" + puts "Please make a selection:\n" + puts "1. list users\n" + puts "2. list channels\n" + puts "3. select user\n" + puts "4. select channel\n" + puts "5. details\n" + puts "6. quit\n" + selection = gets.chomp.to_i if selection == 1 From 3a65cc4b6dad27fbd594a528f03ebae4fc516656 Mon Sep 17 00:00:00 2001 From: Cloudy Lopez Date: Wed, 20 Mar 2019 19:15:29 -0700 Subject: [PATCH 15/27] created workspace and some tests --- lib/workspace.rb | 56 ++++++++++++++++++++++++++++------------- specs/test_helper.rb | 1 + specs/workspace_spec.rb | 12 +++++++++ 3 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 specs/workspace_spec.rb diff --git a/lib/workspace.rb b/lib/workspace.rb index 7f02b89e..fd5e7670 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,25 +1,47 @@ # workspace file for keeping all things slack require 'dotenv' -require 'httparty' +require 'httparty' + +require_relative 'user' +require_relative 'channels' Dotenv.load -class Workspace - URL = "https://slack.com/api/users.list" - attr_reader :user, :channels, :selected - - # def initialize - # @user = user - # @channels = channels - # @selected = selected - # end - - def participants - participants = HTTParty.get(URL, query: {token: ENV["KEY"]}) - end +module Slack + class Workspace + URL = "https://slack.com/api/users.list" + attr_reader :users, :channels, :selected - end + def initialize + @users = Slack::User.list + @channels = channels + @selected = selected + end + + def participants + participants = HTTParty.get(URL, query: {token: ENV["KEY"]}) + end + + def select_user(name) #return user or id/set the selected user + #find user + #set selected to user + #return user + users.each do |user| + if name == user.name || name == user.slack_id + end + + end + def select_channel(channel_name_or_slack_id) - slack = Workspace.new - puts slack.participants + end + + def show_details #currently_selected_recipient + + end + + def send_msg(message) + + end + end +end \ No newline at end of file diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 138257ce..1809b208 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -16,6 +16,7 @@ require_relative "../lib/user" require_relative "../lib/slack" require_relative "../lib/channels" +require_relative "../lib/workspace" VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..93779444 --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1,12 @@ +require 'test_helper' + + +describe "Workspace Class" do + it "will return a list of user instances" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + expect(workspace.users).must_be_kind_of Array + expect(workspace.users[0]).must_be_kind_of Slack::User + end + end +end From 3d1981365123d5dd9bd492c0ce9d369dfb7d6466 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 22:16:28 -0700 Subject: [PATCH 16/27] added Workspace#select_user and updated CLI --- lib/slack.rb | 62 +++++++++++++++++-------------- lib/user.rb | 2 - lib/workspace.rb | 81 ++++++++++++++++++++--------------------- specs/workspace_spec.rb | 17 ++++----- 4 files changed, 83 insertions(+), 79 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index abe28c8b..be1a112e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,39 +1,47 @@ #!/usr/bin/env ruby -require_relative "user" -require_relative "channels" +require_relative "workspace" require "table_print" Dotenv.load # module Slack def main - users = Slack::User - channel = Slack::Channel - + # users = Slack::User + # channel = Slack::Channel + workspace = Slack::Workspace.new + puts "Welcome to the Ada Slack CLI!" + print "#{workspace.users.length} users and #{workspace.channels.length} " + puts "channels were uploaded." + selection = 1 - while selection != 3 - puts "#{users.list.length} users and #{channel.list.length} channels were uploaded.\n\n" - - puts "Please make a selection:\n" - puts "1. list users\n" - puts "2. list channels\n" - puts "3. select user\n" - puts "4. select channel\n" - puts "5. details\n" - puts "6. quit\n" - - selection = gets.chomp.to_i - - if selection == 1 - # users.list.each do |user| user.real_name - tp users.list, "slack_id", "name", "real_name" - elsif selection == 2 - tp channel.list, "slack_id", "name", "topic", "member_count" - elsif selection == 3 - puts "Thank you for using the Ada Slack CLI" - exit - end # end + while selection != 6 + puts "\nPlease make a selection:\n\n" + puts "1. list users" + puts "2. list channels" + puts "3. select user" + puts "4. select channel" + puts "5. details" + puts "6. quit\n\n" + + selection = gets.chomp.to_i + + if selection == 1 || selection == "list users" + puts "" + tp workspace.users, "slack_id", :Name => {:display_method => "real_name"}, + :include => {:User_Name => {:display_method => "name"}} + elsif selection == 2 || selection == "list channels" + tp workspace.channels, "name", "slack_id", "topic", "member_count" + elsif selection == 3 || selection == "select_user" + puts "Please enter in the user's USER_NAME or SLACK_ID:" + user_descriptor = gets.chomp + puts workspace.select_user(user_descriptor) + elsif selection == 4 || selection == "select channel" + elsif selection == 5 || selection == "details" + elsif selection == 6 || selection == "quit" + puts "Thank you for using the Ada Slack CLI" + exit + end # end end end diff --git a/lib/user.rb b/lib/user.rb index 623443b4..47739d02 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -7,8 +7,6 @@ class User < Recipient attr_reader :real_name - puts 'ENV["KEY2"]', ENV["KEY"] - def initialize(slack_id, name, real_name) super(slack_id, name) @real_name = real_name diff --git a/lib/workspace.rb b/lib/workspace.rb index fd5e7670..52a0749f 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,47 +1,46 @@ -# workspace file for keeping all things slack -require 'dotenv' -require 'httparty' +# workspace file for keeping all things slack +require "dotenv" +require "httparty" -require_relative 'user' -require_relative 'channels' +require_relative "user" +require_relative "channels" Dotenv.load module Slack - class Workspace - URL = "https://slack.com/api/users.list" - attr_reader :users, :channels, :selected - - def initialize - @users = Slack::User.list - @channels = channels - @selected = selected - end - - def participants - participants = HTTParty.get(URL, query: {token: ENV["KEY"]}) - end - - def select_user(name) #return user or id/set the selected user - #find user - #set selected to user - #return user - users.each do |user| - if name == user.name || name == user.slack_id - end - - end - - def select_channel(channel_name_or_slack_id) - - end - - def show_details #currently_selected_recipient - - end - - def send_msg(message) - - end + class Workspace + URL = "https://slack.com/api/users.list" + attr_reader :users, :channels + attr_accessor :selected + + def initialize + @users = User.list + @channels = Channel.list + @selected = nil end -end \ No newline at end of file + + def select_user(user_descriptor) #return user or id/set the selected user + users.find do |user| + if user_descriptor == user.name || user_descriptor == user.slack_id + @selected = user + end + end + + if @selected == nil + return "Could not find a user with that USER_NAME or SLACK_ID." + end + end + + # users.each do |user| + # if name == user.name || name == user.slack_id end + + def select_channel(channel_descriptor) + end + + def show_details #currently_selected_recipient + end + + def send_message(message) + end + end +end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 93779444..ac5fbbed 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,12 +1,11 @@ -require 'test_helper' +require_relative "test_helper" - -describe "Workspace Class" do - it "will return a list of user instances" do - VCR.use_cassette("workspace_information_find") do - workspace = Slack::Workspace.new - expect(workspace.users).must_be_kind_of Array - expect(workspace.users[0]).must_be_kind_of Slack::User - end +describe "Workspace Class" do + it "will return a list of user instances" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + expect(workspace.users).must_be_kind_of Array + expect(workspace.users[0]).must_be_kind_of Slack::User end + end end From 42146272b96e8160d277e6ca89d35d841c72b077 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 23:19:12 -0700 Subject: [PATCH 17/27] added testing for Workspace#select_user --- lib/workspace.rb | 14 +++++--- specs/workspace_spec.rb | 78 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 52a0749f..80192430 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -19,7 +19,7 @@ def initialize @selected = nil end - def select_user(user_descriptor) #return user or id/set the selected user + def select_user(user_descriptor) users.find do |user| if user_descriptor == user.name || user_descriptor == user.slack_id @selected = user @@ -31,10 +31,16 @@ def select_user(user_descriptor) #return user or id/set the selected user end end - # users.each do |user| - # if name == user.name || name == user.slack_id end - def select_channel(channel_descriptor) + channels.find do |channel| + if channel_descriptor == channel.name || channel_descriptor == channel.slack_id + @selected = channel + end + end + + if @selected == nil + return "Could not find a channel with that NAME or SLACK_ID." + end end def show_details #currently_selected_recipient diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index ac5fbbed..97dc4bf8 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,11 +1,79 @@ require_relative "test_helper" describe "Workspace Class" do - it "will return a list of user instances" do - VCR.use_cassette("workspace_information_find") do - workspace = Slack::Workspace.new - expect(workspace.users).must_be_kind_of Array - expect(workspace.users[0]).must_be_kind_of Slack::User + describe "#initialize" do + it "has a list of users & a list of channels" do + VCR.use_cassette("workspace_information_find") 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" do + it "finds a user by username and stores it in the selected variable" do + VCR.use_cassette("workspace_information_find") do + user_names = ["kimberly.fasbender", "scarlettsteph07", "cloudylopez"] + real_names = ["Kim Fasbender", "Scarlett", "Cloudy Lopez"] + workspace = Slack::Workspace.new + + user_names.each_with_index do |user_name, i| + find_user = workspace.select_user(user_name) + selected_user = workspace.selected + expect(selected_user).must_be_kind_of Slack::User + expect(selected_user.real_name).must_equal "#{real_names[i]}" + end + end + end + + it "finds a user by slack id and stores it in the selected variable" do + VCR.use_cassette("workspace_information_find") do + slack_ids = ["UH408BVL7", "UH5EEFU0N", "UH599D6J0"] + real_names = ["Kim Fasbender", "Cloudy Lopez", "Scarlett"] + workspace = Slack::Workspace.new + + slack_ids.each_with_index do |slack_id, i| + find_user = workspace.select_user(slack_id) + selected_user = workspace.selected + expect(selected_user).must_be_kind_of Slack::User + expect(selected_user.real_name).must_equal "#{real_names[i]}" + end + end + end + + it "returns an error message if user_name or slack_id are invalid" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + find_user = workspace.select_user("potato daniels") + selected_user = workspace.selected + assert_nil(selected_user, msg = nil) + end + end + end + + describe "#select_channel" do + it "finds a channel by name and stores it in the selected variable" do + VCR.use_cassette("workspace_information_find") do + channel_names = ["puppers", "general", "random"] + 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 + + channel_names.each_with_index do |channel_name, i| + find_channel = workspace.select_channel(channel_name) + selected_channel = workspace.selected + expect(selected_channel).must_be_kind_of Slack::Channel + expect(selected_channel.topic).must_equal "#{topics[i]}" + end + end + end + # select_channel end + end + # end end end From 5c587fa9b4371f5d63af40986de78e87051c7acc Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 23:42:32 -0700 Subject: [PATCH 18/27] added testing for Recipient.list and fixed a few bugs --- specs/channels_spec.rb | 56 ++++++++++++++++++++--------------------- specs/recipient_spec.rb | 11 ++++++++ specs/test_helper.rb | 4 ++- specs/workspace_spec.rb | 26 +++++++++++++++++++ 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/specs/channels_spec.rb b/specs/channels_spec.rb index e2ab2b75..a897ff67 100644 --- a/specs/channels_spec.rb +++ b/specs/channels_spec.rb @@ -1,40 +1,40 @@ require_relative "test_helper" describe "Channels" do - describe "self.get" do - it "will successfully get a response from Slack API for user list" do - VCR.use_cassette("channel_information_find") do - url = "http://slack.com/api/channels.list" - query = {token: ENV["KEY"]} - request = Slack::Channel.get(url, query) + describe "self.get" do + it "will successfully get a response from Slack API for user list" do + VCR.use_cassette("channel_information_find") 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 - - it "will raise an exception if the GET request fails" do - VCR.use_cassette("channel_information_find") do - url = "https://slack.com/api/channel.list" - query = {token: "dkdkdkdkdkdkd"} - - expect { - Slack::Channel.get(url, query) - }.must_raise Slack::ResponseError - end - end + expect(request["ok"]).must_equal true + end end + + it "will raise an exception if the GET request fails" do + VCR.use_cassette("channel_information_find") do + url = "https://slack.com/api/channel.list" + query = {token: "dkdkdkdkdkdkd"} + + expect { + Slack::Channel.get(url, query) + }.must_raise Slack::ResponseError + end + end + end end describe "Self.list" do - it "return an array of channels" do - VCR.use_cassette("channel_information_find") do - channels = Slack::Channel.list + it "return an array of channels" do + VCR.use_cassette("channel_information_find") do + channels = Slack::Channel.list - expect(channels).must_be_kind_of Array + 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 + (0..channels.length - 1).each do |i| + expect(channels[i]).must_be_kind_of Slack::Channel + end end + end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e69de29b..d48af45a 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -0,0 +1,11 @@ +require_relative "test_helper" + +describe "Recipient Class" do + describe "self.list" do + it "raises an Error if invoked directly (without subclassing)" do + expect { + Slack::Recipient.list + }.must_raise NotImplementedError + end + end +end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 1809b208..4389e186 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,5 +1,7 @@ require "simplecov" -SimpleCov.start +SimpleCov.start do + add_filter %r{^/spec/} +end require "minitest" require "minitest/autorun" diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 97dc4bf8..cbf9aa32 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -73,6 +73,32 @@ 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 + + it "returns an error message if user_name or slack_id are invalid" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + find_channel = workspace.select_channel("potato daniels") + selected_channel = workspace.selected + assert_nil(selected_channel, msg = nil) + end + end # select_channel end end # end end From bd9e3957bbb4d4593c33e6dc576792b90b69fafd Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Wed, 20 Mar 2019 23:52:38 -0700 Subject: [PATCH 19/27] made select_channel updates to CLI --- lib/slack.rb | 8 ++++++-- lib/workspace.rb | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index be1a112e..a773317b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require_relative "workspace" require "table_print" +require "colorize" Dotenv.load @@ -33,10 +34,13 @@ def main elsif selection == 2 || selection == "list channels" tp workspace.channels, "name", "slack_id", "topic", "member_count" elsif selection == 3 || selection == "select_user" - puts "Please enter in the user's USER_NAME or SLACK_ID:" + print "Please enter in the user's USER_NAME or SLACK_ID: " user_descriptor = gets.chomp - puts workspace.select_user(user_descriptor) + puts "\n#{workspace.select_user(user_descriptor)}".colorize(:red) elsif selection == 4 || selection == "select channel" + print "Please enter in the channel's NAME or SLACK_ID: " + channel_descriptor = gets.chomp + puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:red) elsif selection == 5 || selection == "details" elsif selection == 6 || selection == "quit" puts "Thank you for using the Ada Slack CLI" diff --git a/lib/workspace.rb b/lib/workspace.rb index 80192430..b56b5394 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -27,7 +27,7 @@ def select_user(user_descriptor) end if @selected == nil - return "Could not find a user with that USER_NAME or SLACK_ID." + return "Could not find a user with USER_NAME or SLACK_ID #{user_descriptor}." end end @@ -39,7 +39,7 @@ def select_channel(channel_descriptor) end if @selected == nil - return "Could not find a channel with that NAME or SLACK_ID." + return "Could not find a channel with NAME or SLACK_ID #{channel_descriptor}." end end From dfbb58bf29da0fea6413faee7e961e99d74612a5 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 09:58:58 -0700 Subject: [PATCH 20/27] added details & show_details pseudocode --- lib/recipient.rb | 3 +++ lib/slack.rb | 1 + lib/user.rb | 4 ++++ lib/workspace.rb | 1 + specs/test_helper.rb | 1 + 5 files changed, 10 insertions(+) diff --git a/lib/recipient.rb b/lib/recipient.rb index 9a80f1a3..bf16699e 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -27,6 +27,9 @@ def self.get(url, params) def details end + def send_message + end + def self.list raise NotImplementedError, "Implement me in a child class!" end diff --git a/lib/slack.rb b/lib/slack.rb index a773317b..1b77b103 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -42,6 +42,7 @@ def main channel_descriptor = gets.chomp puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:red) elsif selection == 5 || selection == "details" + workspace.show_details elsif selection == 6 || selection == "quit" puts "Thank you for using the Ada Slack CLI" exit diff --git a/lib/user.rb b/lib/user.rb index 47739d02..e2d6362e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -19,6 +19,10 @@ def self.list self.new(user["id"], user["name"], user["profile"]["real_name"]) end end + + def details(user) + tp user, "real_name", "name", "slack_id" + end end end diff --git a/lib/workspace.rb b/lib/workspace.rb index b56b5394..69ca3cda 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -44,6 +44,7 @@ def select_channel(channel_descriptor) end def show_details #currently_selected_recipient + User.details(selected) end def send_message(message) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 4389e186..87093470 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,6 +10,7 @@ require "webmock/minitest" require "vcr" require "dotenv" +require "table_print" Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From d1305b49448ea6bbb2d926318d8bb504207b9b63 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 10:27:49 -0700 Subject: [PATCH 21/27] added Recipient#details, User#detail, Channel#details --- lib/channels.rb | 4 ++++ lib/recipient.rb | 5 +++-- lib/user.rb | 4 ++-- lib/workspace.rb | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/channels.rb b/lib/channels.rb index 425d0834..77aaab3d 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -21,6 +21,10 @@ def self.list self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["members"].length) end end + + def details + ["name", "slack_id", "topic", "member_count"] + end end end diff --git a/lib/recipient.rb b/lib/recipient.rb index bf16699e..3223b243 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -24,10 +24,11 @@ def self.get(url, params) return user_data end - def details + def send_message end - def send_message + def details(user_or_channel) + tp user_or_channel end def self.list diff --git a/lib/user.rb b/lib/user.rb index e2d6362e..99ddcfb3 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -20,8 +20,8 @@ def self.list end end - def details(user) - tp user, "real_name", "name", "slack_id" + def details + ["real_name", "name", "slack_id"] end end end diff --git a/lib/workspace.rb b/lib/workspace.rb index 69ca3cda..290f1be4 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -44,7 +44,7 @@ def select_channel(channel_descriptor) end def show_details #currently_selected_recipient - User.details(selected) + tp @selected, @selected.details end def send_message(message) From 472cc1d5e9fbc20bc890802e4ec4c9e8002cc201 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 14:10:14 -0700 Subject: [PATCH 22/27] added testing for User#details and Channel#details --- lib/channels.rb | 2 +- lib/recipient.rb | 4 ++-- lib/slack.rb | 42 +++++++++++++++++++++--------------------- lib/user.rb | 2 +- specs/channels_spec.rb | 12 ++++++++++++ specs/user_spec.rb | 10 ++++++++++ 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/lib/channels.rb b/lib/channels.rb index 77aaab3d..d6b8f46b 100644 --- a/lib/channels.rb +++ b/lib/channels.rb @@ -23,7 +23,7 @@ def self.list end def details - ["name", "slack_id", "topic", "member_count"] + super.push("topic", "member_count") end end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 3223b243..42efa50b 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -27,8 +27,8 @@ def self.get(url, params) def send_message end - def details(user_or_channel) - tp user_or_channel + def details + ["name", "slack_id"] end def self.list diff --git a/lib/slack.rb b/lib/slack.rb index 1b77b103..00a82856 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,40 +11,40 @@ def main # channel = Slack::Channel workspace = Slack::Workspace.new - puts "Welcome to the Ada Slack CLI!" - print "#{workspace.users.length} users and #{workspace.channels.length} " - puts "channels were uploaded." + puts "Welcome to the Ada Slack CLI!".colorize(:magenta) + print "#{workspace.users.length} users and #{workspace.channels.length} ".colorize(:magenta) + puts "channels were uploaded.".colorize(:magenta) selection = 1 while selection != 6 - puts "\nPlease make a selection:\n\n" - puts "1. list users" - puts "2. list channels" - puts "3. select user" - puts "4. select channel" - puts "5. details" - puts "6. quit\n\n" - - selection = gets.chomp.to_i - - if selection == 1 || selection == "list users" - puts "" + puts "\nPlease make a selection:\n".colorize(:magenta) + puts "1. list users".colorize(:blue) + puts "2. list channels".colorize(:green) + puts "3. select user".colorize(:blue) + puts "4. select channel".colorize(:green) + puts "5. details".colorize(:yellow) + puts "6. quit\n\n".colorize(:light_red) + + selection = gets.chomp + + case selection + when "1", "list users" tp workspace.users, "slack_id", :Name => {:display_method => "real_name"}, :include => {:User_Name => {:display_method => "name"}} - elsif selection == 2 || selection == "list channels" + when "2", "list channels" tp workspace.channels, "name", "slack_id", "topic", "member_count" - elsif selection == 3 || selection == "select_user" + when "3", "select_user" print "Please enter in the user's USER_NAME or SLACK_ID: " user_descriptor = gets.chomp puts "\n#{workspace.select_user(user_descriptor)}".colorize(:red) - elsif selection == 4 || selection == "select channel" + when "4", "select channel" print "Please enter in the channel's NAME or SLACK_ID: " channel_descriptor = gets.chomp puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:red) - elsif selection == 5 || selection == "details" + when "5", "details" workspace.show_details - elsif selection == 6 || selection == "quit" - puts "Thank you for using the Ada Slack CLI" + when "6", "quit" + puts "Thank you for using the Ada Slack CLI".colorize(:magenta) exit end # end end diff --git a/lib/user.rb b/lib/user.rb index 99ddcfb3..49b2b271 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -21,7 +21,7 @@ def self.list end def details - ["real_name", "name", "slack_id"] + super << "real_name" end end end diff --git a/specs/channels_spec.rb b/specs/channels_spec.rb index a897ff67..b4d83199 100644 --- a/specs/channels_spec.rb +++ b/specs/channels_spec.rb @@ -36,5 +36,17 @@ expect(channels[i]).must_be_kind_of Slack::Channel end end + + describe "#details" do + it "must return an array that contains the correct strings" do + channel = Slack::Channel.new("potato", "head", "toy", "story") + 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 diff --git a/specs/user_spec.rb b/specs/user_spec.rb index c7f06460..b70c52a2 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -45,6 +45,16 @@ expect(users.length).must_equal 3 end end + + describe "#details" do + it "must return an array that contains the correct strings" do + user = Slack::User.new("potato", "head", "toy") + 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 end end From 5ce756a8bb0829489ba392c1a5cd4f0b3fbef5cc Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 14:29:40 -0700 Subject: [PATCH 23/27] updated Workspace#show_details to return false if no user or channel selected --- lib/slack.rb | 14 +++++++++----- lib/workspace.rb | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 00a82856..c9dfa699 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -34,15 +34,19 @@ def main when "2", "list channels" tp workspace.channels, "name", "slack_id", "topic", "member_count" when "3", "select_user" - print "Please enter in the user's USER_NAME or SLACK_ID: " + print "Please enter in the user's USER_NAME or SLACK_ID: ".colorize(:blue) user_descriptor = gets.chomp - puts "\n#{workspace.select_user(user_descriptor)}".colorize(:red) + puts "\n#{workspace.select_user(user_descriptor)}".colorize(:light_red) when "4", "select channel" - print "Please enter in the channel's NAME or SLACK_ID: " + print "Please enter in the channel's NAME or SLACK_ID: ".colorize(:green) channel_descriptor = gets.chomp - puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:red) + puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:light_red) when "5", "details" - workspace.show_details + if !workspace.show_details + puts "No channel or user has been selected.".colorize(:light_red) + else + workspace.show_details + end when "6", "quit" puts "Thank you for using the Ada Slack CLI".colorize(:magenta) exit diff --git a/lib/workspace.rb b/lib/workspace.rb index 290f1be4..c1a2604f 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -44,6 +44,7 @@ def select_channel(channel_descriptor) end def show_details #currently_selected_recipient + return false if @selected == nil tp @selected, @selected.details end From 1e11e83c565772b460c42cb7c4d30adfb918059a Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 14:58:32 -0700 Subject: [PATCH 24/27] added testing for Workspace#show_details for invalid parameters --- specs/workspace_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index cbf9aa32..1dc447a2 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -99,6 +99,31 @@ assert_nil(selected_channel, msg = nil) end end + + describe "#show_details" do + it "returns an instance of table_print when a valid paramter is passed" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + select_user = workspace.select_user("kimberly.fasbender") + expect(workspace.show_details).must_be_kind_of TablePrint::Returnable + + select_channel = workspace.select_channel("puppers") + expect(workspace.show_details).must_be_kind_of TablePrint::Returnable + end + end + + it "returns false if an invalid channel/user is passed to it" do + VCR.use_cassette("workspace_information_find") do + workspace = Slack::Workspace.new + select_user = workspace.select_user("Reginald") + expect(workspace.show_details).must_equal false + + select_channel = workspace.select_channel("Thomas") + expect(workspace.show_details).must_equal false + end + end + end + # select_channel end end # end end From 0ef65c24330a3e8fd806eaa81d97ffebd828771d Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 15:52:27 -0700 Subject: [PATCH 25/27] added send message functionality to the CLI --- lib/recipient.rb | 16 +++++++++++++++- lib/slack.rb | 16 +++++++++++++--- lib/workspace.rb | 4 ++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 42efa50b..a956bc7b 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -7,6 +7,8 @@ module Slack class ResponseError < StandardError; end class Recipient + URL = "https://slack.com/api/chat.postMessage" + attr_reader :slack_id, :name def initialize(slack_id, name) @@ -24,7 +26,19 @@ def self.get(url, params) return user_data end - def send_message + def send_message(message, selected) + message_request = HTTParty.post(URL, + body: { + token: ENV["KEY"], + text: message, + channel: selected, + as_user: true, + }, + headers: {"Content-Type" => "application/x-www-form-urlencoded"}) + + if message_request["ok"] == false + raise ResponseError, "There was an error!\nMessage: #{message_request["error"]}" + end end def details diff --git a/lib/slack.rb b/lib/slack.rb index c9dfa699..b86a05f0 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -23,7 +23,8 @@ def main puts "3. select user".colorize(:blue) puts "4. select channel".colorize(:green) puts "5. details".colorize(:yellow) - puts "6. quit\n\n".colorize(:light_red) + puts "6. send message".colorize(:yellow) + puts "7. quit\n".colorize(:light_red) selection = gets.chomp @@ -42,12 +43,21 @@ def main channel_descriptor = gets.chomp puts "\n#{workspace.select_channel(channel_descriptor)}".colorize(:light_red) when "5", "details" - if !workspace.show_details + if workspace.show_details == false puts "No channel or user has been selected.".colorize(:light_red) else workspace.show_details end - when "6", "quit" + when "6", "send message" + print "Please enter in a message to send: ".colorize(:yellow) + message = gets.chomp + sending_message = workspace.send_message(message) + if sending_message == false + puts "--- No channel or user selected. ----".colorize(:light_red) + elsif sending_message == nil + puts "--- Invalid message, unable to send. ---".colorize(:light_red) + else puts "--- Message sent succesfully! ---".colorize(:blue) end + when "7", "quit" puts "Thank you for using the Ada Slack CLI".colorize(:magenta) exit end # end diff --git a/lib/workspace.rb b/lib/workspace.rb index c1a2604f..8aa9212f 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -49,6 +49,10 @@ def show_details #currently_selected_recipient end def send_message(message) + return false if @selected == nil + return nil if message == "" + + @selected.send_message(message, @selected.slack_id) end end end From 9222bf3cf42fdbfeae9aa85e3d6ff284359635c4 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 16:44:14 -0700 Subject: [PATCH 26/27] updated testing for Workspace#send_message --- lib/recipient.rb | 2 ++ lib/workspace.rb | 2 +- specs/recipient_spec.rb | 5 +++++ specs/workspace_spec.rb | 28 +++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index a956bc7b..8000a418 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -39,6 +39,8 @@ def send_message(message, selected) if message_request["ok"] == false raise ResponseError, "There was an error!\nMessage: #{message_request["error"]}" end + + return message_request end def details diff --git a/lib/workspace.rb b/lib/workspace.rb index 8aa9212f..5b5a2650 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -52,7 +52,7 @@ def send_message(message) return false if @selected == nil return nil if message == "" - @selected.send_message(message, @selected.slack_id) + return @selected.send_message(message, @selected.slack_id) end end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index d48af45a..1d94ca52 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -8,4 +8,9 @@ }.must_raise NotImplementedError end end + + describe "#send_message" do + it "raises an Error if incorrect parameters are given" do + end + end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 1dc447a2..b5f0af63 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -123,8 +123,34 @@ end end end + end - # select_channel end + describe "#send_message" do + it "sends a message if all requirements are met" do + VCR.use_cassette("recipient_information_find") do + workspace = Slack::Workspace.new + workspace.select_user("cloudylopez") + message = workspace.send_message("You're the best!! :dog:") + expect(message["ok"]).must_equal true + end + end + + it "returns false if no channel or user are selected" do + VCR.use_cassette("recipient_information_find") do + workspace = Slack::Workspace.new + message = workspace.send_message("You're the best!! :dog:") + expect(message).must_equal false + end + end + + it "returns nil if invalid message is entered" do + VCR.use_cassette("recipient_information_find") do + workspace = Slack::Workspace.new + message = workspace.send_message("") + expect(message).must_equal false + end + end end + # end end end From edc7cc47562e0db57128f0d428cd2a946f466113 Mon Sep 17 00:00:00 2001 From: Kimberly-Fasbender Date: Thu, 21 Mar 2019 19:33:46 -0700 Subject: [PATCH 27/27] fixed a typo in CLI --- lib/slack.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index b86a05f0..b95d3e81 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -34,7 +34,7 @@ def main :include => {:User_Name => {:display_method => "name"}} when "2", "list channels" tp workspace.channels, "name", "slack_id", "topic", "member_count" - when "3", "select_user" + when "3", "select user" print "Please enter in the user's USER_NAME or SLACK_ID: ".colorize(:blue) user_descriptor = gets.chomp puts "\n#{workspace.select_user(user_descriptor)}".colorize(:light_red)