Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c64313a
Created User class, Channel class, Recipient
OlgaSe Oct 6, 2020
de592f0
Refactor methods and use Table Print gem to format output
beauttie Oct 7, 2020
07f2d41
Implement select_user and select_channel methods
beauttie Oct 7, 2020
9a43b8a
Brainstorm for show_details method
beauttie Oct 7, 2020
aa1712d
added details methods
OlgaSe Oct 7, 2020
00b808a
added Argument Error check forselect_user and select_channel methods
OlgaSe Oct 7, 2020
b104162
Move SlackApiError to parent class, modify details output for User an…
beauttie Oct 7, 2020
a1cfe33
Clean up code
beauttie Oct 8, 2020
d342458
added tests for user's methods details and list_all
OlgaSe Oct 8, 2020
a74b1db
Add test for Channel class
beauttie Oct 8, 2020
a8eb562
Add tests for Channel and Workspace classes
beauttie Oct 8, 2020
cfdd460
Fix tests for channel
beauttie Oct 8, 2020
e38b4ed
Rescue errors, finish tests for Workspace class
beauttie Oct 8, 2020
9b1d7d1
Added send_message method and test for it
OlgaSe Oct 8, 2020
b3059c2
Refactor URL constant and parameters
beauttie Oct 8, 2020
ef55a29
Fix test for edge case in workspace_test
beauttie Oct 8, 2020
0eb2dcf
Empty message case test for send_message method is fixed
OlgaSe Oct 9, 2020
3a6fced
Added all testes for the user class
OlgaSe Oct 9, 2020
2df5a04
Handle errors with unless clause in Recipient class
beauttie Oct 9, 2020
099fd46
Modify output of select methods to include Slack ID or name
beauttie Oct 9, 2020
707ca76
Reorganize tests for User class
beauttie Oct 9, 2020
9c1fb94
Clean up format of Channel tests
beauttie Oct 9, 2020
331919a
Add tests for Workspace class
beauttie Oct 9, 2020
2098016
Fix output from CLI when sending message
beauttie Oct 9, 2020
dbd35c6
Fix name of test
beauttie Oct 10, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

.DS_Store

# Ignore environemnt variables
# Ignore environment variables
.env
29 changes: 29 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'recipient'

class Channel < Recipient
attr_reader :topic, :member_count

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

def details
return "Channel #{@name}'s topic is #{@topic} and has #{@member_count} members. Its ID on Slack is #{@slack_id}."
end

def self.list_all
response = self.get("#{BASE_URL}conversations.list", { token: SLACK_TOKEN })

channels_list = response["channels"].map do |channels_hash|
self.new(channels_hash["id"],
channels_hash["name"],
channels_hash["topic"]["value"],
channels_hash["num_members"])
end

return channels_list
end

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

require_relative 'slack_api_error'

Dotenv.load
SLACK_TOKEN = ENV["SLACK_TOKEN"]

BASE_URL = "https://slack.com/api/"

class Recipient
attr_reader :slack_id, :name

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

def send_message(message)
response = HTTParty.post("#{BASE_URL}chat.postMessage",
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
body: {
token: SLACK_TOKEN,
channel: self.slack_id,
text: message
})

unless response.code == 200 && response["ok"]
raise SlackApiError, "API call failed with error: #{response["error"]}"
end

return true
end

def self.get(url, params)
response = HTTParty.get(url, query: params)

unless response.code == 200 && response["ok"]
raise SlackApiError, "API call failed with error: #{response["error"]}"
end

return response
end

def details
raise NotImplementedError, "Implement me in a child class!"
end

def self.list_all
raise NotImplementedError, "Implement me in a child class!"
end
end
54 changes: 54 additions & 0 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
#!/usr/bin/env ruby
require_relative 'workspace'

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

# TODO project
puts "What do you want to do?"
puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. details\n6. send message\n7. quit"
print ">> "

user_input = gets.chomp.downcase

until user_input == "quit" || user_input == "7"
case user_input
when "list users", "1"
workspace.list_users
when "list channels", "2"
workspace.list_channels
when "select user", "3"
print "Enter the user's name or Slack ID: "
user_input = gets.chomp

begin
puts workspace.select_user(user_input)
rescue ArgumentError => exception
puts "#{exception}"
end
when "select channel", "4"
print "Enter the channel's name or Slack ID: "
user_input = gets.chomp

begin
puts workspace.select_channel(user_input)
rescue ArgumentError => exception
puts "#{exception}"
end
when "details", "5"
begin
puts workspace.show_details
rescue NoMethodError => exception
puts "#{exception}"
end
when "send message", "6"
print "Type out a message: "
user_input = gets.chomp

begin
puts workspace.send_message(user_input)
rescue NoMethodError => exception
puts "#{exception}"
end
else
puts "Invalid input. Try again."
end

print "What do you want to do next? "
user_input = gets.chomp.downcase
end

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

main if __FILE__ == $PROGRAM_NAME
1 change: 1 addition & 0 deletions lib/slack_api_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class SlackApiError < Exception; end
31 changes: 31 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative 'recipient'

class User < Recipient
attr_reader :real_name, :status_text, :status_emoji

def initialize(slack_id, name, real_name, status_text, status_emoji)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def details
return "User #{@name}'s real name is #{@real_name}, and their ID on Slack is #{@slack_id}. Their current status reads: #{@status_text} #{@status_emoji}."
end

def self.list_all
response = self.get("#{BASE_URL}users.list", { token: SLACK_TOKEN })

users_list = response["members"].map do |members_hash|
self.new(members_hash["id"],
members_hash["name"],
members_hash["profile"]["real_name"],
members_hash["profile"]["status_text"],
members_hash["profile"]["status_emoji"])
end

return users_list
end

end
51 changes: 51 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'table_print'

require_relative 'user'
require_relative 'channel'

class Workspace
attr_reader :users, :channels, :selected

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

def list_users
tp @users, "slack_id", "name", "real_name", "status_text", "status_emoji"
end

def list_channels
tp @channels, "slack_id", "name", "topic", "member_count"
end

def no_recipient
return @selected.nil?
end

def select_user(id_or_name)
@selected = @users.find { |user| user.slack_id == id_or_name || user.name == id_or_name }

raise ArgumentError, "User with that name or Slack ID not found." if no_recipient
return "User #{id_or_name} selected."
end

def select_channel(id_or_name)
@selected = @channels.find { |channel| channel.slack_id == id_or_name || channel.name == id_or_name }

raise ArgumentError, "Channel with that name or Slack ID not found." if no_recipient
return "Channel #{id_or_name} selected."
end

def show_details
raise NoMethodError, "No recipient is currently selected." if no_recipient
return @selected.details
end

def send_message(message)
raise NoMethodError, "No recipient is currently selected." if no_recipient
return "Message sent to #{@selected.class} #{@selected.name}!" if @selected.send_message(message)
end

end
133 changes: 133 additions & 0 deletions test/cassettes/bad_post_request.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading