Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
30aa57b
Updated files
seattlefurby17 Oct 6, 2020
a163d39
Set up environment to hide secrets
Beatress Oct 6, 2020
fa174ca
Updated .gitignore
seattlefurby17 Oct 6, 2020
e9738dc
Resolved merge conflict
Beatress Oct 6, 2020
d226388
Files updated
seattlefurby17 Oct 6, 2020
3e95c9e
Updated token_test file
Beatress Oct 6, 2020
f7049b2
Updated token_test
Beatress Oct 6, 2020
687b1de
Finished token_test API verification
Beatress Oct 6, 2020
90e57de
Create Recipient, user, and channel classes
seattlefurby17 Oct 6, 2020
bebea20
Updated classes
seattlefurby17 Oct 6, 2020
38021cb
Updated changes
seattlefurby17 Oct 6, 2020
c06a7ce
Finished class scaffolding
Beatress Oct 6, 2020
d9d56f6
Merged
Beatress Oct 6, 2020
908e7bf
Merged changes
seattlefurby17 Oct 6, 2020
252b5b2
Merged changes
seattlefurby17 Oct 6, 2020
4dcb082
First draft of workspace, recipient, user, and channel methods
Beatress Oct 7, 2020
2122f6c
Got TablePrint working to display table of users/channels'
Beatress Oct 7, 2020
15f429c
Changed gemfile and user.rb
seattlefurby17 Oct 7, 2020
e66c111
Merge branch 'master' of https://github.com/seattlefurby17/slack-cli …
seattlefurby17 Oct 7, 2020
660007a
First draft of driver code in slack.rb
seattlefurby17 Oct 8, 2020
9e098ed
Added ability to select users and channels and view details on them
Beatress Oct 8, 2020
76312ac
Updated methods in 3 classes
seattlefurby17 Oct 8, 2020
ab48fac
Workspace initialize method tests
seattlefurby17 Oct 8, 2020
688cd98
Finished workspace unit tests
Beatress Oct 8, 2020
1667bbd
Update testing for workspace and channel
seattlefurby17 Oct 8, 2020
b15a42c
Added tests for User class
Beatress Oct 9, 2020
814dddc
Updated methods for wave 3
seattlefurby17 Oct 9, 2020
e549e99
Updated Wave 3 send_message method
seattlefurby17 Oct 9, 2020
3b5f69e
Wave 3 files updated
seattlefurby17 Oct 9, 2020
2adb74e
Made sure send_message was using cassette + DRYd up code
Beatress Oct 9, 2020
f36c7c7
Updated testings for send_message method.
seattlefurby17 Oct 9, 2020
93e00b6
Wave 3 completed, all tests passed
seattlefurby17 Oct 9, 2020
88fd821
Updated files base on review comments from instructor Devin
seattlefurby17 Oct 27, 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

.DS_Store

# Ignore environemnt variables
# Ignore environment variables
# This is how you add .env to .gitignore
.env
.idea
command-query
18 changes: 18 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

source 'https://rubygems.org'
ruby '~> 2.6.5'

group :development do
gem 'dotenv'
gem 'guard'
gem 'guard-minitest'
gem 'httparty'
gem 'minitest-reporters'
gem 'minitest-vcr'
gem 'rake'
gem 'table_print'
gem 'vcr'
gem 'webmock'
end

92 changes: 92 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
builder (3.2.4)
coderay (1.1.3)
crack (0.4.4)
dotenv (2.7.6)
ffi (1.13.1)
formatador (0.2.5)
guard (2.16.2)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-minitest (2.4.6)
guard-compat (~> 1.2)
minitest (>= 3.0)
hashdiff (1.0.1)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lumberjack (1.2.8)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
minispec-metadata (2.0.0)
minitest
minitest (5.14.2)
minitest-reporters (1.4.2)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
minitest-vcr (1.4.0)
minispec-metadata (~> 2.0)
minitest (>= 4.7.5)
vcr (>= 2.9)
multi_xml (0.6.0)
nenv (0.3.0)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
rake (13.0.1)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
ruby-progressbar (1.10.1)
shellany (0.0.1)
table_print (1.5.7)
thor (1.0.1)
vcr (6.0.0)
webmock (3.9.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby

DEPENDENCIES
dotenv
guard
guard-minitest
httparty
minitest-reporters
minitest-vcr
rake
table_print
vcr
webmock

RUBY VERSION
ruby 2.6.5p114

BUNDLED WITH
2.1.4
35 changes: 35 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Channel < Recipient
attr_reader :slack_id, :name, :topic, :member_count

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

def get_details
return "Slack ID: #{slack_id}\n" + "Name: #{name}\n"\
"Topic: #{topic}\n" + "Member Count: #{member_count}"
end

def self.list_all
url = 'https://slack.com/api/conversations.list'
params = { token: ENV['SLACK_TOKEN'] }
response = Channel.get(url, params)
channel_list = []

response['channels'].each do |channel|
topic = channel['topic']['value']
topic = 'No topic set' if topic == ''
channel_list << Channel.new(
slack_id: channel['id'],
name: channel['name'],
topic: topic,
member_count: channel['num_members']
)
end
return channel_list
end

end

57 changes: 57 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'dotenv'
require 'httparty'

Dotenv.load

BASE_URL = 'https://slack.com/api'
API_KEY = ENV['SLACK_TOKEN']

class Recipient
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)
sleep(0.5)
if response.code != 200
raise SlackApiError, 'Slack API call failed!'
elsif response['ok'] != true
raise SlackApiError, (response['error']).to_s
end

return response
end

def send_message(message)
response = HTTParty.post(
"#{BASE_URL}/chat.postMessage",
body: {
token: API_KEY,
text: message,
channel: slack_id
},
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
)
sleep(0.5)
unless response.code == 200 && response.parsed_response['ok']
raise SlackApiError, "Error when posting #{message} to #{name}, error: #{response.parsed_response['error']}"
end

return true
end

# These are just template methods to be implemented in User and Channel
def details
raise NotImplementedError, 'Implement me in the child class'
end

def self.list_all
raise NotImplementedError, 'Implement me in the child class'
end
end
101 changes: 97 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,105 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative 'workspace'
require 'table_print'

def get_command
puts 'What would you like to do? '
puts '1 = list users'
puts '2 = list channels'
puts '3 = select user'
puts '4 = select channel'
puts '5 = details'
puts '6 = send message'
puts '7 = quit/q'
command = gets.chomp.downcase
return command
end

def get_object_type
puts 'Great! Do you want to select by name or ID?'
puts '1. Name'
puts '2. ID'
command = gets.chomp.downcase
loop do
case command
when '1', 'name'
return 'name'
when '2', 'id'
return 'slack_id'
else
puts 'Please enter a valid choice!'
command = gets.chomp.downcase
end
end
end

def message_sent(workspace)
if workspace.selected
puts 'What is your message?'
message = gets.chomp
begin
success = workspace.selected.send_message(message)
rescue SlackApiError => exception
puts "Messages can not be send, #{exception.message}"
end
puts "Message \"#{message}\" was sent to #{workspace.selected.name}!" if success
else
puts 'Please select a user or channel first'
end
end

def main
puts "Welcome to the Ada Slack CLI!"
puts 'Welcome to the Ada Slack CLI!'
puts 'Buffering...'
workspace = Workspace.new
puts "We have #{workspace.channels.length} channels and #{workspace.users.length} users."

command = get_command
while command != '7' && command != 'quit' && command != 'q'
case command
when '1', 'list users'
tp workspace.users, 'slack_id', 'name', 'real_name'

when '2', 'list channels'
tp workspace.channels, 'name', 'topic', 'member_count', 'slack_id'

when '3', 'select user'
type = get_object_type
puts "Enter the #{type}:"
input = gets.chomp
if workspace.select_user(input, type)
puts "User #{workspace.selected.name} selected!"
else
puts 'User not found! Please try again.'
end

when '4', 'select channel'
type = get_object_type
puts "Enter the #{type}:"
input = gets.chomp
if workspace.select_channel(input, type)
puts "Channel #{workspace.selected.name} selected!"
else
puts 'Channel not found! Please try again.'
end

# TODO project
when '5', 'details'
if workspace.selected
puts workspace.selected.get_details
else
puts 'No user/channel selected. Select one and try again'
end
when '6', 'send message'
message_sent(workspace)
else
puts 'Please only select the commands from the list'
end
command = get_command

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

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
45 changes: 45 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class User < Recipient
attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji

def initialize(slack_id:, name:, real_name:, status_text:, status_emoji:)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def get_details
return "Slack ID: #{slack_id}\n" + "Name: #{name}\n"\
"Real Name: #{real_name}\n" + "Status Text: #{status_text}\n"\
"Status Emoji: #{status_emoji}"
end

def self.list_all
url = 'https://slack.com/api/users.list'
params = { token: ENV['SLACK_TOKEN'] }
begin
response = User.get(url, params)
rescue SlackApiError => exception
puts "Unsuccessful request, #{exception.message}. Please try again"
exit
end
user_list = []

response['members'].each do |user|
status_text = user['profile']['status_text']
status_text = 'No status' if status_text == ''
status_emoji = user['profile']['status_emoji']
status_emoji = 'No emoji' if status_emoji == ''

user_list << User.new(
slack_id: user['id'],
name: user['name'],
real_name: user['real_name'],
status_text: status_text,
status_emoji: status_emoji
)
end
return user_list

end
end
Loading