Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3ef7e84
verified auth setup
njch5 Sep 10, 2019
90a85d1
created classes & started building channels class
njch5 Sep 10, 2019
0431360
we created our Channels.list method & created a test that passes
njch5 Sep 10, 2019
dc18ee0
created User.list method and added tests
njch5 Sep 10, 2019
2729dff
started on creating 3 methods for Workspace class
njch5 Sep 11, 2019
9170784
added DS_Store
njch5 Sep 11, 2019
9fed5d3
Created User objects in Workspace initialize
njch5 Sep 11, 2019
6cc2355
created select_user method
njch5 Sep 11, 2019
00b51ce
added additional params for Channel init method
njch5 Sep 11, 2019
0122cc4
added user & workspace tests
njch5 Sep 11, 2019
dde4c3a
added details methods, created select_channel method, and also create…
njch5 Sep 11, 2019
f48cc00
added tests for User.details & Channel.details
njch5 Sep 11, 2019
323fbd8
added tests for show_details in Workspace class
njch5 Sep 11, 2019
eabeada
started on send_message methods
njch5 Sep 12, 2019
723089a
fixed send_message method
njch5 Sep 12, 2019
3ad2e39
added conditional statements in main command loop
njch5 Sep 12, 2019
86b047e
started on tests for send message
njch5 Sep 12, 2019
e76db3c
created custom exception class
njch5 Sep 12, 2019
79ae3eb
refactored command line for SlackApiError & added tests for Workspace…
njch5 Sep 12, 2019
9ee31a6
created more tests!
njch5 Sep 13, 2019
364b6aa
editing text for readability
njch5 Sep 13, 2019
dbcb504
Created recipient_test.rb and moves recipient focused tests from work…
njch5 Sep 13, 2019
3d019f4
removed extra comments
njch5 Sep 13, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ build-iPhoneSimulator/

# Ignore cassette files
/specs/cassettes/

.DS_Store

Choose a reason for hiding this comment

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

.env should be in .gitignore.

38 changes: 38 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "httparty"
require "dotenv"
require "awesome_print"

Dotenv.load

require_relative "recipient"

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

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

def self.list
url = "https://slack.com/api/channels.list"
query_parameters = {
token: ENV["SLACK_TOKEN"],
}
response = Channel.get(url, query: query_parameters)

Choose a reason for hiding this comment

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

We do not have the tools yet to test a bad get request in this instance, but it is good to think about and build in functionality to deal with bad requests.

channels_array = []

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

def details
return "\nname: #{name}, slack_id: #{slack_id}, topic: #{topic}, member count: #{member_count}"
end
end
end
41 changes: 41 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "httparty"
require "dotenv"
MESSAGE_URL = "https://slack.com/api/chat.postMessage"

module Slack
class Recipient
attr_reader :name, :slack_id

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

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

def send_message(message)
body = {
token: ENV["SLACK_TOKEN"],
channel: slack_id,
text: message,
}

response = HTTParty.post(MESSAGE_URL, body: body)

if response["ok"] != true
raise SlackApiError.new("Invalid request. Error is #{response.code}: #{response.message}")
end
return response
end

def self.list
raise NotImplementedError, "Implement me in a child class!"
end

def details
raise NotImplementedError, "Implement me in a child class!"
end
end
end
70 changes: 67 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
#!/usr/bin/env ruby
require "httparty"
require "dotenv"
require "awesome_print"
require "table_print"
require_relative "channel"
require_relative "user"
require_relative "workspace"
require_relative "recipient"

# For specific SlackApiError messages (https://stackoverflow.com/questions/3382866/rubys-exception-error-classes)

def main
puts "Welcome to the Ada Slack CLI!"

# TODO project
workspace = Slack::Workspace.new

input = ""
until input == "quit"
puts "\nPlease select an option:

-list users
-list channels
-select user
-select channel
-show details
-send message
-quit\n"
input = gets.chomp.downcase

puts "Thank you for using the Ada Slack CLI"
case input
when "list users"
user_array = Slack::User.list
tp user_array
when "list channels"
channel_array = Slack::Channel.list
tp channel_array
when "select user"
puts "\n[Enter a username or slack id]"
identifier = gets.chomp
begin
workspace.select_user(identifier)
rescue SlackApiError => e
puts "\n#{e.message}"
end
when "select channel"
puts "\n[Enter a name or slack id]"
identifier = gets.chomp
begin
workspace.select_channel(identifier)
rescue SlackApiError => e
puts "\n#{e.message}"
end
when "show details"
if workspace.show_details == nil
puts "\n[No user or channel has been selected]"
else
puts "#{workspace.show_details}"
end
when "send message"
if workspace.selected == nil
puts "\n[No user or channel has been selected]"
else
puts "What do you want to send?"
message = gets.chomp
workspace.send_message(message)
end
when "quit"
puts "[Thank you for using the Ada Slack CLI]"
exit
end
end
end

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

Dotenv.load

require_relative "recipient"

module Slack
class User < Recipient
attr_reader :name, :real_name, :slack_id

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

def self.list
url = "https://slack.com/api/users.list"
query_parameters = {
token: ENV["SLACK_TOKEN"],
}
response = User.get(url, query: query_parameters)
users_array = []
response["members"].each do |member|
user_hash = { "real name" => member["real_name"], "user name" => member["name"], "slack id" => member["id"] }
users_array << user_hash
end
return users_array
end

def details
return "\nreal name: #{real_name}, user name: #{name}, slack_id: #{slack_id}"
end
end
end
28 changes: 28 additions & 0 deletions lib/verify_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "httparty"
require "dotenv"
require "awesome_print"

Dotenv.load

url = "https://slack.com/api/channels.list"
query_parameters = {
token: ENV["SLACK_TOKEN"],
}

# channel_names =
# puts query_parameters
response = HTTParty.get(url, query: query_parameters)

# Loops through the results and prints name of each channel
# response["channels"].each do |channel|
# ap channel["name"]
# end
# ap response["channels"][1]["name"]

# response["channels"].each do |channel|
# ap channel["members"].length
# end

response["channels"].each do |channel|
ap channel
end
55 changes: 55 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative "user"
require_relative "channel"
require_relative "recipient"

class SlackApiError < StandardError; end

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

def initialize
@users = Slack::User.list.map do |user_obj|
Slack::User.new(name: user_obj["user name"], slack_id: user_obj["slack id"], real_name: user_obj["real name"])
end
@channels = Slack::Channel.list.map do |chan_obj|
Slack::Channel.new(name: chan_obj["name"], slack_id: chan_obj["slack id"], topic: chan_obj["topic"], member_count: chan_obj["member count"])
end
@selected = nil
end

def select_channel(identifier)

Choose a reason for hiding this comment

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

This code is very similar to the code for select_user below. Could you DRY this up somehow?

@channels.each do |channel|
if [channel.name, channel.slack_id].include?(identifier)
@selected = channel
return @selected
end
end
raise SlackApiError.new("[We couldn't find this channel!]")
end

def select_user(identifier)
@users.each do |user|
if [user.name, user.slack_id].include?(identifier)
@selected = user
return @selected
end
end
raise SlackApiError.new("[We couldn't find this user!]")
end

def show_details
if @selected
@selected.details
end
end

def send_message(message)
if @selected
@selected.send_message(message)
else
return nil
end
end
end
end
Loading