Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Local File",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${file}"
}
]
}
40 changes: 40 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "dotenv"
require "HTTParty"
require "pry"

require_relative "recipient"

Dotenv.load

module Slack
class Channel < Recipient
CHANNEL_URL = "https://slack.com/api/channels.list"
@query = { token: KEY }

attr_reader :slack_id, :name, :topic, :member_count

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

def self.list
response = self.get(CHANNEL_URL, query: @query)
# binding.pry
channels = response["channels"].map do |channel|
id = channel["id"]
name = channel["name"]
topic = channel["topic"]["value"]
member_count = channel["num_members"]
self.new(slack_id: id, name: name, topic: topic, member_count: member_count)
end
return channels
end

def details
return "slack_id: #{slack_id}, name: #{name}, topic: #{topic}, member_count: #{member_count}"
end
end
end
54 changes: 54 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "dotenv"
require "HTTParty"
require "pry"

Dotenv.load
KEY = ENV["SLACK_TOKEN"]
MESSAGE_URL = "https://slack.com/api/chat.postMessage"

module Slack
class SlackApiError < StandardError; end

class Recipient
attr_reader :slack_id, :name

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

def self.get(url, query:)
response = HTTParty.get(url, query: query)
if response["ok"] == false
raise SlackApiError.new("Invalid request, error #{response.code}: #{response.message}")
end

return response
end

def send_message(message)
body = {
token: KEY,
channel: slack_id,
text: message,
}
response = HTTParty.post(MESSAGE_URL, body: body)

if response["ok"] == false
raise SlackApiError.new("Invalid request, error #{response.code}: #{response.message}")
end

return response
end

# private

def self.list

Choose a reason for hiding this comment

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

Why is private commented out?

raise NotImplementedError, "implement in child class"
end

def details
raise NotImplementedError, "implement in child class"
end
end
end
64 changes: 61 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,69 @@
#!/usr/bin/env ruby

require_relative "workspace"
require "table_print"

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Slack::Workspace.new
puts "Welcome to the Ada Slack CLI! \n
There are #{workspace.channels.length} channels and #{workspace.users.length} users\n"

commands = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"]

# TODO project
while true
puts "\nWhat would you like to do?"
puts
puts commands
puts
input = gets.chomp.downcase

case input
when "list users"
tp workspace.users, :name, :slack_id, :real_name
when "list channels"
tp workspace.channels, :name, :topic, :slack_id, :member_count
when "select user"
puts "Please type in username or Slack ID"
user = gets.chomp
selected_user = workspace.select_user(user)
if selected_user
puts "You selected user #{selected_user.name}"
else
puts "This user does not exist"
end
when "select channel"
puts "Please type in channel name or Slack ID"
channel = gets.chomp
selected_channel = workspace.select_channel(channel)
if selected_channel
puts "You selected the channel called \'#{selected_channel.name}\'"
else
puts "This channel does not exist"
end
when "details"
if workspace.show_details == nil
puts "No user or channel is selected"
else
puts workspace.show_details
end
when "send message"
if workspace.selected == nil
puts "No user or channel selected. Try again"
else
puts "Write your message here"
message = gets.chomp
workspace.send_message(message)
end
when "quit"
break
else
puts "Command not valid. Please type one of the following"
end
end

puts "Thank you for using the Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME
# Make sure all user input gets checked/converted for casing issues (.upcase for usernames, etc.)

main if __FILE__ == $PROGRAM_NAME
1 change: 1 addition & 0 deletions lib/slackapierror.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SlackApiError < StandardError
41 changes: 41 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "dotenv"
require "HTTParty"
require "pry"

require_relative "recipient"

Dotenv.load

module Slack
class User < Recipient
USER_URL = "https://slack.com/api/users.list"
@query = { token: KEY }

attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji

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

def self.list
response = self.get(USER_URL, query: @query)
users = response["members"].map do |user|
id = user["id"]
name = user["name"]
real_name = user["real_name"]
status_text = user["profile"]["status_text"]
status_emoji = user["profile"]["status_emoji"]
self.new(slack_id: id, name: name, real_name: real_name, status_text: status_text, status_emoji: status_emoji)
end
return users
end

def details
return "slack_id: #{slack_id}, name: #{name}, real_name: #{real_name}, status_text: #{status_text}, status_emoji: #{status_emoji}"
end
end
end
25 changes: 25 additions & 0 deletions lib/verification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use the dotenv gem to load environment variables
# Use HTTParty to send a GET request to the channels.list endpoint
# Check that the request completed successfully, and print relevant information to the console if it didn't
# Loop through the results and print out the name of each channel

require "dotenv"
require "HTTParty"
require "pry"

Dotenv.load

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

response = HTTParty.get(url, query: query)
binding.pry
if response.code != 200
puts response.reason
puts response.code
else
response["channels"].each do |channel|
puts channel["name"]
end
end
53 changes: 53 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative "user"
require_relative "channel"

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

Choose a reason for hiding this comment

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

I think an attr_reader might be the right call for selected as well, since it's only changing from within this class.


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

def select_user(value)
@users.each do |user|
if user.name == value
@selected = user
return user
elsif user.slack_id == value
@selected = user
return user
end
end
return nil
end

def select_channel(value)
@channels.each do |channel|
if channel.name == value
@selected = channel

Choose a reason for hiding this comment

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

You have a lot of repeated code between this method and select_user above. Is there a way you could DRY this up?

return channel
elsif channel.slack_id == value
@selected = channel
return channel
end
end
return nil
end

def show_details
return selected.details if selected
end

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

describe "Channel" do
describe "self.list" do
before do
VCR.use_cassette("make channels list") do
query_params = {
token: KEY,
}
@channels = Slack::Channel.list
@channel = @channels.first
end
end

it "will return an array of Channels" do
expect(@channels).must_be_kind_of Array
expect(@channel).must_be_kind_of Slack::Channel
end

it "will assign the correct attributes to each Channel" do
expect(@channel.slack_id).must_equal "CH2V3FHEY"
expect(@channel.name).must_equal "general"
expect(@channel.topic).must_equal "Company-wide announcements and work-based matters"
expect(@channel.member_count).must_equal 2
end

it "has the correct data tyes for each attribute" do
expect(@channel.slack_id).must_be_kind_of String
expect(@channel.name).must_be_kind_of String
expect(@channel.topic).must_be_kind_of String
expect(@channel.member_count).must_be_kind_of Integer
end
end

describe "details" do
before do
VCR.use_cassette("show channel details") do
query_params = {
token: KEY,
}
@channels = Slack::Channel.list
@channel = @channels.first
end
end

it "returns the correct data" do
expect(@channel.details).must_be_kind_of String
end
end
end
Loading