Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack CLI - Fire - Gessica and Noor #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

# Ignore environemnt variables
.env
.floo
.flooignore
Comment on lines +7 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!!!

Binary file added images/SLACK CLI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions lib/Slack_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'httparty'
require 'dotenv'

Dotenv.load

BASE_URL_CONVERSATIONS = 'https://slack.com/api/conversations.list'
BASE_URL_USERS = 'https://slack.com/api/users.list'
BASE_URL_CHAT = 'https://slack.com/api/chat.postMessage'
Comment on lines +1 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty big deviation from the design you were asked to use! Most of the following comments talk about how this was expected to be in the original design!


class SlackApiError < StandardError; end

class SlackRecord

def self.users
response = HTTParty.get( BASE_URL_USERS, query: {
token: ENV['SLACK_API_TOKEN']
})
return response["members"]
end

def self.channels
response = HTTParty.get( BASE_URL_CONVERSATIONS, query: {
token: ENV['SLACK_API_TOKEN']
})
return response["channels"]
end

def self.chat(message,channel)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just live in recipient as is! no changes! :D

chat = HTTParty.post( BASE_URL_CHAT, body: {
token: ENV['SLACK_API_TOKEN'],
channel: channel,
text: message
})
unless chat.code == 200 && chat.parsed_response["ok"]
raise SlackApiError, "Error when posting #{message} to #{channel}, error: #{chat.parsed_response["error"]}"
end
return chat
end

end
33 changes: 33 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative 'slack_record'
require_relative 'recipient'
class Channel < Recipient

attr_reader :topic, :member_count

def initialize(name:, topic:, member_count:, slack_id:)
super(name: name, slack_id: slack_id)
@topic = topic
@member_count = member_count
end

def self.list_channels
channels = SlackRecord.channels.map do |channel|
Channel.new(
name: channel["name"],
topic: channel["topic"]["value"],
member_count: channel["num_members"],
slack_id: channel["id"]
)
end
return channels
end


def details
return "name: #{@name}\n" +
"member_count: #{@member_count}\n" +
"topic: #{@topic}\n" +
"slack_id: #{@slack_id}."

end
end
13 changes: 13 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Recipient
attr_reader :name, :slack_id

def initialize(slack_id:, name:)
@slack_id = slack_id
@name = name
end

def details
raise NotImplementedError
end

end
56 changes: 55 additions & 1 deletion lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
#!/usr/bin/env ruby
require 'dotenv'
require 'table_print'
require_relative 'workspace'

def main
Dotenv.load

puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
user_input = ""

# TODO project

until user_input == "quit"
puts "\nWhat would you like to do? Please select one options: \n1. list users\n2. list channels\n3. select user\n4. select channel\n5. details\n6. send message\n7. quit"
user_input = gets.chomp.downcase
puts
case user_input
when "1", "list users"
tp workspace.users, :real_name, :user_name, :slack_id
when "2", "list channels"
tp workspace.channels, :name, :topic, :member_count, :slack_id
when "3", "select user"
Comment on lines +19 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a touch more readable!

Suggested change
when "1", "list users"
tp workspace.users, :real_name, :user_name, :slack_id
when "2", "list channels"
tp workspace.channels, :name, :topic, :member_count, :slack_id
when "3", "select user"
when "1", "list users"
tp workspace.users, :real_name, :user_name, :slack_id
when "2", "list channels"
tp workspace.channels, :name, :topic, :member_count, :slack_id
when "3", "select user"

Etc!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I'm confused, I didn't understand what you meant by Etc

print "Please enter a user name or ID:"
selection_option = gets.chomp
selected = workspace.select_user(selection_option)
if selected
puts "User found"
else
puts "User not found"
end
when "4", "select channel"
print "Please enter a channel's name or ID:"
selection_option = gets.chomp
selected = workspace.select_channel(selection_option)
if selected
puts "Channel found"
else
puts "Channel not found"
end
Comment on lines +33 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might even consider taking these longer pieces and putting them in helper methods!

when "5", "details"
begin
puts workspace.show_details
rescue WorkspaceError => error
puts error.message
end
Comment on lines +42 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

begin and rescue should also be used on anything that sends a message to slack, to better handle those errors!

when "6", "send message"
if selected
puts "Please write a message:"
message = gets.chomp
SlackRecord.chat(message, selected.slack_id)
else
puts "You have not entered a user or channel. Please make your selection:"
end
when "7", "quit"
user_input = "quit"
else
puts "Not a valid option! Please enter one of the options again:"

end
end

puts "Thank you for using the Ada Slack CLI"
end

Expand Down
32 changes: 32 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative 'slack_record'
require_relative 'recipient'

class User < Recipient
attr_reader :real_name

def initialize(user_name:,real_name:, slack_id:)
super(slack_id: slack_id, name: user_name)
@real_name = real_name
end

def user_name
@name
end

def self.list_users
members = SlackRecord.users.map do |user|
User.new(
user_name: user["name"],
real_name: user["real_name"],
slack_id: user["id"])
end
return members
end

def details
return "real name: #{@real_name}\n" +
"user name : #{@name}\n" +
"slack ID : #{@slack_id}"
end

end
34 changes: 34 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'user'
require_relative 'channel'

class WorkspaceError < StandardError; end

class Workspace
attr_reader :users, :channels, :selected

def initialize
@users = User.list_users
@channels = Channel.list_channels
@selected = nil
end

def select_user(user_input)
@users = User.list_users
@selected = @users.find {|user| user.user_name == user_input || user.slack_id == user_input}
end


def select_channel(user_input)
@channels = Channel.list_channels
@selected = @channels.find {|channel| channel.name == user_input || channel.slack_id == user_input}
end
Comment on lines +9 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just popping in to say that this is really lovely to read. :D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Devin :)


def show_details
if @selected.nil?
raise WorkspaceError, "You have not selected a user or channel."
else
return @selected.details
end
end

end
Loading