Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ build-iPhoneSimulator/

# Ignore environemnt variables
.env
instructor-design.png

# Ignore cassette files
/specs/cassettes/
46 changes: 46 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'dotenv'
require 'httparty'
require 'awesome_print'
Dotenv.load

require_relative 'recipient'

module SlackCli
class Channel < Recipient
attr_reader :name, :topic, :id, :num_members

def initialize(name:, id:, topic:, num_members:)
@name = name
@id = id
@topic = topic
@num_members = num_members
end

def self.list
method_url = "https://slack.com/api/channels.list"
Copy link

Choose a reason for hiding this comment

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

Minor suggestion: Could this be a constant variable?

query_params = {
token: ENV["SLACK_TOKEN"]
}
response = HTTParty.get(method_url, query: query_params)

channels = response.parsed_response["channels"]
i = 0
all_channels = []
channels.each do |channel|
Copy link

Choose a reason for hiding this comment

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

Minor suggestion for refactoring: instead of using .each and i and incrementing i, consider using channels.each_with_index do |channel, index|, which has built-in support for an index

channel_hash = {}
name = response.parsed_response["channels"][i]["name"]
topic = response.parsed_response["channels"][i]["topic"]["value"]
id = response.parsed_response["channels"][i]["id"]
num_members = response.parsed_response["channels"][i]["num_members"]
channel_hash[:name] = name
channel_hash[:topic] = topic
channel_hash[:id] = id
channel_hash[:num_members] = num_members
all_channels.push(channel_hash)
i += 1
end
return all_channels
end

end
end
20 changes: 20 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'dotenv'
require 'httparty'
require 'awesome_print'
Dotenv.load

module SlackCli
class Recipient
def self.send_message(recipient:, message:)
method_url = "https://slack.com/api/chat.postMessage"
query_params = {
token: ENV["SLACK_TOKEN"],
channel: recipient,
text: message
}
slack_post = HTTParty.post(method_url, query: query_params)
Copy link

Choose a reason for hiding this comment

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

Something to think about for the future: What happens if this POST request comes back with a response that has an error? Your code currently doesn't handle that!

return slack_post
end
end

end
89 changes: 82 additions & 7 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,86 @@
#!/usr/bin/env ruby
require 'dotenv'
require 'httparty'
require 'awesome_print'
Dotenv.load

def main
puts "Welcome to the Ada Slack CLI!"
require_relative './channel'
require_relative 'user'

# TODO project
def start_program
ap "Welcome to the Ada Slack CLI!"
print "What would you like to do? Type one of the following:\n - list channels\n - list users\n - select user\n - select channel\n - details\n - send message\n - quit\n"
user_input = gets.chomp.downcase

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

main if __FILE__ == $PROGRAM_NAME
while user_input != "quit"
case user_input
when "list channels"
ap SlackCli::Channel.list
when "list users"
ap SlackCli::User.list
when "select user"
ap "Type a username or Slack ID."
search_user = gets.chomp
selected_recipient = SlackCli::User.list.find do |i|
Copy link

Choose a reason for hiding this comment

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

Minor suggestion: Instead of the variable name i, could we change it to something like recipient or user? This just helps me understand what kinds of elements are in the SlackCli::User.list array

i[:user_name]== search_user || i[:id] == search_user
end
if selected_recipient == nil
Copy link

Choose a reason for hiding this comment

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

Minor suggestion: the code selected_recipient.nil? does the same thing as selected_recipient == nil, in case that looks better in your opinion

ap "User not found"
else
selected_recipient = SlackCli::User.new(
name: selected_recipient[:user_name],
real_name: selected_recipient[:real_name],
id: selected_recipient[:id]
)
ap "Recipient has been selected."
end

when "select channel"
ap "Type a username or Slack ID."
search_channel = gets.chomp
selected_recipient = SlackCli::Channel.list.find do |i|
i[:name]== search_channel || i[:id] == search_channel
end
if selected_recipient == nil
ap "Channel not found"
else
selected_recipient = SlackCli::Channel.new(
name: selected_recipient[:name],
topic: selected_recipient[:topic],
id: selected_recipient[:id],
num_members: selected_recipient[:num_members]
)
ap "Recipient has been selected."
end

when "details"
if selected_recipient.class == SlackCli::Channel
ap "Channel name: #{selected_recipient.name}"
ap "Channel topic: #{selected_recipient.topic}"
ap "Channel id: #{selected_recipient.id}"
ap "Channel members count: #{selected_recipient.num_members}"
elsif selected_recipient.class == SlackCli::User
ap "Username: #{selected_recipient.name}"
ap "User real name: #{selected_recipient.real_name}"
ap "User id: #{selected_recipient.id}"
else
ap "No recipient is currently selected. Select a recipient in the main menu."
end
ap selected_recipient
when "send message"
if selected_recipient == nil
ap "No recipient selected."
else
ap "Type a message to send:"
message = gets.chomp
SlackCli::Recipient.send_message(recipient:selected_recipient.id, message:message)
end
else
ap "Not a valid command"
end
print "What would you like to do? Type one of the following:\n - list channels\n - list users\n - select user\n - select channel\n - details\n - send message\n - quit\n"
user_input = gets.chomp.downcase
end
ap "Thank you for using the Sara & Monick Slack CLI."
end
start_program
42 changes: 42 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'dotenv'
require 'httparty'
require 'awesome_print'
Dotenv.load

require_relative 'recipient'

module SlackCli
class User < Recipient
attr_reader :name, :real_name, :id

def initialize(name:, real_name:, id:)
@real_name = real_name
@name = name
@id = id
end

def self.list
method_url = "https://slack.com/api/users.list"
query_params = {
token: ENV["SLACK_TOKEN"]
}
response = HTTParty.get(method_url, query: query_params)

users = response.parsed_response["members"]
i = 0
all_users = []
users.each do |user|
user_hash = {}
user_name = response.parsed_response["members"][i]["name"]
real_name = response.parsed_response["members"][i]["real_name"]
id = response.parsed_response["members"][i]["id"]
user_hash[:user_name] = user_name
user_hash[:real_name] = real_name
user_hash[:id] = id
all_users.push(user_hash)
i += 1
end
return all_users
end
end
end
Loading