Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Earth - Genevieve and Schanen #16

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
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
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
59 changes: 59 additions & 0 deletions lib/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'httparty'
require 'dotenv'

Dotenv.load



class Builder

class SlackAPIError < StandardError; end

attr_reader :id, :name

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

def self.get(url, parameters)

sleep(2)
response = HTTParty.get(url, query: parameters)

raise SlackAPIError, "API call failed - error: #{response["error"]}" if !response["ok"] && !response["error"].nil?

return response
end

def self.details

Choose a reason for hiding this comment

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

This is an instance method in the child classes so the abstract method here should be an instance method as well.


raise NotImplementedError, "Implement in child class"
end

def self.list_all

raise NotImplementedError, "Implement in child class"
end

def send_message(message)
url = "https://slack.com/api/chat.postMessage"
query = {
token: ENV["SLACK_TOKEN"],
channel: self.id,
text: message
}

sleep(2)

response = HTTParty.post(url, query: query)

unless response["ok"]
raise SlackAPIError, "Send message failed, error: #{response["error"]}"
end

return response
end


end
36 changes: 36 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative 'builder'



class Channel < Builder

SLACK_URL = "https://slack.com/api/conversations.list"

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
return "\n ID: #{id}\n Name: #{name}\n Topic:#{topic}\n Number of Members: #{member_count}"
end

def self.list_all
parameters = {token: ENV["SLACK_TOKEN"]}
response = self.get(SLACK_URL, parameters)

response["channels"].map do |channel|
new(
id: channel["id"],
name: channel["name"],
topic: channel["topic"]["value"],
member_count: channel["num_members"]
)
end
end
end
111 changes: 108 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,117 @@
#!/usr/bin/env ruby
#
require 'tabulo'
require 'table_print'
require_relative 'workspace'

VALID_ACTIONS = [
"list users",
"list channels",
"select user",
"select channel",
"details",
"send message",
"quit"
].freeze

def main
puts "Welcome to the Ada Slack CLI!"

tp.set :max_width, 120

puts "\nWelcome to the Ada Slack CLI!\n"
workspace = Workspace.new
puts "\n#{workspace.users.count} Users loaded!"
puts "#{workspace.channels.count} Channels loaded!"

input = request_input
input = validate_input(input)

until input == "quit"
if input == "list users"
table = Tabulo::Table.new(workspace.users, title: "USER LIST")

# TODO project
table.add_column("ID", &:id)
table.add_column("NAME", &:name)
table.add_column("REAL NAME", &:real_name)

puts table.pack
elsif input == "list channels"
table = Tabulo::Table.new(workspace.channels, title: "CHANNEL LIST")

table.add_column("ID", &:id)
table.add_column("NAME", &:name)
table.add_column("MEMBER COUNT", &:member_count)
table.add_column("CHANNEL TOPIC", &:topic)

table.pack(max_table_width: 80)
puts table
elsif input == "select channel" || input == "select user"
makes_selection(workspace)
elsif input == "details"
gets_details(workspace)
elsif input == "send message"
sending_message(workspace)
end
input = request_input
end

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

main if __FILE__ == $PROGRAM_NAME
def request_input
puts "\n Please select an action:\n\n"
VALID_ACTIONS.each_with_index do |action, index|
puts " #{index + 1}. #{action}"
end
print "\n===> "
selection = gets.chomp.downcase
validate_input(selection)
end

def validate_input(input)
until VALID_ACTIONS.include?(input)
puts "Sorry, that's not a valid selection"
input = request_input
validate_input(input)
end
return input
end

def sending_message(workspace)
if workspace.current_selection.nil?
puts "Sorry, a user or channel must be selected first"
else
puts "Please enter a message:"
message = gets.chomp
begin
confirmation = workspace.send_message(message)
puts confirmation
rescue ArgumentError
puts "Sorry, your message must have at least one character"
rescue MessageError
puts "Sorry, something went wrong - your message was not sent"
end
end
end

def gets_details(workspace)
if workspace.current_selection.nil?
puts "Sorry, a user or channel must be selected first"
else
puts workspace.show_details
end
end

def makes_selection(workspace)
puts "\nPlease enter an ID or name:"
print "\n===> "
identifier = gets.chomp
begin
response = workspace.select_attribute(identifier)
puts response
rescue ArgumentError
puts "No user or channel found"
end
end

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

require_relative 'builder'

SLACK_URL = "https://slack.com/api/users.list"

class User < Builder

attr_reader :real_name

def initialize(id:, name:, real_name: )

super(id: id, name: name)
@real_name = real_name

end

def details
return "\n ID: #{id}\n Username: #{name}\n Real Name: #{real_name}"
end

def self.list_all
parameters = {token: ENV["SLACK_TOKEN"]}
response = self.get(SLACK_URL, parameters)

response["members"].map do |user|
new(
id: user["id"],
name: user["name"],
real_name: user["profile"]["real_name"]
)
end
end

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

require_relative 'user'
require_relative 'channel'

class Workspace
class MessageError < StandardError; end

attr_reader :users, :channels, :current_selection

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

def select_attribute(identifier)
if identifier[0] == "U"
@current_selection = users.find { |user| user.id == identifier }
elsif identifier[0] == "C"
@current_selection = channels.find { |channel| channel.id == identifier }
else
@current_selection = search_names(identifier)
end
if current_selection.nil?
raise ArgumentError, "Search unsuccesful"
end

return "Selected #{current_selection.class}: #{current_selection.name}"
end

def show_details
return current_selection.details
end

def send_message(message)
raise ArgumentError, "Message must have at least one character" if message.length == 0

response = current_selection.send_message(message)

if response["ok"]
return "Message sent to #{current_selection.class}: #{current_selection.name}"
else
raise MessageError, "Something went wrong - message not sent"
end
end

private
def search_names(name)
selection = []
selection << users.find { |user| user.name == name }
selection << channels.find { |channel| channel.name == name }

if selection.first.nil? && selection.last.nil?
return nil
elsif selection.first.nil?
return selection.last
elsif selection.last.nil?
return selection.first
else
raise ArgumentError, "Multiple matching names"
end
end


end
Loading