Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
97d7f3d
Setup environment
kdow Mar 19, 2019
6e0dee7
More environment setup
norrise120 Mar 19, 2019
29cfeef
Add SlackCLI module and Recipient constructor test
kdow Mar 19, 2019
deb3634
Wrote constructor for Recepicient, fixed recipient spec
norrise120 Mar 19, 2019
58df5a4
Added test for self.get to Recipient spec and removed VCR from constr…
norrise120 Mar 19, 2019
166fb29
Add recipient self.get method
kdow Mar 19, 2019
72304f9
Add recipient self.list
kdow Mar 19, 2019
c07db08
Change recipient to take kwargs and wrote constructor tests for User
kdow Mar 19, 2019
d9c962e
Wrote constructor for User class
norrise120 Mar 19, 2019
37e9abb
"Wrote test for self.list for User class"
norrise120 Mar 19, 2019
8f16cfc
Create user.list method
kdow Mar 19, 2019
658fbc8
Added constructor test for channel class
norrise120 Mar 19, 2019
c81f7d5
Create Channel constructor
kdow Mar 19, 2019
87c5bd3
Create channel.list test
kdow Mar 19, 2019
7e66ef9
Created self.list for Channel class
norrise120 Mar 19, 2019
f436034
Add workspace constructor tests
kdow Mar 19, 2019
85a195d
Wrote constructor for Workspace and updated test helper to include wo…
norrise120 Mar 19, 2019
ede491e
Display number of users and channels when program is launched
kdow Mar 20, 2019
1a287b7
Updated Slack.rb to ask user for input and list possible options so f…
norrise120 Mar 20, 2019
112cd39
Add functionality to list users and channels
kdow Mar 20, 2019
a7138ae
Write tests for select_user and add additional options to main
kdow Mar 20, 2019
b5478f3
Created select_user and find_user for workspace class, passed tests
norrise120 Mar 20, 2019
a36f2b7
Added tests for select_channel and updated tests for select_user
norrise120 Mar 20, 2019
b62b045
Add select_channel and find_channel methods to workspace
kdow Mar 20, 2019
4438a24
Create details method in recipient and details test for user
kdow Mar 20, 2019
adfeb09
Created details for user class
norrise120 Mar 20, 2019
a688cb8
Added tests for channel details method
norrise120 Mar 20, 2019
78628bd
Add details method to channel and added select user and channel funct…
kdow Mar 20, 2019
fd5dba1
Added tests for show_details
norrise120 Mar 20, 2019
f6ffe2d
Wrote method for show details and added its functionality into main
kdow Mar 20, 2019
4180431
Added recipient send message test
norrise120 Mar 20, 2019
2ef85bd
Add send_message to recipient
kdow Mar 21, 2019
9504122
Fix typos
kdow Mar 21, 2019
5f8ef78
create SlackApiError class
kdow Mar 21, 2019
a21aff7
Added send message to Workspace class
norrise120 Mar 21, 2019
383341b
Update error handling for recipient send_message and add test for it
kdow Mar 22, 2019
c6c2086
Added error handling and test for self.get in recipient class
norrise120 Mar 22, 2019
0f8aa9e
Add tests for workspace send_message
kdow Mar 22, 2019
1b7bbbb
Updated recipient for send message, updated slack to include send mes…
norrise120 Mar 22, 2019
d8e88aa
DRYed workspace.rb
norrise120 Mar 22, 2019
32b66ce
Format output with table_print
kdow Mar 22, 2019
f7a0ed8
Hide main code from SimpleCov
kdow 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ build-iPhoneSimulator/

# Ignore cassette files
/specs/cassettes/

# Ignore test file
test.rb

38 changes: 38 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "recipient"
require 'dotenv'
Dotenv.load

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

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

@topic = topic
@member_count = member_count
end

def self.list
url = "https://slack.com/api/channels.list"
data = {
"token": ENV["SLACK_API_TOKEN"],
}
response = self.get(url, data)
channels = []
response["channels"].each do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["topic"]["value"]
member_count = channel["num_members"]
channel = self.new(slack_id: slack_id, name: name, topic: topic, member_count: member_count)
channels << channel
end
return channels
end

def details
return "Slack ID: #{slack_id}, Name: #{name}, Topic: #{topic}, Member count: #{member_count}"
end
end
end
51 changes: 51 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "httparty"

module SlackCLI
class Recipient
class SlackApiError < StandardError; end

attr_reader :slack_id, :name

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

def self.get(url, params)
response = HTTParty.get(url, query: params)
unless response.code == 200 && response.parsed_response["ok"]
raise SlackApiError, "Error when getting response, error: #{response.parsed_response["error"]}"
end
return response
end

def send_message(message)
url = "https://slack.com/api/chat.postMessage"
api_key = ENV["SLACK_API_TOKEN"]

body = {
token: api_key,
text: message,
channel: self.slack_id,
}
headers = {"Content-Type" => "application/x-www-form-urlencoded"}
response = HTTParty.post(url, body: body, headers: headers)

unless response.code == 200 && response.parsed_response["ok"]
raise SlackApiError, "Error when posting #{message} to #{self.slack_id}, error: #{response.parsed_response["error"]}"
end

return true
end

private

def self.list
raise NotImplementedError, "Implement me in a child class!"
end

def details
raise NotImplementedError, "Implement me in a child class!"
end
end
end
83 changes: 81 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,90 @@
#!/usr/bin/env ruby
require_relative "workspace.rb"
require "table_print"

# :nocov:

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

# TODO project
workspace = SlackCLI::Workspace.new
puts "There are #{workspace.channels.count} channels in this workspace"
puts "There are #{workspace.users.count} users in this workspace"

puts "You can: List Users, List Channels, Select User, Select Channel, Details, Send Message, or Quit."
puts "Please enter what option you would like to take. You can quit by entering 'quit'."
response = gets.chomp.downcase
while response != "quit"
if response == "list users"
list_users(workspace)
elsif response == "list channels"
list_channels(workspace)
elsif response == "select user"
select_user_by_input(workspace)
elsif response == "select channel"
select_channel_by_input(workspace)
elsif response == "details"
puts get_details(workspace)
elsif response == "send message"
send_message(workspace)
else
puts "Please make sure to select from the choices above. You can quit be entering 'quit'."
end
puts "\nPlease enter what you would like to do: \nList Users \nList Channels \nSelect User \nSelect Channel \nDetails \nSend Message \nQuit"
puts
response = gets.chomp.downcase
end

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

main if __FILE__ == $PROGRAM_NAME
def list_users(workspace)
tp workspace.users, "slack_id", "name", "real_name"
end

def list_channels(workspace)
tp workspace.channels, "slack_id", "name", "topic", "member_count"
end

def select_user_by_input(workspace)
puts "What is the Slack ID or username? "
user_info = gets.chomp
user = workspace.select_user(user_info)
if user == nil
puts "We couldn't find that user"
else
puts "User has been selected"
end
end

def select_channel_by_input(workspace)
puts "What is the Slack ID or name? "
channel_info = gets.chomp
channel = workspace.select_channel(channel_info)
if channel == nil
puts "We couldn't find that channel"
else
puts "Channel has been selected"
end
end

def get_details(workspace)
detail_info = workspace.show_details
if detail_info == nil
puts "There is nothing selected to show details about. Please select a user or channel."
else
return detail_info
end
end

def send_message(workspace)
puts "What would you like to send?"
message = gets.chomp
if workspace.send_message(message)
puts "Message successfully sent"
else
puts "Please select a channel or user before sending message."
end
end
# :nocov:
main if __FILE__ == $PROGRAM_NAME
36 changes: 36 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative "recipient"
require "dotenv"
Dotenv.load

module SlackCLI
class User < Recipient
attr_reader :real_name

def initialize(slack_id:, name:, real_name:)
super(slack_id: slack_id, name: name)

@real_name = real_name
end

def self.list
url = "https://slack.com/api/users.list"
data = {
"token": ENV["SLACK_API_TOKEN"],
}

Choose a reason for hiding this comment

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

Most of this data is going to be common to all subclasses of Recipient, the only thing that's changing is the end of the URL (users.list). Could you move some or all of it to Recipient.get?

response = self.get(url, data)
users = []
response["members"].each do |member|
slack_id = member["id"]
name = member["name"]
real_name = member["real_name"]
user = self.new(slack_id: slack_id, name: name, real_name: real_name)
users << user
end
return users
end

def details
return "Slack ID: #{slack_id}, Username: #{name}, Real Name: #{real_name}"
end
end
end
52 changes: 52 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require_relative "user"
require_relative "channel"

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

def initialize
@users = SlackCLI::User.list
@channels = SlackCLI::Channel.list
@selected = nil
end

def select_user(user_info)
@selected = find_user(user_info)
end

def select_channel(channel_info)
@selected = find_channel(channel_info)
end

def show_details
return @selected == nil ? nil : @selected.details
end

def send_message(message)
return false if @selected == nil
@selected.send_message(message)
return true
end

private

def find_user(user_info)
@users.each do |user|
if user_info == user.slack_id || user_info == user.name
return user
end
end
return nil
end

def find_channel(channel_info)
@channels.each do |channel|
if channel_info == channel.slack_id || channel_info == channel.name
return channel

Choose a reason for hiding this comment

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

This code and find_user above are almost exactly the same. Could you DRY this up?

end
end
return nil
end
end
end
55 changes: 55 additions & 0 deletions specs/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative "test_helper"

describe SlackCLI::Channel do
describe "constructor" do
before do
@slack_id = "CH2SKTDBN"
@name = "Catssss"
@topic = "It's about cats, duh"
@member_count = "2,000,000"
@record = SlackCLI::Channel.new(slack_id: @slack_id, name: @name, topic: @topic, member_count: @member_count)
end

it "is an instance of Channel" do
expect(@record).must_be_kind_of SlackCLI::Channel
end

it "takes and saves a Slack id, name, topic, and member count" do
expect(@record.slack_id).must_equal @slack_id
expect(@record.name).must_equal @name
expect(@record.topic).must_equal @topic
expect(@record.member_count).must_equal @member_count
end
end

describe "self.list" do
it "returns an array of channels" do
VCR.use_cassette("channels") do
channels_array = SlackCLI::Channel.list

expect(channels_array).must_be_kind_of Array
expect(channels_array.first).must_be_kind_of SlackCLI::Channel
expect(channels_array.length).wont_equal 0
end
end
end

describe "details" do
before do
@slack_id = "CH2SKTDBN"
@name = "Catssss"
@topic = "It's about cats, duh"
@member_count = "2,000,000"
@record = SlackCLI::Channel.new(slack_id: @slack_id, name: @name, topic: @topic, member_count: @member_count)
@detail = @record.details
end

it "returns a string" do
expect(@detail).must_be_kind_of String
end

it "returns the correct information" do
expect(@detail).must_equal "Slack ID: #{@slack_id}, Name: #{@name}, Topic: #{@topic}, Member count: #{@member_count}"
end
end
end
Loading