From c1721fb1a188396d56ad18dbcbcb86701b5ccd9e Mon Sep 17 00:00:00 2001 From: Jessica Date: Mon, 18 Mar 2019 22:20:42 -0700 Subject: [PATCH 01/40] Verify API connection --- lib/slack.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..63fa9165 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,28 @@ #!/usr/bin/env ruby +require "dotenv" +require "httparty" + +Dotenv.load def main puts "Welcome to the Ada Slack CLI!" + url = "https://slack.com/api/channels.list" + + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(url, query: query_parameters) # TODO project + if response.code == 200 + puts response + else + puts "#{response.code} #{response.message}" + end + 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 3f6355f425d4e315ef2394ded259fc123d61f377 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 10:21:03 -0700 Subject: [PATCH 02/40] Moved code that tests for API connection from slack.rb to sample.rb --- lib/slack.rb | 16 ---------------- sample.rb | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 sample.rb diff --git a/lib/slack.rb b/lib/slack.rb index 63fa9165..904cf21d 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -2,26 +2,10 @@ require "dotenv" require "httparty" -Dotenv.load - def main puts "Welcome to the Ada Slack CLI!" - - url = "https://slack.com/api/channels.list" - - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - - response = HTTParty.get(url, query: query_parameters) # TODO project - if response.code == 200 - puts response - else - puts "#{response.code} #{response.message}" - end - puts "Thank you for using the Ada Slack CLI" end diff --git a/sample.rb b/sample.rb new file mode 100644 index 00000000..5f89b888 --- /dev/null +++ b/sample.rb @@ -0,0 +1,18 @@ +require "dotenv" +require "httparty" + +Dotenv.load + +url = "https://slack.com/api/channels.list" + +query_parameters = { + token: ENV["SLACK_API_TOKEN"], +} + +response = HTTParty.get(url, query: query_parameters) + +if response.code == 200 + puts response +else + puts "#{response.code} #{response.message}" +end From a2888cb14ae89e0f0061a91b272d1a61b1900fd3 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 10:22:44 -0700 Subject: [PATCH 03/40] Change single quotes to double quotes (autoformat) --- specs/test_helper.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..76a2955a 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,15 +1,15 @@ -require 'simplecov' +require "simplecov" SimpleCov.start -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require 'vcr' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require "vcr" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock -end \ No newline at end of file +end From 4d215ed979f375dfb9d9aa30ad5de07829a70f09 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 10:50:00 -0700 Subject: [PATCH 04/40] Change puts to awesome print --- sample.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sample.rb b/sample.rb index 5f89b888..899b3248 100644 --- a/sample.rb +++ b/sample.rb @@ -1,5 +1,6 @@ require "dotenv" require "httparty" +require "awesome_print" Dotenv.load @@ -12,7 +13,7 @@ response = HTTParty.get(url, query: query_parameters) if response.code == 200 - puts response + ap response.parsed_response else - puts "#{response.code} #{response.message}" + ap "#{response.code} #{response.message}" end From 2ea5cbb53dffd8041224a3a04e88a8426faaa9cf Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 11:28:05 -0700 Subject: [PATCH 05/40] Move sample.rb to lib folder --- sample.rb => lib/sample.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sample.rb => lib/sample.rb (100%) diff --git a/sample.rb b/lib/sample.rb similarity index 100% rename from sample.rb rename to lib/sample.rb From 7721094a0491600d44c44c63d13e736a038da7cd Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:42:48 -0700 Subject: [PATCH 06/40] Set up frame work and created self.list (unfinished) --- lib/channel.rb | 45 +++++++++++++++++++++++++++++++++++++++++++ specs/channel_spec.rb | 19 ++++++++++++++++++ 2 files changed, 64 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..6ec5e74b --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,45 @@ +require "dotenv" +require "httparty" +require "awesome_print" + +require_relative "recipient" + +module Slack + 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 + + #temp + def self.list + list = [] + Dotenv.load + + url = "https://slack.com/api/channels.list" + + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(url, query: query_parameters) + + if response.code == 200 + response.parsed_response["channels"].each do |channel| + slack_id = channel["id"] + name = channel["name"] + topic = channel["topic"]["value"] + member_count = channel["num_members"] + + list << Slack::Channel.new(slack_id, name, topic, member_count) + end + else + ap "#{response.code} #{response.message}" + end + return list + end + end +end diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..6eb16c8f --- /dev/null +++ b/specs/channel_spec.rb @@ -0,0 +1,19 @@ +require_relative "test_helper" + +describe "Channel" do + describe "Initialization" do + let(:slack_id) { 20 } + let(:channelname) { "everyone" } + let(:topic) { "Slack API" } + let(:member_count) { 2 } + + it "can be instantiated" do + channel = Slack::Channel.new(slack_id, channelname, topic, member_count) + expect(channel).must_be_instance_of Slack::Channel + expect(channel.slack_id).must_equal 20 + expect(channel.name).must_equal "everyone" + expect(channel.topic).must_equal "Slack API" + expect(channel.member_count).must_equal 2 + end + end +end From 2124ce84cce836b732ee90db11bcf634d89865f9 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:43:02 -0700 Subject: [PATCH 07/40] Set up frame work and created self.list (unfinished) --- lib/user.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ specs/user_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 lib/user.rb create mode 100644 specs/user_spec.rb diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..fa881ddf --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,43 @@ +require_relative "recipient" + +module Slack + 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 + + #temp + def self.list + list = [] + Dotenv.load + + url = "https://slack.com/api/users.list" + + query_parameters = { + token: ENV["SLACK_API_TOKEN"], + } + + response = HTTParty.get(url, query: query_parameters) + + if response.code == 200 + response.parsed_response["members"].each do |member| + slack_id = member["id"] + name = member["name"] + real_name = member["real_name"] + status_text = member["profile"]["status_text"] + status_emoji = member["status_emoji"] + + list << Slack::User.new(slack_id, name, real_name, status_text, status_emoji) + end + else + ap "#{response.code} #{response.message}" + end + return list + end + end +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e093601e --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,21 @@ +require_relative "test_helper" + +describe "User" do + describe "Initialization" do + let(:slack_id) { 20 } + let(:username) { "soph" } + let(:real_name) { "Sopheary" } + let(:status_text) { "Good to go" } + let(:status_emoji) { ":)" } + + it "can be instantiated" do + user = Slack::User.new(slack_id, username, real_name, status_text, status_emoji) + expect(user).must_be_instance_of Slack::User + expect(user.slack_id).must_equal 20 + expect(user.name).must_equal "soph" + expect(user.real_name).must_equal "Sopheary" + expect(user.status_text).must_equal "Good to go" + expect(user.status_emoji).must_equal ":)" + end + end +end From 741eb1a425cd2bb4c844c50100a47dbb14f84de7 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:43:29 -0700 Subject: [PATCH 08/40] Created framework and test for instantiation --- lib/recipient.rb | 26 ++++++++++++++++++++++++++ specs/recipient_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 lib/recipient.rb create mode 100644 specs/recipient_spec.rb diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..c67f42ed --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,26 @@ +module Slack + class Recipient + attr_reader :slack_id, :name + + def initialize(slack_id, name) + @slack_id = slack_id + @name = name + end + + def send_message(message) + # Implement here. + end + + def self.get(url, params) + # Implement here. + end + + def self.list + raise NotImplementedError, "Implement me in a child class!" + end + + def details + raise NotImplementedError, "Implement me in a child class!" + end + end +end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..f1566318 --- /dev/null +++ b/specs/recipient_spec.rb @@ -0,0 +1,26 @@ +require_relative "test_helper" + +describe "Recipient" do + let(:slack_id) { 20 } + let(:username) { "Sopheary" } + let(:recipient) { Slack::Recipient.new(slack_id, username) } + describe "Initialization" do + it "can be instantiated" do + expect(recipient).must_be_instance_of Slack::Recipient + expect(recipient.slack_id).must_equal 20 + expect(recipient.name).must_equal "Sopheary" + end + end + + describe "self.list" do + it "raises error if attempted" do + expect { Slack::Recipient.list }.must_raise NotImplementedError + end + end + + describe "details" do + it "raises error if attempted" do + expect { recipient.details }.must_raise NotImplementedError + end + end +end From 93f9d432e8c64e64bfc32e53bd2213a7c86ff633 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:43:43 -0700 Subject: [PATCH 09/40] Created framework and test for instantiation --- lib/workspace.rb | 23 +++++++++++++++++++++++ specs/workspace_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/workspace.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..12872c22 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,23 @@ +module Slack + class Workspace + attr_reader :users, :channels, :selected + + def initialize + @users = User.list + @channels = Channel.list + @selected = nil + end + + def select_channel + end + + def select_user + end + + def show_details + end + + def send_message + end + end +end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..8105896c --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1,20 @@ +require_relative "test_helper" + +describe "Workspace" do + let(:workspace) { Slack::Workspace.new } + describe "Initialization" do + it "can be instantiated" do + expect(workspace).must_be_instance_of Slack::Workspace + end + + it "establishes the base structures when instantiated" do + [:users, :channels, :selected].each do |prop| + expect(workspace).must_respond_to prop + end + + expect(workspace.users).must_be_kind_of Array + expect(workspace.channels).must_be_kind_of Array + expect(workspace.selected).must_equal nil + end + end +end From 85e1e425fda4b7b4feaf2cbe1234c382e99d0d1c Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:44:57 -0700 Subject: [PATCH 10/40] Add require_relatives --- lib/slack.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 904cf21d..315c8583 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby -require "dotenv" -require "httparty" +require_relative "workspace" +require_relative "user" +require_relative "channel" def main puts "Welcome to the Ada Slack CLI!" From 3bfc6004461e7f104060cdd5115a53fe0c87dacd Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 19 Mar 2019 15:45:29 -0700 Subject: [PATCH 11/40] Add require_relatives and started messing with VCR (not finished) --- specs/test_helper.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 76a2955a..1143553e 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -5,11 +5,25 @@ require "minitest/autorun" require "minitest/reporters" require "minitest/skip_dsl" -require "vcr" +# require "vcr" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -VCR.configure do |config| - config.cassette_library_dir = "specs/cassettes" - config.hook_into :webmock -end +require_relative "../lib/channel" +require_relative "../lib/user" +require_relative "../lib/recipient" +require_relative "../lib/workspace" +# require_relative "../lib/slack" # Do we need this? Don't think so. + +# VCR.configure do |config| +# config.cassette_library_dir = "specs/cassettes" # folder where casettes will be +# config.hook_into :webmock # tie into this other rool 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], +# } +# # Don't leave our token lying around in cassette file. +# config.filter_sensitive_data("") do +# ENV["SLACK_API_TOKEN"] +# end +# end From 974dea1bb2605097e55720c794aebc0ce50923c9 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Tue, 19 Mar 2019 16:31:11 -0700 Subject: [PATCH 12/40] started experimenting testing api --- lib/channel.rb | 1 - lib/user.rb | 1 - specs/test_helper.rb | 31 ++++++++++++++++++------------- specs/user_spec.rb | 10 ++++++++++ 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 6ec5e74b..8c1d2f8b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -17,7 +17,6 @@ def initialize(slack_id, name, topic, member_count) #temp def self.list list = [] - Dotenv.load url = "https://slack.com/api/channels.list" diff --git a/lib/user.rb b/lib/user.rb index fa881ddf..6c7e3645 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -14,7 +14,6 @@ def initialize(slack_id, name, real_name, status_text, status_emoji) #temp def self.list list = [] - Dotenv.load url = "https://slack.com/api/users.list" diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 1143553e..874ee13a 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -5,7 +5,12 @@ require "minitest/autorun" require "minitest/reporters" require "minitest/skip_dsl" -# require "vcr" +require "vcr" +require "webmock/minitest" +require "dotenv" +require "httparty" +require "awesome_print" +Dotenv.load Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -15,15 +20,15 @@ require_relative "../lib/workspace" # require_relative "../lib/slack" # Do we need this? Don't think so. -# VCR.configure do |config| -# config.cassette_library_dir = "specs/cassettes" # folder where casettes will be -# config.hook_into :webmock # tie into this other rool 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], -# } -# # Don't leave our token lying around in cassette file. -# config.filter_sensitive_data("") do -# ENV["SLACK_API_TOKEN"] -# end -# end +VCR.configure do |config| + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be + config.hook_into :webmock # tie into this other rool 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], + # } + # # Don't leave our token lying around in cassette file. + # config.filter_sensitive_data("") do + # ENV["SLACK_API_TOKEN"] + # end +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e093601e..b20b4c25 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -17,5 +17,15 @@ expect(user.status_text).must_equal "Good to go" expect(user.status_emoji).must_equal ":)" end + + # it "can find a location" do + # VCR.use_cassette("users_found") do + # response = get_location(location) + + # expect(response["Seattle"]).wont_be_nil + # expect(response["Seattle"][:lon]).must_equal "-122.3300624" + # expect(response["Seattle"][:lat]).must_equal "47.6038321" + # end + # end end end From 6b164899013a206ef1b66292ebd5bc0fb1221b95 Mon Sep 17 00:00:00 2001 From: Jessica Date: Wed, 20 Mar 2019 09:57:52 -0700 Subject: [PATCH 13/40] Set up VCR - all current tests passing --- .DS_Store | Bin 0 -> 6148 bytes lib/recipient.rb | 10 ++- lib/user.rb | 25 +++--- specs/channel_spec.rb | 14 +-- specs/test_helper.rb | 19 ++-- specs/user_spec.rb | 18 ++-- specs/workspace_spec.rb | 23 +++-- test/cassettes/users_found.yml | 78 +++++++++++++++++ test/cassettes/workspace.yml | 153 +++++++++++++++++++++++++++++++++ 9 files changed, 294 insertions(+), 46 deletions(-) create mode 100644 .DS_Store create mode 100644 test/cassettes/users_found.yml create mode 100644 test/cassettes/workspace.yml diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a2eb533abf551d042629249e6fe5fb378b3b5ee0 GIT binary patch literal 6148 zcmeH~u?oUK42Bc!Ah>jNyu}Cb4Gz&K=nFU~E>c0O^F6wMazU^xC+1b{XuyJ79K1T}(S!u1*@b}wNMJ-@TJzTK|1JE}{6A`8N&+PC zX9Tp_belC^D(=>|*R%RAs :new_episodes, # record new data when we don't have it yet - # :match_requests_on => [:method, :uri, :body], - # } - # # Don't leave our token lying around in cassette file. - # config.filter_sensitive_data("") do - # ENV["SLACK_API_TOKEN"] - # end + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body], + } + # Don't leave our token lying around in cassette file. + config.filter_sensitive_data("") do + ENV["SLACK_API_TOKEN"] + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b20b4c25..a347a8e6 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -18,14 +18,16 @@ expect(user.status_emoji).must_equal ":)" end - # it "can find a location" do - # VCR.use_cassette("users_found") do - # response = get_location(location) + describe "List" do + it "can list itself" do + VCR.use_cassette("users_found") do + Slack::User.list - # expect(response["Seattle"]).wont_be_nil - # expect(response["Seattle"][:lon]).must_equal "-122.3300624" - # expect(response["Seattle"][:lat]).must_equal "47.6038321" - # end - # end + # expect(list.first["Seattle"]).wont_be_nil + # expect(response["Seattle"][:lon]).must_equal "-122.3300624" + # expect(response["Seattle"][:lat]).must_equal "47.6038321" + end + end + end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8105896c..c43ab9aa 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,20 +1,27 @@ require_relative "test_helper" describe "Workspace" do - let(:workspace) { Slack::Workspace.new } + # let(:workspace) { Slack::Workspace.new } describe "Initialization" do it "can be instantiated" do - expect(workspace).must_be_instance_of Slack::Workspace + VCR.use_cassette("workspace") do + workspace = Slack::Workspace.new + expect(workspace).must_be_instance_of Slack::Workspace + end end it "establishes the base structures when instantiated" do - [:users, :channels, :selected].each do |prop| - expect(workspace).must_respond_to prop - end + VCR.use_cassette("workspace") do + workspace = Slack::Workspace.new + expect(workspace) + [:users, :channels, :selected].each do |prop| + expect(workspace).must_respond_to prop + end - expect(workspace.users).must_be_kind_of Array - expect(workspace.channels).must_be_kind_of Array - expect(workspace.selected).must_equal nil + expect(workspace.users).must_be_kind_of Array + expect(workspace.channels).must_be_kind_of Array + expect(workspace.selected).must_equal nil + end end end end diff --git a/test/cassettes/users_found.yml b/test/cassettes/users_found.yml new file mode 100644 index 00000000..028aa7a6 --- /dev/null +++ b/test/cassettes/users_found.yml @@ -0,0 +1,78 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '911' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 16:32:32 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 2f74e371-21c6-4cfc-87c9-c0e5925deaa6 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-3gq2 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 16a12520cb84572aced3b0a8e5f80bae.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - VYAtAF7j_k43949iMEYCQoUmPr4RUEyc3SdI2aO2FX_WHTaR3V6WTg== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TH2P32R19","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_192.png","image_512":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"UH0EG0QHE","team_id":"TH2P32R19","name":"sopheary.chiv","deleted":false,"color":"4bbe2e","real_name":"Sopheary + Chiv","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Sopheary + Chiv","real_name_normalized":"Sopheary Chiv","display_name":"Sopheary Chiv","display_name_normalized":"Sopheary + Chiv","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g3fc544bd693","image_24":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952644,"has_2fa":false},{"id":"UH2RGJ36Y","team_id":"TH2P32R19","name":"jessica.homet","deleted":false,"color":"9f69e7","real_name":"Jessica + Homet","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Jessica + Homet","real_name_normalized":"Jessica Homet","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gf6a25943381","first_name":"Jessica","last_name":"Homet","image_24":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952990,"has_2fa":false}],"cache_ts":1553099552,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 16:32:32 GMT +recorded_with: VCR 4.0.0 diff --git a/test/cassettes/workspace.yml b/test/cassettes/workspace.yml new file mode 100644 index 00000000..d571ed9c --- /dev/null +++ b/test/cassettes/workspace.yml @@ -0,0 +1,153 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '912' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 16:29:03 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 3a40e75c-ff70-4bd5-beee-89f96db3f04a + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-sfxy + X-Cache: + - Miss from cloudfront + Via: + - 1.1 5971d213ff39e16c310a05523f08e121.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - KxMAzswAVs8JTGw_NOCzpJOw8nmrSZs7Bgo3dPys1qGlEmP0g-o55A== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TH2P32R19","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_192.png","image_512":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"UH0EG0QHE","team_id":"TH2P32R19","name":"sopheary.chiv","deleted":false,"color":"4bbe2e","real_name":"Sopheary + Chiv","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Sopheary + Chiv","real_name_normalized":"Sopheary Chiv","display_name":"Sopheary Chiv","display_name_normalized":"Sopheary + Chiv","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g3fc544bd693","image_24":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952644,"has_2fa":false},{"id":"UH2RGJ36Y","team_id":"TH2P32R19","name":"jessica.homet","deleted":false,"color":"9f69e7","real_name":"Jessica + Homet","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Jessica + Homet","real_name_normalized":"Jessica Homet","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gf6a25943381","first_name":"Jessica","last_name":"Homet","image_24":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952990,"has_2fa":false}],"cache_ts":1553099343,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 16:29:03 GMT +- request: + method: get + uri: https://slack.com/api/channels.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '554' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 16:29:03 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 4cad8c8f-b26e-4a6e-8eb7-1eba6f8c2bc9 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-o038 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 53332bd6d55cfd374862eac4265e274a.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - ITn0lSyCHHFm3hY2RAMs3J2-2XGWPTajS1JYJDZeEws8L199sa5s3g== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CH0EFGJHE","name":"everyone","is_channel":true,"created":1552952596,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"everyone","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2},{"id":"CH0EFGQ72","name":"slack-api","is_channel":true,"created":1552952597,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"slack-api","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"","creator":"","last_set":0},"previous_names":[],"num_members":2},{"id":"CH2P330M9","name":"random","is_channel":true,"created":1552952596,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 16:29:04 GMT +recorded_with: VCR 4.0.0 From 38bb11ab9049a0958f4a0f79a4ddd751ab2fe6ba Mon Sep 17 00:00:00 2001 From: Jessica Date: Wed, 20 Mar 2019 13:40:34 -0700 Subject: [PATCH 14/40] Fix cassettes structure and functionality --- .DS_Store | Bin 6148 -> 6148 bytes .gitignore | 3 ++- {test => specs}/cassettes/users_found.yml | 16 ++++++------- {test => specs}/cassettes/workspace.yml | 26 +++++++++++----------- specs/test_helper.rb | 2 +- specs/user_spec.rb | 13 +++++++---- 6 files changed, 33 insertions(+), 27 deletions(-) rename {test => specs}/cassettes/users_found.yml (94%) rename {test => specs}/cassettes/workspace.yml (93%) diff --git a/.DS_Store b/.DS_Store index a2eb533abf551d042629249e6fe5fb378b3b5ee0..d55d496598e54a797757c9fda1345f34fc1d0e5c 100644 GIT binary patch delta 125 zcmZoMXfc=|&e%4wP>hv>fq{WzVxfo>6OaJ{AexbZL4YAGr8qe$KR<_YVxg8ONR)-4 zgdvron4ttFGf`V+W4br{WCIbF&D @@ -100,13 +100,13 @@ http_interactions: Connection: - keep-alive Date: - - Wed, 20 Mar 2019 16:29:03 GMT + - Wed, 20 Mar 2019 20:38:14 GMT Server: - Apache X-Content-Type-Options: - nosniff X-Slack-Req-Id: - - 4cad8c8f-b26e-4a6e-8eb7-1eba6f8c2bc9 + - fc83d2de-cc61-4e93-8318-a42adbd122a3 X-Oauth-Scopes: - identify,channels:read,users:read,chat:write:bot Expires: @@ -132,13 +132,13 @@ http_interactions: Access-Control-Allow-Origin: - "*" X-Via: - - haproxy-www-o038 + - haproxy-www-imty X-Cache: - Miss from cloudfront Via: - - 1.1 53332bd6d55cfd374862eac4265e274a.cloudfront.net (CloudFront) + - 1.1 4f81f573d1d8e804c79450e430cd47be.cloudfront.net (CloudFront) X-Amz-Cf-Id: - - ITn0lSyCHHFm3hY2RAMs3J2-2XGWPTajS1JYJDZeEws8L199sa5s3g== + - AyY7VVidP4X7OCxVg6qdYaeygM_SOb-ipTZl2k9sE5XTsWAu6boqSQ== body: encoding: ASCII-8BIT string: '{"ok":true,"channels":[{"id":"CH0EFGJHE","name":"everyone","is_channel":true,"created":1552952596,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"everyone","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Company-wide @@ -149,5 +149,5 @@ http_interactions: place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' http_version: - recorded_at: Wed, 20 Mar 2019 16:29:04 GMT + recorded_at: Wed, 20 Mar 2019 20:38:14 GMT recorded_with: VCR 4.0.0 diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 039b38de..ffcf4b4e 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -22,7 +22,7 @@ # require_relative "../lib/slack" # Do we need this? Don't think so. VCR.configure do |config| - config.cassette_library_dir = "test/cassettes" # folder where casettes will be + config.cassette_library_dir = "specs/cassettes" # folder where casettes will be config.hook_into :webmock # tie into this other rool called webmock config.default_cassette_options = { :record => :new_episodes, # record new data when we don't have it yet diff --git a/specs/user_spec.rb b/specs/user_spec.rb index a347a8e6..b1887c7f 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -21,11 +21,16 @@ describe "List" do it "can list itself" do VCR.use_cassette("users_found") do - Slack::User.list + response = Slack::User.list - # expect(list.first["Seattle"]).wont_be_nil - # expect(response["Seattle"][:lon]).must_equal "-122.3300624" - # expect(response["Seattle"][:lat]).must_equal "47.6038321" + expect(response[0].name).must_equal "slackbot" + end + end + it "does something else" do + VCR.use_cassette("users_found") do + response = Slack::User.list + + expect(response[1].name).must_equal "sopheary.chiv" end end end From c6b47283e1d39e29d74d8c42b3ffd5d5487677bb Mon Sep 17 00:00:00 2001 From: Jessica Date: Wed, 20 Mar 2019 13:59:37 -0700 Subject: [PATCH 15/40] Optimize self.get method and added some tests --- lib/channel.rb | 25 ++++------- lib/recipient.rb | 9 +++- lib/user.rb | 8 +--- specs/cassettes/recipient.yml | 78 +++++++++++++++++++++++++++++++++++ specs/cassettes/workspace.yml | 75 +++++++++++++++++++++++++++++++++ specs/recipient_spec.rb | 29 +++++++++++++ specs/user_spec.rb | 6 --- 7 files changed, 197 insertions(+), 33 deletions(-) create mode 100644 specs/cassettes/recipient.yml diff --git a/lib/channel.rb b/lib/channel.rb index 8c1d2f8b..b68d117b 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -18,27 +18,16 @@ def initialize(slack_id, name, topic, member_count) def self.list list = [] - url = "https://slack.com/api/channels.list" + response = self.get("https://slack.com/api/channels.list") - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } + response["channels"].each do |channel| + slack_id = channel["id"] + name = channel["name"] + topic = channel["topic"]["value"] + member_count = channel["num_members"] - response = HTTParty.get(url, query: query_parameters) - - if response.code == 200 - response.parsed_response["channels"].each do |channel| - slack_id = channel["id"] - name = channel["name"] - topic = channel["topic"]["value"] - member_count = channel["num_members"] - - list << Slack::Channel.new(slack_id, name, topic, member_count) - end - else - ap "#{response.code} #{response.message}" + list << Slack::Channel.new(slack_id, name, topic, member_count) end - return list end end end diff --git a/lib/recipient.rb b/lib/recipient.rb index e710d82d..b9d9576a 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -11,8 +11,13 @@ def send_message(message) # Implement here. end - def self.get(url, params) - response = HTTParty.get(url, query: params) + def self.get(url) + response = HTTParty.get( + url, + query: { + token: ENV["SLACK_API_TOKEN"], + }, + ) if response.code == 200 return response diff --git a/lib/user.rb b/lib/user.rb index 9c2782fb..b8b32c72 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -15,13 +15,7 @@ def initialize(slack_id, name, real_name, status_text, status_emoji) def self.list list = [] - url = "https://slack.com/api/users.list" - - query_parameters = { - token: ENV["SLACK_API_TOKEN"], - } - - response = self.get(url, query_parameters) + response = self.get("https://slack.com/api/users.list") response["members"].each do |member| slack_id = member["id"] diff --git a/specs/cassettes/recipient.yml b/specs/cassettes/recipient.yml new file mode 100644 index 00000000..4f930d20 --- /dev/null +++ b/specs/cassettes/recipient.yml @@ -0,0 +1,78 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/channels.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '554' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 20:56:10 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - fb8ab65c-afab-4d83-93ef-31b2daaf44e1 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-xhix + X-Cache: + - Miss from cloudfront + Via: + - 1.1 a077f80f2fe737f90e09bad4a75fa2bc.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - 8bdvYbzthGc4hM0vhdCjp5L5jqsXuvAdsXA0sZf9xw1UcovSSrs7RA== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CH0EFGJHE","name":"everyone","is_channel":true,"created":1552952596,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"everyone","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2},{"id":"CH0EFGQ72","name":"slack-api","is_channel":true,"created":1552952597,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"slack-api","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"","creator":"","last_set":0},"previous_names":[],"num_members":2},{"id":"CH2P330M9","name":"random","is_channel":true,"created":1552952596,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 20:56:10 GMT +recorded_with: VCR 4.0.0 diff --git a/specs/cassettes/workspace.yml b/specs/cassettes/workspace.yml index ffd80aa8..6d96c34b 100644 --- a/specs/cassettes/workspace.yml +++ b/specs/cassettes/workspace.yml @@ -150,4 +150,79 @@ http_interactions: you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' http_version: recorded_at: Wed, 20 Mar 2019 20:38:14 GMT +- request: + method: get + uri: https://slack.com/api/channels.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '554' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 20:57:43 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 37c512df-2b36-43dd-ae24-2fcbdd2d6c4d + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-rxp9 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 64a11a52a1b20918fec274138dd1ba05.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - XtGSRIeQY9jrWnOkt41YcD76pnaRDzqB-9kcn8SSnoLj4S3wLXoByA== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CH0EFGJHE","name":"everyone","is_channel":true,"created":1552952596,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"everyone","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2},{"id":"CH0EFGQ72","name":"slack-api","is_channel":true,"created":1552952597,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"slack-api","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"","creator":"","last_set":0},"previous_names":[],"num_members":2},{"id":"CH2P330M9","name":"random","is_channel":true,"created":1552952596,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 20:57:43 GMT recorded_with: VCR 4.0.0 diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index f1566318..10a8c40a 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -4,6 +4,7 @@ let(:slack_id) { 20 } let(:username) { "Sopheary" } let(:recipient) { Slack::Recipient.new(slack_id, username) } + describe "Initialization" do it "can be instantiated" do expect(recipient).must_be_instance_of Slack::Recipient @@ -12,6 +13,34 @@ end end + describe "send message" do + end + + describe "self.get" do + it "can get valid user data from the API" do + # VCR.use_cassette("recipient") do + # url = "https://slack.com/api/users.list" + # params = { + # token: + # } + + # return_value = Slack::Recipient.get(url, params) + # end + end + + it "can get valid channel data from the API" do + VCR.use_cassette("recipient") do + url = "https://slack.com/api/channels.list" + params = + + return_value = Slack::Recipient.get(url) + end + end + + it "no URL?" do + end + end + describe "self.list" do it "raises error if attempted" do expect { Slack::Recipient.list }.must_raise NotImplementedError diff --git a/specs/user_spec.rb b/specs/user_spec.rb index b1887c7f..e475c7b0 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -24,12 +24,6 @@ response = Slack::User.list expect(response[0].name).must_equal "slackbot" - end - end - it "does something else" do - VCR.use_cassette("users_found") do - response = Slack::User.list - expect(response[1].name).must_equal "sopheary.chiv" end end From 984651aeacc339b37c8283da1bd0f539e4081d6b Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 20 Mar 2019 14:43:06 -0700 Subject: [PATCH 16/40] added tests Recipient and User --- lib/channel.rb | 6 +-- lib/recipient.rb | 10 +++-- specs/cassettes/no_users.yml | 72 +++++++++++++++++++++++++++++++++ specs/cassettes/recipient.yml | 75 +++++++++++++++++++++++++++++++++++ specs/recipient_spec.rb | 22 ++++------ specs/user_spec.rb | 16 ++++++++ 6 files changed, 180 insertions(+), 21 deletions(-) create mode 100644 specs/cassettes/no_users.yml diff --git a/lib/channel.rb b/lib/channel.rb index b68d117b..b07b003f 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,6 +1,6 @@ -require "dotenv" -require "httparty" -require "awesome_print" +# require "dotenv" +# require "httparty" +# require "awesome_print" require_relative "recipient" diff --git a/lib/recipient.rb b/lib/recipient.rb index b9d9576a..7705c94e 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,4 +1,6 @@ module Slack + class SlackError < StandardError; end + class Recipient attr_reader :slack_id, :name @@ -19,13 +21,13 @@ def self.get(url) }, ) - if response.code == 200 - return response + if response.code == 200 && response["ok"] + return response.parsed_response else - raise ArgumentError, "Error: #{response.code} #{response.message}" # change this to a better error response + raise Slack::SlackError, "Error: #{response.code} #{response.message}" end - return response.parsed_response + # return response.parsed_response end def self.list diff --git a/specs/cassettes/no_users.yml b/specs/cassettes/no_users.yml new file mode 100644 index 00000000..764d7876 --- /dev/null +++ b/specs/cassettes/no_users.yml @@ -0,0 +1,72 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '912' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 20:38:14 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 59127942-e278-465b-9cbf-1fd5e5b56b3c + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-5baq + X-Cache: + - Miss from cloudfront + Via: + - 1.1 010086e284fd9714db3b3e3d1d295b60.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - d_-gcMclsWiu9OptE-SF6PqQ_Ab3_eFk0XGaFNfjZmWSNxZxdmTYZw== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[],"cache_ts":1553114294,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 20:38:14 GMT +recorded_with: VCR 4.0.0 diff --git a/specs/cassettes/recipient.yml b/specs/cassettes/recipient.yml index 4f930d20..dfb652b9 100644 --- a/specs/cassettes/recipient.yml +++ b/specs/cassettes/recipient.yml @@ -75,4 +75,79 @@ http_interactions: you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' http_version: recorded_at: Wed, 20 Mar 2019 20:56:10 GMT +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '912' + Connection: + - keep-alive + Date: + - Wed, 20 Mar 2019 21:08:27 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 5de30e3d-5529-4f45-9f75-14dbf601d157 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-8twt + X-Cache: + - Miss from cloudfront + Via: + - 1.1 7430a54821bbaeddfc77b56ba1b84eae.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - lL0UPoDcXLYHyBBdcgq5eIoJy8bGabylKcT020GitjgsvbKpSVdSYg== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TH2P32R19","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_192.png","image_512":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"UH0EG0QHE","team_id":"TH2P32R19","name":"sopheary.chiv","deleted":false,"color":"4bbe2e","real_name":"Sopheary + Chiv","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Sopheary + Chiv","real_name_normalized":"Sopheary Chiv","display_name":"Sopheary Chiv","display_name_normalized":"Sopheary + Chiv","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g3fc544bd693","image_24":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952644,"has_2fa":false},{"id":"UH2RGJ36Y","team_id":"TH2P32R19","name":"jessica.homet","deleted":false,"color":"9f69e7","real_name":"Jessica + Homet","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Jessica + Homet","real_name_normalized":"Jessica Homet","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gf6a25943381","first_name":"Jessica","last_name":"Homet","image_24":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952990}],"cache_ts":1553116107,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Wed, 20 Mar 2019 21:08:27 GMT recorded_with: VCR 4.0.0 diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 10a8c40a..5f70ea06 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -18,27 +18,21 @@ describe "self.get" do it "can get valid user data from the API" do - # VCR.use_cassette("recipient") do - # url = "https://slack.com/api/users.list" - # params = { - # token: - # } - - # return_value = Slack::Recipient.get(url, params) - # end + VCR.use_cassette("recipient") do + url = "https://slack.com/api/users.list" + + return_value = Slack::Recipient.get(url) + expect(return_value["ok"]).must_equal true + end end it "can get valid channel data from the API" do VCR.use_cassette("recipient") do url = "https://slack.com/api/channels.list" - params = - - return_value = Slack::Recipient.get(url) + return_value = Slack::Recipient.get(url) + expect(return_value["ok"]).must_equal true end end - - it "no URL?" do - end end describe "self.list" do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index e475c7b0..566373a9 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -27,6 +27,22 @@ expect(response[1].name).must_equal "sopheary.chiv" end end + + it "returns the right data types" do + VCR.use_cassette("users_found") do + response = Slack::User.list + + expect(response).must_be_kind_of Array + expect(response[0]).must_be_instance_of Slack::User + expect(response.last).must_be_instance_of Slack::User + end + end + + # it "raise an error if no users found" do + # VCR.use_cassette("no_users") do + # expect { Slack::User.list }.must_equal [] + # end + # end end end end From a08df6ed1ab4e409bb867500c70c50b93ca3c27c Mon Sep 17 00:00:00 2001 From: Jessica Date: Wed, 20 Mar 2019 15:37:27 -0700 Subject: [PATCH 17/40] Create CLI interface to list users, list channels, and quit (wave 1) --- lib/channel.rb | 4 +++- lib/recipient.rb | 6 ++++++ lib/slack.rb | 23 +++++++++++++++++++++++ lib/user.rb | 1 - 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index b07b003f..d2b665bd 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,6 +1,7 @@ # require "dotenv" # require "httparty" # require "awesome_print" +require "pry" require_relative "recipient" @@ -14,7 +15,6 @@ def initialize(slack_id, name, topic, member_count) @member_count = member_count end - #temp def self.list list = [] @@ -28,6 +28,8 @@ def self.list list << Slack::Channel.new(slack_id, name, topic, member_count) end + + return list end end end diff --git a/lib/recipient.rb b/lib/recipient.rb index 7705c94e..81f02e3b 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,3 +1,5 @@ + + module Slack class SlackError < StandardError; end @@ -34,6 +36,10 @@ def self.list raise NotImplementedError, "Implement me in a child class!" end + def self.print_list + raise NotImplementedError, "Implement me in a child class!" + end + def details raise NotImplementedError, "Implement me in a child class!" end diff --git a/lib/slack.rb b/lib/slack.rb index 315c8583..370e240b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,9 +3,32 @@ require_relative "user" require_relative "channel" +# require_relative "../specs/test_helper" +require "table_print" +require "httparty" +require "pry" +require "dotenv" +Dotenv.load + def main puts "Welcome to the Ada Slack CLI!" # TODO project + loop do + puts "What would you like to do next?" + puts "[list users] [list channels] [quit]" + print "> " + + user_input = gets.chomp + + case user_input.downcase + when "list users" + tp Slack::User.list, :name, :real_name, :slack_id + when "list channels" + tp Slack::Channel.list, :name, :topic, :member_count, :slack_id + end + + break if user_input == "quit" + end puts "Thank you for using the Ada Slack CLI" end diff --git a/lib/user.rb b/lib/user.rb index b8b32c72..1eed4474 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,7 +11,6 @@ def initialize(slack_id, name, real_name, status_text, status_emoji) @status_emoji = status_emoji end - #temp def self.list list = [] From a597ac3ff6d6190cf1180209655fb55141cad497 Mon Sep 17 00:00:00 2001 From: Jessica Date: Thu, 21 Mar 2019 10:03:18 -0700 Subject: [PATCH 18/40] Modify list users and list channels to pull from workspace instead of user and channel classes --- lib/slack.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 370e240b..b0e8e7ab 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -11,20 +11,28 @@ Dotenv.load def main + workspace = Slack::Workspace.new + puts "Welcome to the Ada Slack CLI!" # TODO project loop do puts "What would you like to do next?" - puts "[list users] [list channels] [quit]" + puts "[list users] [list channels] [select user] [select channel] [details] [quit]" print "> " user_input = gets.chomp case user_input.downcase when "list users" - tp Slack::User.list, :name, :real_name, :slack_id + tp workspace.users, :name, :real_name, :slack_id when "list channels" - tp Slack::Channel.list, :name, :topic, :member_count, :slack_id + tp workspace.channels, :name, :topic, :member_count, :slack_id + when "select user" + puts "hehe haven't done this yet" + when "select channel" + puts "hehe haven't done this yet" + when "details" + puts "hehe haven't done this yet" end break if user_input == "quit" From 64db0f373d80e4c6c020cd2609e0d742f97e6c98 Mon Sep 17 00:00:00 2001 From: Jessica Date: Thu, 21 Mar 2019 12:25:52 -0700 Subject: [PATCH 19/40] Implement select user, select channel, and details user options --- lib/recipient.rb | 4 ---- lib/slack.rb | 11 ++++++++--- lib/workspace.rb | 27 ++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 81f02e3b..06b61369 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -36,10 +36,6 @@ def self.list raise NotImplementedError, "Implement me in a child class!" end - def self.print_list - raise NotImplementedError, "Implement me in a child class!" - end - def details raise NotImplementedError, "Implement me in a child class!" end diff --git a/lib/slack.rb b/lib/slack.rb index b0e8e7ab..c9dd736d 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -4,6 +4,7 @@ require_relative "channel" # require_relative "../specs/test_helper" +require "colorize" require "table_print" require "httparty" require "pry" @@ -28,11 +29,15 @@ def main when "list channels" tp workspace.channels, :name, :topic, :member_count, :slack_id when "select user" - puts "hehe haven't done this yet" + print "User ID or Username > " + input = gets.chomp + puts workspace.select_user(input) when "select channel" - puts "hehe haven't done this yet" + print "Channel ID or Channel Name > " + input = gets.chomp + puts workspace.select_channel(input) when "details" - puts "hehe haven't done this yet" + workspace.show_details # This is the only method that prints things within the method. end break if user_input == "quit" diff --git a/lib/workspace.rb b/lib/workspace.rb index 12872c22..ba2eb3fe 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -8,16 +8,37 @@ def initialize @selected = nil end - def select_channel + def select_channel(input) + select(input, channels) end - def select_user + def select_user(input) + select(input, users) end - def show_details + def show_details # This is the only method that prints anything. + puts "No recipient selected".red unless selected + + if selected.class == Slack::User + tp selected, :name, :real_name, :slack_id + elsif selected.class == Slack::Channel + tp selected, :name, :topic, :member_count, :slack_id + end end def send_message end + + private + + def select(input, recipients) + recipients.each do |recipient| + if input == recipient.slack_id || input == recipient.name + @selected = recipient + return "#{input} selected".green + end + end + return "#{input} does not exist".red + end end end From 6095d8c4229350eebd320f9efbcb6cb3af9cd055 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Thu, 21 Mar 2019 20:10:41 -0700 Subject: [PATCH 20/40] added tests for select_channel and select_user --- lib/slack.rb | 13 +++++- lib/workspace.rb | 6 ++- specs/test_helper.rb | 1 + specs/workspace_spec.rb | 100 ++++++++++++++++++++++++++++++++++------ 4 files changed, 103 insertions(+), 17 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index c9dd736d..cd9b592e 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -31,11 +31,13 @@ def main when "select user" print "User ID or Username > " input = gets.chomp - puts workspace.select_user(input) + validate_input(input) + puts "#{workspace.select_user(input)} selected".green when "select channel" print "Channel ID or Channel Name > " input = gets.chomp - puts workspace.select_channel(input) + validate_input(input) + puts workspace.select_channel(input).green when "details" workspace.show_details # This is the only method that prints things within the method. end @@ -46,4 +48,11 @@ def main puts "Thank you for using the Ada Slack CLI" end +def validate_input(input) + unless input != nil + puts "Please choose one option:" + puts "[list users] [list channels] [select user] [select channel] [details] [quit]" + end +end + main if __FILE__ == $PROGRAM_NAME diff --git a/lib/workspace.rb b/lib/workspace.rb index ba2eb3fe..ebb39ffb 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -32,13 +32,15 @@ def send_message private def select(input, recipients) + raise ArgumentError, "no input entered" if input == nil + raise ArgumentError, "no input entered" if input == "" recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient - return "#{input} selected".green + return input end end - return "#{input} does not exist".red + return "#{input} does not exist" end end end diff --git a/specs/test_helper.rb b/specs/test_helper.rb index ffcf4b4e..13e9b914 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -10,6 +10,7 @@ require "dotenv" require "httparty" require "awesome_print" +require "table_print" Dotenv.load diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index c43ab9aa..b709c48c 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -1,27 +1,101 @@ require_relative "test_helper" describe "Workspace" do - # let(:workspace) { Slack::Workspace.new } + before do + VCR.use_cassette("workspace") do + @workspace = Slack::Workspace.new + end + end describe "Initialization" do it "can be instantiated" do VCR.use_cassette("workspace") do - workspace = Slack::Workspace.new - expect(workspace).must_be_instance_of Slack::Workspace + @workspace = Slack::Workspace.new + expect(@workspace).must_be_instance_of Slack::Workspace end end it "establishes the base structures when instantiated" do - VCR.use_cassette("workspace") do - workspace = Slack::Workspace.new - expect(workspace) - [:users, :channels, :selected].each do |prop| - expect(workspace).must_respond_to prop - end - - expect(workspace.users).must_be_kind_of Array - expect(workspace.channels).must_be_kind_of Array - expect(workspace.selected).must_equal nil + [:users, :channels, :selected].each do |prop| + expect(@workspace).must_respond_to prop end + + expect(@workspace.users).must_be_kind_of Array + expect(@workspace.channels).must_be_kind_of Array + expect(@workspace.selected).must_equal nil + # end + end + end + + describe "select_user" do + it "returns the right selected user" do + input = "sopheary.chiv" + recipients = @workspace.users + expect(@workspace.select_user(input)).must_equal "sopheary.chiv" + end + + it "stores the right user in the selected variable" do + before_selecting_user = @workspace.selected + expect(before_selecting_user).must_be_nil + @workspace.select_user("sopheary.chiv") + after_selecting_user = @workspace.selected + expect(after_selecting_user.name).must_equal "sopheary.chiv" + end + + it "@selected must be an instance of the user" do + @workspace.select_user("sopheary.chiv") + expect(@workspace.selected).must_be_instance_of Slack::User + end + + it "handles the non-existing user" do + non_exiting_user = @workspace.select_user("jennifer.chiv") + expect(non_exiting_user).must_equal "jennifer.chiv does not exist" + end + + it "raises ArgumentError if given an empty input" do + expect { @workspace.select_user("") }.must_raise ArgumentError + end + + it "raises ArgumentError if input is nil" do + expect { @workspace.select_user }.must_raise ArgumentError + end + end + + describe "select_channel" do + it "returns the right selected channel" do + input = "slack-api" + recipients = @workspace.users + expect(@workspace.select_channel(input)).must_equal "slack-api" + end + + it "stores the right channel in the selected variable" do + before_selecting_user = @workspace.selected + expect(before_selecting_user).must_be_nil + @workspace.select_channel("slack-api") + after_selecting_user = @workspace.selected + expect(after_selecting_user.name).must_equal "slack-api" + end + + it "@selected must be an instance of the channel" do + @workspace.select_channel("slack-api") + expect(@workspace.selected).must_be_instance_of Slack::Channel + end + + it "handles the non-existing channel" do + non_exiting_user = @workspace.select_channel("my-channel") + expect(non_exiting_user).must_equal "my-channel does not exist" + end + + it "raises ArgumentError if given an empty input" do + expect { @workspace.select_user("") }.must_raise ArgumentError + end + + it "raises ArgumentError if input is nil" do + expect { @workspace.select_user }.must_raise ArgumentError + end + end + + describe "show_details" do + it "shows the details of the selected recipient" do end end end From 01093401c6ab3961596962d2cdcddac42ce45c84 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Thu, 21 Mar 2019 20:28:20 -0700 Subject: [PATCH 21/40] changed the tests for the select methods and took out the validate method in main --- lib/slack.rb | 9 --------- lib/workspace.rb | 2 -- specs/workspace_spec.rb | 16 ---------------- 3 files changed, 27 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index cd9b592e..0be53cf9 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -31,12 +31,10 @@ def main when "select user" print "User ID or Username > " input = gets.chomp - validate_input(input) puts "#{workspace.select_user(input)} selected".green when "select channel" print "Channel ID or Channel Name > " input = gets.chomp - validate_input(input) puts workspace.select_channel(input).green when "details" workspace.show_details # This is the only method that prints things within the method. @@ -48,11 +46,4 @@ def main puts "Thank you for using the Ada Slack CLI" end -def validate_input(input) - unless input != nil - puts "Please choose one option:" - puts "[list users] [list channels] [select user] [select channel] [details] [quit]" - end -end - main if __FILE__ == $PROGRAM_NAME diff --git a/lib/workspace.rb b/lib/workspace.rb index ebb39ffb..f7980608 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -32,8 +32,6 @@ def send_message private def select(input, recipients) - raise ArgumentError, "no input entered" if input == nil - raise ArgumentError, "no input entered" if input == "" recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b709c48c..a6a40ee1 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -50,14 +50,6 @@ non_exiting_user = @workspace.select_user("jennifer.chiv") expect(non_exiting_user).must_equal "jennifer.chiv does not exist" end - - it "raises ArgumentError if given an empty input" do - expect { @workspace.select_user("") }.must_raise ArgumentError - end - - it "raises ArgumentError if input is nil" do - expect { @workspace.select_user }.must_raise ArgumentError - end end describe "select_channel" do @@ -84,14 +76,6 @@ non_exiting_user = @workspace.select_channel("my-channel") expect(non_exiting_user).must_equal "my-channel does not exist" end - - it "raises ArgumentError if given an empty input" do - expect { @workspace.select_user("") }.must_raise ArgumentError - end - - it "raises ArgumentError if input is nil" do - expect { @workspace.select_user }.must_raise ArgumentError - end end describe "show_details" do From 3c94fdd906e95ac762044e5dd38405361a0907cc Mon Sep 17 00:00:00 2001 From: Jessica Date: Thu, 21 Mar 2019 21:11:33 -0700 Subject: [PATCH 22/40] Add test for show_details (99% code coverage) --- lib/workspace.rb | 7 ++++++- specs/workspace_spec.rb | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index ebb39ffb..d50bb292 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,6 +1,7 @@ module Slack class Workspace - attr_reader :users, :channels, :selected + attr_reader :users, :channels + attr_accessor :selected # only needed here because of tests... that seems wrong? def initialize @users = User.list @@ -21,8 +22,12 @@ def show_details # This is the only method that prints anything. if selected.class == Slack::User tp selected, :name, :real_name, :slack_id + return selected elsif selected.class == Slack::Channel tp selected, :name, :topic, :member_count, :slack_id + return selected + else + return nil end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index b709c48c..95e63eb1 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -21,7 +21,7 @@ expect(@workspace.users).must_be_kind_of Array expect(@workspace.channels).must_be_kind_of Array - expect(@workspace.selected).must_equal nil + assert_nil(@workspace.selected) # Changed based on CL feedback... does this make sense? # end end end @@ -94,8 +94,23 @@ end end - describe "show_details" do - it "shows the details of the selected recipient" do + describe "show_details" do # don't know if this is the best way to test this + let(:user) { @workspace.users[0] } + let(:channel) { @workspace.channels[0] } + it "returns selected recipient if recipient is selected" do + @workspace.selected = user + method = @workspace.show_details + expect(method).must_equal @workspace.selected + expect(method).must_be_instance_of Slack::User + end + it "shows the details of the selected channel" do + @workspace.selected = channel + method = @workspace.show_details + expect(method).must_equal @workspace.selected + expect(method).must_be_instance_of Slack::Channel + end + it "returns nil if no selected user" do + assert_nil(@workspace.show_details) end end end From 04f47fe3641a06ccc9a93a3c761969a2207e3212 Mon Sep 17 00:00:00 2001 From: Jessica Date: Thu, 21 Mar 2019 21:13:43 -0700 Subject: [PATCH 23/40] Add tests for show details --- lib/slack.rb | 9 --------- lib/workspace.rb | 2 -- specs/workspace_spec.rb | 16 ---------------- 3 files changed, 27 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index cd9b592e..0be53cf9 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -31,12 +31,10 @@ def main when "select user" print "User ID or Username > " input = gets.chomp - validate_input(input) puts "#{workspace.select_user(input)} selected".green when "select channel" print "Channel ID or Channel Name > " input = gets.chomp - validate_input(input) puts workspace.select_channel(input).green when "details" workspace.show_details # This is the only method that prints things within the method. @@ -48,11 +46,4 @@ def main puts "Thank you for using the Ada Slack CLI" end -def validate_input(input) - unless input != nil - puts "Please choose one option:" - puts "[list users] [list channels] [select user] [select channel] [details] [quit]" - end -end - main if __FILE__ == $PROGRAM_NAME diff --git a/lib/workspace.rb b/lib/workspace.rb index d50bb292..ca277595 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -37,8 +37,6 @@ def send_message private def select(input, recipients) - raise ArgumentError, "no input entered" if input == nil - raise ArgumentError, "no input entered" if input == "" recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 95e63eb1..dd12e629 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -50,14 +50,6 @@ non_exiting_user = @workspace.select_user("jennifer.chiv") expect(non_exiting_user).must_equal "jennifer.chiv does not exist" end - - it "raises ArgumentError if given an empty input" do - expect { @workspace.select_user("") }.must_raise ArgumentError - end - - it "raises ArgumentError if input is nil" do - expect { @workspace.select_user }.must_raise ArgumentError - end end describe "select_channel" do @@ -84,14 +76,6 @@ non_exiting_user = @workspace.select_channel("my-channel") expect(non_exiting_user).must_equal "my-channel does not exist" end - - it "raises ArgumentError if given an empty input" do - expect { @workspace.select_user("") }.must_raise ArgumentError - end - - it "raises ArgumentError if input is nil" do - expect { @workspace.select_user }.must_raise ArgumentError - end end describe "show_details" do # don't know if this is the best way to test this From 0cfd52ff113d5d9fe858ac162b24b0503e37c856 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 09:00:47 -0700 Subject: [PATCH 24/40] Add test keyword argument to show_details so printing doens't happen when testing --- lib/workspace.rb | 8 ++++---- specs/workspace_spec.rb | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index ca277595..93732d70 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -17,14 +17,14 @@ def select_user(input) select(input, users) end - def show_details # This is the only method that prints anything. - puts "No recipient selected".red unless selected + def show_details(test: false) # This is the only method that prints anything. + puts "No recipient selected".red unless selected || test if selected.class == Slack::User - tp selected, :name, :real_name, :slack_id + tp selected, :name, :real_name, :slack_id unless test return selected elsif selected.class == Slack::Channel - tp selected, :name, :topic, :member_count, :slack_id + tp selected, :name, :topic, :member_count, :slack_id unless test return selected else return nil diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index dd12e629..afdead3d 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -83,18 +83,18 @@ let(:channel) { @workspace.channels[0] } it "returns selected recipient if recipient is selected" do @workspace.selected = user - method = @workspace.show_details + method = @workspace.show_details(test: true) # test true stops it from printing anything expect(method).must_equal @workspace.selected expect(method).must_be_instance_of Slack::User end it "shows the details of the selected channel" do @workspace.selected = channel - method = @workspace.show_details + method = @workspace.show_details(test: true) expect(method).must_equal @workspace.selected expect(method).must_be_instance_of Slack::Channel end it "returns nil if no selected user" do - assert_nil(@workspace.show_details) + assert_nil(@workspace.show_details(test: true)) end end end From 25c23142f9951b3c11a229902dd071f74320d211 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 10:30:48 -0700 Subject: [PATCH 25/40] Modify token to keyword argument so test can check if argument is raised if invalid query is sent to API --- lib/recipient.rb | 6 ++-- specs/cassettes/no_token.yml | 62 +++++++++++++++++++++++++++++++++++ specs/cassettes/recipient.yml | 59 +++++++++++++++++++++++++++++++++ specs/recipient_spec.rb | 7 ++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 specs/cassettes/no_token.yml diff --git a/lib/recipient.rb b/lib/recipient.rb index 06b61369..d728bb27 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,6 +1,8 @@ module Slack + TOKEN = ENV["SLACK_API_TOKEN"] + class SlackError < StandardError; end class Recipient @@ -15,11 +17,11 @@ def send_message(message) # Implement here. end - def self.get(url) + def self.get(url, token: TOKEN) response = HTTParty.get( url, query: { - token: ENV["SLACK_API_TOKEN"], + token: token, }, ) diff --git a/specs/cassettes/no_token.yml b/specs/cassettes/no_token.yml new file mode 100644 index 00000000..c9077997 --- /dev/null +++ b/specs/cassettes/no_token.yml @@ -0,0 +1,62 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/channels.list?token=this_is_wrong + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '55' + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 17:21:35 GMT + Server: + - Apache + Access-Control-Expose-Headers: + - x-slack-req-id + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 911bb648-8fa3-4227-8150-f1406d3bff0e + X-Xss-Protection: + - '0' + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-ax66 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 9cd19437b0d776b24c104d0c56377ca8.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - lNMuZGnvFzuw5SdOrcTyJo2zALGhEwImjCw-_yFSPmLSN_MpqN9SvQ== + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + http_version: + recorded_at: Fri, 22 Mar 2019 17:21:35 GMT +recorded_with: VCR 4.0.0 diff --git a/specs/cassettes/recipient.yml b/specs/cassettes/recipient.yml index dfb652b9..d354f307 100644 --- a/specs/cassettes/recipient.yml +++ b/specs/cassettes/recipient.yml @@ -150,4 +150,63 @@ http_interactions: Homet","real_name_normalized":"Jessica Homet","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gf6a25943381","first_name":"Jessica","last_name":"Homet","image_24":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952990}],"cache_ts":1553116107,"response_metadata":{"next_cursor":""}}' http_version: recorded_at: Wed, 20 Mar 2019 21:08:27 GMT +- request: + method: get + uri: https://slack.com/api/channels.list?token=this_is_wrong + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '55' + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 17:22:18 GMT + Server: + - Apache + Access-Control-Expose-Headers: + - x-slack-req-id + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - df883bb9-f145-4654-9aed-d14f342aefd5 + X-Xss-Protection: + - '0' + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-26um + X-Cache: + - Miss from cloudfront + Via: + - 1.1 44914fa6421b789193cec8998428f8bd.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - "-jNduFJ-VXScFe4-M8RUqBwQaNZvaMrVuKDPO4zyQy0Nx7Kzl0wNXQ==" + body: + encoding: ASCII-8BIT + string: '{"ok":false,"error":"invalid_auth"}' + http_version: + recorded_at: Fri, 22 Mar 2019 17:22:18 GMT recorded_with: VCR 4.0.0 diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 5f70ea06..2e65dc4f 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -33,6 +33,13 @@ expect(return_value["ok"]).must_equal true end end + + it "raises a SlackError if invalid queries are provided" do + VCR.use_cassette("recipient") do + url = "https://slack.com/api/channels.list" + expect { Slack::Recipient.get(url, token: "this_is_wrong") }.must_raise Slack::SlackError + end + end end describe "self.list" do From d35621b069e7a06c54ad7c03f023e217554925b7 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 10:42:07 -0700 Subject: [PATCH 26/40] Delete unused variable assignments --- specs/workspace_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index afdead3d..98e58220 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -29,7 +29,6 @@ describe "select_user" do it "returns the right selected user" do input = "sopheary.chiv" - recipients = @workspace.users expect(@workspace.select_user(input)).must_equal "sopheary.chiv" end @@ -55,7 +54,6 @@ describe "select_channel" do it "returns the right selected channel" do input = "slack-api" - recipients = @workspace.users expect(@workspace.select_channel(input)).must_equal "slack-api" end From c1a156a6244609a16d2b8f1dd7ec56b6ee9fe290 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 13:44:45 -0700 Subject: [PATCH 27/40] Implement send messaage --- lib/recipient.rb | 20 ++++++++++++++++---- lib/slack.rb | 15 ++++++++++++--- lib/workspace.rb | 34 +++++++++++++++++----------------- specs/workspace_spec.rb | 38 +++++++++++++++++++------------------- 4 files changed, 64 insertions(+), 43 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index d728bb27..97bd99f8 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,8 +1,6 @@ module Slack - TOKEN = ENV["SLACK_API_TOKEN"] - class SlackError < StandardError; end class Recipient @@ -14,10 +12,24 @@ def initialize(slack_id, name) end def send_message(message) - # Implement here. + response = HTTParty.post( + "https://slack.com/api/chat.postMessage", + headers: {"Content-Type" => "application/x-www-form-urlencoded"}, + body: { + token: ENV["SLACK_API_TOKEN"], + channel: self.slack_id, + text: message, + }, + ) + + if response["ok"] + return true + else + raise SlackApi::SlackError, "Error when posting #{text} to #{channel}, error: #{response.parsed_response["error"]}" + end end - def self.get(url, token: TOKEN) + def self.get(url, token: ENV["SLACK_API_TOKEN"]) response = HTTParty.get( url, query: { diff --git a/lib/slack.rb b/lib/slack.rb index 0be53cf9..2c05f45b 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -3,7 +3,6 @@ require_relative "user" require_relative "channel" -# require_relative "../specs/test_helper" require "colorize" require "table_print" require "httparty" @@ -18,7 +17,7 @@ def main # TODO project loop do puts "What would you like to do next?" - puts "[list users] [list channels] [select user] [select channel] [details] [quit]" + puts "[list users] [list channels] [select user] [select channel] [details] [send message] [quit]" print "> " user_input = gets.chomp @@ -37,7 +36,17 @@ def main input = gets.chomp puts workspace.select_channel(input).green when "details" - workspace.show_details # This is the only method that prints things within the method. + puts "No recipient selected".red unless workspace.selected + + if workspace.selected.class == Slack::User + tp workspace.selected, :name, :real_name, :slack_id + elsif workspace.selected.class == Slack::Channel + tp workspace.selected, :name, :topic, :member_count, :slack_id + end + when "send message" + puts "Message > " + input = gets.chomp + workspace.send_message(input) end break if user_input == "quit" diff --git a/lib/workspace.rb b/lib/workspace.rb index 93732d70..49ab8a70 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,7 +1,6 @@ module Slack class Workspace - attr_reader :users, :channels - attr_accessor :selected # only needed here because of tests... that seems wrong? + attr_reader :users, :channels, :selected def initialize @users = User.list @@ -17,21 +16,22 @@ def select_user(input) select(input, users) end - def show_details(test: false) # This is the only method that prints anything. - puts "No recipient selected".red unless selected || test - - if selected.class == Slack::User - tp selected, :name, :real_name, :slack_id unless test - return selected - elsif selected.class == Slack::Channel - tp selected, :name, :topic, :member_count, :slack_id unless test - return selected - else - return nil - end - end - - def send_message + # def show_details(test: false) # This is the only method that prints anything. + # puts "No recipient selected".red unless selected || test + + # if selected.class == Slack::User + # tp selected, :name, :real_name, :slack_id unless test + # return selected + # elsif selected.class == Slack::Channel + # tp selected, :name, :topic, :member_count, :slack_id unless test + # return selected + # else + # return nil + # end + # end + + def send_message(message) + selected.send_message(message) end private diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 98e58220..54de77cb 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -76,23 +76,23 @@ end end - describe "show_details" do # don't know if this is the best way to test this - let(:user) { @workspace.users[0] } - let(:channel) { @workspace.channels[0] } - it "returns selected recipient if recipient is selected" do - @workspace.selected = user - method = @workspace.show_details(test: true) # test true stops it from printing anything - expect(method).must_equal @workspace.selected - expect(method).must_be_instance_of Slack::User - end - it "shows the details of the selected channel" do - @workspace.selected = channel - method = @workspace.show_details(test: true) - expect(method).must_equal @workspace.selected - expect(method).must_be_instance_of Slack::Channel - end - it "returns nil if no selected user" do - assert_nil(@workspace.show_details(test: true)) - end - end + # describe "show_details" do # don't know if this is the best way to test this + # let(:user) { @workspace.users[0] } + # let(:channel) { @workspace.channels[0] } + # it "returns selected recipient if recipient is selected" do + # @workspace.selected = user + # method = @workspace.show_details(test: true) # test true stops it from printing anything + # expect(method).must_equal @workspace.selected + # expect(method).must_be_instance_of Slack::User + # end + # it "shows the details of the selected channel" do + # @workspace.selected = channel + # method = @workspace.show_details(test: true) + # expect(method).must_equal @workspace.selected + # expect(method).must_be_instance_of Slack::Channel + # end + # it "returns nil if no selected user" do + # assert_nil(@workspace.show_details(test: true)) + # end + # end end From 0854acd07fbacdf909b0cf5853723ace3dcf0dba Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Fri, 22 Mar 2019 14:39:57 -0700 Subject: [PATCH 28/40] added a test to send.message --- specs/cassettes/slack_message.yml | 285 ++++++++++++++++++++++++++++++ specs/recipient_spec.rb | 7 + 2 files changed, 292 insertions(+) create mode 100644 specs/cassettes/slack_message.yml diff --git a/specs/cassettes/slack_message.yml b/specs/cassettes/slack_message.yml new file mode 100644 index 00000000..a327c517 --- /dev/null +++ b/specs/cassettes/slack_message.yml @@ -0,0 +1,285 @@ +--- +http_interactions: +- request: + method: get + uri: https://slack.com/api/users.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '911' + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 21:17:30 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 7791f565-813c-413f-8b9e-2718f4ac597c + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - users:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-wm3w + X-Cache: + - Miss from cloudfront + Via: + - 1.1 c9bb13136100bc969a43d76962ec0705.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - yaAV8n2wjixsmVPRbGYV9ECOq1QLrIJDPY5Mdy1sEo4AX__8dX4I7A== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"members":[{"id":"USLACKBOT","team_id":"TH2P32R19","name":"slackbot","deleted":false,"color":"757575","real_name":"Slackbot","tz":null,"tz_label":"Pacific + Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Slackbot","real_name_normalized":"Slackbot","display_name":"Slackbot","display_name_normalized":"Slackbot","fields":null,"status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"sv41d8cd98f0","always_active":true,"first_name":"slackbot","last_name":"","image_24":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_24.png","image_32":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_32.png","image_48":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_48.png","image_72":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_72.png","image_192":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_192.png","image_512":"https:\/\/a.slack-edge.com\/16510\/img\/slackbot_512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":0},{"id":"UH0EG0QHE","team_id":"TH2P32R19","name":"sopheary.chiv","deleted":false,"color":"4bbe2e","real_name":"Sopheary + Chiv","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Sopheary + Chiv","real_name_normalized":"Sopheary Chiv","display_name":"Sopheary Chiv","display_name_normalized":"Sopheary + Chiv","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"g3fc544bd693","image_24":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/3fc544bd693b9af7dcfb713f1cb65dc1.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0022-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":false,"is_owner":false,"is_primary_owner":false,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952644,"has_2fa":false},{"id":"UH2RGJ36Y","team_id":"TH2P32R19","name":"jessica.homet","deleted":false,"color":"9f69e7","real_name":"Jessica + Homet","tz":"America\/Los_Angeles","tz_label":"Pacific Daylight Time","tz_offset":-25200,"profile":{"title":"","phone":"","skype":"","real_name":"Jessica + Homet","real_name_normalized":"Jessica Homet","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gf6a25943381","first_name":"Jessica","last_name":"Homet","image_24":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-24.png","image_32":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-32.png","image_48":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-48.png","image_72":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-72.png","image_192":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-192.png","image_512":"https:\/\/secure.gravatar.com\/avatar\/f6a259433810cefa3237505b0b21fb51.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F00b63%2Fimg%2Favatars%2Fava_0014-512.png","status_text_canonical":"","team":"TH2P32R19"},"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1552952990}],"cache_ts":1553289450,"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Fri, 22 Mar 2019 21:17:30 GMT +- request: + method: get + uri: https://slack.com/api/channels.list?token= + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '554' + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 21:17:31 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 9d42d672-07f9-446b-ba72-d7b08fb16e9a + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - channels:read + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-6bmk + X-Cache: + - Miss from cloudfront + Via: + - 1.1 5da5773a6acab8f3aabf385b38683f20.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - ibxRZhWPGblTBoHknKXZH33jwGMopPPuMzfTNQNlMntH4-Me1S_f1Q== + body: + encoding: ASCII-8BIT + string: '{"ok":true,"channels":[{"id":"CH0EFGJHE","name":"everyone","is_channel":true,"created":1552952596,"is_archived":false,"is_general":true,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"everyone","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Company-wide + announcements and work-based matters","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"This + channel is for workspace-wide communication and announcements. All members + are in this channel.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2},{"id":"CH0EFGQ72","name":"slack-api","is_channel":true,"created":1552952597,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"slack-api","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"","creator":"","last_set":0},"purpose":{"value":"","creator":"","last_set":0},"previous_names":[],"num_members":2},{"id":"CH2P330M9","name":"random","is_channel":true,"created":1552952596,"is_archived":false,"is_general":false,"unlinked":0,"creator":"UH2RGJ36Y","name_normalized":"random","is_shared":false,"is_org_shared":false,"is_member":true,"is_private":false,"is_mpim":false,"members":["UH0EG0QHE","UH2RGJ36Y"],"topic":{"value":"Non-work + banter and water cooler conversation","creator":"UH2RGJ36Y","last_set":1552952596},"purpose":{"value":"A + place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber + you''d prefer to keep out of more focused work-related channels.","creator":"UH2RGJ36Y","last_set":1552952596},"previous_names":[],"num_members":2}],"response_metadata":{"next_cursor":""}}' + http_version: + recorded_at: Fri, 22 Mar 2019 21:17:31 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=UH0EG0QHE&text=hi + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 21:27:52 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 7c263187-3e45-43a8-9892-82e48fddba1c + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-1836 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 ccbc918e3ddfbe40c4d786475a6e7606.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - G6fwFeeBBs45xuED8LwCEKDyhFQ4e8UAY_BaNGAoAU7fXn9XI6gviw== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DH36UDYP7","ts":"1553290072.000600","message":{"type":"message","subtype":"bot_message","text":"hi","ts":"1553290072.000600","username":"Ports + - SophearyChiv - API Project","bot_id":"BH4A4C4EA"}}' + http_version: + recorded_at: Fri, 22 Mar 2019 21:27:52 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=USLACKBOT&text=hi + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 21:36:51 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 59ae8ce6-1a52-4cbe-a429-da07595cc466 + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-uyrj + X-Cache: + - Miss from cloudfront + Via: + - 1.1 7a5c063eecab094c6dee10829ad7c521.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - dYRF6BTa8g4jjCw9H4LPS_RJA8rufgSgRMZ0SdYqED7qWcf9hsKeYA== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DH36UE98D","ts":"1553290611.000100","message":{"type":"message","subtype":"bot_message","text":"hi","ts":"1553290611.000100","username":"Ports + - SophearyChiv - API Project","bot_id":"BH4A4C4EA"}}' + http_version: + recorded_at: Fri, 22 Mar 2019 21:36:51 GMT +recorded_with: VCR 4.0.0 diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 2e65dc4f..e325bf7a 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -14,6 +14,13 @@ end describe "send message" do + it "sends a valid message" do + VCR.use_cassette("slack_message") do + users = Slack::User.list + return_value = users[0].send_message("hi") + expect(return_value).must_equal true + end + end end describe "self.get" do From e64b72bf24ebb8356e3e0e8d608720092fa49b44 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 14:41:22 -0700 Subject: [PATCH 29/40] Add tp option methods to workspace --- lib/slack.rb | 15 +++++---------- lib/workspace.rb | 32 +++++++++++++++++--------------- specs/workspace_spec.rb | 20 -------------------- 3 files changed, 22 insertions(+), 45 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 2c05f45b..ae28039c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -24,25 +24,20 @@ def main case user_input.downcase when "list users" - tp workspace.users, :name, :real_name, :slack_id + tp workspace.users, workspace.tp_user_options when "list channels" - tp workspace.channels, :name, :topic, :member_count, :slack_id + tp workspace.channels, workspace.tp_channel_options when "select user" print "User ID or Username > " input = gets.chomp - puts "#{workspace.select_user(input)} selected".green + puts workspace.select_user(input) when "select channel" print "Channel ID or Channel Name > " input = gets.chomp - puts workspace.select_channel(input).green + puts workspace.select_channel(input) when "details" puts "No recipient selected".red unless workspace.selected - - if workspace.selected.class == Slack::User - tp workspace.selected, :name, :real_name, :slack_id - elsif workspace.selected.class == Slack::Channel - tp workspace.selected, :name, :topic, :member_count, :slack_id - end + tp workspace.selected, workspace.tp_details_options when "send message" puts "Message > " input = gets.chomp diff --git a/lib/workspace.rb b/lib/workspace.rb index 49ab8a70..1d386f77 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -16,19 +16,21 @@ def select_user(input) select(input, users) end - # def show_details(test: false) # This is the only method that prints anything. - # puts "No recipient selected".red unless selected || test - - # if selected.class == Slack::User - # tp selected, :name, :real_name, :slack_id unless test - # return selected - # elsif selected.class == Slack::Channel - # tp selected, :name, :topic, :member_count, :slack_id unless test - # return selected - # else - # return nil - # end - # end + def tp_details_options + if selected.class == Slack::User + return tp_user_options + elsif selected.class == Slack::Channel + return tp_channel_options + end + end + + def tp_user_options + return :name, :real_name, :slack_id + end + + def tp_channel_options + return :name, :topic, :member_count, :slack_id + end def send_message(message) selected.send_message(message) @@ -40,10 +42,10 @@ def select(input, recipients) recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient - return input + return "#{input} selected".green end end - return "#{input} does not exist" + return "#{input} does not exist".red end end end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 54de77cb..ea45ec5b 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -75,24 +75,4 @@ expect(non_exiting_user).must_equal "my-channel does not exist" end end - - # describe "show_details" do # don't know if this is the best way to test this - # let(:user) { @workspace.users[0] } - # let(:channel) { @workspace.channels[0] } - # it "returns selected recipient if recipient is selected" do - # @workspace.selected = user - # method = @workspace.show_details(test: true) # test true stops it from printing anything - # expect(method).must_equal @workspace.selected - # expect(method).must_be_instance_of Slack::User - # end - # it "shows the details of the selected channel" do - # @workspace.selected = channel - # method = @workspace.show_details(test: true) - # expect(method).must_equal @workspace.selected - # expect(method).must_be_instance_of Slack::Channel - # end - # it "returns nil if no selected user" do - # assert_nil(@workspace.show_details(test: true)) - # end - # end end From 679d9504814d872280dac8297a9a1374298a5730 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Fri, 22 Mar 2019 14:42:17 -0700 Subject: [PATCH 30/40] testing git conflicts --- specs/recipient_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e325bf7a..a2311a05 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -32,6 +32,7 @@ expect(return_value["ok"]).must_equal true end end + # sopheary it "can get valid channel data from the API" do VCR.use_cassette("recipient") do From 222abdb51e8aca74c757a1cbd218fd0000da9378 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 14:43:52 -0700 Subject: [PATCH 31/40] Add comment to test git merge --- specs/recipient_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e325bf7a..8e3737d8 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -1,4 +1,5 @@ require_relative "test_helper" +# THIS IS MY COMMENT - Jessica describe "Recipient" do let(:slack_id) { 20 } From 0177af850bdb8cefc189f87557fe48382060de8c Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Fri, 22 Mar 2019 14:46:49 -0700 Subject: [PATCH 32/40] changed slack_id to 40 --- specs/recipient_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index a2311a05..9e61827f 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -1,7 +1,7 @@ require_relative "test_helper" describe "Recipient" do - let(:slack_id) { 20 } + let(:slack_id) { 40 } let(:username) { "Sopheary" } let(:recipient) { Slack::Recipient.new(slack_id, username) } From 974f1a5ca86bb85cd7d02846ad48ccb1a7f43d56 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 14:47:57 -0700 Subject: [PATCH 33/40] Change slack_id in recipient spec to test git conflicts --- specs/recipient_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 6f713e2c..9f40bb26 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -2,7 +2,7 @@ # THIS IS MY COMMENT - Jessica describe "Recipient" do - let(:slack_id) { 20 } + let(:slack_id) { 30 } let(:username) { "Sopheary" } let(:recipient) { Slack::Recipient.new(slack_id, username) } From f6287cdf1e6923312e8be88901bb1e6deaa1809d Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 14:53:56 -0700 Subject: [PATCH 34/40] Resolve errors coming from testing conflicts --- lib/workspace.rb | 4 ++-- specs/recipient_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index 1d386f77..5bc457cd 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -42,10 +42,10 @@ def select(input, recipients) recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient - return "#{input} selected".green + return input end end - return "#{input} does not exist".red + return "#{input} does not exist" end end end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 9f40bb26..6f713e2c 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -2,7 +2,7 @@ # THIS IS MY COMMENT - Jessica describe "Recipient" do - let(:slack_id) { 30 } + let(:slack_id) { 20 } let(:username) { "Sopheary" } let(:recipient) { Slack::Recipient.new(slack_id, username) } From 24c2ae3007ebcbe08d8e874a9c784eccd6a2e28d Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 15:27:42 -0700 Subject: [PATCH 35/40] Add logic to print errors to user and modified tests --- lib/recipient.rb | 6 ++-- lib/slack.rb | 11 +++---- lib/workspace.rb | 4 +-- specs/cassettes/recipient.yml | 55 +++++++++++++++++++++++++++++++++++ specs/recipient_spec.rb | 7 +++++ specs/workspace_spec.rb | 8 ++--- 6 files changed, 77 insertions(+), 14 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 97bd99f8..75b92457 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -11,12 +11,12 @@ def initialize(slack_id, name) @name = name end - def send_message(message) + def send_message(message, token: ENV["SLACK_API_TOKEN"]) response = HTTParty.post( "https://slack.com/api/chat.postMessage", headers: {"Content-Type" => "application/x-www-form-urlencoded"}, body: { - token: ENV["SLACK_API_TOKEN"], + token: token, channel: self.slack_id, text: message, }, @@ -25,7 +25,7 @@ def send_message(message) if response["ok"] return true else - raise SlackApi::SlackError, "Error when posting #{text} to #{channel}, error: #{response.parsed_response["error"]}" + raise Slack::SlackError, "Error: #{response.parsed_response["error"]}" end end diff --git a/lib/slack.rb b/lib/slack.rb index ae28039c..fea1127c 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -30,18 +30,19 @@ def main when "select user" print "User ID or Username > " input = gets.chomp - puts workspace.select_user(input) + puts "#{input} does not exist".red unless workspace.select_user(input) when "select channel" print "Channel ID or Channel Name > " input = gets.chomp - puts workspace.select_channel(input) + puts "#{input} does not exist".red unless workspace.select_channel(input) when "details" puts "No recipient selected".red unless workspace.selected - tp workspace.selected, workspace.tp_details_options + tp workspace.selected, workspace.tp_details_options if workspace.selected when "send message" - puts "Message > " + print "Message > " input = gets.chomp - workspace.send_message(input) + puts "No message entered".red if input == "" + workspace.send_message(input) unless input == "" end break if user_input == "quit" diff --git a/lib/workspace.rb b/lib/workspace.rb index 5bc457cd..cb76d558 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -42,10 +42,10 @@ def select(input, recipients) recipients.each do |recipient| if input == recipient.slack_id || input == recipient.name @selected = recipient - return input + return @selected end end - return "#{input} does not exist" + return false end end end diff --git a/specs/cassettes/recipient.yml b/specs/cassettes/recipient.yml index d354f307..7148d714 100644 --- a/specs/cassettes/recipient.yml +++ b/specs/cassettes/recipient.yml @@ -209,4 +209,59 @@ http_interactions: string: '{"ok":false,"error":"invalid_auth"}' http_version: recorded_at: Fri, 22 Mar 2019 17:22:18 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=This-is-wrong&channel=USLACKBOT&text=hi + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 22:24:55 GMT + Server: + - Apache + Access-Control-Expose-Headers: + - x-slack-req-id + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - 937a1c33-d890-410b-9dc0-ebf9c951e38b + X-Xss-Protection: + - '0' + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-fvbn + X-Cache: + - Miss from cloudfront + Via: + - 1.1 5da5773a6acab8f3aabf385b38683f20.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - FPoN-I7YURWQbiGw7fgX5uHF56QbvgLjbJKbOSZhOXML_Q1vylrYrw== + body: + encoding: UTF-8 + string: '{"ok":false,"error":"invalid_auth"}' + http_version: + recorded_at: Fri, 22 Mar 2019 22:24:55 GMT recorded_with: VCR 4.0.0 diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index 6f713e2c..586395e4 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -22,6 +22,13 @@ expect(return_value).must_equal true end end + + it "raises an error if api sends back not okay" do + VCR.use_cassette("recipient") do + users = Slack::User.list + expect { users[0].send_message("hi", token: "This-is-wrong") }.must_raise Slack::SlackError + end + end end describe "self.get" do diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index ea45ec5b..8b8faad0 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -29,7 +29,7 @@ describe "select_user" do it "returns the right selected user" do input = "sopheary.chiv" - expect(@workspace.select_user(input)).must_equal "sopheary.chiv" + expect(@workspace.select_user(input).name).must_equal "sopheary.chiv" end it "stores the right user in the selected variable" do @@ -47,14 +47,14 @@ it "handles the non-existing user" do non_exiting_user = @workspace.select_user("jennifer.chiv") - expect(non_exiting_user).must_equal "jennifer.chiv does not exist" + expect(non_exiting_user).must_equal false end end describe "select_channel" do it "returns the right selected channel" do input = "slack-api" - expect(@workspace.select_channel(input)).must_equal "slack-api" + expect(@workspace.select_channel(input).name).must_equal "slack-api" end it "stores the right channel in the selected variable" do @@ -72,7 +72,7 @@ it "handles the non-existing channel" do non_exiting_user = @workspace.select_channel("my-channel") - expect(non_exiting_user).must_equal "my-channel does not exist" + expect(non_exiting_user).must_equal false end end end From 831aad72336225ea771ab44434592c659bf0aa07 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Fri, 22 Mar 2019 15:37:47 -0700 Subject: [PATCH 36/40] added tests for send_message in workspace --- specs/cassettes/slack_message.yml | 66 +++++++++++++++++++++++++++++++ specs/workspace_spec.rb | 9 +++++ 2 files changed, 75 insertions(+) diff --git a/specs/cassettes/slack_message.yml b/specs/cassettes/slack_message.yml index a327c517..ac9c7511 100644 --- a/specs/cassettes/slack_message.yml +++ b/specs/cassettes/slack_message.yml @@ -282,4 +282,70 @@ http_interactions: - SophearyChiv - API Project","bot_id":"BH4A4C4EA"}}' http_version: recorded_at: Fri, 22 Mar 2019 21:36:51 GMT +- request: + method: post + uri: https://slack.com/api/chat.postMessage + body: + encoding: UTF-8 + string: token=&channel=UH0EG0QHE&text=test + headers: + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Date: + - Fri, 22 Mar 2019 22:37:04 GMT + Server: + - Apache + X-Content-Type-Options: + - nosniff + X-Slack-Req-Id: + - '09c03a68-6ec2-4462-9f6d-03a7d3075d6b' + X-Oauth-Scopes: + - identify,channels:read,users:read,chat:write:bot + Expires: + - Mon, 26 Jul 1997 05:00:00 GMT + Cache-Control: + - private, no-cache, no-store, must-revalidate + Access-Control-Expose-Headers: + - x-slack-req-id + X-Xss-Protection: + - '0' + X-Accepted-Oauth-Scopes: + - chat:write:bot + Vary: + - Accept-Encoding + Pragma: + - no-cache + Access-Control-Allow-Headers: + - slack-route, x-slack-version-ts + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + Access-Control-Allow-Origin: + - "*" + X-Via: + - haproxy-www-glin + X-Cache: + - Miss from cloudfront + Via: + - 1.1 5971d213ff39e16c310a05523f08e121.cloudfront.net (CloudFront) + X-Amz-Cf-Id: + - W_SAGwqX5q57vFIVkLscmaBjQf6dty2v5WhZD9uIZ0I51rBo_-BKVQ== + body: + encoding: UTF-8 + string: '{"ok":true,"channel":"DH36UDYP7","ts":"1553294224.000700","message":{"type":"message","subtype":"bot_message","text":"test","ts":"1553294224.000700","username":"Ports + - SophearyChiv - API Project","bot_id":"BH4A4C4EA"}}' + http_version: + recorded_at: Fri, 22 Mar 2019 22:37:04 GMT recorded_with: VCR 4.0.0 diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8b8faad0..3c0541aa 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -75,4 +75,13 @@ expect(non_exiting_user).must_equal false end end + + describe "send_message" do + it "sends a valid message" do + VCR.use_cassette("slack_message") do + select_user = @workspace.select_user("sopheary.chiv") + expect(select_user.send_message("test")).must_equal true + end + end + end end From 48487a28d32436b8f8994763b75ea137eed54044 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 15:38:23 -0700 Subject: [PATCH 37/40] Add tests for tp options --- lib/workspace.rb | 8 ++++---- specs/workspace_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index cb76d558..f729da54 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -16,6 +16,10 @@ def select_user(input) select(input, users) end + def send_message(message) + selected.send_message(message) + end + def tp_details_options if selected.class == Slack::User return tp_user_options @@ -32,10 +36,6 @@ def tp_channel_options return :name, :topic, :member_count, :slack_id end - def send_message(message) - selected.send_message(message) - end - private def select(input, recipients) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 8b8faad0..2e8fba2b 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -75,4 +75,16 @@ expect(non_exiting_user).must_equal false end end + + describe "tp_details_options, tp_user_options, tp_channel_options" do + it "returns user options if selected class is a user" do + @workspace.select_user("jessica.homet") + expect(@workspace.tp_details_options).must_equal [:name, :real_name, :slack_id] + end + + it "returns channel options if selected class is a user" do + @workspace.select_channel("slack-api") + expect(@workspace.tp_details_options).must_equal [:name, :topic, :member_count, :slack_id] + end + end end From 3326d7360d3508f9736ac818dc4e65301dddb464 Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 22 Mar 2019 15:40:21 -0700 Subject: [PATCH 38/40] Modify test for sending message --- 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 e422a6af..0f108029 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -92,7 +92,7 @@ it "sends a valid message" do VCR.use_cassette("slack_message") do select_user = @workspace.select_user("sopheary.chiv") - expect(select_user.send_message("test")).must_equal true + expect(@workspace.send_message("test")).must_equal true end end end From 3188b22283e3687a33bdefd540cacba919af108e Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Fri, 22 Mar 2019 15:41:39 -0700 Subject: [PATCH 39/40] fixed send_message 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 0f108029..da564414 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -91,7 +91,7 @@ describe "send_message" do it "sends a valid message" do VCR.use_cassette("slack_message") do - select_user = @workspace.select_user("sopheary.chiv") + @workspace.select_user("sopheary.chiv") expect(@workspace.send_message("test")).must_equal true end end From 31a500998ae12b53494d565eda90d1b354150682 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 26 Mar 2019 16:36:09 -0700 Subject: [PATCH 40/40] Add SimpleCov filter --- lib/recipient.rb | 2 +- specs/test_helper.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 75b92457..d2f85f0b 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -22,7 +22,7 @@ def send_message(message, token: ENV["SLACK_API_TOKEN"]) }, ) - if response["ok"] + if response.code == 200 && response["ok"] return true else raise Slack::SlackError, "Error: #{response.parsed_response["error"]}" diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 13e9b914..44c536fe 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -1,5 +1,7 @@ require "simplecov" -SimpleCov.start +SimpleCov.start do + add_filter %r{^/specs/} +end require "minitest" require "minitest/autorun"