From c4dc49a63dc7b7b7772b99c017e738b8a0f7da1c Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 11:56:51 -0700 Subject: [PATCH 01/25] update test_helper for VCR --- .gitignore | 1 + specs/test_helper.rb | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 8d6a243f..f1fc2633 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ build/ build-iPhoneOS/ build-iPhoneSimulator/ + ## Specific to RubyMotion (use of CocoaPods): # # We recommend against adding the Pods directory to your .gitignore. However diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..e20560df 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 "dotenv" +Dotenv.load + +require_relative "" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock -end \ No newline at end of file + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match + } + # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end +end From 5152b712ea5bfb275e4554b28cfc2f06ef521885 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 13:52:08 -0700 Subject: [PATCH 02/25] Created lib and spac files. --- lib/{.keep => channel.rb} | 0 lib/recipient.rb | 28 ++++++++++++++++++++++++++++ lib/user.rb | 0 lib/workspace.rb | 0 specs/channel_spec.rb | 0 specs/recipient_spec.rb | 0 specs/user_spec.rb | 0 specs/workspace_spec.rb | 0 8 files changed, 28 insertions(+) rename lib/{.keep => channel.rb} (100%) create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 lib/workspace.rb create mode 100644 specs/channel_spec.rb create mode 100644 specs/recipient_spec.rb create mode 100644 specs/user_spec.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/.keep b/lib/channel.rb similarity index 100% rename from lib/.keep rename to lib/channel.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..4673f6b2 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,28 @@ +class Recipient + attr_reader :slack_id, :name + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end + + channel_url = 'https://slack.com/api/channels.list' + user_url = 'https://slack.com/api/users.list' + + params = { + token = ENV["SLACK_API_TOKEN"] + } + def self.get(url, params) + response = HTTParty.get(url, query:params) + end + + def send_message(message) + response = HTTParty.post(url, message?) + end + private + def details + raise NotImplementedError, "Implement this in the child class." + end + def self.list + raise NotImplementedError, "Implement this in the child class." + end +end \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..e69de29b From 9098d75b7dd6c7e6efbc3fc6621a23316cb54102 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 14:14:38 -0700 Subject: [PATCH 03/25] successful call to users api --- lib/recipient.rb | 38 ++++++++++++++++++++++++++------------ lib/user.rb | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 4673f6b2..252105e7 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,28 +1,42 @@ -class Recipient +require "dotenv" +require "httparty" +Dotenv.load + +class Recipient attr_reader :slack_id, :name + def initialize(slack_id, name) - @slack_id = slack_id - @name = name + @slack_id = slack_id + @name = name end - channel_url = 'https://slack.com/api/channels.list' - user_url = 'https://slack.com/api/users.list' + CHANNEL_URL = "https://slack.com/api/channels.list" + USER_URL = "https://slack.com/api/users.list" + + def self.get(type) + params = { + "token" => ENV["SLACK_API_TOKEN"], + } + if type == "user" + url = USER_URL + elsif type == "channel" + url = CHANNEL_URL + end - params = { - token = ENV["SLACK_API_TOKEN"] - } - def self.get(url, params) - response = HTTParty.get(url, query:params) + response = HTTParty.get(url, query: params) end def send_message(message) response = HTTParty.post(url, message?) end + private - def details + + def details raise NotImplementedError, "Implement this in the child class." end + def self.list raise NotImplementedError, "Implement this in the child class." end -end \ No newline at end of file +end diff --git a/lib/user.rb b/lib/user.rb index e69de29b..d29c96d6 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -0,0 +1,25 @@ +require "pry" + +require_relative "recipient" + +class User < Recipient + attr_reader :real_name, :status_text, :status_emoji + + def initialize(slack_id, name, real_name, status_text, status_emoji) + super(slack_id, name) + @real_name = real_name + @status_text = status_text + @status_emoji = status_emoji + end + + def details + end + + def self.list + raw_data = self.get("user") + puts raw_data + end +end + +binding.pry +self.list From 6f06b65dae464f3cfd2cd381da80037f84fcc79b Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 14:24:35 -0700 Subject: [PATCH 04/25] Added channel class and self.list method --- lib/channel.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/channel.rb b/lib/channel.rb index e69de29b..b6e6f789 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -0,0 +1,25 @@ +require "pry" + +require_relative "recipient" + +class Channel < Recipient + attr_reader :topic, :member_count + + def initialize(slack_id, name, topic, member_count) + super(slack_id, name) + @topic = topic + @member_count = member_count + end + + def details + + end + + def self.list + raw_data = self.get("channel") + puts raw_data + end +end + +binding.pry +self.list \ No newline at end of file From a9ca644086c7f002db0d74682c3fb3ea2218e7df Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 14:35:16 -0700 Subject: [PATCH 05/25] create list of users --- lib/user.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/user.rb b/lib/user.rb index d29c96d6..b8d7a261 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -18,6 +18,19 @@ def details def self.list raw_data = self.get("user") puts raw_data + user_list = [] + members = raw_data["members"] + members.each do |member| + slack_id = member["id"] + name = member["name"] + real_name = member["real_name"] + status_text = member["status_text"] + status_emoji = member["status_emoji"] + + user = User.new(slack_id, name, real_name, status_text, status_emoji) + user_list << user + end + return user_list end end From ca2213cbd94fe37010d1c624e465e5d03a109f48 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 14:45:57 -0700 Subject: [PATCH 06/25] Added self.list method for channel.rb --- lib/channel.rb | 14 +++++++++++++- lib/recipient.rb | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/channel.rb b/lib/channel.rb index b6e6f789..df71f21c 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -11,13 +11,25 @@ def initialize(slack_id, name, topic, member_count) @member_count = member_count end - def details + def details end def self.list raw_data = self.get("channel") puts raw_data + channel_list = [] + channels = raw_data["channels"] + channels.each do |channel| + slack_id = channel["id"] + name = channel["name"] + topic = channel["topic"]["value"] + member_count = channel["members"].count + + new_channel = Channel.new(slack_id, name, topic, member_count) + channel_list << new_channel + end + return channel_list end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 252105e7..8299860c 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -3,6 +3,7 @@ Dotenv.load class Recipient + class SlackApiError < Exception; end attr_reader :slack_id, :name def initialize(slack_id, name) From ab56d8a8a8af48df1150f5664814d7cf7c96b454 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 15:04:03 -0700 Subject: [PATCH 07/25] Added tests to channel_spec.rb --- lib/channel.rb | 4 ++-- lib/user.rb | 4 ++-- specs/channel_spec.rb | 14 ++++++++++++++ specs/test_helper.rb | 5 ++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index df71f21c..9ae944f6 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -33,5 +33,5 @@ def self.list end end -binding.pry -self.list \ No newline at end of file +# binding.pry +# self.list \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index b8d7a261..9c77b8f1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -34,5 +34,5 @@ def self.list end end -binding.pry -self.list +# binding.pry +# self.list diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e69de29b..e6281593 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -0,0 +1,14 @@ +require_relative "test_helper" + +describe "Channel class" do + describe "self.list" do + it "can return all channels" do + VCR.use_cassette("slack_channel") do + response = Channel.get("channel") + + expect(response["channels"]).wont_be_nil + expect(response["channels"].first["name"]).must_equal "random" + end + end + end +end \ No newline at end of file diff --git a/specs/test_helper.rb b/specs/test_helper.rb index e20560df..60844ebf 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,7 +10,10 @@ require "dotenv" Dotenv.load -require_relative "" +require_relative '../lib/channel.rb' +require_relative '../lib/user.rb' +require_relative '../lib/recipient.rb' +require_relative '../lib/workspace.rb' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From c8ae6e9abcabe9cd3ba4df33420386ffa770c5ac Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 15:10:33 -0700 Subject: [PATCH 08/25] added user_spec --- specs/channel_spec.rb | 5 +++-- specs/user_spec.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb index e6281593..b5c9c1b0 100644 --- a/specs/channel_spec.rb +++ b/specs/channel_spec.rb @@ -8,7 +8,8 @@ expect(response["channels"]).wont_be_nil expect(response["channels"].first["name"]).must_equal "random" + expect(response["channels"].first["members"].count).must_equal 2 end - end + end end -end \ No newline at end of file +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e69de29b..0564563a 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -0,0 +1,15 @@ +require_relative "test_helper" + +describe "User class" do + describe "self.list" do + it "can return all users" do + VCR.use_cassette("slack_user") do + response = Channel.get("user") + + expect(response["members"]).wont_be_nil + expect(response["members"].first["name"]).must_equal "slackbot" + expect(response["members"].first["id"]).must_equal "USLACKBOT" + end + end + end +end From 2cf0c050238fa6274379681b7ac0bebf06a13199 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 15:25:10 -0700 Subject: [PATCH 09/25] handled !200 errors --- lib/channel.rb | 12 +++++++----- lib/recipient.rb | 1 + lib/user.rb | 5 +++++ specs/user_spec.rb | 9 ++++++++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 9ae944f6..c4c54631 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -3,7 +3,7 @@ require_relative "recipient" class Channel < Recipient - attr_reader :topic, :member_count + attr_reader :topic, :member_count def initialize(slack_id, name, topic, member_count) super(slack_id, name) @@ -12,12 +12,14 @@ def initialize(slack_id, name, topic, member_count) end def details - end - def self.list + def self.list raw_data = self.get("channel") - puts raw_data + + unless raw_data.code == 200 + raise SlackApiError, "Improper request: #{raw_data.message}" + end channel_list = [] channels = raw_data["channels"] channels.each do |channel| @@ -34,4 +36,4 @@ def self.list end # binding.pry -# self.list \ No newline at end of file +# self.list diff --git a/lib/recipient.rb b/lib/recipient.rb index 8299860c..42a9d4d6 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -4,6 +4,7 @@ class Recipient class SlackApiError < Exception; end + attr_reader :slack_id, :name def initialize(slack_id, name) diff --git a/lib/user.rb b/lib/user.rb index 9c77b8f1..40ceea21 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -17,6 +17,11 @@ def details def self.list raw_data = self.get("user") + + unless raw_data.code == 200 + raise SlackApiError, "Improper request: #{raw_data.message}" + end + puts raw_data user_list = [] members = raw_data["members"] diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 0564563a..4f455494 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,12 +4,19 @@ describe "self.list" do it "can return all users" do VCR.use_cassette("slack_user") do - response = Channel.get("user") + response = User.get("user") expect(response["members"]).wont_be_nil expect(response["members"].first["name"]).must_equal "slackbot" expect(response["members"].first["id"]).must_equal "USLACKBOT" end end + + # it "raises an exception if response is invalid" do + # VCR.use_cassette("slack_user") do + # response = User.get("user") + # expect(response["ok"]).must_equal false + # end + # end end end From 8baa4848665407f1d66c445793659e70b2f1b9e8 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 15:42:59 -0700 Subject: [PATCH 10/25] Updated slack.rb to get user input --- lib/slack.rb | 19 +++++++++++++++++-- lib/user.rb | 1 - lib/workspace.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..aa7f7c7a 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,9 +1,24 @@ #!/usr/bin/env ruby +require_relative 'workspace' def main puts "Welcome to the Ada Slack CLI!" - - # TODO project + puts "Choose an option: \n list users \n list channels \n quit" + workspace = Workspace.new + input = "" + while input != "quit" + input = gets.chomp + case input + when "list users" + puts workspace.users + when "list channels" + puts workspace.channels + when "quit" + + else + puts "Please select one of the options" + end + end puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index 40ceea21..fd486c4f 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -22,7 +22,6 @@ def self.list raise SlackApiError, "Improper request: #{raw_data.message}" end - puts raw_data user_list = [] members = raw_data["members"] members.each do |member| diff --git a/lib/workspace.rb b/lib/workspace.rb index e69de29b..c0ece87b 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -0,0 +1,29 @@ +require "pry" + +require_relative "user" +require_relative "channel" + +class Workspace + attr_reader :users, :channels + def initialize + @users = User.list + @channels = Channel.list + # @selected = selected + end + + def select_channel + + end + + def select_user + + end + + def show_details + + end + + def send_message + + end +end \ No newline at end of file From a431470979936cf5363d46859c57f433f4d04a05 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 15:51:05 -0700 Subject: [PATCH 11/25] list_user_details --- lib/slack.rb | 14 +++++++------- lib/user.rb | 1 + lib/workspace.rb | 15 +++++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index aa7f7c7a..627bae3c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,21 +1,21 @@ #!/usr/bin/env ruby -require_relative 'workspace' +require_relative "workspace" def main puts "Welcome to the Ada Slack CLI!" - puts "Choose an option: \n list users \n list channels \n quit" + workspace = Workspace.new input = "" while input != "quit" + puts "Choose an option: \n list users \n list channels \n quit" input = gets.chomp - case input + case input when "list users" - puts workspace.users + workspace.print_user_details when "list channels" puts workspace.channels when "quit" - - else + else puts "Please select one of the options" end end @@ -23,4 +23,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 diff --git a/lib/user.rb b/lib/user.rb index fd486c4f..a06c835a 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -13,6 +13,7 @@ def initialize(slack_id, name, real_name, status_text, status_emoji) end def details + return "#{name} (#{real_name}) slack id: #{slack_id}" end def self.list diff --git a/lib/workspace.rb b/lib/workspace.rb index c0ece87b..a3171f6a 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -5,25 +5,28 @@ class Workspace attr_reader :users, :channels - def initialize + + def initialize @users = User.list @channels = Channel.list # @selected = selected end def select_channel - end def select_user - end - def show_details + def show_details + end + def print_user_details + users.each do |user| + puts user.details + end end def send_message - end -end \ No newline at end of file +end From 6d54995d30176337d33546c2373678a1b034a7db Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 19 Mar 2019 16:04:08 -0700 Subject: [PATCH 12/25] added channels print_details method --- lib/channel.rb | 1 + lib/slack.rb | 4 ++-- lib/workspace.rb | 12 +++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index c4c54631..2e223f56 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -12,6 +12,7 @@ def initialize(slack_id, name, topic, member_count) end def details + return "#{name} #{topic} member count: #{member_count} slack id: #{slack_id}" end def self.list diff --git a/lib/slack.rb b/lib/slack.rb index 627bae3c..92aa29ed 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,9 +11,9 @@ def main input = gets.chomp case input when "list users" - workspace.print_user_details + workspace.print_details("users") when "list channels" - puts workspace.channels + workspace.print_details("channels") when "quit" else puts "Please select one of the options" diff --git a/lib/workspace.rb b/lib/workspace.rb index a3171f6a..92979450 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -21,9 +21,15 @@ def select_user def show_details end - def print_user_details - users.each do |user| - puts user.details + def print_details(recipients) + if recipients == "users" + users.each do |user| + puts user.details + end + elsif recipients == "channels" + channels.each do |channel| + puts channel.details + end end end From f8fc3181068f0fad582b7ab3419c296b23059fd0 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Tue, 19 Mar 2019 16:29:31 -0700 Subject: [PATCH 13/25] added workspace init tests --- specs/workspace_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index e69de29b..db349128 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -0,0 +1,14 @@ +require_relative "test_helper" + +describe "Workspace class" do + describe "initialize" do + it "creates a list of channels and users" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + expect(workspace.channels.first).must_be_kind_of Channel + + expect(workspace.users.first).must_be_kind_of User + end + end + end +end From 9f3bb6da1a215d885c5d4b1a075fb7ad44460a84 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Wed, 20 Mar 2019 13:58:57 -0700 Subject: [PATCH 14/25] select_user method --- lib/recipient.rb | 2 +- lib/slack.rb | 4 ++-- lib/workspace.rb | 10 +++++++--- specs/recipient_spec.rb | 1 + specs/workspace_spec.rb | 20 ++++++++++++++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 42a9d4d6..7d365a05 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -3,7 +3,7 @@ Dotenv.load class Recipient - class SlackApiError < Exception; end + class SlackApiError < StandardError; end attr_reader :slack_id, :name diff --git a/lib/slack.rb b/lib/slack.rb index 92aa29ed..2741c987 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,9 +11,9 @@ def main input = gets.chomp case input when "list users" - workspace.print_details("users") + puts workspace.print_details("users") when "list channels" - workspace.print_details("channels") + puts workspace.print_details("channels") when "quit" else puts "Please select one of the options" diff --git a/lib/workspace.rb b/lib/workspace.rb index 92979450..42b8044d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -15,7 +15,11 @@ def initialize def select_channel end - def select_user + def select_user(user_input) + selected_user = users.select do |user| + user.name == user_input || user.slack_id == user_input + end + return selected_user.first end def show_details @@ -24,11 +28,11 @@ def show_details def print_details(recipients) if recipients == "users" users.each do |user| - puts user.details + return user.details end elsif recipients == "channels" channels.each do |channel| - puts channel.details + return channel.details end end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e69de29b..0ad635fe 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -0,0 +1 @@ +require "test_helper" diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index db349128..cea735f5 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -11,4 +11,24 @@ end end end + + describe "instance methods" do + it "returns text from print_details" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + expect(workspace.print_details("users")).must_be_kind_of String + + expect(workspace.print_details("channels")).must_be_kind_of String + end + end + + it "returns a user object from select_user" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + expect(workspace.select_user("slackbot")).must_be_kind_of User + + expect(workspace.select_user("USLACKBOT")).must_be_kind_of User + end + end + end end From eac545eb249740d9a6e23cf7d48de79478446c29 Mon Sep 17 00:00:00 2001 From: Nara Date: Wed, 20 Mar 2019 14:09:54 -0700 Subject: [PATCH 15/25] Added select_channel method and test --- lib/workspace.rb | 12 ++++++++---- specs/workspace_spec.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 42b8044d..04ead75d 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -9,17 +9,21 @@ class Workspace def initialize @users = User.list @channels = Channel.list - # @selected = selected + @selected = nil end - def select_channel + def select_channel(user_input) + selected = channels.select do |channel| + channel.name == user_input || channel.slack_id == user_input + end + return selected.first end def select_user(user_input) - selected_user = users.select do |user| + selected = users.select do |user| user.name == user_input || user.slack_id == user_input end - return selected_user.first + return selected.first end def show_details diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index cea735f5..c51898df 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -30,5 +30,14 @@ expect(workspace.select_user("USLACKBOT")).must_be_kind_of User end end + + it "returns a channel object from select_channel" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + expect(workspace.select_channel("random")).must_be_kind_of Channel + + expect(workspace.select_channel("CH2RY8RQT")).must_be_kind_of Channel + end + end end end From fd0bb90f0bc4a75fe62afc58bdb516e0991894a7 Mon Sep 17 00:00:00 2001 From: Nara Date: Wed, 20 Mar 2019 14:33:04 -0700 Subject: [PATCH 16/25] Added show details, select user and channel methods and tests. --- lib/workspace.rb | 7 ++++--- specs/workspace_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 04ead75d..b6adb728 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -4,7 +4,7 @@ require_relative "channel" class Workspace - attr_reader :users, :channels + attr_reader :users, :channels, :selected def initialize @users = User.list @@ -16,17 +16,18 @@ def select_channel(user_input) selected = channels.select do |channel| channel.name == user_input || channel.slack_id == user_input end - return selected.first + @selected = selected.first end def select_user(user_input) selected = users.select do |user| user.name == user_input || user.slack_id == user_input end - return selected.first + @selected = selected.first end def show_details + return @selected.details end def print_details(recipients) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index c51898df..8925abc3 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -39,5 +39,25 @@ expect(workspace.select_channel("CH2RY8RQT")).must_be_kind_of Channel end end + + it "returns channel details from show_details" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + workspace.select_channel("random") + expect(workspace.show_details).must_be_kind_of String + workspace.select_channel("CH2RY8RQT") + expect(workspace.show_details).must_be_kind_of String + end + end + + it "returns user details from show_details" do + VCR.use_cassette("slack_workspace") do + workspace = Workspace.new + workspace.select_user("slackbot") + expect(workspace.show_details).must_be_kind_of String + workspace.select_user("USLACKBOT") + expect(workspace.show_details).must_be_kind_of String + end + end end end From 9f1b726f7fcb24d0bf71fedfaadbc4dccb16aa97 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Wed, 20 Mar 2019 14:46:10 -0700 Subject: [PATCH 17/25] updated slack.rb to include new menu items --- lib/slack.rb | 16 +++++++++++++++- lib/workspace.rb | 7 +++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 2741c987..b7201b41 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,13 +7,27 @@ def main workspace = Workspace.new input = "" while input != "quit" - puts "Choose an option: \n list users \n list channels \n quit" + puts "Choose an option: \n list users \n list channels \n \n select user \n select channel \n details \n quit" input = gets.chomp case input when "list users" puts workspace.print_details("users") when "list channels" puts workspace.print_details("channels") + when "select user" + print "Enter the user name or Slack ID: " + input_user = gets.chomp + workspace.select_user(input_user) + when "select channel" + print "Enter the channel name or Slack ID: " + input_channel = gets.chomp + workspace.select_channel(input_channel) + when "details" + if workspace.selected == nil + puts "Please select a user or channel as a recipient" + else + puts workspace.show_details + end when "quit" else puts "Please select one of the options" diff --git a/lib/workspace.rb b/lib/workspace.rb index b6adb728..844af658 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -32,14 +32,17 @@ def show_details def print_details(recipients) if recipients == "users" + return_array = [] users.each do |user| - return user.details + return_array << user.details end elsif recipients == "channels" + return_array = [] channels.each do |channel| - return channel.details + return_array << channel.details end end + return return_array end def send_message From 1fc9558bddb2719d4995104428b0d72ac3111a5f Mon Sep 17 00:00:00 2001 From: Nara Date: Wed, 20 Mar 2019 15:40:57 -0700 Subject: [PATCH 18/25] Added send message to recipient and worskpace. --- lib/recipient.rb | 18 +++++++++++++++--- lib/workspace.rb | 4 +++- specs/workspace_spec.rb | 15 +++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 7d365a05..18d87a10 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -14,7 +14,7 @@ def initialize(slack_id, name) CHANNEL_URL = "https://slack.com/api/channels.list" USER_URL = "https://slack.com/api/users.list" - + POST_URL = "https://slack.com/api/chat.postMessage" def self.get(type) params = { "token" => ENV["SLACK_API_TOKEN"], @@ -28,8 +28,20 @@ def self.get(type) response = HTTParty.get(url, query: params) end - def send_message(message) - response = HTTParty.post(url, message?) + def send_msg(message,recipient) + params = { + "token" => ENV["SLACK_API_TOKEN"], + "channel" => recipient, + "text" => message, + "as_user" => true + } + + response = HTTParty.post( + POST_URL, + body: params, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } + ) + return response end private diff --git a/lib/workspace.rb b/lib/workspace.rb index 844af658..87003823 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -45,6 +45,8 @@ def print_details(recipients) return return_array end - def send_message + def send_message(text) + selected.send_msg(text, selected.slack_id) end end +binding.pry \ No newline at end of file diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8925abc3..b2ed2570 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -16,9 +16,9 @@ it "returns text from print_details" do VCR.use_cassette("slack_workspace") do workspace = Workspace.new - expect(workspace.print_details("users")).must_be_kind_of String + expect(workspace.print_details("users")).must_be_kind_of Array - expect(workspace.print_details("channels")).must_be_kind_of String + expect(workspace.print_details("channels")).must_be_kind_of Array end end @@ -60,4 +60,15 @@ end end end + + describe "post message to slack" do + it "creates sends a message to a recipient" do + VCR.use_cassette("slack-posts") do + workspace = Workspace.new + workspace.select_channel("random") + response = workspace.send_message("This post should work") + expect(response["ok"]).must_equal true + end + end + end end From 7136feca457db92e70893c9293e700f3a0c4e41d Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Thu, 21 Mar 2019 10:18:21 -0700 Subject: [PATCH 19/25] updated CLI and fixed print_details in workspace --- lib/recipient.rb | 11 +++++++---- lib/slack.rb | 10 +++++++++- lib/workspace.rb | 3 ++- specs/workspace_spec.rb | 11 ++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 18d87a10..f06da4eb 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -5,7 +5,7 @@ class Recipient class SlackApiError < StandardError; end - attr_reader :slack_id, :name + attr_accessor :slack_id, :name def initialize(slack_id, name) @slack_id = slack_id @@ -28,19 +28,22 @@ def self.get(type) response = HTTParty.get(url, query: params) end - def send_msg(message,recipient) + def send_msg(message, recipient) params = { "token" => ENV["SLACK_API_TOKEN"], "channel" => recipient, "text" => message, - "as_user" => true + "as_user" => true, } response = HTTParty.post( POST_URL, body: params, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } + headers: { "Content-Type" => "application/x-www-form-urlencoded" }, ) + unless response.code == 200 && response.parsed_response["ok"] + raise SlackApiError, "Error: #{response.parsed_response["error"]}" + end return response end diff --git a/lib/slack.rb b/lib/slack.rb index b7201b41..ff01290c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,7 +7,7 @@ def main workspace = Workspace.new input = "" while input != "quit" - puts "Choose an option: \n list users \n list channels \n \n select user \n select channel \n details \n quit" + puts "Choose an option: \n list users \n list channels \n \n select user \n select channel \n details \n \n send message \n quit" input = gets.chomp case input when "list users" @@ -28,6 +28,14 @@ def main else puts workspace.show_details end + when "send message" + if workspace.selected == nil + puts "Please select a user or channel as a recipient" + else + print "Enter your message: " + text = gets.chomp + workspace.send_message(text) + end when "quit" else puts "Please select one of the options" diff --git a/lib/workspace.rb b/lib/workspace.rb index 87003823..7acf3cce 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -49,4 +49,5 @@ def send_message(text) selected.send_msg(text, selected.slack_id) end end -binding.pry \ No newline at end of file + +# binding.pry diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b2ed2570..8ddbc23b 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -61,7 +61,7 @@ end end - describe "post message to slack" do + describe "post message to slack" do it "creates sends a message to a recipient" do VCR.use_cassette("slack-posts") do workspace = Workspace.new @@ -70,5 +70,14 @@ expect(response["ok"]).must_equal true end end + + it "raises an error for invalid channel" do + VCR.use_cassette("slack-posts") do + workspace = Workspace.new + workspace.select_channel("random") + workspace.selected.slack_id = "garbage" + expect { workspace.send_message("This post should not work") }.must_raise Recipient::SlackApiError + end + end end end From d076c811e44ba44b3ddacf46f74643e1a14c9726 Mon Sep 17 00:00:00 2001 From: Nara Date: Thu, 21 Mar 2019 10:27:07 -0700 Subject: [PATCH 20/25] Added a test to handle empty messages. --- specs/workspace_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8ddbc23b..faccb60a 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -79,5 +79,23 @@ expect { workspace.send_message("This post should not work") }.must_raise Recipient::SlackApiError end end + + it "raises an error for invalid user" do + VCR.use_cassette("slack-posts") do + workspace = Workspace.new + workspace.select_user("slackbot") + workspace.selected.slack_id = "garbage" + expect { workspace.send_message("This post should not work") }.must_raise Recipient::SlackApiError + end + end + + it "raises an error for invalid user" do + VCR.use_cassette("slack-posts") do + workspace = Workspace.new + workspace.select_user("slackbot") + workspace.selected.slack_id = "garbage" + expect { workspace.send_message("") }.must_raise Recipient::SlackApiError + end + end end end From c687709fecb266b415a307de714674d9bcb5df4d Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Thu, 21 Mar 2019 10:29:30 -0700 Subject: [PATCH 21/25] Added error message if invalid user or channel selected --- lib/slack.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/slack.rb b/lib/slack.rb index ff01290c..ff78aa19 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -18,10 +18,16 @@ def main print "Enter the user name or Slack ID: " input_user = gets.chomp workspace.select_user(input_user) + if workspace.selected == nil + puts "User not found" + end when "select channel" print "Enter the channel name or Slack ID: " input_channel = gets.chomp workspace.select_channel(input_channel) + if workspace.selected == nil + puts "Channel not found" + end when "details" if workspace.selected == nil puts "Please select a user or channel as a recipient" From abb531d6e3d58433e87456fbb9383b00a23bde26 Mon Sep 17 00:00:00 2001 From: Nara Date: Fri, 22 Mar 2019 10:39:24 -0700 Subject: [PATCH 22/25] Corrected a typo in the test --- specs/workspace_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index faccb60a..0451e99a 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -89,7 +89,7 @@ end end - it "raises an error for invalid user" do + it "raises an error for invalid message" do VCR.use_cassette("slack-posts") do workspace = Workspace.new workspace.select_user("slackbot") From bfc548655664eb695527f85fd130c4548bea8317 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Fri, 22 Mar 2019 11:34:51 -0700 Subject: [PATCH 23/25] started bot-settings code --- lib/bot-settings.json | 0 lib/slack.rb | 8 +++++++- lib/workspace.rb | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 lib/bot-settings.json diff --git a/lib/bot-settings.json b/lib/bot-settings.json new file mode 100644 index 00000000..e69de29b diff --git a/lib/slack.rb b/lib/slack.rb index ff78aa19..813348cf 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -7,7 +7,7 @@ def main workspace = Workspace.new input = "" while input != "quit" - puts "Choose an option: \n list users \n list channels \n \n select user \n select channel \n details \n \n send message \n quit" + puts "Choose an option: \n list users \n list channels \n \n select user \n select channel \n details \n \n send message \n \n change settings \n quit" input = gets.chomp case input when "list users" @@ -42,6 +42,12 @@ def main text = gets.chomp workspace.send_message(text) end + when "change settings" + print "Enter new username: " + name = gets.chomp + print "Enter new emoji: " + emoji = gets.chomp + workspace.update_settings(name, emoji) when "quit" else puts "Please select one of the options" diff --git a/lib/workspace.rb b/lib/workspace.rb index 7acf3cce..7462b655 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,4 +1,5 @@ require "pry" +require "json" require_relative "user" require_relative "channel" @@ -45,6 +46,11 @@ def print_details(recipients) return return_array end + def update_settings(name, emoji) + json_hash = { "username" => name, + "icon_emoji" => emoji } + end + def send_message(text) selected.send_msg(text, selected.slack_id) end From c496d3a89f91d9c58ee51e671206867ff4d17187 Mon Sep 17 00:00:00 2001 From: Kirsten Anderson Date: Fri, 22 Mar 2019 11:56:06 -0700 Subject: [PATCH 24/25] set new bot-settings and write to json file --- lib/bot-settings.json | 1 + lib/recipient.rb | 11 ++++++++++- lib/workspace.rb | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/bot-settings.json b/lib/bot-settings.json index e69de29b..7eed9135 100644 --- a/lib/bot-settings.json +++ b/lib/bot-settings.json @@ -0,0 +1 @@ +{"username":"Turtle","icon_emoji":":turtle:"} \ No newline at end of file diff --git a/lib/recipient.rb b/lib/recipient.rb index f06da4eb..bf4fc05d 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,5 +1,6 @@ require "dotenv" require "httparty" +require "json" Dotenv.load class Recipient @@ -29,13 +30,21 @@ def self.get(type) end def send_msg(message, recipient) + # settings = {} + file = File.read("lib/bot-settings.json") + settings = JSON.parse(file) + params = { "token" => ENV["SLACK_API_TOKEN"], "channel" => recipient, "text" => message, - "as_user" => true, + "as_user" => false, + "username" => settings["username"], + "icon_emoji" => settings["icon_emoji"], } + puts params + response = HTTParty.post( POST_URL, body: params, diff --git a/lib/workspace.rb b/lib/workspace.rb index 7462b655..0711c317 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -49,6 +49,10 @@ def print_details(recipients) def update_settings(name, emoji) json_hash = { "username" => name, "icon_emoji" => emoji } + + File.open("lib/bot-settings.json", "w") do |f| + f.write(json_hash.to_json) + end end def send_message(text) From f1f47a5d2cdf7aad8abe2c84655e1a7a4a494826 Mon Sep 17 00:00:00 2001 From: Nara Date: Fri, 22 Mar 2019 12:06:42 -0700 Subject: [PATCH 25/25] added conditions to check if recipient is user when posting. --- lib/recipient.rb | 9 ++++----- lib/workspace.rb | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index bf4fc05d..2ffa1121 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -33,17 +33,16 @@ def send_msg(message, recipient) # settings = {} file = File.read("lib/bot-settings.json") settings = JSON.parse(file) - params = { "token" => ENV["SLACK_API_TOKEN"], - "channel" => recipient, + "channel" => recipient.slack_id, "text" => message, - "as_user" => false, "username" => settings["username"], "icon_emoji" => settings["icon_emoji"], } - - puts params + if recipient.class == User + params["as_user"] = true + end response = HTTParty.post( POST_URL, diff --git a/lib/workspace.rb b/lib/workspace.rb index 0711c317..4e9be038 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -56,7 +56,7 @@ def update_settings(name, emoji) end def send_message(text) - selected.send_msg(text, selected.slack_id) + selected.send_msg(text, selected) end end