Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
11cf327
Verified slack token on my machine! -Shubha
shubha-rajan Mar 19, 2019
d115731
Set up test environment with VCR
carlabosco Mar 19, 2019
01724b4
created wrapper class for channels.list endpoint
shubha-rajan Mar 19, 2019
cee8153
resolving merge conflict
shubha-rajan Mar 19, 2019
48168f7
added tests for user class
shubha-rajan Mar 19, 2019
5baef6d
Created test file for channel
carlabosco Mar 19, 2019
3f76128
Solving merge conflicts
carlabosco Mar 19, 2019
8bbb51b
added driver code to main.rb and test for User#display_details
shubha-rajan Mar 19, 2019
a9563a5
Added tests for channel
carlabosco Mar 19, 2019
0c0da23
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco Mar 19, 2019
574e045
channel passes all tests in spec!
shubha-rajan Mar 19, 2019
35c7ad7
added test for display_details method in channel
shubha-rajan Mar 19, 2019
a98abe3
added display_details method to channel
shubha-rajan Mar 19, 2019
168e55b
Created User class
carlabosco Mar 19, 2019
5c8cd29
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco Mar 19, 2019
9d32a17
added test to workspace_spec.rb
shubha-rajan Mar 19, 2019
f220a3c
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco Mar 19, 2019
3533a24
added display details method to User class
shubha-rajan Mar 19, 2019
3fca687
Created Workspace class and initialize method
carlabosco Mar 19, 2019
3221395
Fixed merge conflicts
carlabosco Mar 19, 2019
73ae2d8
created instance of workspace in slack.rb
shubha-rajan Mar 19, 2019
e40c523
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan Mar 19, 2019
0040ee8
moved creation of new workspace to inside main method
shubha-rajan Mar 19, 2019
ef3bba4
Added SlackApiError class
carlabosco Mar 19, 2019
5d604cb
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco Mar 19, 2019
f3d9dac
added tests for Workspace#display_users and Workspace#display_channels
shubha-rajan Mar 19, 2019
b9142ef
added display_users and display_channels methods to workspace
shubha-rajan Mar 19, 2019
b6512eb
fixed bugs in main driver code
shubha-rajan Mar 19, 2019
4bf29db
fixed another bug where == was used instead of =
shubha-rajan Mar 19, 2019
3589eb6
Added table_print format to users and channels
carlabosco Mar 20, 2019
e3b305e
fine, github. i will commit my deleted whitespace before i pull.
shubha-rajan Mar 20, 2019
1bd94ab
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan Mar 20, 2019
7b37d1e
refactored tests to use before block for VCR. removed tests for displ…
shubha-rajan Mar 20, 2019
8a99248
Refactored tests
carlabosco Mar 20, 2019
ab10a31
Solving merge conflicts
carlabosco Mar 20, 2019
aed77af
Added tests for select_user and select_channel
carlabosco Mar 20, 2019
34db870
wrote methods for select user and select channel that pass tests. ref…
shubha-rajan Mar 21, 2019
5f36162
Refactored options menu
carlabosco Mar 21, 2019
1734b3e
Added show_details method
carlabosco Mar 21, 2019
33a79bf
added display_details back to channel and user classes
shubha-rajan Mar 21, 2019
003e8c1
added recipient class and set up inheritance
shubha-rajan Mar 21, 2019
5296aa5
took out inherited readers from user class
shubha-rajan Mar 21, 2019
ca18852
Added test for send_message
carlabosco Mar 21, 2019
cc72d9a
send message method works with names! need to fix bug with id lookup
shubha-rajan Mar 21, 2019
44831fa
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan Mar 21, 2019
56e8a0c
Added error for invalid recipient
carlabosco Mar 21, 2019
28bb9a0
added test for sending message when selected recipient is nil
shubha-rajan Mar 21, 2019
43deaef
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan Mar 21, 2019
a0e7f7f
added tests for send_message method
shubha-rajan Mar 21, 2019
4455918
created display_selected_menu method in slack.rb to DRY code
shubha-rajan Mar 21, 2019
5e6222f
reverted changes in slack.rb. drying up code broke something!
shubha-rajan Mar 21, 2019
04ac604
all tests passing. fixed bug where it failed to select by id!!!
shubha-rajan Mar 22, 2019
5c39d8d
Added colors to menu
carlabosco Mar 22, 2019
0509249
refactored driver code in lib/slack.rb
shubha-rajan Mar 26, 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
48 changes: 48 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "httparty"
require "dotenv"
require_relative "recipient"

Dotenv.load

module SlackCLI
class Channel < Recipient
BASE_URL = "https://slack.com/api/channels.list"

attr_reader :topic, :members

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

def self.get_from_api
query_parameters = {
token: ENV["OAUTH_ACCESS_TOKEN"],
}

response = get(BASE_URL, query_parameters)

if (response.code == 200)
channels = response["channels"].map do |channel|
slack_id = channel["id"]
channel_name = channel["name"]
topic = channel["topic"]["value"]
members = channel["members"].length
new(slack_id, channel_name, topic, members)
end
return channels
else
raise SlackApiError, "Error #{response.code} : #{response["message"]}"
end
end

def display_details
info_string = "\nSlack ID : #{slack_id}" +
"\nChannel name : #{name}" +
"\nTopic : #{topic}" +
"\nMember count: #{members}"
return info_string
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 @@
module SlackCLI
class Recipient
CHAT_ENDPOINT = "https://slack.com/api/chat.postMessage"
attr_reader :slack_id, :name

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

def self.get(url, params)
return HTTParty.get(url, query: params)
end

def send_message(message)
query_parameters = {
token: ENV["OAUTH_ACCESS_TOKEN"],
channel: slack_id,
text: message,
}
response = HTTParty.post(
CHAT_ENDPOINT,
body: query_parameters,
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
)

return response.code == 200 && response.parsed_response["ok"]
end

def display_details
raise NotImplementedError, "Implement me in a child class!"
end

def self.get_from_api
raise NotImplementedError, "Implement me in a child class!"
end
end
end
82 changes: 78 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,85 @@
#!/usr/bin/env ruby
require_relative "channel"
require_relative "user"
require_relative "workspace"

require "httparty"
require "dotenv"
require "awesome_print"
require "colorize"

Dotenv.load

def set_main_menu_input
puts "What would you like to do?".colorize(:yellow)
puts "\nOptions: \n1.list users \n2.list channels \n3.select user \n4.select channel \n5.quit".colorize(:blue)
input = gets.chomp.downcase
return input
end

def set_selected_input
puts "\nOptions: \ndetails \nsend message \nreturn to main menu".colorize(:blue)
input = gets.chomp.downcase
return input
end

def selected_loop(input, workspace)
until (input == "return to main menu")
case input
when "details"
puts workspace.show_details.colorize(:green)
when "send message"
puts "Type a message to send:".colorize(:blue)
message = gets.chomp
workspace.send_message(message)
when "return to main menu"
break
else
puts "That wasn't one of the options!".colorize(:red)
end
input = set_selected_input
end
end

def main
puts "Welcome to the Ada Slack CLI!"
users = SlackCLI::User.get_from_api
channels = SlackCLI::Channel.get_from_api
workspace = SlackCLI::Workspace.new(users: users, channels: channels)

puts "Welcome to the Ada Slack CLI!".colorize(:yellow)
input = set_main_menu_input

until (input == "quit")
case input
when "list users"
workspace.display_users
when "list channels"
workspace.display_channels
when "select user"
puts "Enter username or slack id:".colorize(:yellow)

name_or_id = gets.chomp
workspace.select_user(name_or_id)

input = set_selected_input
selected_loop(input, workspace)
when "select channel"
puts "Enter channel name or id:".colorize(:blue)
name_or_id = gets.chomp
workspace.select_channel(name_or_id)

input = set_selected_input

# TODO project
selected_loop(input, workspace)
when "quit"
break
else
puts "That wasn't one of the options!".colorize(:red)
end
input = set_main_menu_input
end

puts "Thank you for using the Ada Slack CLI"
puts "Thank you for using the Ada Slack CLI.".colorize(:yellow)
end

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

Dotenv.load

module SlackCLI
class User < Recipient
BASE_URL = "https://slack.com/api/users.list"
attr_reader :real_name

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

def self.get_from_api
query_parameters = {
token: ENV["OAUTH_ACCESS_TOKEN"],
}

response = get(BASE_URL, query_parameters)

if (response.code == 200)
users = response["members"].map do |member|
name = member["name"]
real_name = member["real_name"]
slack_id = member["id"]
new(name, real_name, slack_id)
end
return users
else
raise SlackApiError, "Error #{response.code} : #{response["message"]}"
end
end

def display_details
info_string = "\nSlack ID : #{slack_id}" +
"\nUsername : #{name}" +
"\nReal name : #{real_name}"
return info_string
end
end
end
56 changes: 56 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "httparty"
require "dotenv"
require "pry"

Dotenv.load

module SlackCLI
class SlackApiError < Exception; end

class Workspace
attr_reader :users, :channels, :selected

def initialize(users:, channels:)
@users = users
@channels = channels
end

def display_users
return tp users, :name, :real_name, :slack_id
end

def display_channels
return tp channels, :slack_id, :name, :members, :topic => { :width => 120 }
end

def select_user(name_or_id)
@selected = users.find { |user|
user.name == name_or_id
}

@selected ||= users.find { |user|
user.slack_id == name_or_id
}

return @selected
end

def select_channel(name_or_id)
@selected = channels.find { |channel| channel.name == name_or_id }
@selected ||= channels.find { |channel| channel.slack_id == name_or_id }
return @selected

Choose a reason for hiding this comment

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

Good use of ||= here.

end

def show_details
selected.display_details
end

def send_message(message)
if selected == nil
raise SlackApiError, "Invalid recipient"
else
selected.send_message(message)
end
end
end
end
46 changes: 46 additions & 0 deletions specs/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative "test_helper"

describe "Channel" do
let (:channel_data) {
{
id: "CH2P2QWMR",
channel_name: "everyone",
topic: "Company-wide announcements and work-based matters",
members: 2,
}
}
it "instantiate a channel object" do
new_channel = SlackCLI::Channel.new(
channel_data[:id],
channel_data[:channel_name],
channel_data[:topic],
channel_data[:members]
)

expect(new_channel).must_be_instance_of SlackCLI::Channel
end

it "has working reader methods" do
new_channel = SlackCLI::Channel.new(
channel_data[:id],
channel_data[:channel_name],
channel_data[:topic],
channel_data[:members]
)

expect(new_channel.slack_id).must_equal channel_data[:id]
expect(new_channel.name).must_equal channel_data[:channel_name]
expect(new_channel.topic).must_equal channel_data[:topic]
expect(new_channel.members).must_equal channel_data[:members]
end

it "loads array of channels from Slack's API" do
VCR.use_cassette("list_channels") do
channels = SlackCLI::Channel.get_from_api
expect(channels).must_be_instance_of Array
channels.each do |channel|
expect(channel).must_be_instance_of SlackCLI::Channel
end
end
end
end
35 changes: 25 additions & 10 deletions specs/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
require 'simplecov'
SimpleCov.start

require 'minitest'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'vcr'
require "simplecov"
SimpleCov.start do
add_filter %r{^/specs?/}
add_filter "slack.rb"
end
require "minitest/autorun"
require "minitest/reporters"
require "vcr"
require "webmock/minitest"
require "dotenv"

Dotenv.load

require_relative "../lib/slack.rb"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

VCR.configure do |config|
config.cassette_library_dir = "specs/cassettes"
config.hook_into :webmock
end
config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located
config.hook_into :webmock # tie into this other tool called webmock
config.default_cassette_options = {
:record => :new_episodes, # record new data when we don't have it yet
:match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match
}
# Don't leave our token lying around in a cassette file.
config.filter_sensitive_data("<OAUTH_ACCESS_TOKEN>") do
ENV["OAUTH_ACCESS_TOKEN"]
end
end
Loading