Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0690957
Created verification.rb to check connection to Slack API.
msfinnan Mar 19, 2019
de7b750
Wrote first test for channel class (list_channels)
aribray Mar 19, 2019
3268e0c
passed tests for list_channel method
msfinnan Mar 19, 2019
92c9449
Trying to solve git pull issue
aribray Mar 19, 2019
e27145f
Fixed git pull issue
aribray Mar 19, 2019
8c63b50
Added tests for user, wrote method to pass tests, got slack.rb CLI ru…
msfinnan Mar 19, 2019
4468323
Wrote tests and code for select user method
aribray Mar 19, 2019
e646028
Added tests and methods for select channel
msfinnan Mar 19, 2019
806f6cd
Started details part of slack.rb
msfinnan Mar 19, 2019
a588b63
moved dotevn to test_helper file
msfinnan Mar 20, 2019
38cd541
Added module, added SlackError & tests for SlackError, added see_deta…
msfinnan Mar 20, 2019
53c7eac
added tests for user details. Trying to fix cli for details prompt
aribray Mar 20, 2019
dd1a21c
fixed bug in details output in CLI, added recipient.rb
msfinnan Mar 21, 2019
7d2956a
added recipient class and made User and Channel inherit from recipient
msfinnan Mar 21, 2019
948b30b
Added send msg method to channel class and wrote test
msfinnan Mar 21, 2019
6c9e142
Wrote send_msg tests and code for user and channel
aribray Mar 21, 2019
e5099a6
Added CLI code for send_msg method
aribray Mar 21, 2019
8821a24
Fixed test error for selected user response
aribray Mar 21, 2019
33211e7
Changed data structure for list user, list channel, user details, and…
msfinnan Mar 22, 2019
ec223d6
Added send message method to Recipient class and updated CLI
msfinnan Mar 22, 2019
a7cec70
added tests for error message
msfinnan Mar 22, 2019
6470849
Added workspace and moved see_details and select_recipient methods to…
aribray Mar 22, 2019
6934c0c
Updated formatting and functionality on slack.rb
msfinnan Mar 22, 2019
63a8f96
Removed commented out code
aribray 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
26 changes: 26 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "pry"
require "httparty"
require_relative "recipient"

module SlackAPI
class SlackError < StandardError; end

class Channel < Recipient
BASE_URL = "https://slack.com/api/"

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

channel_list = []

if response["ok"] != true
raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}"
else
response["channels"].each do |channel|
channel_list << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] }
Copy link

@CheezItMan CheezItMan Mar 31, 2019

Choose a reason for hiding this comment

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

You have missed an opportunity to create an array of Channel instances instead of an array of hashes.

end
end
return channel_list
end
end #end of class
end #end of module
31 changes: 31 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module SlackAPI
class Recipient
BASE_URL = "https://slack.com/api/"

def self.get(sub_url)
query = {
token: ENV["SLACK_API_TOKEN"],
}

response = HTTParty.get("#{BASE_URL}#{sub_url}", query: query)

return response
end

def send_msg(recipient, text)
response = HTTParty.post("#{BASE_URL}chat.postMessage",
headers: {"Content-Type" => "application/x-www-form-urlencoded"},
body: {
token: ENV["SLACK_API_TOKEN"],
channel: recipient,
text: text,
})

if response["ok"] == false
raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error #{response["error"]}"
else
return true
end
end
end
end
71 changes: 67 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
#!/usr/bin/env ruby
require_relative "user"
require_relative "channel"
require_relative "workspace"
require "httparty"
require "table_print"
require "dotenv"
Dotenv.load

def main
puts "Welcome to the Ada Slack CLI!"
puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users"
ask_again = "Select an option: \n1. list users \n2. list channels \n3. select user \n4. select channel \n5. details (select user or channel first)\n6. send message (select user or channel first) \n7. quit"
puts ask_again
response = gets.chomp.downcase

# TODO project

puts "Thank you for using the Ada Slack CLI"
until response == "7" || response == "quit"

Choose a reason for hiding this comment

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

Oh thank god you respond to the numbers!

if response == "list users" || response == "1"

Choose a reason for hiding this comment

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

This set of if-else blocks, really looks like it could be broken up into methods.

tp SlackAPI::User.list_users, "name", "real name", "slack id"
puts ask_again
response = gets.chomp.downcase
elsif response == "list channels" || response == "2"
tp SlackAPI::Channel.list_channels, "name", "topic", "member count", "id"
puts ask_again
response = gets.chomp.downcase
elsif response == "select user" || response == "3"
select_response = "select user"
puts "What is the user id or Slack name?"
identifier = gets.chomp.downcase
user = SlackAPI::Workspace.new
selected_recipient = user.select_user(identifier)
if selected_recipient == ""
puts "There is no user with that identifier"
end
puts ask_again
response = gets.chomp.downcase
elsif response == "select channel" || response == "4"
select_response = "select channel"
puts "What is the user id or Slack name?"
identifier = gets.chomp.downcase
channel = SlackAPI::Workspace.new
selected_recipient = channel.select_channel(identifier)
if selected_recipient == ""
puts "There is no channel with that identifier"
end
puts ask_again
response = gets.chomp.downcase
elsif response == "details" || response == "5"
if select_response == "select user"
details = user.see_user_details(selected_recipient)
tp details, "name", "real name", "slack id"
puts ask_again
response = gets.chomp.downcase
elsif select_response == "select channel"
details = channel.see_channel_details(selected_recipient)
tp details, "name", "topic", "member count", "id"
puts ask_again
response = gets.chomp.downcase
else
puts "Select a user or channel first"
puts ask_again
response = gets.chomp.downcase
end
elsif response == "send message" || response == "6"
puts "What would you like to say?"
text = gets.chomp.downcase
msg = SlackAPI::Recipient.new.send_msg(selected_recipient, text)
puts ask_again
response = gets.chomp.downcase
end
end
end

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

module SlackAPI
class SlackError < StandardError; end

class User < Recipient
BASE_URL = "https://slack.com/api/"

def self.list_users
response = Recipient.get("users.list")

user_list = []

if response["ok"]
response["members"].each do |member|
user_list << {"name" => member["name"], "real name" => member["real_name"], "slack id" => member["id"]}

Choose a reason for hiding this comment

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

This method would make more sense if it returned a list of User instances with name, read name and id properties.

end
else
raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}"
end
return user_list
end
end # end of class
end # end of module
19 changes: 19 additions & 0 deletions lib/verification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "httparty"
require "pry"
require "dotenv"
Dotenv.load

BASE_URL = "https://slack.com/api/channels.list"
query = {
token: ENV["SLACK_API_TOKEN"],
}

response = HTTParty.get(BASE_URL, query: query)

response["channels"].each do |channel|
print channel["name"]
end
# code snips to save:
# puts channel["topic"]["value"] # gets topic
# puts channel["members"].length # gets # of members
# puts channel["id"] # gets slack id for channel
71 changes: 71 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require_relative "recipient"
require "httparty"

module SlackAPI
class Workspace
def select_channel(identifier)
response = Recipient.get("channels.list")

selected_channel = ""
response["channels"].each do |channel|
if channel["id"] == identifier
selected_channel = identifier
elsif channel["name"] == identifier
selected_channel = identifier
end
end
if selected_channel == ""
raise SlackAPI::SlackError, "That is not a vaild channel"
end
return selected_channel
end

def see_channel_details(identifier)
response = Recipient.get("channels.list")

channel_details = []

response["channels"].each do |channel|
if channel["id"] == identifier || channel["name"] == identifier
channel_details << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] }
return channel_details
end
end
end

def select_user(identifier)
response = Recipient.get("users.list")

selected_user = ""

response["members"].each do |member|

Choose a reason for hiding this comment

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

You could also use the .find method as well.

if member["id"] == identifier
selected_user = member["id"]
elsif member["name"] == identifier
selected_user = member["id"]
end
end
if selected_user == ""
raise SlackAPI::SlackError, "That is not a valid user"
end
return selected_user
end

def see_user_details(identifier)
response = Recipient.get("users.list")

user_details = []

response["members"].each do |member|
if member["id"] == identifier || member["name"] == identifier
user_details <<
{ "name" => member["name"],
"real name" => member["real_name"],
"slack id" => member["id"] }

return user_details
end
end
end
end # end of class
end # end of module
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 "self.list" do
it "list all channels" do
VCR.use_cassette("list_channels") do
response = SlackAPI::Channel.list_channels

expect(response).wont_be_nil
expect(response[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC"
expect(response.length).must_equal 3
end
end
it "will raise an error if given an invalid key" do

Choose a reason for hiding this comment

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

👍

real_token = ENV["SLACK_API_TOKEN"]
ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN"

VCR.use_cassette("list_channels_2") do
error = expect {
SlackAPI::Channel.list_channels
}.must_raise SlackAPI::SlackError
expect(error.message).must_equal "There was an error. The error message is invalid_auth"
end

ENV["SLACK_API_TOKEN"] = real_token
end
end

describe "send_msg" do
it "sends a message to selected channel" do
channel = SlackAPI::Channel.new()
VCR.use_cassette("send_msg") do
msg = channel.send_msg("CH2SKTKPC", "This is a test message!")
expect(msg).must_equal true
end
end

it "generates an error if given an invalid channel" do
VCR.use_cassette("send_msg") do
expect {
SlackAPI::Channel.new.send_msg("bogus", "Test message")
}.must_raise SlackAPI::SlackError
end
end

it "will raise an error if given an empty message" do
VCR.use_cassette("send_msg") do
error = expect {
SlackAPI::Channel.new.send_msg("general",
"")
}.must_raise SlackAPI::SlackError

# expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text"
end
end
end
32 changes: 25 additions & 7 deletions specs/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
require 'simplecov'
require "simplecov"
SimpleCov.start
require "dotenv"
Dotenv.load

require 'minitest'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'vcr'
require "httparty"
require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"
require "webmock/minitest"
require "vcr"

require_relative "../lib/channel.rb"
require_relative "../lib/slack.rb"
require_relative "../lib/user.rb"
require_relative "../lib/recipient.rb"
require_relative "../lib/workspace.rb"

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

VCR.configure do |config|
config.cassette_library_dir = "specs/cassettes"
config.hook_into :webmock
end
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("<SLACK_API_TOKEN>") do
ENV["SLACK_API_TOKEN"]
end
end
Loading