From 3d87a0774bc8130f0aaacf48543eebd8eb61f4eb Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 11:38:22 -0700 Subject: [PATCH 01/33] checked that could connect to API and print out channel list --- lib/slack.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..23b2dbff 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,4 +1,14 @@ #!/usr/bin/env ruby +require "pry" +require "httparty" +require "dotenv" +Dotenv.load +BASE_URL = "https://slack.com/api/channels.list" +query_params = {token: ENV["SLACK_API_TOKEN"]} +response = HTTParty.get(BASE_URL, query: query_params) +# Binding.pry + +p response["channels"].map { |channel| channel["name"] } def main puts "Welcome to the Ada Slack CLI!" @@ -8,4 +18,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 50d8f7a1fb71495ffc35c83be75cb06b0e085891 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 11:55:18 -0700 Subject: [PATCH 02/33] set up test_helper --- lib/slack.rb | 1 + specs/test_helper.rb | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 23b2dbff..2624d905 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -6,6 +6,7 @@ BASE_URL = "https://slack.com/api/channels.list" query_params = {token: ENV["SLACK_API_TOKEN"]} response = HTTParty.get(BASE_URL, query: query_params) +p response # Binding.pry p response["channels"].map { |channel| channel["name"] } diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..a81543f4 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,28 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'vcr' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "vcr" +require "webmock/minitest" +require "dotenv" +Dotenv.load + +require_relative "../lib/slack" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock -end \ No newline at end of file + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } + # Don't leave our Slack token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["LOCATIONIQ_TOKEN"] + end +end From 18580f97448ee1b2f09000e13efd5cf4bda6eec3 Mon Sep 17 00:00:00 2001 From: Maria Wissler Date: Tue, 19 Mar 2019 13:42:18 -0700 Subject: [PATCH 03/33] created user class --- lib/user.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/user.rb diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..a6075d9d --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,10 @@ +require 'pry' +require 'httparty' + +class User < Recipient + attr_reader :real_name + + def initialize(real_name) + @real_name = real_name + end +end From d40ea592a60c5d63a45fffc1a7bce759728471e4 Mon Sep 17 00:00:00 2001 From: Maria Wissler Date: Tue, 19 Mar 2019 14:07:07 -0700 Subject: [PATCH 04/33] created test or Recipient --- specs/recipient_spec.rb | 9 +++++++++ specs/test_helper.rb | 4 ++-- specs/user_spec.rb | 0 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 specs/recipient_spec.rb create mode 100644 specs/user_spec.rb diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..ae18ea91 --- /dev/null +++ b/specs/recipient_spec.rb @@ -0,0 +1,9 @@ +require_relative 'test_helper' + +describe "recipient class" do + describe "initialize" do + it "creates an instance of Recipient" do + expect(Recipient.new(slack_id, name)).must_be_kind_of Recipient + end + end +end \ No newline at end of file diff --git a/specs/test_helper.rb b/specs/test_helper.rb index a81543f4..f5c00a55 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -22,7 +22,7 @@ :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match } # Don't leave our Slack token lying around in a cassette file. - config.filter_sensitive_data("") do - ENV["LOCATIONIQ_TOKEN"] + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e69de29b From 2264e5b1331ff5ad3afb50e475d1b66de13f041c Mon Sep 17 00:00:00 2001 From: Maria Wissler Date: Tue, 19 Mar 2019 14:07:57 -0700 Subject: [PATCH 05/33] created Reciepient class --- lib/recipient.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/recipient.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..dc08d448 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,13 @@ +require 'pry' +require 'httparty' + +class Recipient + attr_reader :slack_id, :name + +def initialize(slack_id:, name:) + @slack_id = slack_id + @name = name +end + +end + From 041a674e8b347dbd5ed5d1ae67074bd559d7458f Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 14:08:23 -0700 Subject: [PATCH 06/33] api token variable changed --- specs/test_helper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index a81543f4..bf496a66 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -21,8 +21,9 @@ :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 Slack token lying around in a cassette file. - config.filter_sensitive_data("") do - ENV["LOCATIONIQ_TOKEN"] + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] end end From e11022cd1cbca721008125fd2711b3f13799a268 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 14:22:37 -0700 Subject: [PATCH 07/33] initialized recipient and tested instance of recipient --- lib/recipient.rb | 14 ++++++-------- lib/slack.rb | 12 ++++++------ specs/recipient_spec.rb | 18 ++++++++++-------- specs/test_helper.rb | 1 + 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index dc08d448..aba98753 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,13 +1,11 @@ -require 'pry' -require 'httparty' +require "pry" +require "httparty" -class Recipient +class Recipient attr_reader :slack_id, :name -def initialize(slack_id:, name:) + def initialize(slack_id: nil, name: nil) @slack_id = slack_id @name = name -end - -end - + end +end diff --git a/lib/slack.rb b/lib/slack.rb index 2624d905..34a290f2 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,13 +3,13 @@ require "httparty" require "dotenv" Dotenv.load -BASE_URL = "https://slack.com/api/channels.list" -query_params = {token: ENV["SLACK_API_TOKEN"]} -response = HTTParty.get(BASE_URL, query: query_params) -p response -# Binding.pry +# BASE_URL = "https://slack.com/api/channels.list" +# query_params = {token: ENV["SLACK_API_TOKEN"]} +# response = HTTParty.get(BASE_URL, query: query_params) +# p response +# # Binding.pry -p response["channels"].map { |channel| channel["name"] } +# p response["channels"].map { |channel| channel["name"] } def main puts "Welcome to the Ada Slack CLI!" diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index ae18ea91..3bb77da3 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -1,9 +1,11 @@ -require_relative 'test_helper' +require_relative "test_helper" -describe "recipient class" do - describe "initialize" do - it "creates an instance of Recipient" do - expect(Recipient.new(slack_id, name)).must_be_kind_of Recipient - end - end -end \ No newline at end of file +describe "recipient class" do + describe "initialize" do + it "creates an instance of Recipient" do + slack_id = 1 + name = "Maria" + expect(Recipient.new(slack_id: slack_id, name: name)).must_be_kind_of Recipient + end + end +end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 4130764b..c880844b 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -11,6 +11,7 @@ Dotenv.load require_relative "../lib/slack" +require_relative "../lib/recipient" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From b6f7258c58982409b0b39955ef4382a281ec4ac6 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 14:46:20 -0700 Subject: [PATCH 08/33] added get method and test for connectivity --- lib/recipient.rb | 8 +++++++- specs/recipient_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index aba98753..7ff610f9 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -4,8 +4,14 @@ class Recipient attr_reader :slack_id, :name - def initialize(slack_id: nil, name: nil) + def initialize(slack_id:, name:) @slack_id = slack_id + raise ArgumentError if !name.is_a? String + @name = name end + + def self.get(url, params) + response = HTTParty.get(url, query: params) + end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 3bb77da3..8d01379c 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -3,9 +3,26 @@ describe "recipient class" do describe "initialize" do it "creates an instance of Recipient" do + #check slack_if format slack_id = 1 name = "Maria" expect(Recipient.new(slack_id: slack_id, name: name)).must_be_kind_of Recipient end + + it "raises an argument error if name is not a string" do + expect { Recipient.new(slack_id: 1, name: 21) }.must_raise ArgumentError + end + end + + describe "can connect to API" do + it "can connect" do + VCR.use_cassette("find channels") do + BASE_URL = "https://slack.com/api/channels.list" + params = {token: ENV["SLACK_API_TOKEN"]} + response = Recipient.get(BASE_URL, params) + expect(response["channels"]).wont_be_nil + expect(response["channels"].map { |channel| channel["name"] }).must_equal ["general", "api-project", "random"] + end + end end end From 66d466260b4e1b74e7772757ee387e62103994e8 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 15:01:38 -0700 Subject: [PATCH 09/33] name connection --- specs/recipient_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 8d01379c..652591da 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -1,5 +1,5 @@ require_relative "test_helper" - +require "pry" describe "recipient class" do describe "initialize" do it "creates an instance of Recipient" do @@ -24,5 +24,15 @@ expect(response["channels"].map { |channel| channel["name"] }).must_equal ["general", "api-project", "random"] end end + + it "gives a list of two names" do + VCR.use_cassette("find channels") do + BASE_URL = "https://slack.com/api/users.list" + params = {token: ENV["SLACK_API_TOKEN"]} + response = Recipient.get(BASE_URL, params) + Binding.pry + expect(response["name"]).wont_be_nil + end + end end end From 8f410b296c575e1e24c8bfd40b3ebf68bc74a5a2 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 16:29:34 -0700 Subject: [PATCH 10/33] modified self.get and self.list --- lib/recipient.rb | 12 +++++++++++- lib/user.rb | 11 +++++++++-- specs/recipient_spec.rb | 24 ++++++++++++++++-------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 7ff610f9..6d59ae99 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -2,6 +2,8 @@ require "httparty" class Recipient + BASE_URL = "https://slack.com/api/" + attr_reader :slack_id, :name def initialize(slack_id:, name:) @@ -11,7 +13,15 @@ def initialize(slack_id:, name:) @name = name end - def self.get(url, params) + private + + def self.get(endpoint, params = {}) + url = BASE_URL + endpoint + params[:token] = ENV["SLACK_API_TOKEN"] response = HTTParty.get(url, query: params) end + + def self.list + raise NotImplementedError, "Implement me in a child class!" + end end diff --git a/lib/user.rb b/lib/user.rb index a6075d9d..0fb4bfb9 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,5 +1,5 @@ -require 'pry' -require 'httparty' +require "pry" +require "httparty" class User < Recipient attr_reader :real_name @@ -7,4 +7,11 @@ class User < Recipient def initialize(real_name) @real_name = real_name end + + def self.list + response = self.get("channels.list") + response.map do + self.new + end + end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 652591da..10a9d709 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -17,9 +17,8 @@ describe "can connect to API" do it "can connect" do VCR.use_cassette("find channels") do - BASE_URL = "https://slack.com/api/channels.list" - params = {token: ENV["SLACK_API_TOKEN"]} - response = Recipient.get(BASE_URL, params) + endpoint = "channels.list" + response = Recipient.get("channels.list") expect(response["channels"]).wont_be_nil expect(response["channels"].map { |channel| channel["name"] }).must_equal ["general", "api-project", "random"] end @@ -27,11 +26,20 @@ it "gives a list of two names" do VCR.use_cassette("find channels") do - BASE_URL = "https://slack.com/api/users.list" - params = {token: ENV["SLACK_API_TOKEN"]} - response = Recipient.get(BASE_URL, params) - Binding.pry - expect(response["name"]).wont_be_nil + endpoint = "users.list" + response = Recipient.get(endpoint) + # Binding.pry + expect(response).wont_be_nil + expect(response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] + end + end + it "correctly finds the status of a member" do + VCR.use_cassette("user status") do + endpoint = "users.list" + response = Recipient.get(endpoint) + expect(response["members"][0]["profile"]["status_text"].length).must_equal 0 + p response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"] + expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" end end end From 91ee4379338d2f788b90e5b44aab3fae5a145910 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Tue, 19 Mar 2019 18:01:28 -0700 Subject: [PATCH 11/33] Created test for self_list in User_spec , modified self_list in User --- lib/recipient.rb | 2 +- lib/user.rb | 19 ++++++++++++++----- specs/recipient_spec.rb | 2 -- specs/test_helper.rb | 1 + specs/user_spec.rb | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 6d59ae99..38db710a 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -18,7 +18,7 @@ def initialize(slack_id:, name:) def self.get(endpoint, params = {}) url = BASE_URL + endpoint params[:token] = ENV["SLACK_API_TOKEN"] - response = HTTParty.get(url, query: params) + HTTParty.get(url, query: params) end def self.list diff --git a/lib/user.rb b/lib/user.rb index 0fb4bfb9..8ff3b20a 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -2,16 +2,25 @@ require "httparty" class User < Recipient - attr_reader :real_name + attr_reader :real_name, :status_text, :status_emoji - def initialize(real_name) + def initialize(real_name:, status_text: nil, status_emoji: nil) @real_name = real_name + @status_text = status_text + @status_emoji = status_emoji end def self.list - response = self.get("channels.list") - response.map do - self.new + response = self.get("users.list") + user_list = [] + response["members"].each do |member| + real_name = member["real_name"] + status_text = member["profile"]["status_text"] + status_emoji = member["profile"]["status_emoji"] + # response.map do + + user_list << self.new(real_name: real_name, status_text: status_text, status_emoji: status_emoji) end + return user_list end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 10a9d709..c4d0f3df 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -17,7 +17,6 @@ describe "can connect to API" do it "can connect" do VCR.use_cassette("find channels") do - endpoint = "channels.list" response = Recipient.get("channels.list") expect(response["channels"]).wont_be_nil expect(response["channels"].map { |channel| channel["name"] }).must_equal ["general", "api-project", "random"] @@ -38,7 +37,6 @@ endpoint = "users.list" response = Recipient.get(endpoint) expect(response["members"][0]["profile"]["status_text"].length).must_equal 0 - p response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"] expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" end end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index c880844b..aa06e040 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -12,6 +12,7 @@ require_relative "../lib/slack" require_relative "../lib/recipient" +require_relative "../lib/user" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e69de29b..b62d656b 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -0,0 +1,39 @@ +require_relative "test_helper" +require "pry" +describe "user class" do + describe "initialize" do + it "creates and instance of user" do + real_name = "Maria Wissler" + status_text = "" + status_emoji = "" + expect(User.new(real_name: real_name, status_text: status_text, status_emoji: status_emoji)).must_be_kind_of User + end + end + + describe "can connect to API" do + it "gives a list of three names" do + VCR.use_cassette("find members") do + endpoint = "users.list" + response = User.get(endpoint) + expect(response).wont_be_nil + expect(response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] + end + end + it "correctly finds the status of a member" do + VCR.use_cassette("user status") do + endpoint = "users.list" + response = User.get(endpoint) + expect(response["members"][0]["profile"]["status_text"].length).must_equal 0 + expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" + end + end + describe "create list of users" do + it "returns an array with instance of user" do + VCR.use_cassette("user status") do + user_list = User.list + expect(user_list).must_be_kind_of Array + end + end + end + end +end From ecf4c114043b0abfe2fc9248cd897f3e346bb05f Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 13:44:58 -0700 Subject: [PATCH 12/33] added details method to be implemented in child class --- lib/recipient.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/recipient.rb b/lib/recipient.rb index 38db710a..643af6e1 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -24,4 +24,8 @@ def self.get(endpoint, params = {}) def self.list raise NotImplementedError, "Implement me in a child class!" end + + def self.details + raise NotImplementedError, "Implement me in a child class!" + end end From 697db067380647a4655a5511ba5d7aab06fb7b47 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:24:06 -0700 Subject: [PATCH 13/33] option to quit added --- lib/slack.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 34a290f2..d6338520 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -15,7 +15,9 @@ def main puts "Welcome to the Ada Slack CLI!" # TODO project - + puts "What would you like to do? Type 1 to select a user or 2 to quit" + option = gets.chomp() + return if option == 2 puts "Thank you for using the Ada Slack CLI" end From a9ba71f60604f25b86881fd6117508eff3682e8e Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:24:34 -0700 Subject: [PATCH 14/33] require relative channel --- specs/test_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index aa06e040..834a44bf 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -13,6 +13,7 @@ require_relative "../lib/slack" require_relative "../lib/recipient" require_relative "../lib/user" +require_relative "../lib/channel" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 58b7b462c383a96ed119c7d6d0c68d5f366045b7 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:25:18 -0700 Subject: [PATCH 15/33] added tests to check count of users returned --- specs/user_spec.rb | 51 +++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b62d656b..d0745b33 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -11,28 +11,51 @@ end describe "can connect to API" do + before do + VCR.use_cassette("connect to endpoints users_list") do + endpoint = "users.list" + @response = User.get(endpoint) + end + end it "gives a list of three names" do VCR.use_cassette("find members") do - endpoint = "users.list" - response = User.get(endpoint) - expect(response).wont_be_nil - expect(response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] + expect(@response).wont_be_nil + expect(@response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] end end it "correctly finds the status of a member" do VCR.use_cassette("user status") do - endpoint = "users.list" - response = User.get(endpoint) - expect(response["members"][0]["profile"]["status_text"].length).must_equal 0 - expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" + expect(@response["members"][0]["profile"]["status_text"].length).must_equal 0 + # expect(@response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" + end + end + end + describe "create list of users" do + before do + VCR.use_cassette("list_users") do + @user_list = User.list end end - describe "create list of users" do - it "returns an array with instance of user" do - VCR.use_cassette("user status") do - user_list = User.list - expect(user_list).must_be_kind_of Array - end + it "returns an array of users" do + VCR.use_cassette("list_users") do + expect(@user_list).must_be_kind_of Array + end + end + it "contains instances of user within the array" do + VCR.use_cassette("instance of User within array") do + expect(@user_list[0]).must_be_kind_of User + end + end + # can this be used for other channel? + it "returns a list with length 3" do + VCR.use_cassette("length user list") do + expect(@user_list.length).must_equal 3 + end + end + + it "returns name of first user correctly" do + VCR.use_cassette("length user list") do + expect(@user_list.first.real_name).must_equal "Slackbot" end end end From efc62ed1f1cca1a126bebcbfc40e7708c9239d93 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:27:11 -0700 Subject: [PATCH 16/33] initialized channel and self.list and tests for it --- lib/channel.rb | 28 +++++++++++++++++++++++ specs/channel_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 lib/channel.rb create mode 100644 specs/channel_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..71c0ca50 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,28 @@ +require "pry" +require "httparty" + +class Channel < Recipient + attr_reader :topic, :member_count + + def initialize(name:, slack_id:, topic:, member_count:) + super(name: name, slack_id: slack_id) + @topic = topic + @member_count = member_count + end + + def self.list + response = self.get("channels.list") + channel_list = [] + response["channels"].each do |channel| + name = channel["name"] + slack_id = channel["id"] + topic = channel["topic"] + member_count = channel["members"].count + channel_list << self.new(name: name, slack_id: slack_id, topic: topic, member_count: member_count) + end + return channel_list + end + + def self.details + end +end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..b97035cb --- /dev/null +++ b/specs/channel_spec.rb @@ -0,0 +1,53 @@ +require_relative "test_helper" +require "pry" +describe "channel class" do + describe "initialize" do + it "creates and instance of channel" do + topic = "random" + member_count = 3 + name = "cyndilopez6" + slack_id = 1 + expect(Channel.new(name: name, slack_id: slack_id, topic: topic, member_count: member_count)).must_be_kind_of Channel + end + end + + describe "can connect to API" do + before do + VCR.use_cassette("connect to endpoints channels_list") do + endpoint = "channels.list" + @response = Channel.get(endpoint) + end + end + + it "accesses api" do + expect(@response.code == 200 && @response.parsed_response["ok"]).must_equal true + end + end + + describe "creates list of channels" do + # before do + # VCR.use_cassette("connect to endpoints channels_list") do + # endpoint = "channels.list" + # @response = Channel.get(endpoint) + # end + # end + + it "returns a type of array" do + VCR.use_cassette("returns array") do + expect(Channel.list.is_a? Array).must_equal true + end + end + + it "returns an array of Channel objects" do + VCR.use_cassette("returns object Channel") do + expect(Channel.list[0]).must_be_kind_of Channel + end + end + + it "returns an accurate list of channels in slack workspace" do + VCR.use_cassette("correct channels") do + expect(Channel.list.map { |channel| channel.name }).must_equal ["general", "api-project", "random"] + end + end + end +end From 769c283c04e7a5b86e63a01f20e01fcdf6e2d30e Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:34:27 -0700 Subject: [PATCH 17/33] now calling super in channel, fixed tests for this --- lib/user.rb | 12 ++++++++---- specs/user_spec.rb | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 8ff3b20a..e4fb5e82 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -4,7 +4,8 @@ class User < Recipient attr_reader :real_name, :status_text, :status_emoji - def initialize(real_name:, status_text: nil, status_emoji: nil) + def initialize(name:, slack_id:, real_name:, status_text: nil, status_emoji: nil) + super(name: name, slack_id: slack_id) @real_name = real_name @status_text = status_text @status_emoji = status_emoji @@ -14,13 +15,16 @@ def self.list response = self.get("users.list") user_list = [] response["members"].each do |member| + name = member["name"] + slack_id = member["id"] real_name = member["real_name"] status_text = member["profile"]["status_text"] status_emoji = member["profile"]["status_emoji"] - # response.map do - - user_list << self.new(real_name: real_name, status_text: status_text, status_emoji: status_emoji) + user_list << self.new(name: name, slack_id: slack_id, real_name: real_name, status_text: status_text, status_emoji: status_emoji) end return user_list end + + def self.details + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index d0745b33..89295f01 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -3,10 +3,12 @@ describe "user class" do describe "initialize" do it "creates and instance of user" do + name = "mcarmelina" + slack_id = 123 real_name = "Maria Wissler" status_text = "" status_emoji = "" - expect(User.new(real_name: real_name, status_text: status_text, status_emoji: status_emoji)).must_be_kind_of User + expect(User.new(name: name, slack_id: slack_id, real_name: real_name, status_text: status_text, status_emoji: status_emoji)).must_be_kind_of User end end From 3287b9ff37baf2f8440c83459d6d34168099de8a Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 15:48:44 -0700 Subject: [PATCH 18/33] filled out details methods for user and channel. no tests --- lib/channel.rb | 6 +++++- lib/user.rb | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 71c0ca50..1c9f41f5 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -23,6 +23,10 @@ def self.list return channel_list end - def self.details + def details(channel) + puts "Name: #{channel.name}" + puts "ID: #{channel.slack_id}" + puts "Topic: #{channel.topic}" + puts "Number of members: #{channel.member_count}" end end diff --git a/lib/user.rb b/lib/user.rb index e4fb5e82..b783d370 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -25,6 +25,10 @@ def self.list return user_list end - def self.details + def details(user) + puts "Username: #{user.name}" + puts "ID: #{user.slack_id}" + puts "Name: #{user.real_name}" + puts "Status: #{user.status_text}" end end From 30297eb21cdfb550f4efdc6fefcdf967b505a44c Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 17:46:41 -0700 Subject: [PATCH 19/33] refactored to make tests more general --- specs/channel_spec.rb | 2 +- specs/recipient_spec.rb | 12 ++++++------ specs/user_spec.rb | 13 +++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index b97035cb..372b6921 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -46,7 +46,7 @@ it "returns an accurate list of channels in slack workspace" do VCR.use_cassette("correct channels") do - expect(Channel.list.map { |channel| channel.name }).must_equal ["general", "api-project", "random"] + expect(Channel.list.map { |channel| channel.name }.length).must_be :>, 0 end end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index c4d0f3df..b077118b 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -19,25 +19,25 @@ VCR.use_cassette("find channels") do response = Recipient.get("channels.list") expect(response["channels"]).wont_be_nil - expect(response["channels"].map { |channel| channel["name"] }).must_equal ["general", "api-project", "random"] + expect(response["channels"].map { |channel| channel["name"] }.length).must_be :>, 0 end end - it "gives a list of two names" do + it "gives a list with more than one user name" do VCR.use_cassette("find channels") do endpoint = "users.list" response = Recipient.get(endpoint) # Binding.pry expect(response).wont_be_nil - expect(response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] + expect(response["members"].map { |member| member["name"] }.length).must_be :>, 0 end end - it "correctly finds the status of a member" do + it "can find the status of a member" do VCR.use_cassette("user status") do endpoint = "users.list" response = Recipient.get(endpoint) - expect(response["members"][0]["profile"]["status_text"].length).must_equal 0 - expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" + expect(response["members"][0]["profile"]["status_text"].length).wont_be_nil + expect(response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_be_kind_of String end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 89295f01..f017b7b7 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -17,21 +17,22 @@ VCR.use_cassette("connect to endpoints users_list") do endpoint = "users.list" @response = User.get(endpoint) + expect(@response.code == 200 && @response.parsed_response["ok"]).must_equal true end end - it "gives a list of three names" do + it "gives a list of names" do VCR.use_cassette("find members") do expect(@response).wont_be_nil - expect(@response["members"].map { |member| member["name"] }).must_equal ["slackbot", "wmcarmelina", "cyndilopez6"] + expect(@response["members"].map { |member| member["name"] }.length).must_be :>, 0 end end - it "correctly finds the status of a member" do + it "finds the status of a member" do VCR.use_cassette("user status") do - expect(@response["members"][0]["profile"]["status_text"].length).must_equal 0 - # expect(@response["members"].select { |member| member["real_name"] == "Maria Wissler" }[0]["profile"]["status_text"]).must_equal "Working remotely from Kauai" + expect(@response["members"][0]["profile"]["status_text"].length).wont_be_nil end end end + describe "create list of users" do before do VCR.use_cassette("list_users") do @@ -51,7 +52,7 @@ # can this be used for other channel? it "returns a list with length 3" do VCR.use_cassette("length user list") do - expect(@user_list.length).must_equal 3 + expect(@user_list.length).must_be :>, 0 end end From d38535663a5097f656477baedac752008655d179 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 21:19:09 -0700 Subject: [PATCH 20/33] apierror class --- lib/apierror.rb | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib/apierror.rb diff --git a/lib/apierror.rb b/lib/apierror.rb new file mode 100644 index 00000000..e65f8de8 --- /dev/null +++ b/lib/apierror.rb @@ -0,0 +1 @@ +class SlackApiError < StandardError; end From e8fd7fdbb1153384cce9b8ded7f4e2018ddb041b Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 21:20:43 -0700 Subject: [PATCH 21/33] now raises error if the API call didn't work --- lib/recipient.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 643af6e1..45cd782b 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -18,7 +18,13 @@ def initialize(slack_id:, name:) def self.get(endpoint, params = {}) url = BASE_URL + endpoint params[:token] = ENV["SLACK_API_TOKEN"] - HTTParty.get(url, query: params) + p params + response = HTTParty.get(url, query: params) + unless response.code == 200 && response.parsed_response["ok"] + p response["error"] + raise SlackApiError, response["error"] + end + return response end def self.list From 2823311b2e6cdb6711e0b1664775aca4b6e407d4 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 21:21:35 -0700 Subject: [PATCH 22/33] test for error raised when method is unknown --- specs/channel_spec.rb | 22 +++++++++++++++++++--- specs/user_spec.rb | 10 ++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 372b6921..4868cf9c 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -12,16 +12,32 @@ end describe "can connect to API" do - before do + it "accesses api" do VCR.use_cassette("connect to endpoints channels_list") do endpoint = "channels.list" @response = Channel.get(endpoint) end + expect(@response.code == 200 && @response.parsed_response["ok"]).must_equal true end + end - it "accesses api" do - expect(@response.code == 200 && @response.parsed_response["ok"]).must_equal true + describe "raises errors for incorrect endpoint" do + it "raises an error for incorrect endpoint" do + VCR.use_cassette("check_method_error_raised") do + endpoint = "ret424252E#1231+=.y" + exception = expect { Channel.get(endpoint) }.must_raise SlackApiError + expect(exception.message).must_equal "unknown_method" + end end + + # it "raises an error for incorrect token" do + # VCR.use_cassette("check_auth_error_raised") do + # endpoint = "channels.list" + # params = {:token => "0123456789abcdef"} + # p params + # expect(Channel.get(endpoint, params)).must_equal "invalid_auth" + # end + # end end describe "creates list of channels" do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index f017b7b7..fd0a900c 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -33,6 +33,16 @@ end end + describe "raises errors for incorrect endpoint" do + it "raises an error for incorrect endpoint" do + VCR.use_cassette("check_method_error_raised") do + endpoint = "ret424252E#1231+=.y" + exception = expect { User.get(endpoint) }.must_raise SlackApiError + expect(exception.message).must_equal "unknown_method" + end + end + end + describe "create list of users" do before do VCR.use_cassette("list_users") do From 264f5af158db95bb5594efee21b4f3ea8e24dde7 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 21:22:07 -0700 Subject: [PATCH 23/33] require relative apierror --- specs/test_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 834a44bf..acbef725 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/channel" +require_relative "../lib/apierror" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From fa897136d28683e6de667744ec739d302a7c4a27 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 22:02:29 -0700 Subject: [PATCH 24/33] set up workspace class file and specs --- lib/slack.rb | 7 ------- lib/workspace.rb | 22 ++++++++++++++++++++++ specs/channel_spec.rb | 7 ------- specs/test_helper.rb | 1 + specs/user_spec.rb | 12 ++++++------ specs/workspace_spec.rb | 8 ++++++++ 6 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 lib/workspace.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/slack.rb b/lib/slack.rb index d6338520..8fc38954 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,13 +3,6 @@ require "httparty" require "dotenv" Dotenv.load -# BASE_URL = "https://slack.com/api/channels.list" -# query_params = {token: ENV["SLACK_API_TOKEN"]} -# response = HTTParty.get(BASE_URL, query: query_params) -# p response -# # Binding.pry - -# p response["channels"].map { |channel| channel["name"] } def main puts "Welcome to the Ada Slack CLI!" diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..d39e0804 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,22 @@ +require "httparty" + +class Workspace + attr_reader :users, :channels, :selected + + def initialize(users:, channels:, selected:) + @users = User.list + @channels = Channel.list + @selected = selected #either user or channel + end + + # methods that initialize selected (either select_user or select_method) + def select_user + #find @selected within @users info + + end + + def show_details + user = select_user + user.details #need to change details so that this produces a hash and can be pretty printed out here + end +end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index 4868cf9c..aa142641 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -41,13 +41,6 @@ end describe "creates list of channels" do - # before do - # VCR.use_cassette("connect to endpoints channels_list") do - # endpoint = "channels.list" - # @response = Channel.get(endpoint) - # end - # end - it "returns a type of array" do VCR.use_cassette("returns array") do expect(Channel.list.is_a? Array).must_equal true diff --git a/specs/test_helper.rb b/specs/test_helper.rb index acbef725..2b796040 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -15,6 +15,7 @@ require_relative "../lib/user" require_relative "../lib/channel" require_relative "../lib/apierror" +require_relative "../lib/workspace" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/user_spec.rb b/specs/user_spec.rb index fd0a900c..3cd9f865 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -21,7 +21,7 @@ end end it "gives a list of names" do - VCR.use_cassette("find members") do + VCR.use_cassette("find members not empty") do expect(@response).wont_be_nil expect(@response["members"].map { |member| member["name"] }.length).must_be :>, 0 end @@ -45,12 +45,12 @@ describe "create list of users" do before do - VCR.use_cassette("list_users") do + VCR.use_cassette("self_list") do @user_list = User.list end end it "returns an array of users" do - VCR.use_cassette("list_users") do + VCR.use_cassette("list of users array") do expect(@user_list).must_be_kind_of Array end end @@ -59,15 +59,15 @@ expect(@user_list[0]).must_be_kind_of User end end - # can this be used for other channel? - it "returns a list with length 3" do + + it "returns a list that is not empty" do VCR.use_cassette("length user list") do expect(@user_list.length).must_be :>, 0 end end it "returns name of first user correctly" do - VCR.use_cassette("length user list") do + VCR.use_cassette("bot user name") do expect(@user_list.first.real_name).must_equal "Slackbot" end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..53793b67 --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1,8 @@ +require_relative "test_helper" + +describe "workspace class" do + describe "initialize" do + it "creates an instance of workspace" do + end + end +end From 4cabb7c82542d86da7c3d6f0ce20785a012e1573 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 20 Mar 2019 22:02:56 -0700 Subject: [PATCH 25/33] pseudocode cli --- lib/slack.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/slack.rb b/lib/slack.rb index 8fc38954..1b2e6767 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,6 +11,12 @@ def main puts "What would you like to do? Type 1 to select a user or 2 to quit" option = gets.chomp() return if option == 2 + + #if option = #, select_user + #username = gets.chomp + #workspace = Workspace.new(selected: username) + #if select show_details, workspace.show_details + puts "Thank you for using the Ada Slack CLI" end From 2f53d3b3e74a164ff656115e2ef162ff9167eab7 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 15:46:31 -0700 Subject: [PATCH 26/33] created select_user and select_channel methods, implemented them in the main file --- lib/channel.rb | 10 ++++---- lib/recipient.rb | 3 --- lib/slack.rb | 49 +++++++++++++++++++++++++++++++++++--- lib/user.rb | 14 ++++++----- lib/workspace.rb | 18 ++++++++++++-- specs/workspace_spec.rb | 52 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+), 19 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 1c9f41f5..72685669 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -23,10 +23,10 @@ def self.list return channel_list end - def details(channel) - puts "Name: #{channel.name}" - puts "ID: #{channel.slack_id}" - puts "Topic: #{channel.topic}" - puts "Number of members: #{channel.member_count}" + def details + puts "Name: #{self.name}" + puts "ID: #{self.slack_id}" + puts "Topic: #{self.topic}" + puts "Number of members: #{self.member_count}" end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 45cd782b..323f5ac7 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -9,7 +9,6 @@ class Recipient def initialize(slack_id:, name:) @slack_id = slack_id raise ArgumentError if !name.is_a? String - @name = name end @@ -18,10 +17,8 @@ def initialize(slack_id:, name:) def self.get(endpoint, params = {}) url = BASE_URL + endpoint params[:token] = ENV["SLACK_API_TOKEN"] - p params response = HTTParty.get(url, query: params) unless response.code == 200 && response.parsed_response["ok"] - p response["error"] raise SlackApiError, response["error"] end return response diff --git a/lib/slack.rb b/lib/slack.rb index 1b2e6767..17047f6c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,14 +2,57 @@ require "pry" require "httparty" require "dotenv" +require_relative "../lib/workspace" Dotenv.load +def display_options + puts "\nWhat would you like to do?" + puts "\nChoose from one of the following:" + puts "------------------------------" + puts "\nSelect user" + puts "\nSelect channel" + puts "\nDetails" + puts "\nQuit" + option = gets.chomp.downcase + return option +end + def main puts "Welcome to the Ada Slack CLI!" + option = display_options + + until option == "quit" + case option + when "select user" + puts "You chose to select a user. Please provide a username or Slack ID" + selected = gets.chomp() + workspace = Workspace.new(selected: selected) + user = workspace.select_user + until !user.nil? + puts "Please provide a valid username or Slack ID" + selected = gets.chomp + workspace = Workspace.new(selected: selected) + user = workspace.select_user + end + puts "You have selected #{user.real_name}" + option = display_options + when "select channel" + puts "You chose to select a channel. Please provide a channel name or Slack ID" + selected = gets.chomp() + workspace = Workspace.new(selected: selected) + channel = workspace.select_channel + until !channel.nil? + puts "Please provide a valid Channel name or Slack ID" + selected = gets.chomp + workspace = Workspace.new(selected: selected) + channel = workspace.select_channel + end + puts "You have selected #{channel.name}" + option = display_options + when "details" + end + end - # TODO project - puts "What would you like to do? Type 1 to select a user or 2 to quit" - option = gets.chomp() return if option == 2 #if option = #, select_user diff --git a/lib/user.rb b/lib/user.rb index b783d370..b55a3c11 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,5 +1,6 @@ require "pry" require "httparty" +require_relative "recipient.rb" class User < Recipient attr_reader :real_name, :status_text, :status_emoji @@ -11,7 +12,7 @@ def initialize(name:, slack_id:, real_name:, status_text: nil, status_emoji: nil @status_emoji = status_emoji end - def self.list + def self.list #factory method response = self.get("users.list") user_list = [] response["members"].each do |member| @@ -25,10 +26,11 @@ def self.list return user_list end - def details(user) - puts "Username: #{user.name}" - puts "ID: #{user.slack_id}" - puts "Name: #{user.real_name}" - puts "Status: #{user.status_text}" + def details #business logic + # check that this works + puts "Username: #{self.name}" + puts "ID: #{self.slack_id}" + puts "Name: #{self.real_name}" + puts "Status: #{self.status_text}" end end diff --git a/lib/workspace.rb b/lib/workspace.rb index d39e0804..72539124 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,20 +1,34 @@ require "httparty" +require_relative "../lib/user" +require_relative "../lib/channel" +require_relative "../lib/recipient" class Workspace attr_reader :users, :channels, :selected - def initialize(users:, channels:, selected:) + def initialize(selected:) @users = User.list @channels = Channel.list - @selected = selected #either user or channel + @selected = selected # either user or channel end # methods that initialize selected (either select_user or select_method) def select_user + user_selected = users.detect do |user| + user.slack_id == selected || user.name == selected + end + return user_selected #find @selected within @users info end + def select_channel + channel_selected = channels.detect do |channel| + channel.slack_id == selected || channel.name == selected + end + return channel_selected + end + def show_details user = select_user user.details #need to change details so that this produces a hash and can be pretty printed out here diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 53793b67..f6b2ce6a 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,8 +1,60 @@ require_relative "test_helper" describe "workspace class" do + before do + VCR.use_cassette("create workspace object") do + selected = "USLACKBOT" #id of slackbot + @workspace = Workspace.new(selected: selected) + end + end + describe "initialize" do it "creates an instance of workspace" do + expect(@workspace).must_be_kind_of Workspace + end + end + + describe "#select_user" do + it "returns an instance of User" do + expect(@workspace.select_user).must_be_kind_of User + end + + it "returned the correct user" do + user = @workspace.select_user + expect(user.name).must_equal "slackbot" + end + + it "returns nil if username or slack id is not found" do + VCR.use_cassette("check nil returned") do + selected = "chewy" #id of slackbot + @workspace2 = Workspace.new(selected: selected) + end + expect(@workspace2.select_user).must_be_nil + end + end + + describe "#selected_channel" do + before do + VCR.use_cassette("create workspace object") do + selected = "CH0ED08E4" #id for general channel + @workspace = Workspace.new(selected: selected) + end + end + it "returns an instance of Channel" do + expect(@workspace.select_channel).must_be_kind_of Channel + end + + it "returned the correct user" do + channel = @workspace.select_channel + expect(channel.name).must_be_kind_of String + end + + it "returns nil if username or slack id is not found" do + VCR.use_cassette("check nil returned") do + selected = "name your channel" + @workspace_channel = Workspace.new(selected: selected) + end + expect(@workspace_channel.select_channel).must_be_nil end end end From a8c729ccd9fedddc3ce5c83d90d1b4e58b977260 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 16:25:25 -0700 Subject: [PATCH 27/33] corrected topic display --- lib/channel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/channel.rb b/lib/channel.rb index 72685669..47e001a0 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -26,7 +26,7 @@ def self.list def details puts "Name: #{self.name}" puts "ID: #{self.slack_id}" - puts "Topic: #{self.topic}" + puts "Topic: #{self.topic["value"]}" puts "Number of members: #{self.member_count}" end end From 2423c691c973736e50dd583558035db1e787f610 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 16:26:05 -0700 Subject: [PATCH 28/33] added details option and options for incorrect user input --- lib/slack.rb | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 17047f6c..a63bb3c1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -6,7 +6,6 @@ Dotenv.load def display_options - puts "\nWhat would you like to do?" puts "\nChoose from one of the following:" puts "------------------------------" puts "\nSelect user" @@ -19,7 +18,14 @@ def display_options def main puts "Welcome to the Ada Slack CLI!" + puts "\nWhat would you like to do?" + options = ["select user", "select channel", "details", "quit"] + option = display_options + until options.include?(option) + puts "Please input a valid option." + option = display_options + end until option == "quit" case option @@ -27,34 +33,36 @@ def main puts "You chose to select a user. Please provide a username or Slack ID" selected = gets.chomp() workspace = Workspace.new(selected: selected) - user = workspace.select_user - until !user.nil? + selection = workspace.select_user + until !selection.nil? puts "Please provide a valid username or Slack ID" selected = gets.chomp workspace = Workspace.new(selected: selected) - user = workspace.select_user + selection = workspace.select_user end - puts "You have selected #{user.real_name}" + puts "You have selected #{selection.real_name}" + puts "\nWhat would you like to do next?" option = display_options when "select channel" puts "You chose to select a channel. Please provide a channel name or Slack ID" selected = gets.chomp() workspace = Workspace.new(selected: selected) - channel = workspace.select_channel - until !channel.nil? + selection = workspace.select_channel + until !selection.nil? puts "Please provide a valid Channel name or Slack ID" selected = gets.chomp workspace = Workspace.new(selected: selected) - channel = workspace.select_channel + selection = workspace.select_channel end - puts "You have selected #{channel.name}" + puts "You have selected #{selection.name}" option = display_options when "details" + selection.details + puts "\nWhat would you like to do next?" + option = display_options end end - return if option == 2 - #if option = #, select_user #username = gets.chomp #workspace = Workspace.new(selected: username) From 25304d65880d843cf88e855d0fc369b04c394b2a Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 18:49:14 -0700 Subject: [PATCH 29/33] created rescue methods so that a user or channel was required to be selected before calling message or details --- lib/recipient.rb | 7 +++++ lib/slack.rb | 69 ++++++++++++++++++++++++++++++++++-------------- lib/workspace.rb | 15 ++++++++--- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 323f5ac7..7960a66c 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -12,6 +12,13 @@ def initialize(slack_id:, name:) @name = name end + def send_message(params) + endpoint = "chat.postMessage" + url = BASE_URL + endpoint + params[:token] = ENV["SLACK_API_TOKEN"] + response = HTTParty.post(url, body: params) + end + private def self.get(endpoint, params = {}) diff --git a/lib/slack.rb b/lib/slack.rb index a63bb3c1..0ae4c2a3 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,21 +11,26 @@ def display_options puts "\nSelect user" puts "\nSelect channel" puts "\nDetails" + puts "\nMessage" puts "\nQuit" option = gets.chomp.downcase return option end +def verify_options(option) + options = ["select user", "select channel", "details", "message", "quit"] + until options.include?(option) + puts "Please input a valid option." + option = display_options + end +end + def main puts "Welcome to the Ada Slack CLI!" puts "\nWhat would you like to do?" - options = ["select user", "select channel", "details", "quit"] option = display_options - until options.include?(option) - puts "Please input a valid option." - option = display_options - end + verify_options(option) until option == "quit" case option @@ -33,40 +38,64 @@ def main puts "You chose to select a user. Please provide a username or Slack ID" selected = gets.chomp() workspace = Workspace.new(selected: selected) - selection = workspace.select_user - until !selection.nil? + recipient = workspace.select_user + until !recipient.nil? puts "Please provide a valid username or Slack ID" selected = gets.chomp workspace = Workspace.new(selected: selected) - selection = workspace.select_user + recipient = workspace.select_user end - puts "You have selected #{selection.real_name}" + puts "You have selected #{recipient.real_name}" puts "\nWhat would you like to do next?" option = display_options + verify_options(option) when "select channel" puts "You chose to select a channel. Please provide a channel name or Slack ID" selected = gets.chomp() workspace = Workspace.new(selected: selected) - selection = workspace.select_channel - until !selection.nil? + recipient = workspace.select_channel + until !recipient.nil? puts "Please provide a valid Channel name or Slack ID" selected = gets.chomp workspace = Workspace.new(selected: selected) - selection = workspace.select_channel + recipient = workspace.select_channel end - puts "You have selected #{selection.name}" + puts "You have selected #{recipient.name}" option = display_options + verify_options(option) when "details" - selection.details - puts "\nWhat would you like to do next?" - option = display_options + begin + recipient.details + puts "\nWhat would you like to do next?" + option = display_options + verify_options(option) + rescue + puts "You must select a user or channel first." + puts "\nWhat would you like to do next?" + option = display_options + verify_options(option) + end + when "message" + begin + recipient.slack_id #checking if recipient exists; if it doesn't will throw name error => rescue clause + puts "What message would you like to send?" + message = gets.chomp + workspace.send_message(message, recipient) + puts "\nYou're message has been sent." + puts "\nWhat would you like to do next?" + option = display_options + verify_options(option) + rescue + puts "You must select a user or channel first." + + puts "\nWhat would you like to do next?" + option = display_options + verify_options(option) + end end end - #if option = #, select_user - #username = gets.chomp - #workspace = Workspace.new(selected: username) - #if select show_details, workspace.show_details + #if repeat details puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/workspace.rb b/lib/workspace.rb index 72539124..9c9e6d98 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -29,8 +29,17 @@ def select_channel return channel_selected end - def show_details - user = select_user - user.details #need to change details so that this produces a hash and can be pretty printed out here + def send_message(message, recipient) + params = {} + params[:text] = message + params[:channel] = recipient.slack_id + p recipient.slack_id + # params[:as_user] = true + recipient.send_message(params) end + + # def show_details + # user = select_user + # user.details #need to change details so that this produces a hash and can be pretty printed out here + # end end From d9442ea941e612fd3f5f01186e1413a89bd5beff Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 19:10:32 -0700 Subject: [PATCH 30/33] added tests for send_message --- lib/recipient.rb | 4 ++++ specs/workspace_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/recipient.rb b/lib/recipient.rb index 7960a66c..a55d847f 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -17,6 +17,10 @@ def send_message(params) url = BASE_URL + endpoint params[:token] = ENV["SLACK_API_TOKEN"] response = HTTParty.post(url, body: params) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, response["error"] + end + return response end private diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index f6b2ce6a..e3136490 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -57,4 +57,29 @@ expect(@workspace_channel.select_channel).must_be_nil end end + + describe "#send_message" do + it "connects to the api ok" do + VCR.use_cassette("") do + selected = "CH0ED08E4" #id for general channel + @workspace = Workspace.new(selected: selected) + channel_selected = @workspace.select_channel + message = "hello" + response = @workspace.send_message(message, channel_selected) + expect(response.code).must_equal 200 + expect(response.parsed_response["ok"]).must_equal true + end + end + + it "returns an error if the message is empty" do + VCR.use_cassette("") do + selected = "CH0ED08E4" #id for general channel + @workspace = Workspace.new(selected: selected) + channel_selected = @workspace.select_channel + message = "" + exception = expect { @workspace.send_message(message, channel_selected) }.must_raise SlackApiError + expect(exception.message).must_equal "no_text" + end + end + end end From f5a5011ca241f951851ee6b6fc3edacd1cbca845 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 22:11:06 -0700 Subject: [PATCH 31/33] formatting --- lib/slack.rb | 12 ++---------- lib/user.rb | 1 - lib/workspace.rb | 6 +----- specs/workspace_spec.rb | 2 +- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 0ae4c2a3..505a22bb 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -14,7 +14,7 @@ def display_options puts "\nMessage" puts "\nQuit" option = gets.chomp.downcase - return option + return verify_options(option) end def verify_options(option) @@ -23,6 +23,7 @@ def verify_options(option) puts "Please input a valid option." option = display_options end + return option end def main @@ -30,8 +31,6 @@ def main puts "\nWhat would you like to do?" option = display_options - verify_options(option) - until option == "quit" case option when "select user" @@ -48,7 +47,6 @@ def main puts "You have selected #{recipient.real_name}" puts "\nWhat would you like to do next?" option = display_options - verify_options(option) when "select channel" puts "You chose to select a channel. Please provide a channel name or Slack ID" selected = gets.chomp() @@ -62,18 +60,15 @@ def main end puts "You have selected #{recipient.name}" option = display_options - verify_options(option) when "details" begin recipient.details puts "\nWhat would you like to do next?" option = display_options - verify_options(option) rescue puts "You must select a user or channel first." puts "\nWhat would you like to do next?" option = display_options - verify_options(option) end when "message" begin @@ -84,13 +79,10 @@ def main puts "\nYou're message has been sent." puts "\nWhat would you like to do next?" option = display_options - verify_options(option) rescue puts "You must select a user or channel first." - puts "\nWhat would you like to do next?" option = display_options - verify_options(option) end end end diff --git a/lib/user.rb b/lib/user.rb index b55a3c11..c9ae3521 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -27,7 +27,6 @@ def self.list #factory method end def details #business logic - # check that this works puts "Username: #{self.name}" puts "ID: #{self.slack_id}" puts "Name: #{self.real_name}" diff --git a/lib/workspace.rb b/lib/workspace.rb index 9c9e6d98..a6dc80ba 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,17 +9,14 @@ class Workspace def initialize(selected:) @users = User.list @channels = Channel.list - @selected = selected # either user or channel + @selected = selected # either user or channel info end - # methods that initialize selected (either select_user or select_method) def select_user user_selected = users.detect do |user| user.slack_id == selected || user.name == selected end return user_selected - #find @selected within @users info - end def select_channel @@ -33,7 +30,6 @@ def send_message(message, recipient) params = {} params[:text] = message params[:channel] = recipient.slack_id - p recipient.slack_id # params[:as_user] = true recipient.send_message(params) end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index e3136490..024f796f 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -26,7 +26,7 @@ it "returns nil if username or slack id is not found" do VCR.use_cassette("check nil returned") do - selected = "chewy" #id of slackbot + selected = "chewy" #id of nonexistent slackbot @workspace2 = Workspace.new(selected: selected) end expect(@workspace2.select_user).must_be_nil From d7b5afb55373ced21c46176d4e9878d8f1bbb3b3 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Thu, 21 Mar 2019 22:11:46 -0700 Subject: [PATCH 32/33] formatting --- lib/slack.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 505a22bb..e05cb924 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -87,8 +87,6 @@ def main end end - #if repeat details - puts "Thank you for using the Ada Slack CLI" end From 0a0decaf61721c63e633e55519ad5cb742d50a97 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Fri, 22 Mar 2019 15:31:55 -0700 Subject: [PATCH 33/33] added ability to change settings and select bot username --- bot-settings.json | 1 + lib/slack.rb | 19 +++++++++++++++++-- lib/workspace.rb | 26 +++++++++++++++++++++----- settings_slackapi.csv | 1 + 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 bot-settings.json create mode 100644 settings_slackapi.csv diff --git a/bot-settings.json b/bot-settings.json new file mode 100644 index 00000000..885447b1 --- /dev/null +++ b/bot-settings.json @@ -0,0 +1 @@ +{"username":"Maria Wissler"} \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index e05cb924..9906d9c1 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -12,13 +12,14 @@ def display_options puts "\nSelect channel" puts "\nDetails" puts "\nMessage" + puts "\nChange Settings" puts "\nQuit" option = gets.chomp.downcase return verify_options(option) end def verify_options(option) - options = ["select user", "select channel", "details", "message", "quit"] + options = ["select user", "select channel", "details", "message", "change settings", "quit"] until options.include?(option) puts "Please input a valid option." option = display_options @@ -62,7 +63,7 @@ def main option = display_options when "details" begin - recipient.details + workspace.show_details(recipient) puts "\nWhat would you like to do next?" option = display_options rescue @@ -84,6 +85,17 @@ def main puts "\nWhat would you like to do next?" option = display_options end + when "change settings" + puts "You can change the username displayed" + # puts "Please type either 'username' or 'icon emoji' or both to change either" + puts "What username would you like to use?" + setting_username_change = gets.chomp + params = {} + params[:username] = setting_username_change + Workspace.save_settings(params) + puts "Thanks, username is now #{setting_username_change}." + puts "Quit and restart the program for this change to be implemented" + option = display_options end end @@ -91,3 +103,6 @@ def main end main if __FILE__ == $PROGRAM_NAME + +def verify_icon_emojis +end diff --git a/lib/workspace.rb b/lib/workspace.rb index a6dc80ba..5b8b4ce8 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -2,6 +2,7 @@ require_relative "../lib/user" require_relative "../lib/channel" require_relative "../lib/recipient" +require "json" class Workspace attr_reader :users, :channels, :selected @@ -26,16 +27,31 @@ def select_channel return channel_selected end + def self.save_settings(params) + settings_file = File.open("bot-settings.json", "w") do |f| + f.write(params.to_json) + end + end + + def bot_settings_file_exist + begin + readfile = File.read("bot-settings.json") + return readfile + rescue + readfile = nil + end + end + def send_message(message, recipient) params = {} params[:text] = message params[:channel] = recipient.slack_id - # params[:as_user] = true + readfile = bot_settings_file_exist #check if settings have been changed + params.merge!(eval(readfile)) if !readfile.nil? recipient.send_message(params) end - # def show_details - # user = select_user - # user.details #need to change details so that this produces a hash and can be pretty printed out here - # end + def show_details(recipient) + recipient.details + end end diff --git a/settings_slackapi.csv b/settings_slackapi.csv new file mode 100644 index 00000000..6e5ef4b6 --- /dev/null +++ b/settings_slackapi.csv @@ -0,0 +1 @@ +"{:username=>""cyndi""}"