From 36c8f5e4f90d431f507c5308c96a360f4b0b9265 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Tue, 19 Mar 2019 15:18:34 -0700 Subject: [PATCH 01/15] create files --- lib/channel.rb | 0 lib/recipient.rb | 0 lib/user.rb | 0 lib/workspace.rb | 0 specs/ENV.rb | 1 + specs/channel_spec.rb | 0 specs/recipient_spec.rb | 0 specs/user_spec.rb | 0 specs/workspace_spec.rb | 0 9 files changed, 1 insertion(+) create mode 100644 lib/channel.rb create mode 100644 lib/recipient.rb create mode 100644 lib/user.rb create mode 100644 lib/workspace.rb create mode 100644 specs/ENV.rb create mode 100644 specs/channel_spec.rb create mode 100644 specs/recipient_spec.rb create mode 100644 specs/user_spec.rb create mode 100644 specs/workspace_spec.rb diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/ENV.rb b/specs/ENV.rb new file mode 100644 index 00000000..d5f99795 --- /dev/null +++ b/specs/ENV.rb @@ -0,0 +1 @@ +KEY=ImUF5aeQbf74EukMuzmWI0Mm diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..e69de29b From e2806c612cacc4594ced2ff65db32f76ea25c5f5 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Tue, 19 Mar 2019 15:52:53 -0700 Subject: [PATCH 02/15] skeleton added to files --- lib/channel.rb | 12 ++++++++++++ lib/recipient.rb | 20 ++++++++++++++++++++ lib/user.rb | 13 +++++++++++++ lib/workspace.rb | 21 +++++++++++++++++++++ specs/recipient_spec.rb | 1 + 5 files changed, 67 insertions(+) diff --git a/lib/channel.rb b/lib/channel.rb index e69de29b..632e3f11 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -0,0 +1,12 @@ +class Channel + +@topic = topic +@member_count = member_count + +def details + #code here +end + +def self.list + #code here +end diff --git a/lib/recipient.rb b/lib/recipient.rb index e69de29b..64e5f89e 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -0,0 +1,20 @@ +class Recipent + + @slack_id = slack_id + @name = name + + def send_message(message) + #code here + end + + def self.get(url, params) + #code here + end + + def details + #code here + end + + def self.list + #code here + end diff --git a/lib/user.rb b/lib/user.rb index e69de29b..cde01586 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -0,0 +1,13 @@ +class User + + @real_name = real_name + #@status_text = status_text + #@status_emoji = status_emoji + + def details do + #code here + end + + def self.list + #code here + end diff --git a/lib/workspace.rb b/lib/workspace.rb index e69de29b..d744a64b 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -0,0 +1,21 @@ +class Workspace + +@users = users +@channel = channel +@selected = selected + +def select_channel + #code +end + +def select_user + #code +end + +def show_details + #code +end + +def send_message + #code +end diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb index e69de29b..8b137891 100644 --- a/specs/recipient_spec.rb +++ b/specs/recipient_spec.rb @@ -0,0 +1 @@ + From f17073cc08e25c1ea16b077f307e01736b6f6548 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Wed, 20 Mar 2019 21:46:32 -0700 Subject: [PATCH 03/15] added channels --- lib/slack.rb | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/slack.rb b/lib/slack.rb index 960cf2f7..5f113992 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,11 +1,37 @@ -#!/usr/bin/env ruby +require "httparty" +require "dotenv" +require_relative "./user" +require_relative "./channel" -def main - puts "Welcome to the Ada Slack CLI!" - # TODO project +response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') +puts response.body, response.code, response.message, response.headers.inspect - puts "Thank you for using the Ada Slack CLI" -end +https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1 +class Slack + include HTTParty + base_uri 'slack.com/api' + + def initialize(api_token) + @options = { query: { token: api_token, pretty: 1 } } + end + + def channels + response = self.class.get("/channels.list", @options).parsed_response + raise 'Failed to get channels' unless response['ok'] + channels = response['channels'].map { |channel_map| + Channel.new(channel_map) + } + return channels + end -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file + def users + response = self.class.get("/users.list", @options).parsed_response + raise 'Failed to get users' unless response['ok'] + users = response['members'].map { |user_map| + User.new(user_map) + } + return users + end + +end From cce321684416ed8620bdbfa73b3fa358185825b6 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Thu, 21 Mar 2019 15:18:16 -0700 Subject: [PATCH 04/15] initialized channel --- lib/channel.rb | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 632e3f11..85b67437 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -1,12 +1,23 @@ -class Channel +require 'table_print' +require "httparty" +require "dotenv" -@topic = topic -@member_count = member_count +class Channel + # @topic = topic + # @member_count = member_count -def details - #code here -end + def initialize(channel_map) + @id = channel_map['id'] + @topic = channel_map['topic']['value'] + @member_count = channel_map['num_members'] + @name = channel_map['name'] + end -def self.list - #code here + # def details + # #code here + # end + # + # def self.list + # #code here + # end end From b866ff124410be42211cfb5d776e2bdf48655fdb Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Thu, 21 Mar 2019 15:36:35 -0700 Subject: [PATCH 05/15] initialized user --- lib/user.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index cde01586..3c38dd59 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,13 +1,17 @@ class User - - @real_name = real_name - #@status_text = status_text - #@status_emoji = status_emoji - - def details do - #code here + def initialize(user_map) + @username = user_map['display_name'] + @real_name = user_map['real_name'] + @slack_id = user_map['id'] + # @status_text = user_map['status_text'] + # @status_emoji = user_map['status_emoji'] end - def self.list - #code here - end + # def details do + # #code here + # end + # + # def self.list + # #code here + # end +end From c2c16df7ee2a68cc6e4fa03e979a7cfbfb7d0907 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Thu, 21 Mar 2019 16:19:48 -0700 Subject: [PATCH 06/15] recipient raise please implement me --- lib/recipient.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/recipient.rb b/lib/recipient.rb index 64e5f89e..957e8e79 100644 --- a/lib/recipient.rb +++ b/lib/recipient.rb @@ -1,5 +1,8 @@ -class Recipent +require 'dotenv' +require 'httparty' +Dotenv.load +class Recipient @slack_id = slack_id @name = name @@ -7,14 +10,15 @@ def send_message(message) #code here end - def self.get(url, params) - #code here - end + # def self.get(url, params) + # #code here + # end def details - #code here + raise 'Please implement me' end def self.list - #code here + raise 'Please implement me' end +end From c281e2e9e27b798d5960455aa9e77fd8dbb64c2f Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Thu, 21 Mar 2019 19:44:13 -0700 Subject: [PATCH 07/15] quit and list channels working --- slack-cli | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 slack-cli diff --git a/slack-cli b/slack-cli new file mode 100755 index 00000000..6c96c91e --- /dev/null +++ b/slack-cli @@ -0,0 +1,43 @@ +#! /usr/bin/env ruby +require "dotenv" +require_relative "./lib/slack" + +$time_to_go = false + +Dotenv.load(".env") +$slack = Slack.new(ENV['SLACK_API_TOKEN']) + +# puts slack.users + +def quit() + $time_to_go = true +end + +def list_users + puts 'yup, list users' + #code +end + +def list_channels + puts $slack.channels +end + +commands = { + 'exit' => method(:quit), + 'quit' => method(:quit), + 'list users' => method(:list_users), + 'list channels' => method(:list_channels) +} + +while !$time_to_go do + puts "Please enter one of the available commands: #{commands.keys}" + print 'Command: ' + command = gets.chomp + if commands.key? command + commands[command].call + else + puts 'You silly goose! That is no command!' + end +end + +puts 'Toodles!' From 306eaab995743fb7e45a7bce0c37b2b4b0eaf5cd Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Thu, 21 Mar 2019 21:42:27 -0700 Subject: [PATCH 08/15] list users working --- slack-cli | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slack-cli b/slack-cli index 6c96c91e..891b5c2c 100755 --- a/slack-cli +++ b/slack-cli @@ -1,4 +1,5 @@ #! /usr/bin/env ruby +#shebang line. require "dotenv" require_relative "./lib/slack" @@ -14,8 +15,7 @@ def quit() end def list_users - puts 'yup, list users' - #code + puts $slack.users end def list_channels @@ -23,10 +23,10 @@ def list_channels end commands = { + 'list users' => method(:list_users), + 'list channels' => method(:list_channels), 'exit' => method(:quit), 'quit' => method(:quit), - 'list users' => method(:list_users), - 'list channels' => method(:list_channels) } while !$time_to_go do From c9e7a7e290b17d50a19f32544bbbc66a7223691e Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Fri, 22 Mar 2019 11:37:47 -0700 Subject: [PATCH 09/15] added workspace --- lib/workspace.rb | 61 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/workspace.rb b/lib/workspace.rb index d744a64b..6399b8cb 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,21 +1,52 @@ +require "httparty" +require "dotenv" +require_relative "./user" +require_relative "./channel" + +#response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') +#puts response.body, response.code, response.message, response.headers.inspect + +# https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1 class Workspace + include HTTParty + base_uri 'slack.com/api' -@users = users -@channel = channel -@selected = selected + def initialize(api_token) + @options = { query: { token: api_token, pretty: 1 } } + end -def select_channel - #code -end + def channels + response = self.class.get("/channels.list", @options).parsed_response + raise RuntimeError.new('Failed to get channels') unless response['ok'] + channels = response['channels'].map { |channel_map| + Channel.new(channel_map) + } + return channels + end -def select_user - #code -end + def users + response = self.class.get("/users.list", @options).parsed_response + raise RuntimeError.new('Failed to get users') unless response['ok'] + users = response['members'].map { |user_map| + User.new(user_map) + } + return users + end -def show_details - #code -end + # def select_channel + # #code + # end + # + # def select_user + # #code + # end + # + # def show_details + # #code + # end + # + # def send_message + # #code + # end -def send_message - #code -end +end From 360cd914b758ac96d6a9744bd74762a7ae92a7cb Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Fri, 22 Mar 2019 11:39:38 -0700 Subject: [PATCH 10/15] added 2 tests for wave 1, both passing --- specs/workspace_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index e69de29b..60428c21 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -0,0 +1,28 @@ +require "dotenv" + +require_relative './test_helper' +require_relative '../lib/workspace' + +describe "workspace" do + before do + Dotenv.load('.env') + api_token = ENV['SLACK_API_TOKEN'] + @workspace = Workspace.new(api_token) + end + + describe "channels" do + it "must be instance of array" do + VCR.use_cassette("good channels") do + channels = @workspace.channels + expect(channels).must_be_kind_of Array + end + end + + it "must raise exception for bad response" do + workspace = Workspace.new('badapitoken') + VCR.use_cassette("bad channels") do + expect{workspace.channels}.must_raise RuntimeError + end + end + end +end From e35daafd32428ae36918ce324e2ebc534d5b522f Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Fri, 22 Mar 2019 11:46:46 -0700 Subject: [PATCH 11/15] added last passing test for wave1, wave1 complete --- specs/workspace_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 60428c21..7c63e78c 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -18,6 +18,14 @@ end end + describe "users" do + it "must be instance of array" do + VCR.use_cassette("good users") do + users = @workspace.users + expect(users).must_be_kind_of Array + end + end + it "must raise exception for bad response" do workspace = Workspace.new('badapitoken') VCR.use_cassette("bad channels") do @@ -25,4 +33,5 @@ end end end + end end From 7702f169b3bc5a70383e4f659574b3152841d072 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Fri, 22 Mar 2019 18:47:09 -0700 Subject: [PATCH 12/15] wave 2 progress and tests --- .gitignore | 1 + lib/channel.rb | 10 ++++----- lib/slack-cli.rb | 40 ++++++++++++++++++++++++++++++++++++ lib/slack.rb | 37 --------------------------------- lib/user.rb | 7 ++++++- lib/workspace.rb | 45 ++++++++++++++++++++++++++++++----------- slack-cli | 44 +++++----------------------------------- specs/test_helper.rb | 2 +- specs/workspace_spec.rb | 33 ++++++++++++++++++++++++------ 9 files changed, 118 insertions(+), 101 deletions(-) create mode 100755 lib/slack-cli.rb delete mode 100755 lib/slack.rb diff --git a/.gitignore b/.gitignore index 8d6a243f..443e046c 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ build-iPhoneSimulator/ # Ignore cassette files /specs/cassettes/ +.DS_Store diff --git a/lib/channel.rb b/lib/channel.rb index 85b67437..273ff2ff 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -14,10 +14,10 @@ def initialize(channel_map) end # def details - # #code here - # end - # - # def self.list - # #code here + # return "slack id: #{@id} #{@topic} member count: #{@member_count}" # end + + def to_s + "Name:#{@name},Topic:#{@topic},Member count :#{@member_count},Slack ID: #{@id}" + end end diff --git a/lib/slack-cli.rb b/lib/slack-cli.rb new file mode 100755 index 00000000..a90381c7 --- /dev/null +++ b/lib/slack-cli.rb @@ -0,0 +1,40 @@ +require_relative "./workspace" + +class SlackCLI + def initialize(api_token) + @commands = { + 'list users' => self.method(:list_users), + 'list channels' => self.method(:list_channels), + 'exit' => self.method(:quit), + 'quit' => self.method(:quit), + } + @time_to_go = false + @workspace = Workspace.new(api_token) + end + + def quit + @time_to_go = true + end + + def list_users + puts @workspace.users + end + + def list_channels + puts @workspace.channels + end + + def run + while !@time_to_go do + puts "Please enter one of the available commands: #{@commands.keys}" + print 'Command: ' + command = gets.chomp + if @commands.key? command + @commands[command].call + else + puts 'You silly goose! That is no command!' + end + end + puts 'Toodles!' + end +end diff --git a/lib/slack.rb b/lib/slack.rb deleted file mode 100755 index 5f113992..00000000 --- a/lib/slack.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "httparty" -require "dotenv" -require_relative "./user" -require_relative "./channel" - - -response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') -puts response.body, response.code, response.message, response.headers.inspect - -https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1 -class Slack - include HTTParty - base_uri 'slack.com/api' - - def initialize(api_token) - @options = { query: { token: api_token, pretty: 1 } } - end - - def channels - response = self.class.get("/channels.list", @options).parsed_response - raise 'Failed to get channels' unless response['ok'] - channels = response['channels'].map { |channel_map| - Channel.new(channel_map) - } - return channels - end - - def users - response = self.class.get("/users.list", @options).parsed_response - raise 'Failed to get users' unless response['ok'] - users = response['members'].map { |user_map| - User.new(user_map) - } - return users - end - -end diff --git a/lib/user.rb b/lib/user.rb index 3c38dd59..2c2657b8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,6 +1,7 @@ class User + attr_accessor :username, :real_name, :slack_id def initialize(user_map) - @username = user_map['display_name'] + @username = user_map['name'] @real_name = user_map['real_name'] @slack_id = user_map['id'] # @status_text = user_map['status_text'] @@ -14,4 +15,8 @@ def initialize(user_map) # def self.list # #code here # end + + def to_s + "Username: #{@username}, Real name: #{@real_name}, Slack ID: #{@slack_id}" + end end diff --git a/lib/workspace.rb b/lib/workspace.rb index 6399b8cb..23d2d332 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -33,20 +33,41 @@ def users return users end - # def select_channel - # #code - # end - # - # def select_user - # #code - # end - # - # def show_details + #DRY this to do "or" statements + def select_channel(value) + @channels.each do |channel| + if channel.name == value + @selected = channel + return channel + elsif channel.slack_id == value + @selected = channel + return channel + end + end + return nil + end + + + #DRY this to do "or" statements + def select_user(value) + users.each do |user| + if user.username == value + @selected = user + return user + elsif user.slack_id == value + @selected = user + return user + end + end + return nil + end + end + + + # def show_details(details) # #code # end - # + # def send_message # #code # end - -end diff --git a/slack-cli b/slack-cli index 891b5c2c..56087527 100755 --- a/slack-cli +++ b/slack-cli @@ -1,43 +1,9 @@ #! /usr/bin/env ruby #shebang line. require "dotenv" -require_relative "./lib/slack" +require_relative 'lib/slack-cli' -$time_to_go = false - -Dotenv.load(".env") -$slack = Slack.new(ENV['SLACK_API_TOKEN']) - -# puts slack.users - -def quit() - $time_to_go = true -end - -def list_users - puts $slack.users -end - -def list_channels - puts $slack.channels -end - -commands = { - 'list users' => method(:list_users), - 'list channels' => method(:list_channels), - 'exit' => method(:quit), - 'quit' => method(:quit), -} - -while !$time_to_go do - puts "Please enter one of the available commands: #{commands.keys}" - print 'Command: ' - command = gets.chomp - if commands.key? command - commands[command].call - else - puts 'You silly goose! That is no command!' - end -end - -puts 'Toodles!' +Dotenv.load('.env') +api_token = ENV['SLACK_API_TOKEN'] +cli = SlackCLI.new(api_token) +cli.run() diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..e23ea304 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -12,4 +12,4 @@ VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock -end \ No newline at end of file +end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 7c63e78c..89299ab4 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -17,14 +17,15 @@ expect(channels).must_be_kind_of Array end end + end - describe "users" do - it "must be instance of array" do - VCR.use_cassette("good users") do - users = @workspace.users - expect(users).must_be_kind_of Array - end + describe "users" do + it "must be instance of array" do + VCR.use_cassette("good users") do + users = @workspace.users + expect(users).must_be_kind_of Array end + end it "must raise exception for bad response" do workspace = Workspace.new('badapitoken') @@ -33,5 +34,25 @@ end end end + + describe "select_user" do + it "finds a user by username" do + VCR.use_cassette("select user") do + # expect{workspace.select_user}.must_be + # arrange + user_name = "kateannnichols" + # act + selected_user = @workspace.select_user(user_name) + # assert + expect(selected_user.username).must_equal 'kateannnichols' + end + end + it "finds a user by id" do + VCR.use_cassette("select user") do + slack_id = "UH2SA7YJE" + selected_user = @workspace.select_user(slack_id) + expect(selected_user.slack_id).must_equal 'UH2SA7YJE' + end + end end end From 426b9e4b3d86b52026f2154f4b45e2204898e0c1 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Sat, 23 Mar 2019 14:30:40 -0700 Subject: [PATCH 13/15] wave 2 tested select channels method --- lib/channel.rb | 1 + lib/user.rb | 6 +++--- lib/workspace.rb | 6 +++--- specs/workspace_spec.rb | 29 +++++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/channel.rb b/lib/channel.rb index 273ff2ff..c898a556 100644 --- a/lib/channel.rb +++ b/lib/channel.rb @@ -3,6 +3,7 @@ require "dotenv" class Channel + attr_reader :name, :id, :topic, :member_count # @topic = topic # @member_count = member_count diff --git a/lib/user.rb b/lib/user.rb index 2c2657b8..8e8bdb8d 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,9 +1,9 @@ class User - attr_accessor :username, :real_name, :slack_id + attr_accessor :username, :real_name, :id def initialize(user_map) @username = user_map['name'] @real_name = user_map['real_name'] - @slack_id = user_map['id'] + @id = user_map['id'] # @status_text = user_map['status_text'] # @status_emoji = user_map['status_emoji'] end @@ -17,6 +17,6 @@ def initialize(user_map) # end def to_s - "Username: #{@username}, Real name: #{@real_name}, Slack ID: #{@slack_id}" + "Username: #{@username}, Real name: #{@real_name}, Slack ID: #{@id}" end end diff --git a/lib/workspace.rb b/lib/workspace.rb index 23d2d332..c75b04d6 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -35,11 +35,11 @@ def users #DRY this to do "or" statements def select_channel(value) - @channels.each do |channel| + channels.each do |channel| if channel.name == value @selected = channel return channel - elsif channel.slack_id == value + elsif channel.id == value @selected = channel return channel end @@ -54,7 +54,7 @@ def select_user(value) if user.username == value @selected = user return user - elsif user.slack_id == value + elsif user.id == value @selected = user return user end diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb index 89299ab4..d0cb7287 100644 --- a/specs/workspace_spec.rb +++ b/specs/workspace_spec.rb @@ -19,6 +19,27 @@ end end + describe "select_channel" do + it "finds a chanel by channel name" do + VCR.use_cassette("select channel by name") do + # expect{workspace.select_user}.must_be + # arrange + channel_name = "random" + # act + selected_channel = @workspace.select_channel(channel_name) + # assert + expect(selected_channel.name).must_equal 'random' + end + end + + it "finds a channel by id" do + VCR.use_cassette("select channel by id") do + id = "CH36Q9YKX" + selected_channel = @workspace.select_channel(id) + expect(selected_channel.id).must_equal 'CH36Q9YKX' + end + end + describe "users" do it "must be instance of array" do VCR.use_cassette("good users") do @@ -38,7 +59,6 @@ describe "select_user" do it "finds a user by username" do VCR.use_cassette("select user") do - # expect{workspace.select_user}.must_be # arrange user_name = "kateannnichols" # act @@ -49,9 +69,10 @@ end it "finds a user by id" do VCR.use_cassette("select user") do - slack_id = "UH2SA7YJE" - selected_user = @workspace.select_user(slack_id) - expect(selected_user.slack_id).must_equal 'UH2SA7YJE' + id = "UH2SA7YJE" + selected_user = @workspace.select_user(id) + expect(selected_user.id).must_equal 'UH2SA7YJE' + end end end end From 33eead27de44d5dc79fa2841002e0b2d7649cc17 Mon Sep 17 00:00:00 2001 From: Chantal Demissie Date: Sat, 23 Mar 2019 16:26:14 -0700 Subject: [PATCH 14/15] "wave 2 refactored code and select user + channel options running" --- lib/slack-cli.rb | 20 ++++++++++++++ lib/workspace.rb | 70 +++++++++++++++++++++--------------------------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/lib/slack-cli.rb b/lib/slack-cli.rb index a90381c7..8f181021 100755 --- a/lib/slack-cli.rb +++ b/lib/slack-cli.rb @@ -5,6 +5,8 @@ def initialize(api_token) @commands = { 'list users' => self.method(:list_users), 'list channels' => self.method(:list_channels), + 'select user' => self.method(:select_user), + 'select channel' => self.method(:select_channel), 'exit' => self.method(:quit), 'quit' => self.method(:quit), } @@ -24,6 +26,24 @@ def list_channels puts @workspace.channels end + def select_user + print "which user? " + user = gets.chomp + selected_user = @workspace.select_user(user) + if selected_user.nil? + puts "thats not a user try again" + end + end + + def select_channel + print "which channel? " + channel = gets.chomp + selected_channel = @workspace.select_channel(channel) + if selected_channel.nil? + puts "thats not a user try again" + end + end + def run while !@time_to_go do puts "Please enter one of the available commands: #{@commands.keys}" diff --git a/lib/workspace.rb b/lib/workspace.rb index c75b04d6..6961ecb7 100644 --- a/lib/workspace.rb +++ b/lib/workspace.rb @@ -1,10 +1,10 @@ -require "httparty" -require "dotenv" -require_relative "./user" -require_relative "./channel" +require 'httparty' +require 'dotenv' +require_relative './user' +require_relative './channel' -#response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') -#puts response.body, response.code, response.message, response.headers.inspect +# response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') +# puts response.body, response.code, response.message, response.headers.inspect # https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1 class Workspace @@ -16,53 +16,44 @@ def initialize(api_token) end def channels - response = self.class.get("/channels.list", @options).parsed_response - raise RuntimeError.new('Failed to get channels') unless response['ok'] - channels = response['channels'].map { |channel_map| + response = self.class.get('/channels.list', @options).parsed_response + raise 'Failed to get channels' unless response['ok'] + + channels = response['channels'].map do |channel_map| Channel.new(channel_map) - } - return channels + end + channels end def users - response = self.class.get("/users.list", @options).parsed_response - raise RuntimeError.new('Failed to get users') unless response['ok'] - users = response['members'].map { |user_map| + response = self.class.get('/users.list', @options).parsed_response + raise 'Failed to get users' unless response['ok'] + + users = response['members'].map do |user_map| User.new(user_map) - } - return users + end + users end - #DRY this to do "or" statements - def select_channel(value) - channels.each do |channel| - if channel.name == value - @selected = channel - return channel - elsif channel.id == value + def select_channel(selected_channel) + channels.each do |channel| + if channel.name == selected_channel || channel.id == selected_channel @selected = channel return channel end end - return nil + nil end - - #DRY this to do "or" statements - def select_user(value) + def select_user(selected_user) users.each do |user| - if user.username == value - @selected = user - return user - elsif user.id == value - @selected = user - return user - end - end - return nil - end - end - + if user.username == selected_user || user.id == selected_user + @selected = user + return user + end + end + nil + end # def show_details(details) # #code @@ -71,3 +62,4 @@ def select_user(value) # def send_message # #code # end +end From 5f41b8ef70b33aa7c425f2b3bde961b1a8992293 Mon Sep 17 00:00:00 2001 From: Chantal Demissie <42252976+ChantalDemissie@users.noreply.github.com> Date: Sat, 23 Mar 2019 16:43:01 -0700 Subject: [PATCH 15/15] Delete ENV.rb --- specs/ENV.rb | 1 - 1 file changed, 1 deletion(-) delete mode 100644 specs/ENV.rb diff --git a/specs/ENV.rb b/specs/ENV.rb deleted file mode 100644 index d5f99795..00000000 --- a/specs/ENV.rb +++ /dev/null @@ -1 +0,0 @@ -KEY=ImUF5aeQbf74EukMuzmWI0Mm