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
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rake/testtask'
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/*_test.rb']
t.test_files = FileList["test/*_test.rb"]
end

task default: :test
25 changes: 25 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "dotenv"
require "httparty"
require "awesome_print"

require_relative "recipient"

Dotenv.load

module SlackCLI
class Channel < Recipient
BASE_URL = "https://slack.com/api/channels.list?"

attr_reader :topic, :member_count

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

def print_details

Choose a reason for hiding this comment

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

Minor suggestion, I would call this method get_details just because it doesn't actually print.

return { topic: topic, member_count: member_count, slack_id: slack_id, name: name }
end
end
end
35 changes: 35 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "httparty"

module SlackCLI
class SlackApiError < Exception; end

class Recipient
Comment on lines +3 to +6

Choose a reason for hiding this comment

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

👍

MESSAGE_URL = "https://slack.com/api/chat.postMessage"
attr_reader :slack_id, :name

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

def send_message(message, selected)
response = HTTParty.post(
MESSAGE_URL,
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
},
body: {
token: ENV["SLACK_TOKEN"],
text: message,
channel: selected.slack_id,
},
)

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

return true
end
end
end
79 changes: 74 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
#!/usr/bin/env ruby
require "dotenv"
require "httparty"
require "table_print"
require "awesome_print"

require_relative "workspace"

Dotenv.load

SLACK_TOKEN = ENV["SLACK_TOKEN"]

def allowed_input
puts "Your options are: list users, list channel, select user,
select channel, details, and send message or quit: what would you like to see?"
return gets.chomp()
end

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
workspace = SlackCLI::Workspace.new
puts "Welcome to the Ada Slack CLI!
at this time, our workspace has #{workspace.users.count} users
and #{workspace.channels.count} channels."

user_input = allowed_input

until user_input == "quit" || user_input == "exit"
case user_input

when "list users"
tp workspace.users, "slack_id", "name"
puts "\n"
when "list channel"
tp workspace.channels, "name", "topic", "member_count", "slack_id"
puts "\n"
when "select user"
puts "please enter a user name or slack_id"
user_input = gets.chomp()
if workspace.select_user(user_input) == nil
puts "no user with that user name exist"
else
puts "selected user with id #{workspace.selected.slack_id}"
end
when "select channel"
puts "please enter a channel name or slack_id"
user_input = gets.chomp()
if workspace.select_channel(user_input) == nil
puts "no channel with that channel name exist"
else
puts "selected channel with id #{workspace.selected.slack_id}"
end
when "detail", "details"
if workspace.selected == nil
puts "no user selected"
else
ap workspace.selected.print_details
end
if workspace.selected == nil
puts "no channel selected"
else
ap workspace.selected.print_details
end
when "send message"
if workspace.selected == nil
puts "you need to selec a user or a channel."
puts "\n"
else
puts "enter your message: "
user_input = gets.chomp()
success = workspace.send_message(user_input)
puts "\n"
puts "sent message" if success
end
end

# TODO project
user_input = allowed_input
end

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

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

Dotenv.load

module SlackCLI
class User < Recipient
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

👍

BASE_URL = "https://slack.com/api/users.list?"
attr_reader :channels, :name, :slack_id

def initialize(name, slack_id, channels = [])
super(slack_id: slack_id, name: name)
@channels = channels
end

def print_details
return { name: name, slack_id: slack_id, channels: channels }
end
end
end
76 changes: 76 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require_relative "user"
require_relative "channel"
require "pry"

module SlackCLI
class SlackApiError < Exception; end

class Workspace
attr_reader :users, :channels, :selected, :selected, :query

Dotenv.load

Choose a reason for hiding this comment

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

I noticed you have this line in several places, just maybe there is a way to only need to do this once. Otherwise well done.


def initialize()
@query = { token: SLACK_TOKEN }
@users = self.list_all_user
@channels = self.list_all_channel
@selected = nil
@selected = nil
end

def list_all_user
all_users = []
response = HTTParty.get(SlackCLI::User::BASE_URL, query: query)["members"]

response.each do |person|
all_users << SlackCLI::User.new(person["name"], person["id"])
end
return all_users
end
Comment on lines +21 to +29

Choose a reason for hiding this comment

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

Methods like this for user and Channel could be instead class methods in the User and Channel class and that would help separate the command-line stuff in workspace with the HTTP requests in Recipient, Channel and User.


def list_all_channel
all_channel = []
response = HTTParty.get(SlackCLI::Channel::BASE_URL, query: query)["channels"]
response.each do |channel|
all_channel << SlackCLI::Channel.new(
channel["topic"]["value"],
channel["members"].length,
channel["name"],
channel["id"]
)
end
return all_channel
end

def select_user(user_id)
@users.each do |user|
if user_id == user.slack_id
@selected = user
elsif user_id == user.name
@selected = user
end
end
return @selected
end

def select_channel(channel_id)
@channels.each do |channel|
if channel_id == channel.slack_id
@selected = channel
elsif channel_id == channel.name
@selected = channel
end
end
return @selected
end

#get request, client wants data from the server (doens't make any change)
#post request, changing stuff on the server (makes changes)

def send_message(message)
if @selected
return @selected.send_message(message, @selected)
end
end
end
end
Loading