-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Changes from all commits
ac73d9e
64fc425
4c3eb76
72a7b57
908b59c
9e7e581
89f2f6f
c212ccf
a6106e3
9dcc1c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
|
||
# Ignore environemnt variables | ||
.env | ||
.floo | ||
.flooignore | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a touch more readable!
Suggested change
Etc! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!!!