Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
74e0a9e
updated slack.rb to acess slack workspace
Sep 11, 2019
c8ec539
created api_verification test and recipient class
Sep 11, 2019
42ccaf6
initialized class channel and tests
Sep 11, 2019
87487e6
initialized class user and tests
Sep 11, 2019
865d4c0
initialized class workspace
Sep 11, 2019
4615362
updated gitignore
Sep 11, 2019
f73e4ba
updated test_helper with relatives
Sep 11, 2019
1e404da
created methods self.get in Recipent and self.list in User and Channel
iamsammysam Sep 11, 2019
a296fd4
fixed bug at recipient.rb
iamsammysam Sep 11, 2019
a93d007
created tests with VCR for user and channel
iamsammysam Sep 11, 2019
654f453
All wave 1 method tests passing. Added and tested wave 1 CLI requirem…
iamsammysam Sep 12, 2019
a5f4665
added VCR to workspace test and uptadated tests
iamsammysam Sep 12, 2019
638df86
created method select_user and tests
iamsammysam Sep 13, 2019
28c942e
Select user tests all passing
Sep 13, 2019
7eb0e64
Created loop for CLI that asks user to select again unless they choos…
Sep 13, 2019
99e7d8b
Added tests and method for Workspace.select_channel. Updated assert n…
Sep 13, 2019
e56fa67
created method detais for user and channel
iamsammysam Sep 13, 2019
ce0a3c4
created details method and refined loop at slack.rb
iamsammysam Sep 13, 2019
bb5619b
implemented user_message and send_message methods
iamsammysam Sep 13, 2019
c4be21b
finished wave3, tests passing
iamsammysam Sep 14, 2019
952b415
Combined select_user and select_channel into one method and updated t…
Sep 14, 2019
322c459
Added summary of channel and user count to beginning of CLI and a new…
Sep 14, 2019
b909f0e
Added tests for Recipient.details and Recipient.self.list
Sep 14, 2019
9e07dbd
Updated git ignore file to include .ds_store file.
Sep 14, 2019
fadcfc2
Deleted old VCR cassette for Recipient class and continued polishing …
Sep 14, 2019
aa7ad08
Refined tests for Recipient.send_message
Sep 14, 2019
a8e70c3
Added tests to beef up coverage report, which is now at 94 percent or…
Sep 15, 2019
8bcc446
Removed working comments.
Sep 15, 2019
53f06cd
refining test for the method user_message
Sep 15, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.rbc
/.config
/coverage/
.ds_store
/InstalledFiles
/pkg/
/spec/reports/
Expand All @@ -11,7 +12,7 @@
/tmp/

# Used by dotenv library to load environment variables.
# .env
.env

## Specific to RubyMotion:
.dat*
Expand Down
34 changes: 34 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative "recipient"
require "httparty"
require 'pry'

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

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

def details
tp self, "slack_id", "name", "topic", "member_count"
end

def self.list
response = Channel.get("https://slack.com/api/conversations.list")
channels = []

response["channels"].each do |channel|
channels << Channel.new(
slack_id = channel["id"],
name = channel["name"],
topic = channel["topic"]["value"],
member_count = channel["num_members"],
)
end
return channels
end
end
end
38 changes: 38 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "httparty"
require 'pry'

class API_Error < StandardError

Choose a reason for hiding this comment

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

nice move implementing this here!

end
module Slack
class Recipient
URL = "https://slack.com/api/users.list"
attr_reader :slack_id, :name

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

def details
raise API_Error.new, 'Implement me in a child class!'
end

def self.list
raise API_Error.new, 'Implement me in a child class!'
end
Comment on lines +16 to +22

Choose a reason for hiding this comment

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

Yeah!


def self.get(url)
response = HTTParty.get(url, query: {token: ENV['SLACK_API_TOKEN']})
return response
end

def send_message(slack_id, message)
response = HTTParty.post("https://slack.com/api/chat.postMessage", query: {token: ENV['SLACK_API_TOKEN'], channel: slack_id, text: message})

unless response.code == 200
raise ExceptionError, "Message not sent."
end
return response
end
end
end
78 changes: 72 additions & 6 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,77 @@
#!/usr/bin/env ruby
require "dotenv"
require "httparty"
require "table_print"

def main
puts "Welcome to the Ada Slack CLI!"
require_relative "recipient"
require_relative "user"
require_relative "channel"
require_relative "workspace"

# TODO project
Dotenv.load

puts "Thank you for using the Ada Slack CLI"
def main
workspace = Slack::Workspace.new
puts "\n"
puts "Welcome to the Ada Slack CLI! This Slack workspace currently has #{workspace.users.count} users and #{workspace.channels.count} channels. Press enter to continue."

user_input = gets.chomp

until user_input == "quit"

Choose a reason for hiding this comment

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

I'd do this slightly differently.

Suggested change
until user_input == "quit"
tracking_int = 1
until tracking_int == 0
case quit
tracking_int = 0
end

Choose a reason for hiding this comment

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

I'd maybe make it a bool, and give it a better name, but this way you address the quit case separately.

print "Please choose an option: list users, list channels, select user, select channel, details, send message, or quit: "
user_input = gets.chomp.downcase

case user_input
when "list users"
tp workspace.users, "slack_id", "name", "real_name"
puts "\n"
user_input = nil

when "list channels"
tp workspace.channels, "name", "topic", "member_count", "slack_id"
puts "\n"
user_input = nil

when "select user"
print "Please enter the user name or ID: "
name_or_id = gets.chomp
puts workspace.select(name_or_id)
puts "\n"

when "select channel"
print "Please enter the channel name or ID: "
name_or_id = gets.chomp
puts workspace.select(name_or_id)
puts "\n"

when "details"
if workspace.selected == nil
puts "Please select a user or channel."
user_input = nil
puts "\n"
else
workspace.show_details
user_input = nil
puts "\n"
end
when "send message"
if workspace.selected == nil
puts "Please select a user or channel."
user_input = nil
puts "\n"
else
print "Please enter your message: "
message = gets.chomp
slack_id = workspace.select(name_or_id)
workspace.user_message(message, slack_id)
puts "\n"
end
else
puts "Sorry, I didn't understand your request. Please try again."

Choose a reason for hiding this comment

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

The quit options should probably not print this message!

puts "\n"
end
end
puts "Thank you for using the ADA Slack CLI!"
puts "\n"
end

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

module Slack
class User < Recipient
attr_reader :real_name

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

def details
tp self, "slack_id", "name", "real_name"
end

def self.list
response = User.get("https://slack.com/api/users.list")
users = []

response["members"].each do |user|
users << User.new(
slack_id = user["id"],
name = user["name"],
real_name = user["real_name"],
)
end
return users
end
end
end
41 changes: 41 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative "user"
require_relative "channel"

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

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

def select(name_or_id)
@channels.each do |channel|
if channel.name == name_or_id || channel.slack_id == name_or_id
@selected = channel
return "Okay, #{@selected.name} is selected."
else
@users.each do |user|
if user.name == name_or_id || user.slack_id == name_or_id
@selected = user
return "Okay, #{@selected.name} is selected."
end
end
end
end
puts "Sorry, I couldn't find that user or channel."
end

def show_details
return @selected.details
end

def user_message(message, slack_id)
@selected.send_message(@selected.slack_id, message)
return "Success! Message sent to #{@selected.name}."
end

end
end
84 changes: 84 additions & 0 deletions test/cassettes/channel-info.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading