diff --git a/.gitignore b/.gitignore index 3ff4fada..ceddff23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ # Ignore environemnt variables .env + diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..a5c0b325 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,30 @@ +require_relative 'recipient.rb' +require 'table_print' + +module SlackCLI + class Channel < Recipient + attr_reader :channel_name, :slack_id, :member_count, :topic + + def initialize(channel_name, slack_id, member_count, topic) + super(slack_id) + + @channel_name = channel_name + @member_count = member_count + @topic = topic + end + + def details + return "Channel name: #{@channel_name}\nSlack ID: #{@slack_id}\nTopic: #{@topic}\nMember count: #{@member_count}" + end + + # Will retrieve the data from the API using the URL + def self.list_all + response = super('conversations.list') + channels = [] + response["channels"].each do |channel| + channels << Channel.new(channel["name"], channel["id"], channel["num_members"].to_i, channel["topic"]["value"]) + end + return channels + end + end +end \ No newline at end of file diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..c1e886d2 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,42 @@ +require 'httparty' +require 'dotenv' + +Dotenv.load + +TOKEN = ENV['SLACK_TOKEN'] +URL = "https://slack.com/api/" + +module SlackCLI + class Recipient + + attr_reader :slack_id + + def initialize(slack_id) + @slack_id = slack_id + end + + def self.list_all(list) + url = "#{URL}"<<"#{list}" + query_params = {token: TOKEN} + response = HTTParty.get(url, query: query_params) + return response + end + + def send_message(message) + url = "#{URL}chat.postMessage" + + response = HTTParty.post(url, + headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, + body: { + token: TOKEN, + channel: @slack_id, + text: message + }) + unless response.code == 200 + raise ArgumentError.new("Request Error") + end + return true + end + + end +end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..9e38c442 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,12 +1,80 @@ #!/usr/bin/env ruby +require 'table_print' +require_relative 'workspace' def main puts "Welcome to the Ada Slack CLI!" - workspace = Workspace.new + workspace = SlackCLI::Workspace.new - # TODO project + options_list + choice = get_user_choice + execute_choice(workspace, choice) puts "Thank you for using the Ada Slack CLI" end +def options_list + print "Choose from the following:\n" + puts "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" +end + +def get_user_choice + user_choice = gets.chomp + options = ["list users", "list channels", "quit", "select user", "select channel", "details", "send message"] + + until options.include?(user_choice) + puts "Invalid option. Please type: list users, list channels, select user, select channel, details, send message or quit" + user_choice = gets.chomp + end + + return user_choice +end + +def execute_choice(workspace, choice) + given_data = nil + until choice == "quit" + if choice == "list users" + tp workspace.users.list, "username", "slack_id", "real_name" + puts "\n" + elsif choice == "list channels" + tp workspace.channels_list, "channel_name", "slack_id", "topic", "member_count" + elsif choice == "select user" + print "Please enter username or Slack ID: " + user_info = gets.chomp + given_data = workspace.select_user(user_info) + if given_data.nil? + puts "User not found" + end + elsif choice == "select channel" + print "Please select channel name or Slack ID: " + channel_info = gets.chomp + given_data = workspace.select_channel(channel_info) + if given_data.nil? + puts "Channel not found." + end + elsif choice == "details" + if given_data != nil + puts given_data.details + elsif given_data != nil + puts given_data.details + else + puts "There is no user or channel that matches your search" + end + elsif choice == "send message" + if given_data.nil? + puts "Cannot send message. Please choose a recipient first." + else + puts "Please type your message:" + user_msg = gets.chomp + given_data.send_message(user_msg) + puts "Success!" + end + end + + puts "*****\n\n" + options_list + choice = get_user_choice + end +end + main if __FILE__ == $PROGRAM_NAME \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..ea49c10c --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,35 @@ +require_relative 'recipient' +require 'httparty' +require 'dotenv' + +Dotenv.load + +module SlackCLI + class User < Recipient + attr_reader :username, :slack_id, :real_name + + def initialize(username, slack_id, real_name) + super (slack_id) + + @username = username + @real_name = real_name + end + + def details + return "username: #{@username}\nSlack ID: #{@slack_id}\nreal name: #{@real_name}\n" + end + + #will retrieve users data from the API using URL + def self.list_all + response = super('users.list') + users = [] + response["members"].each do |user| + users << User.new(user["name"], user["id"], user["real_name"]) + end + return users + end + + end +end + + diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..c81ed0c7 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,51 @@ +require_relative 'channel.rb' +require_relative 'user.rb' + +module SlackCLI + class Workspace + + attr_reader :users, :channels + + def initialize + @users = User.list_all + @channels= Channel.list_all + end + + def users_list + return @users + end + + def channels_list + return @channels + end + + def select_user(user_info) + name_search = @users.find{|user| user.username == user_info} + id_search = @users.find{|user| user.slack_id == user_info} + + if name_search.nil? + @selected_user = id_search + elsif id_search.nil? + @selected_user = name_search + else + puts "Invalid User name or ID" + end + return @selected_user + end + + def select_channel(channel_info) + name_search = @channels.find{|channel| channel.channel_name == channel_info} + id_search = @channels.find{|channel| channel.slack_id == channel_info} + + if name_search.nil? + @selected_channel = id_search + elsif id_search.nil? + @selected_channel = name_search + else + puts "Invalid Channel name or ID" + end + return @selected_channel + end + + end +end \ No newline at end of file diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..98a08f9e --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,22 @@ +require_relative 'test_helper.rb' + +describe "class Channel" do + before do + @channel = SlackCLI::Channel.new("seattle-stuff", "U786YWDVC3D", 71, "coffee shops") + end + + it "creates an instance of channel" do + expect(@channel).must_be_kind_of SlackCLI::Channel + end + + it "returns channel details correctly" do + expect(@channel.details).must_be_kind_of String + end + + it "returns a list of channels" do + VCR.use_cassette "channels_list" do + response = SlackCLI::Channel.list_all + expect(response).must_be_kind_of Array + end + end +end \ No newline at end of file diff --git a/test/recipient_test.rb b/test/recipient_test.rb new file mode 100644 index 00000000..af136fdf --- /dev/null +++ b/test/recipient_test.rb @@ -0,0 +1,20 @@ +require_relative 'test_helper.rb' + +describe "Recipient class" do + + it "stores id correctly" do + recipient_id = "GTB29AAZ" + recipient = SlackCLI::Recipient.new(recipient_id) + expect(recipient.slack_id).must_equal recipient_id + end + + it "retrieves data from chosen list" do + VCR.use_cassette "list_all" do + response = SlackCLI::Recipient.list_all("conversations.list") + expect(response.code).must_equal 200 + end + end + + + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 1fcf2bab..8212f7bb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,14 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require 'vcr' +require 'dotenv' + +Dotenv.load + +require_relative '../lib/workspace' +require_relative '../lib/user' +require_relative '../lib/channel' +require_relative '../lib/recipient' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -25,5 +33,8 @@ } # Don't leave our token lying around in a cassette file. + config.filter_sensitive_data("SLACK_TOKEN") do + ENV["SLACK_TOKEN"] + end end diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..2c74a9cc --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,24 @@ +require_relative 'test_helper' + +describe "User Class" do + before do + @users = SlackCLI::User.new("ruby", "PM15QINBXFZ", "ruby mine") + end + + it "Creates an instance of Users" do + expect(@users).must_be_kind_of SlackCLI::User + end + + it "Return user details correctly" do + expect(@users.details).must_be_kind_of String + expect(@users.username).must_equal "ruby" + expect(@users.slack_id).must_equal "PM15QINBXFZ" + end + + it "Returns a list of users" do + VCR.use_cassette "user_list" do + result = SlackCLI::User.list_all + expect(result).must_be_kind_of Array + end + end +end \ No newline at end of file diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..f39e4abc --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,53 @@ +require_relative 'test_helper' + +describe "Workspace class" do + + it "returns a list of users correctly" do + VCR.use_cassette("users_list") do + response = SlackCLI::Workspace.new + expect(response.users_list).must_be_kind_of Array + end + end + + it "returns a list of channels correctly" do + VCR.use_cassette("channels_list") do + response = SlackCLI::Workspace.new + expect(response.channels_list).must_be_kind_of Array + end + end +end + + + describe "select user or channel" do + + it "selects user correctly" do + VCR.use_cassette("select_user") do + response = SlackCLI::Workspace.new + user = response.select_user("drocha") + expect(user).must_be_kind_of SlackCLI::User + end + end + + it "selects channel correctly" do + VCR.use_cassette("select channel") do + response = SlackCLI::Workspace.new + selected_channel = response.select_channel("seattle-stuff") + expect(selected_channel).must_be_kind_of SlackCLI::Channel + end + end + + + it "returns nil if input is invalid" do + VCR.use_cassette("user_not_found") do + response = SlackCLI::Workspace.new + selected_user = response.select_user("Paris") + expect(selected_user).must_be_nil + end + + VCR.use_cassette("channel_not_found") do + response = SlackCLI::Workspace.new + selected_channel = response.select_channel("breakfast") + expect(selected_channel).must_be_nil + end + end + end \ No newline at end of file