Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
92e7125
Channel class and Slack_cli modulo and getting info (list_all method)
veralizeth Mar 13, 2020
322af90
Recipient class and get method to call the Slack API
veralizeth Mar 13, 2020
4dff683
User class and list_all abstract method to get the info of all users
veralizeth Mar 13, 2020
51797a1
Encapsulated everything in a Slack_cli modulo
veralizeth Mar 13, 2020
079e2a5
Initialize test and correct info about channels
veralizeth Mar 13, 2020
f179ef5
Initilize test and get method test
veralizeth Mar 13, 2020
6f1d615
filter_sensitive_data and require relatives
veralizeth Mar 13, 2020
98ff673
Details method added
veralizeth Mar 16, 2020
c0af31b
Raises and SlackApi class error when API call fails
veralizeth Mar 16, 2020
8198b96
CLI using tty-prompt gem and get_user and selector methods
veralizeth Mar 16, 2020
3602c3b
Detail method added
veralizeth Mar 16, 2020
06ff7f3
Select_user and channel methods, show details added
veralizeth Mar 16, 2020
ccebbcc
re-do test for list of channels
veralizeth Mar 16, 2020
b3f96cd
It raises an NotImplementedError when details and list_all invoke dir…
veralizeth Mar 16, 2020
d8ee7df
Update the require
veralizeth Mar 16, 2020
7aa0a5f
User instantiation, user_list and valid users test methods
veralizeth Mar 16, 2020
24e5013
Workspace instantiation,select_user and channel tests methods,and nil…
veralizeth Mar 16, 2020
44baec0
send_message method added
veralizeth Mar 16, 2020
fe7fee3
Send_message menu choice added, and method
veralizeth Mar 16, 2020
243654c
Require recipient to use send_message method, send_method added
veralizeth Mar 16, 2020
248b9cd
typos fixed recipient, send valid message test
veralizeth Mar 16, 2020
a8337b5
send message tests
veralizeth Mar 16, 2020
5d6751b
Deletin puts and colorized added
veralizeth Mar 16, 2020
5945472
Optional refactoring afert individual reflection
veralizeth Mar 24, 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
43 changes: 43 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative 'recipient'
require "httparty"

module Slack_cli
class Channel < Recipient
attr_reader :topic, :member_count

def initialize(id:, name:, topic:, member_count:)
super(id: id, name: name)

@topic = topic
@member_count = member_count
end

def details
tp self, "id", "name", "topic", "member_count"
end

private
# Get all the channels from Slack API.
# For each channel it creates an intance of channel.
def self.list_all

channels = []

base_url = "https://slack.com/api/"
post_url = "#{base_url}channels.list"
params = { token: ENV["SLACK_API_TOKEN"] }

response = get(post_url, params)

response["channels"].each do |channel|
channels << new(
id: channel["id"],
name: channel["name"],
topic: channel["topic"]["value"],
member_count: channel["num_members"]
)
end
return channels
end
end
end
56 changes: 56 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "httparty"
require "awesome_print"

require "dotenv"
Dotenv.load

module Slack_cli
class Recipient
attr_reader :id, :name

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

# Given the URL and params(KEY) get the response from the API.
def self.get(url,params)
resp = HTTParty.get(url, query: params)
if resp.code == 200 && resp.parsed_response["ok"]
return resp
end
raise SlackAPIError, "We encountered a problem: #{resp["error"]}"
end
# Takes channel as a parameter and text from the user.
def send_message(channel)
message = gets.chomp
base_url = "https://slack.com/api/"
post_url = "#{base_url}/chat.postMessage"

resp = HTTParty.post(post_url,{
headers: { 'Content-Type'=> 'application/x-www-form-urlencoded',
'charset' => 'utf-8' },
body:{
token: ENV["SLACK_API_TOKEN"],
channel: channel,
text: message
}
})
if resp.code == 200 && resp.parsed_response["ok"]
return true
end
raise SlackAPIError, "We encountered a problem: #{resp["error"]}"
end

def self.list_all
raise NotImplementedError, 'Implement me in a child class!'
end

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

class SlackAPIError < Exception
end
98 changes: 93 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,100 @@
#!/usr/bin/env ruby
# menu choice Ruby gem.
require "tty-prompt"
@prompt = TTY::Prompt.new

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

require "dotenv"
require "httparty"
require "awesome_print"
require "table_print"

# TODO project
require_relative 'workspace'
Dotenv.load

# Promt gem @promt -> Using the gem
def get_user_input
user_input = @prompt.select('What do you want to do:') do |menu|
# Each menu is assign to the varibale user_input > Depends of the user choise.
menu.choice "List of users"
menu.choice "List of channels"
menu.choice "Select Channel"
menu.choice "Select User"
menu.choice "Details"
menu.choice "Send message"
menu.choice "Exit"
end
return user_input
end

puts "Thank you for using the Ada Slack CLI"
# To handle the user_input, display the tables,show_details and send messages.
def selector(user_input)
case user_input
when "List of users"
# tp = To print the intances in a table.
puts "\n"
tp @workspace.users, :name, :real_name, :id
puts "\n"
when "List of channels"
puts "\n"
tp @workspace.channels, :name, :topic, :member_count, :id
puts "\n"
when "Select Channel"
print "Please enter the channel name or ID: "
user_selection = @workspace.select_channel
if user_selection == nil
puts "Invalid entry - the channel does not exist.".red
else
puts "#{user_selection} Has been chose".blue
end
puts "\n"
when "Select User"
print "Please enter the channel name or ID: "
user_selection = @workspace.select_user
if user_selection == nil
puts "Invalid entry - the user does not exist.".red
else
puts "#{user_selection} Has been chose".blue
end
puts "\n"
when "Details"

if @workspace.selected == nil
puts "\n"
puts "Invalid entry - You selected an invalid channel or User."
else
puts "\n"
@workspace.show_details
user_input = nil
end

when "Send message"
if @workspace.selected == nil
puts "\n"
puts "Invalid entry"
else
puts "\n"
@workspace.selected.send_message(@workspace.selected.name)
puts "Your message has been sent".blue
user_input = nil
end
when "Exit"
puts "Bye!! Thank you for using Ada slack CLI".yellow
exit
end
end

def main
puts "Welcome to the Ada Slack CLI!"
# Creates a new intance of workspace.
@workspace = Slack_cli::Workspace.new

user_input = nil
while true
user_input = get_user_input
selector(user_input)
end
end

main if __FILE__ == $PROGRAM_NAME
46 changes: 46 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative 'recipient'
require "httparty"

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

def initialize(id:, name:, real_name:, status_text:, status_emoji:)
super(id: id, name: name)

@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

# Table print the details.
def details
tp self, "id", "name", "real_name", "status_text", "status_emoji"
end

private

# Get all the users from Slack API.
# For each list_all creates an intance of user.
def self.list_all

users = []
base_url = "https://slack.com/api/"
post_url = "#{base_url}users.list"
params = { token: ENV["SLACK_API_TOKEN"] }

response = get(post_url, params)

response["members"].each do |user|
users << new(
id: user["id"],
name: user["name"],
real_name: user["real_name"],
status_text: user["profile"]["status_text"],
status_emoji: user["profile"]["status_emoji"]
)
end
return users
end
end
end
49 changes: 49 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# worksapce.rb
require_relative "user"
require_relative "channel"
require_relative "recipient"

module Slack_cli
class Workspace
attr_reader :users, :channels, :selected

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

def select_channel
user_input = gets.chomp
channels.each do |channel|
if user_input == channel.name || user_input == channel.id
@selected = channel
return @selected.name
end
end
return @selected = nil
end

def select_user

user_input = gets.chomp

users.each do |user|
if user_input == user.real_name || user_input == user.id
@selected = user
return @selected.name
end
end
return @selected = nil
end

def show_details
@selected.details
end
# Based on the channel selected.
def send_message
@selected.send_message(channel)
end
end
end

84 changes: 84 additions & 0 deletions test/cassettes/channels-list-endpoint.yml

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

Loading