Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
40fe9e3
Created a workspace test
Mar 10, 2020
2e30c67
Created workspace initailize
Mar 10, 2020
9e37e91
Removed token after that fun email from Slack!
Mar 11, 2020
0667d42
Created a channel array
Mar 11, 2020
3f8a72a
Channel - created initialize &list_channels method
Mar 13, 2020
c55cca0
Recipent- created initialize &get method
Mar 13, 2020
3976c85
Refactored slack so it only hold the main method
Mar 13, 2020
e5feef4
User- created initialize &list_user
Mar 13, 2020
4600771
Moved tokens call to workspace from slack
Mar 13, 2020
5460c0a
Add VCR to channel test
Mar 13, 2020
042a589
added require channels and users to test helper
Mar 13, 2020
62aba6a
Added vcr to workspace
Mar 13, 2020
75ae60b
VCR-tapes 0312
Mar 13, 2020
6e27486
Channels - added 3 test
Mar 13, 2020
7a80936
User- added user test
Mar 13, 2020
fbafcd7
VCR-channels
Mar 13, 2020
dc20dd5
Cleaned up and added apporiapate notes
Mar 13, 2020
06c8d61
Channels - wrote test for Slack Error
Mar 13, 2020
da6401e
Recipent - SlackAPIError class added
Mar 13, 2020
04c2eba
slack.rb - add skelton
Mar 13, 2020
6f41a1d
Updated users
Mar 13, 2020
d360cb9
Channels.rb tested for an error
Mar 13, 2020
7c2c885
User- test added VCR
Mar 13, 2020
778d8b7
Refactored wording
Mar 13, 2020
e5cb0f4
Refactor
Mar 14, 2020
135a3e2
changed the Channel.rb raised an error flagg
Mar 14, 2020
f9d9eff
Added small testing to recipent to test the calling
Mar 14, 2020
0d6d5a0
vcr recipient
Mar 14, 2020
63c4d40
cleaned notes on slack rb.
Mar 15, 2020
82abf67
user and channel.rbs fixed typos
Mar 15, 2020
043daac
channel.rb & user.rb add details method
Mar 16, 2020
ada062d
Channels.rb details method and error handling
Mar 16, 2020
2d27109
Reciepnt add a reader
Mar 16, 2020
0e0279b
Workspace add show_details method
Mar 16, 2020
3cceb2a
WSP_test - added test for select user & chan .meth
Mar 16, 2020
9313f8c
Recipent.rb added send_message
Mar 16, 2020
d5299a2
slack.rb updated puts statments and menu
Mar 16, 2020
f9a0693
Workspace.rb added send_message method
Mar 16, 2020
de52abb
Channel test added test show details method
Mar 16, 2020
be36a5b
User_test added test details
Mar 16, 2020
12dd7fe
Workspace test for show details
Mar 16, 2020
73da58c
updated
Mar 16, 2020
1451f6f
updated slack error code
Mar 28, 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
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
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 "recipient"

class Channel < Recipient
attr_accessor :topic, :member_count

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

# called in the workspace
# in this list channel method we are calling on the Recipient

def details
return "Channel name:#{name}","Slack ID: #{slack_id}", "Topic:#{topic}", "Member count:#{member_count}"
end

def self.list_channels
response = Recipient.get("channels.list")

unless response["ok"] == true
raise SlackError, "There was an error. The error message is #{response["error"]}"
end

channel_list = response["channels"].map do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["topic"]
member_count = channel["members"].length

Channel.new(slack_id, name, topic, member_count)
end
return channel_list
end
end
39 changes: 39 additions & 0 deletions lib/channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "recipient"

class Channel < Recipient
attr_accessor :topic, :member_count

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

# called in the workspace
# in this list channel method we are calling on the Recipient

def details
return "Channel name: #{name}
Slack ID: #{slack_id}
Topic: #{topic}
Member count: #{member_count}"
end

def self.list_channels
response = Recipient.get("channels.list")

unless response["ok"] == true
raise SlackError, "There was an error. The error message is #{response["error"]}"
end

channel_list = response["channels"].map do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["topic"]
member_count = channel["members"].length

Channel.new(slack_id, name, topic, member_count)
end
return channel_list
end
end
37 changes: 37 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "httparty"
require "dotenv"
require "table_print"

class Recipient
attr_reader :slack_id, :name

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

def send_message(message)
url = "https://slack.com/api/chat.postMessage?token=#{SLACK_TOKEN}&channel=#{@slack_id}&text=#{message}"

response = HTTParty.post(url)

if response["ok"] != true || response.code != 200
raise SlackError(("There was an error posting this message: #{response["error"]}"))
else puts "Your message has been sent successfully!" end

return response
end

def self.get(sub_url)
# self calls only know about its self to call in another method
# must call the (Class name).something
# Recipent.get(querty) is called on the respective class that
url = "https://slack.com/api/#{sub_url}?token=#{SLACK_TOKEN}&pretty=1"

response = HTTParty.get(url)
return response
end
end

class SlackError < Exception
end
86 changes: 82 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,90 @@
#!/usr/bin/env ruby
require "dotenv"
require "table_print"

Dotenv.load

require_relative "workspace"

#user of program will see this menu
def menu
puts "Menu"
puts "1. List Channels"
puts "2. List Users"
puts "3. Select User"
puts "4. Select Channel"
puts "5. Send Message"
puts "6. Quit"
end

# all the user interface
def main
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
# sets and instance of workplace and makes it an instance variable
@workspace = Workspace.new
# i is set to negative -1
# as long a user of the program doesn't chose to "quit" the program will countine to run thru the while loop
# i will then become 1 which is more than 1 and will end the program

### need to rescue the Invalid Response error
i = -1
while i < 0
menu
puts "Your workspace has #{@workspace.channels.length} channels and #{@workspace.users.length} users."
puts "--------------------"
puts "Please make a menu selection. You selection by number of keywords"
answer = gets.chomp.downcase

# TODO project
# menu options aarray
choice = ["1", "list channels", "2", "user channels", "3", "select_user", "4", "5", "select_channel", "6", "quit"]

# this block handles errors of user input and resuces them so the program is not completely exited
begin
if !choice.include?(answer)
raise StandardError.new "This exception will be rescued!"
end
rescue StandardError => exception
p exception.message
end
if answer == "list channels" || answer == "1"
# puts workspace.list_channels (method that shows channels)
tp @workspace.channels, :name, :topic, :member_count
elsif answer == "list users" || answer == "2"
# puts workspace.list_users table from user.rb
tp @workspace.users, :slack_id, :real_name, :name
elsif answer == "select user" || answer == "3"
# puts workspace.list_users
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "Please select a user by name or slack_id to send a message"
selection = gets.chomp
@workspace.select_user(selection)
details = @workspace.show_details
# confrims user selection
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "You've selected user: #{@workspace.selected.name}"
puts details
elsif answer == "select channel" || answer == "4"
# puts workspace.list_users
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "Please select a Channel by name or slack_id to send a message"
selection = gets.chomp
@workspace.select_channel(selection)
details = @workspace.show_details
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "You've selected channel: #{@workspace.selected.name}"
puts details
elsif answer == "send message" || answer == "5"
puts "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}"
puts "Please enter your message here to #{@workspace.selected.name}"
message = gets.chomp
@workspace.send_message(message)
elsif answer == "quit" || answer == "6"
i = 1
end #if loop
end #while loop
puts "Thank you for using the Ada Slack CLI"
end
end # main

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

class User < Recipient
attr_accessor :real_name, :status_text, :status_emoji

# every time
def initialize(slack_id, name, real_name, status_text = nil, status_emoji = nil)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def details
return "Name: #{@name}", "Slack_ID: #{@slack_id}", "Real_Name:#{@real_name}"
end

def self.list_users
# also can use self.
response = Recipient.get("users.list")

unless response["ok"] == true
raise SlackError, "There was an error. The error message is #{response["error"]}"
end

user_list = response["members"].map do |user|
slack_id = user["id"]
name = user["name"]
real_name = user["profile"]["real_name"]
status_text = user["profile"]["status_text"]
status_emoji = user["profile"]["status_emoji"]

User.new(slack_id, name, real_name, status_text, status_emoji)
end
return user_list
end
end #load_all method
36 changes: 36 additions & 0 deletions lib/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "httparty"
require "dotenv"
require_relative "recipient"
require "table_print"

class User < Recipient
attr_accessor :real_name, :status_text, :status_emoji

# every time
def initialize(slack_id, name, real_name, status_text = nil, status_emoji = nul)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def self.list_users
# also can use self.
response = Recipient.get("users.list")

if response["ok"] != true
raise SlackAPI::SlackAPIError, "There was an error. The error message is #{response["error"]}"
else
user_list = response["members"].map do |user|
slack_id = user["id"],
name = user["name"],
real_name = user["profile"]["real_name"],
status_text = user["profile"]["status_text"],
status_emoji = user["profile"]["status_emoji"]

User.new(slack_id, name, real_name, status_text, status_emoji)
end
end
return user_list
end
end #load_all method
66 changes: 66 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# create, call, and use User stuff and Channel stuff from here
# but User stuff and Channel stuff are defined in their own clases
require "dotenv"
require "httparty"
require "pry"
require_relative "recipient"
require_relative "channel"
require_relative "user"

Dotenv.load

SLACK_TOKEN = ENV["SLACK_TOKEN"]

class Workspace
attr_reader :users, :channels
attr_accessor :selected

def initialize
@users = User.list_users
@channels = Channel.list_channels
@selected = []
end

# - When I type `details`, the program should print out details for the currently selected recipient. What information is printed depends on whether it's a channel or a user.
# - If no recipient is currently selected, the program should let me know and return to the main command prompt.

def show_details
@selected.details
end

def send_message(message)
@selected.send_message(message)
end

def select_channel(selection)
@selected = @channels.find do |channel| channel.slack_id == selection || channel.name == selection end
return @selected
end

def list_channels
url = "https://slack.com/api/channels.list?token=#{SLACK_TOKEN}&pretty=1"
response = HTTParty.get(url)
channels_list = []
# looking for channels in response
response["channels"].each do |channel|
channels_list << channel["name"]
end
return channels_list
end

def select_user(selection)
@selected = @users.find do |user| user.slack_id == selection || user.name == selection end
return @selected
end

def list_users
url = "https://slack.com/api/users.list?token=#{SLACK_TOKEN}&pretty=1"
response = HTTParty.get(url)
users_list = []
# looking for user in response
response["members"].each do |user|
users_list << ([user["id"], user["profile"]["real_name"], user["name"]])
end
return users_list
end
end
Loading