forked from AdaGold/slack-cli
-
Notifications
You must be signed in to change notification settings - Fork 31
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
schanenR
wants to merge
25
commits into
Ada-C14:master
Choose a base branch
from
schanenR:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8715e45
hide slack token
geneminde 1c20348
script to check if token works
geneminde bcec2d9
added workspace class
schanenR babdb0f
builder class initialized and tests started
schanenR 69f006c
builder, user & channel class initialized. initial tests written and …
schanenR 4c38773
implement Builder#get & tests
geneminde 1713594
bug fix in user class
schanenR 156a6d3
mergre conflics resolved
schanenR 540ae2a
wrote builder methods and accompanying test, most passing
schanenR 7da7f06
typo fixes
geneminde 7768dfa
implement User#details + test
geneminde 6c88d07
implement User#load_all + tests
geneminde b6bd809
implemented Chanel#load_all + tests
geneminde b47aad4
expand Builder#init + tests
geneminde 9f8706d
implemented Workspace#select_attribute + tests
geneminde efa6ad9
implement Workspace#show_details + tests
geneminde d689ad4
implement Workspace#send_message + tests
geneminde 0031034
CLI printing draft 1
schanenR 22bf108
driver code
geneminde b3b6fbf
various typos
geneminde a1dc0f4
refactor main
geneminde e554d16
changed some it statements
geneminde 4483da3
merge
schanenR a9d5faf
resolve conflicts
schanenR fbad480
CLI output formatting draft 1
schanenR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.