-
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
Fire - Alice & Madeline #25
base: master
Are you sure you want to change the base?
Changes from all commits
8148aa4
eaf73bf
318944d
721fb5b
0906103
d624e03
ce30e6d
0d18458
a5aadcb
7484d5d
a7afc06
097459b
32c105d
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
.DS_Store | ||
|
||
# Ignore environemnt variables | ||
.env | ||
.env |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Channel | ||
attr_reader :name, :topic, :member_count, :id | ||
|
||
def initialize(name, topic, member_count, id) | ||
@name = name | ||
@topic = topic | ||
@member_count = member_count | ||
@id = id | ||
end | ||
|
||
def details | ||
return "Name: #{@name}. Topic: #{@topic}. Member count: #{@member_count}. ID: #{@id}." | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,80 @@ | ||
#!/usr/bin/env ruby | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Workspace.new | ||
|
||
# TODO project | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME | ||
#!/usr/bin/env ruby | ||
require_relative "workspace" | ||
require "dotenv" | ||
require "httparty" | ||
require "awesome_print" | ||
require "table_print" | ||
|
||
Dotenv.load | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
|
||
command = 0 | ||
workspace = Workspace.new | ||
total_users = workspace.get_users.length | ||
total_channels = workspace.get_channels.length | ||
puts "There are #{total_users} users in the Ada Slack workspace and #{total_channels} channels in the Ada Slack workspace." | ||
|
||
selected_recipient = nil | ||
|
||
until command == "quit" || command == "q" | ||
print "What do you want to do? Here are the options: \n | ||
list users \n | ||
list channels \n | ||
select user \n | ||
select channel \n | ||
details \n | ||
send message \n | ||
quit => " | ||
command = gets.chomp.downcase | ||
if command == "list users" | ||
tp workspace.get_users, :username, :name, :id | ||
elsif command == "list channels" | ||
tp workspace.get_channels, :name, :topic, :member_count, :id | ||
elsif command == "select user" | ||
print "Enter username or slack ID => " | ||
username_or_id = gets.chomp | ||
# returns User object | ||
selected_recipient = workspace.select_user(username_or_id) | ||
# validate username or ID exists | ||
if selected_recipient == nil | ||
puts "Username or ID not found" | ||
end | ||
elsif command == "select channel" | ||
print "Enter channel name or ID => " | ||
name_or_id = gets.chomp | ||
selected_recipient = workspace.select_channel(name_or_id) | ||
# validate channel name or ID exists | ||
if selected_recipient == nil | ||
puts "Channel name or ID not found" | ||
end | ||
elsif command == "details" | ||
# validate user or channel is selected first before providing details | ||
if selected_recipient == nil | ||
puts "Select a user or channel first" | ||
elsif selected_recipient != nil | ||
puts selected_recipient.details | ||
end | ||
elsif command == "send message" | ||
if selected_recipient == nil | ||
puts "You must select a recipient (channel or user) you would like to message." | ||
else | ||
print "Type your message => " | ||
message = gets.chomp | ||
did_message_send = workspace.send_message(selected_recipient.id, message) | ||
if did_message_send | ||
puts "Message sent!" | ||
else | ||
puts "Message failed to send." | ||
end | ||
end | ||
elsif command == "quit" || command == "q" | ||
puts "Thank you for using the ADA Slack CLI" | ||
else | ||
puts "That is not a valid command. Please provide a command from the provided list." | ||
end | ||
end | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class User | ||
attr_reader :username, :name, :id | ||
|
||
def initialize(username, name, id) | ||
@username = username | ||
@name = name | ||
@id = id | ||
end | ||
|
||
def details | ||
return "Username: #{@username}. Name: #{@name}. ID: #{@id}." | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require "httparty" | ||
require "dotenv" | ||
require "awesome_print" | ||
require_relative "user.rb" | ||
require_relative "channel.rb" | ||
Dotenv.load | ||
|
||
GET_USER_PATH = "https://slack.com/api/users.list" | ||
|
||
GET_CHANNEL_PATH = "https://slack.com/api/conversations.list" | ||
|
||
GET_MESSAGE_PATH = "https://slack.com/api/chat.postMessage" | ||
|
||
class SlackApiError < StandardError; end | ||
|
||
class Workspace | ||
attr_reader :users, :channels | ||
|
||
def initialize | ||
@users = [] | ||
@channels = [] | ||
end | ||
|
||
def get_users | ||
user_response = HTTParty.get(GET_USER_PATH, query: { token: ENV["SLACK_TOKEN"] }) | ||
|
||
@users = user_response["members"].map do |member| | ||
User.new(member["name"], member["real_name"], member["id"]) | ||
end | ||
|
||
unless user_response.code == 200 | ||
raise SlackApiError, "Error: #{user_response.parsed_response["error"]}" | ||
end | ||
|
||
return @users | ||
end | ||
Comment on lines
+24
to
+36
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. I just want to point out that listing users is a lot like listing channels. You could abstract this content out into a parent class. You could also create a factory method in your classes like we did in Ride Share or Grocery Store. |
||
|
||
def select_user(username_or_id) | ||
selected_user = self.get_users | ||
.select { |user| user.username == username_or_id || user.id == username_or_id } | ||
.first | ||
return selected_user | ||
end | ||
Comment on lines
+38
to
+43
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 makes an api call every time you select a user, why not instead use your instance variable |
||
|
||
def get_channels | ||
channel_response = HTTParty.get(GET_CHANNEL_PATH, query: { token: ENV["SLACK_TOKEN"] }) | ||
|
||
@channels = channel_response["channels"].map do |channel| | ||
Channel.new(channel["name"], channel["topic"]["value"], channel["num_members"], channel["id"]) | ||
end | ||
|
||
unless channel_response.code == 200 | ||
raise SlackApiError, "Error: #{channel_response.parsed_response["error"]}" | ||
end | ||
|
||
return @channels | ||
end | ||
|
||
def select_channel(name_or_id) | ||
selected_channel = self.get_channels | ||
.select { |channel| channel.name == name_or_id || channel.id == name_or_id } | ||
.first | ||
return selected_channel | ||
end | ||
Comment on lines
+59
to
+64
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 note how similar this is to |
||
|
||
def send_message(recipient_id, message) | ||
response = HTTParty.post(GET_MESSAGE_PATH, body: { | ||
token: ENV["SLACK_TOKEN"], | ||
channel: recipient_id, | ||
text: message, | ||
}) | ||
|
||
return response["ok"] | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Just noting the User and Channel classes are pretty small, you could make the method to retrieve all the users a method here inside User (class method), similar to how we did Ride share.