Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
afcafe0
created class Channel
katemyer Mar 12, 2020
6b25b8e
created Recipient class
katemyer Mar 12, 2020
e300c71
added require_relative 'workspace' to slack.rb
katemyer Mar 12, 2020
0c5f83b
added filter sensitive data w/ slack token
katemyer Mar 12, 2020
b2ca9b8
added Workspace class
katemyer Mar 12, 2020
21164d1
added User class and load_all method
katemyer Mar 12, 2020
418394b
added CLI for list users
katemyer Mar 12, 2020
8801092
added load all method to Channel
katemyer Mar 12, 2020
8fffeb9
completed check to list channels
katemyer Mar 12, 2020
937d701
loaded Channels
katemyer Mar 12, 2020
7fb66aa
commented test code
katemyer Mar 12, 2020
f28355b
added private load all method in recipient class
katemyer Mar 12, 2020
91cd7c2
added tests to channels
katemyer Mar 13, 2020
1355f47
added tests to users
katemyer Mar 13, 2020
ad3defd
added SlackAPIError < Exception class per Devin
katemyer Mar 13, 2020
1c310b6
added shell for needed methods per Devin
katemyer Mar 13, 2020
594ab25
added require 'dotenv'
katemyer Mar 13, 2020
93c9800
added select user prompt functions
katemyer Mar 15, 2020
f295d30
added format to make it clearer for class methods
katemyer Mar 15, 2020
99ef2e1
added format to make class methods clearer
katemyer Mar 15, 2020
112a96a
added selected, methods to workspace
katemyer Mar 15, 2020
bbe533d
added methods for valid inputs, find user, find chnnl
katemyer Mar 15, 2020
952bc81
methods for select chnnl, details, send msg
katemyer Mar 15, 2020
ba8426e
formatting
katemyer Mar 15, 2020
6c6a0ff
added test to channel and user
katemyer Mar 16, 2020
28875c9
cassettes for all classes
katemyer Mar 16, 2020
e6e67e5
formatted workspace.rb
katemyer Mar 16, 2020
e8de025
removed comments
katemyer Mar 16, 2020
5f8ba50
completed tests
katemyer Mar 16, 2020
dcfbeed
completed tests
katemyer Mar 16, 2020
d2236a4
updated gitignore to exclude history and coverage
katemyer Mar 16, 2020
84ec7f2
updated to ignore launch.json
katemyer Mar 16, 2020
8b68971
formatted
katemyer Mar 16, 2020
0d52e1e
formatted
katemyer Mar 16, 2020
4b66852
added response error
katemyer Mar 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/coverage/
coverage/

.DS_Store

# Ignore environemnt variables
.env

#Ignore history
.history

#launch.Json file
launch.json
41 changes: 41 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'httparty'
require_relative 'recipient'

module SlackCli
class Channel < Recipient
attr_accessor :topic, :member_count

def initialize(id, name, topic, member_count)
super(id, name)
@topic = topic
@member_count = member_count
end #initialize

#--------Channel Class Methods----------------------------

#retrieves list of channels from Slack
#input
#returns list of channels [Channel]
def self.load_all(url)
channels = []
#send request to Slack API using users.list endpoint
response = HTTParty.get(url) #request that will return the response
if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encountered a problem: #{response["error"]}"
end
#parse response and get users
response['channels'].each do |channel|
id = channel["id"]
name = channel["name"]
topic = channel["topic"]["value"]
member_count = channel["num_members"]
# member_count = channel["members"].length
#create a channel
slack_channel = SlackCli::Channel.new(id, name, topic, member_count)
#save to channels
channels.push(slack_channel)
end
return channels
end #load_all method
end #class
end #module
28 changes: 28 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#00 ride share csvrecord
module SlackCli
class Recipient
attr_reader :id
attr_accessor :name

def initialize(id, name)
@id = id

if !name.is_a? String
raise ArgumentError.new("Invalid name, must be String")
end
@name = name

end #initialize

#--------------CLASS METHODS-----------------------------------
private
def self.load_all() #abstract method/template
raise NotImplementedError, 'Implement me in a child class!'
end
end #class
end #module

#Common to tell other files in the program
#Per Devin, include here so it's accessible to other classes
class SlackAPIError < Exception
end
93 changes: 90 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,99 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require 'table_print'
require_relative 'workspace'

def main
workspace = SlackCli::Workspace.new
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
puts "Here are the options:"
options = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"]
options.each do |option|
puts option
end

user_input = gets.chomp.downcase
while user_input != options[-1] #quit
if user_input == options[0] #list users
#GET user details- username, real name, and Slack ID.
tp workspace.users, :id, :name, :real_name #https://github.com/arches/table_print usage
elsif user_input == options[1] #list channels
#GET channel details- name, topic, member count, and Slack ID
tp workspace.channels, :id, :name, :topic, :member_count


#----------------SELECT USER-------------------------------
#userinput equals option--select user
elsif user_input == options[2] #select user
#prompt: "which user do you want to select"
puts "Which user do you want to select?"
#present list of users
tp workspace.users, :id, :name, :real_name
#user input...gets chomp
user_input = gets.chomp

# TODO project
valid_inputs = workspace.valid_inputs_id_names("user")
#check is user's input includes anything in this valid_inputs[]
while !valid_inputs.include?(user_input)
#if not, ask to reenter
puts "Invalid. Enter selection again. Try copy and pasting SlackID or username."
user_input = gets.chomp
end
#find user
found_user = workspace.find_user(user_input)
#assign the user found to selected
workspace.selected = found_user

#-------SELECT CHANNEL -------------------------------------
elsif user_input == options[3]
#prompt: "which channel do you want to select"
puts "Which channel do you want to select?"
#present list of channels
tp workspace.channels, :id, :name, :topic, :member_count
#user input...gets chomp
user_input = gets.chomp

valid_inputs = workspace.valid_inputs_id_names("channel")
#check is user's input includes anything in this valid_inputs[]
while !valid_inputs.include?(user_input)
#if not, ask to reenter
puts "Invalid. Enter selection again. Try copy and pasting SlackID or username."
user_input = gets.chomp
end
#find user
found_channel = workspace.find_channel(user_input)
#assign the user found to selected
workspace.selected = found_channel

#------------- DETAILS USER + CHANNEL--------------------------------
elsif user_input == options[4] #select details
#print the details of what the user selected
#check if selected is User Object
if workspace.selected.is_a? SlackCli::User
#if true, print user details : id, name, @real_name, @status_text, @status_emoji
tp workspace.selected, :id, :name, :real_name, :status_text, :status_emoji
#check if selected is Channel Object
elsif workspace.selected.is_a? SlackCli::Channel
#if true, print channel details: id, name, @topic, @member_count
tp workspace.selected, :id, :name, :topic, :member_count
elsif workspace.selected == nil
puts "No selection was made. Select user or select channel for details."
end

#------------- SEND MESSAGE--------------------------------
elsif user_input == options[5] #send message
#check if a selected is not !=nil
puts "What is your message?"
#get message
user_message = gets.chomp

workspace.send_message(user_message)
end
puts "What is your next selection?"
user_input = gets.chomp #main prompt
end
puts "Thank you for using the Ada Slack CLI"
end
end #main

main if __FILE__ == $PROGRAM_NAME
44 changes: 44 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'httparty'
require_relative 'recipient'


module SlackCli
class User < Recipient
attr_accessor :real_name, :status_text, :status_text, :status_emoji

def initialize(id, name, real_name, status_text = nil, status_emoji = nil)
super(id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end #initialize

#--------User Class Methods----------------------------
#retrieves list of users from Slack
#input
#returns list of users [User]
def self.load_all(url)
users = []
#send request to Slack API using users.list endpoint
response = HTTParty.get(url) #request that will return the response
if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encountered a problem: #{response["error"]}"
end
#parse response and get users
response['members'].each do |member|
#create a user
id = member["id"]
name = member["name"]
real_name = member["real_name"]
status_text = member["profile"]["status_text"]
status_emoji = member["profile"]["status_emoji"]
#handle
#real name
slack_user = SlackCli::User.new(id, name, real_name, status_text, status_emoji)
#save to users
users.push(slack_user)
end
return users
end #load_all method
end #class
end #module
103 changes: 103 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require_relative 'user'
require_relative 'channel'
require 'dotenv'

module SlackCli
class Workspace
attr_reader :users, :channels
attr_accessor :selected

def initialize()
Dotenv.load(__dir__ + "/" + "../.env") #https://github.com/bkeepers/dotenv
url = ENV['BASE_URL'] + ENV['SUB_USER_URL'] + "token=" + ENV['SLACK_TOKEN']
@users = SlackCli::User.load_all(url)
url = ENV['BASE_URL'] + ENV['SUB_CHANNEL_URL'] + "token=" + ENV['SLACK_TOKEN']
@channels = SlackCli::Channel.load_all(url)
@selected = nil
end #initialize

#--------------------------Class Methods ------------------------

#Returns all valid user id and name [Strings]
# inputs: aMode that's either "user" or "channel"
def valid_inputs_id_names(aMode)
valid_inputs = []
if aMode == "user"
self.users.each do |user|
valid_inputs << user.id
valid_inputs << user.name
end
elsif aMode == "channel"
self.channels.each do |channel|
valid_inputs << channel.id
valid_inputs << channel.name
end
end
return valid_inputs
end

#----------------------------------------------------------------

#finds a user
#input: aUser_input (String)
#Returns a found user (User Oject)
def find_user(aUser_input)
found_user = nil
self.users.each do |user|
if (aUser_input == user.id) || (aUser_input == user.name)
return user
end
end
return found_user
end

#----------------------------------------------------------------

#finds a channel
#input: aUser_input (String)
#Returns a found channel (Channel Object)
def find_channel(aUser_input)
found_channel = nil
self.channels.each do |channel|
if (aUser_input == channel.id) || (aUser_input == channel.name)
return channel
end
end
return found_channel
end
#----------------------------------------------------------------

#sends message
#input: aUser_message (String)
#return:response code (int) = 200 means "ok"

def send_message(aUser_message)
response_code = nil
if self.selected != nil
#if true, prompt user: "what's your message"

#then send message to selected
payload = {
:channel => self.selected.id,
:text => aUser_message,
:token => ENV['SLACK_TOKEN']
}
payload_options = {
:body => payload
}
url = ENV['BASE_URL'] + ENV['SUB_MESSAGE_URL']
response = HTTParty.post(url, payload_options)
if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encountered a problem: #{response["error"]}"
end
return response.code
#if it is nil,
else
#prompt user to select before a message can be sent
puts "Select a user or channel before a message can be sent."
end
return response_code
end
end #class
end #module

Loading