Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9db497c
Initializes Recipient class and tests attributes.
goblineer Mar 19, 2019
b333f46
added dotenv
goblineer Mar 19, 2019
1abc147
added test for self.get method in recipient
goblineer Mar 19, 2019
4845c96
created users file
goblineer Mar 19, 2019
05a4eeb
added require
MyriamWD Mar 19, 2019
76c2517
added test for self_get_response method
MyriamWD Mar 19, 2019
6c737b5
added self_get_response method to make test pass
MyriamWD Mar 19, 2019
47195a3
Hides API Key in VCR class
goblineer Mar 19, 2019
96ae0ce
updates to recipient_spec and fixes file system
goblineer Mar 21, 2019
02445df
added get, list, details, message, and initialize methods
MyriamWD Mar 21, 2019
1a48bc2
created tests for each method in the User's class
MyriamWD Mar 21, 2019
7e652fb
has the required relatives for User class to work
MyriamWD Mar 21, 2019
0125360
adjusted test to refactored methos in User class
MyriamWD Mar 21, 2019
3c795fa
refactored methods and instanciated the class with self.new
MyriamWD Mar 21, 2019
bb00d2d
added methods initialize and users_list
MyriamWD Mar 21, 2019
d5493b2
created methods to handle the disoplay of options and information
MyriamWD Mar 21, 2019
44feabb
added some details and eliminated comments
MyriamWD Mar 21, 2019
acdd5df
created separe methods for options
MyriamWD Mar 21, 2019
1ac7513
CLI interface user's functionality
MyriamWD Mar 22, 2019
122ac43
Green to go
goblineer Mar 22, 2019
22ff3a4
adjusts flow of channel_spec and channel tests
goblineer Mar 22, 2019
18a2447
Adds channel finder for select channel option
goblineer Mar 22, 2019
43f095d
re-implements two commented-out tests
goblineer Mar 22, 2019
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
66 changes: 66 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "httparty"
require "dotenv"
require_relative "recipient"

Dotenv.load

module SlackCLI
class Channel < Recipient
URL = "https://slack.com/api/channels.list".freeze
CHAT_URL = "https://slack.com/api/chat.postMessage".freeze
KEY = ENV["SLACK_API_TOKEN"]

attr_reader :topic, :members

def initialize(id, name, topic, members)
super(id, name)
@topic = topic
@members = members
end

def self.get(url, param)

Choose a reason for hiding this comment

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

This method could be inherited from Recipient.

query = { token: param }
response = HTTParty.get(url, query: query)
response
end

def self.list(url, param)
response = get(url, param)
raise StandardError, "Error #{response.code} : #{response['message']}" if response["ok"].nil?

formatted_list = []
channels = response["channels"].each do |channel|
slack_id = channel["id"]
channel_name = channel["name"]
topic = channel["topic"]["value"]
members = channel["members"].length
formatted_list << new(slack_id, channel_name, topic, members)
end
formatted_list
end

def details

Choose a reason for hiding this comment

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

Just a note this method could also be inherited from Recipient and then expanded.

info_string = "\nSlack ID : #{id}" +
"\nChannel name : #{name}" +
"\nTopic : #{topic}" +
"\nMember count: #{members}"
info_string
end

def self.message(text, channel)
response = HTTParty.post("#{CHAT_URL}",
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
body: {
token: KEY,
channel: channel,
text: text
}
)
if response["ok"]
return true
else
raise ArgumentError, "Error when sending message to #{channel}."
end
end
end
end
38 changes: 38 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module SlackCLI
class Recipient
CHAT_URL = "https://slack.com/api/chat.postMessage".freeze
attr_reader :id, :name

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

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

def send_message(message)

Choose a reason for hiding this comment

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

Notice how this method is identical in Channel?

query_parameters = {
token: ENV["SLACK_API_TOKEN"],
channel: id,
text: message
}
response = HTTParty.post(
CHAT_URL,
body: query_parameters,
headers: { "Content-Type" => "application/x-www-form-urlencoded" }
)

response.code == 200 && response["ok"]
end

def display_details

Choose a reason for hiding this comment

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

You call it details in channel.

raise NotImplementedError, "Implement me from a child class!"
end

def self.get

Choose a reason for hiding this comment

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

get could be implemented in Recipient just with a different URL in each child class.

raise NotImplementedError, "Implement me from a child class!"
end
end
end
137 changes: 131 additions & 6 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,136 @@
#!/usr/bin/env ruby
require_relative "workspace"
require_relative "user"

def main
puts "Welcome to the Ada Slack CLI!"
module SlackCLI
class Main
def self.main
puts "Welcome to the Ada Slack CLI!"
option = 0
until option == 5
puts "\n\n---Options---\n\n"
puts " 1. List Users"
puts " 2. List Channels"
puts " 3. Select User"
puts " 4. Select Channels"
puts " 5. Quit"
puts "\nPlease, choose one of the options available"
option = gets.chomp.to_i
options(option) if option != 5
end
puts "Thank you for using the Ada Slack CLI\n\n"
exit
end

# TODO project
def self.options(option)
if option == 1
list_users
elsif option == 2
list_channels
elsif option == 3
select_user
elsif option == 4
select_channel
elsif option == 5
exit
end
end

puts "Thank you for using the Ada Slack CLI"
end
def self.list_users
workspace = Workspace.new
users_list = workspace.users
puts "\n\n----List of Slack Users----\n\n"
users_list.each_with_index do |user, index|
puts "User N.#{index + 1}"
puts " Name: #{user.name}"
puts " Real Name:#{user.real_name}"
puts " ID: #{user.id}\n\n"
end
puts "Press any key to continue...\n\n"

Choose a reason for hiding this comment

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

This should be, "Press Enter to continue"

gets.chomp
end

def self.list_channels
workspace = Workspace.new
channel_list = workspace.channels
puts "\n\n----List of Slack Channels----\n\n"
channel_list.each_with_index do |channel, index|
print "Channel N.#{index + 1}"
puts channel.details + "\n\n"
end
puts "Press any key to continue...\n\n"
gets.chomp
end

def self.select_user
puts "\n-> Type the id or name of the user " #####
user = gets.chomp
puts "\n\n---Recipient Options---\n\n"
puts " 1. See Details"
puts " 2. Send Message"
puts "\nPlease, choose one of the options available"
recipient_option = gets.chomp.to_i
if recipient_option == 1
users_details(user)
elsif recipient_option == 2
puts "\n\n-> Type away..."
text = gets.chomp
user_message(text, user)
end
end

def self.select_channel
puts "\n-> Type the id or name of the channel "
channel = gets.chomp
puts "\n\n---Channel Options---\n\n"
puts " 1. See Details"
puts " 2. Send Message"
puts "\nPlease, choose one of the options available"
recipient_option = gets.chomp.to_i
if recipient_option == 1
puts Workspace.new.channel_details(channel)
elsif recipient_option == 2
puts "\n\n-> Type away..."
text = gets.chomp
channel_message(text, channel)
end
end

main if __FILE__ == $PROGRAM_NAME
def self.users_details(user)
begin
details = Workspace.users_details(user)
puts details
rescue StandardError
puts "\n\nThe user does not exist!"
end
puts "Press any key to continue...\n\n"
gets.chomp
end

def self.user_message(text, user)
begin
status = Workspace.user_message(text, user)
puts "\n\n---Message sucessfully sent!---" if status
rescue StandardError
puts "\n\nOops, something went wrong..."
puts "To send a message you must provide the ID (while we improve this feature)"
puts "Also, make sure you're choosing a valid user."
end
puts "Press any key to continue...\n\n"
gets.chomp
end

def self.channel_message(text, channel)
begin
status = Workspace.channel_message(text, channel)
puts "\n\n---Message sucessfully sent!---" if status
rescue StandardError
puts "\n\nOops, something went wrong..."
puts "Also, make sure you're choosing a valid channel."
end
puts "Press any key to continue...\n\n"
gets.chomp
end
end
end
SlackCLI::Main.main if $PROGRAM_NAME == __FILE__
70 changes: 70 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require_relative "workspace"
require "httparty"
require "dotenv"
Dotenv.load

module SlackCLI
USER_URL_MESSAGE = "https://slack.com/api/".freeze
API_KEY = ENV["SLACK_API_TOKEN"]

class User
attr_reader :name, :real_name, :id

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

def self.get(url, param)
query = { token: param }
response = HTTParty.get(url, query: query)
response
end

def self.list(url, param)
response = get(url, param)
members = response["members"]
formatted_list = []
members.each do |member|
name = member["name"]
real_name = member["real_name"]
id = member["id"]
formatted_list << new(name, real_name, id)
end
formatted_list
end

def self.details(user, url, param)

Choose a reason for hiding this comment

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

This method doesn't make sense as a class method since you want the details of a specific instance of User.

formatted_list = list(url, param)
details_user = ""
formatted_list.each do |member|
next unless member.name == user || member.id == user

details_user = "\n\nUSER'S DETAILS:
Name: #{member.name}
Real name: #{member.real_name}
Id: #{member.id}"
return details_user
end
raise ArgumentError, "The user you are trying to find does not exist"
end

def self.message(text, user)

Choose a reason for hiding this comment

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

Why not take advantage of the Recipient class?

response = HTTParty.post(
"#{USER_URL_MESSAGE}chat.postMessage",
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
body: {
token: API_KEY,
channel: user,
text: text
}
)
if response["ok"]
return true
else
raise ArgumentError, "Error when sending message to #{user}."
end
end
end
end
39 changes: 39 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "channel"
require "httparty"
require "dotenv"

Dotenv.load

module SlackCLI
class Workspace
attr_reader :users, :channels
USER_API = "https://slack.com/api/users.list".freeze
CHANNEL_API = "https://slack.com/api/channels.list".freeze
KEY = ENV["SLACK_API_TOKEN"]

def initialize
@users = SlackCLI::User.list(USER_API, KEY)
@channels = SlackCLI::Channel.list(CHANNEL_API, KEY)
end

def self.users_details(user)

Choose a reason for hiding this comment

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

These methods don't make sense a class methods since each Workspace has a list of users it could also keep track of the currently selected user and this method as an instance method could just return selected.details.

SlackCLI::User.details(user, USER_API, KEY)
end

def self.user_message(text, user)
SlackCLI::User.message(text, user)
end

def self.channel_message(text, channel)
SlackCLI::Channel.message(text, channel)
end

def channel_details(channel)
channel_found = @channels.find do |channel_object|
channel_object.name == channel || channel_object.id == channel
end
return "Channel not found" if !channel_found
channel_found.details
end
end
end
Loading