-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sockets - Tati Hana #20
base: master
Are you sure you want to change the base?
Changes from all commits
b7f136a
3eb6a7c
af9d38f
e0c01e4
b8ef5d2
8d2236b
268e8a7
33024d0
daed6bd
d35b5ea
8095052
531e18f
3688e72
9c3d836
4dc98b3
0ac44df
fd2d0c9
637be8c
a3fc48c
6a2a7c8
0f4cff0
c55c990
a7c294e
b5ff318
489063b
b31c6e7
85cc32e
45f1f1b
bca0e4d
963e0a9
3094413
84530e4
a48e230
cf221f1
3ed78f2
0484e3a
f77a14f
0f9ce9d
bc9cc58
6a9bdf0
d42ca8f
aeb1918
e6fc010
08fc989
6337047
34733dd
e6aa86f
a88b86d
e7ccce6
79412b7
c981d71
e31cace
b230f92
6b0725b
1d12a37
624b2b4
0f95b07
1a18c7d
54b43c0
64d648f
3e80151
1bba049
4de5188
6b7fd8e
bf211e2
0f93205
53788e2
a33963f
2456ebc
de4fa52
bfcc4d5
1d9024e
df3c9f4
9097e75
640e673
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require_relative "recipient" | ||
|
||
module SlackCli | ||
class Channel < SlackCli::Recipient | ||
attr_reader :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_url | ||
return "https://slack.com/api/channels.list" | ||
end | ||
|
||
def self.list | ||
response = get | ||
channels = response["channels"].map do |channel| | ||
slack_id = channel["id"] | ||
name = channel["name"] | ||
topic = channel["topic"]["value"] | ||
member_count = channel["num_members"] | ||
self.new(slack_id, name, topic, member_count) | ||
end | ||
return channels | ||
end | ||
|
||
def details | ||
deets = "Channel name: #{name} \nID: #{slack_id} \ntopic: #{topic}, \nMember count:#{member_count}\n" | ||
return deets | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "httparty" | ||
require "pry" | ||
|
||
require "dotenv" | ||
Dotenv.load | ||
|
||
module SlackCli | ||
class SlackError < StandardError; end | ||
|
||
class Recipient | ||
MSG_URL = "https://slack.com/api/chat.postMessage" | ||
TOKEN = ENV["SLACK_TOKEN"] | ||
|
||
attr_reader :send_message, :name, :slack_id, :error_helper, :details, :list | ||
# :nocov: | ||
def initialize | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
# :nocov: | ||
|
||
def self.list_url | ||
raise NotImplementedError, "TODO: implement me in a child class" | ||
end | ||
|
||
def post_message(name, message) | ||
body_params = { | ||
token: TOKEN, | ||
as_user: true, | ||
channel: name, | ||
text: message, | ||
} | ||
response = HTTParty.post(MSG_URL, body: body_params) | ||
|
||
if response != 200 && !response["ok"] | ||
raise SlackCli::SlackError "Error: #{response["error"]}" | ||
else | ||
return response | ||
end | ||
end | ||
|
||
def self.get | ||
query_params = { | ||
token: TOKEN, | ||
} | ||
response = HTTParty.get(list_url, query: query_params) | ||
return error_helper(response) | ||
end | ||
|
||
# :nocov: | ||
def details | ||
raise NotImplementedError "TODO: implement me in a child class" | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of excluding this from coverage, why not have a test that |
||
|
||
def self.list | ||
raise NotImplementedError "TODO: implement me in a child class" | ||
end | ||
# :nocov: | ||
|
||
def self.error_helper(response) | ||
unless response.code == 200 && response["ok"] | ||
raise SlackError, "Error #{response.code}: #{response["error"]}" | ||
end | ||
return response | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
#!/usr/bin/env ruby | ||
require_relative "workspace" | ||
|
||
# :nocov: | ||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Workspace.new | ||
puts "TatiHana Slack Channels loaded #{SlackCli::Channel.list.length} channels" | ||
puts "TatiHana Users loaded #{SlackCli::User.list.length} users" | ||
|
||
# TODO project | ||
def options | ||
puts "Please choose one of the following options: | ||
- list users | ||
- list channels | ||
- select user | ||
- select channel | ||
- details | ||
- send message | ||
- quit | ||
Enter your choice now:" | ||
# selected = false | ||
end | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
options | ||
choice = gets.chomp | ||
loop do | ||
case choice | ||
when "list users" | ||
puts workspace.list_users | ||
when "list channels" | ||
puts workspace.list_channels | ||
when "select user" | ||
puts "what user would you like to select?" | ||
selected_user = gets.chomp | ||
workspace.select_user(selected_user) | ||
when "select channel" | ||
puts "What channel would you like to select?" | ||
selected_channel = gets.chomp | ||
workspace.select_channel(selected_channel) | ||
when "details" | ||
begin | ||
puts workspace.show_details | ||
rescue | ||
puts "No user or channel selected, try again." | ||
end | ||
when "send message" | ||
if workspace.selected == nil | ||
puts "No user or channel selected" | ||
else | ||
# begin | ||
message = gets.chomp | ||
workspace.send_message(message) | ||
# rescue SlackCli::SlackError | ||
puts "No user or channel selected, try again" | ||
# end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error message always prints out, even if the send is successful. Instead, you should check the result and give the user positive or negative feedback. |
||
end | ||
when "quit" | ||
puts "Thanks for checking out TatiHana! Bye bye..." | ||
exit | ||
end | ||
options | ||
choice = gets.chomp | ||
end | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME | ||
main if __FILE__ == $0 | ||
# :nocov: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "pry" | ||
|
||
require_relative "recipient" | ||
|
||
module SlackCli | ||
class User < Recipient | ||
attr_reader :real_name, :status_text, :status_emoji | ||
|
||
def initialize(name, slack_id, real_name, status_text, status_emoji) | ||
@name = name | ||
@slack_id = slack_id | ||
@real_name = real_name | ||
@status_text = status_text | ||
@status_emoji = status_emoji | ||
end | ||
|
||
def self.list_url | ||
return "https://slack.com/api/users.list" | ||
end | ||
|
||
def self.list | ||
response = get | ||
|
||
users = response["members"].map do |user| | ||
name = user["name"] | ||
slack_id = user["id"] | ||
real_name = user["profile"]["real_name"] | ||
status_text = user["profile"]["status_text"] | ||
status_emoji = user["profile"]["status_emoji"] | ||
self.new(name, slack_id, real_name, status_text, status_emoji) | ||
end | ||
|
||
return users | ||
end | ||
|
||
def details | ||
user_details = "Username: #{name} | ||
Slack ID: #{slack_id} | ||
Real Name: #{real_name} | ||
Status Text: #{status_text} | ||
Status Emoji: #{status_emoji}" | ||
return user_details | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# workspace | ||
require_relative "user" | ||
require_relative "channel" | ||
require "terminal-table" | ||
require "pry" | ||
|
||
class Workspace | ||
attr_reader :users, :channels, :selected, :list_users, :list_channels | ||
|
||
def initialize | ||
@users = SlackCli::User.list | ||
@channels = SlackCli::Channel.list | ||
@selected = nil | ||
end | ||
|
||
def select_channel(name) | ||
@selected = @channels.find do |channel| | ||
channel.name == name || channel.slack_id == name | ||
end | ||
if @selected == nil | ||
puts "That channel does not exist" | ||
else | ||
return @selected | ||
end | ||
end | ||
|
||
def select_user(name) | ||
@selected = @users.find do |user| | ||
user.name == name || user.slack_id == name | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of the |
||
|
||
if @selected == nil | ||
puts "That user does not exist" | ||
else | ||
return @selected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code is here to interact with the user, I would probably consider it view code and move it to the |
||
end | ||
end | ||
|
||
def show_details | ||
if @selected == nil | ||
raise SlackCli::SlackError, "No user or channel selected!" | ||
else | ||
@selected.details | ||
end | ||
end | ||
|
||
def send_message(message) | ||
if @selected == nil | ||
raise SlackCli::SlackError, "No user or channel selected!" | ||
else | ||
message.strip! | ||
if message.length == 0 | ||
raise SlackCli::SlackError, "Message must cannot be nil or blank..." | ||
else | ||
@selected.post_message(@selected.slack_id, message) | ||
end | ||
end | ||
end | ||
|
||
def list_channels | ||
@channels.each do |channel| | ||
puts "Channel name: #{channel.name} | ||
ID: #{channel.slack_id} | ||
Topic: #{channel.topic} | ||
Member count:#{channel.member_count}" | ||
end | ||
return nil | ||
end | ||
|
||
def list_users | ||
@users.each do |user| | ||
puts "#{user.real_name} Slack ID: #{user.slack_id}" | ||
end | ||
return nil | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative "test_helper" | ||
|
||
describe "Channel" do | ||
let(:get_response) do | ||
VCR.use_cassette("slack/channel_get") { SlackCli::Channel.get } | ||
end | ||
let(:channel_list) do | ||
VCR.use_cassette("slack/channel_list") { SlackCli::Channel.list } | ||
end | ||
|
||
describe "self get method" do | ||
it "successfully returns an HTTP response object" do | ||
channels = get_response | ||
|
||
expect(channels["ok"]).must_equal true | ||
end | ||
end | ||
|
||
describe "list" do | ||
it "creates a list of all channels" do | ||
channels = channel_list | ||
|
||
expect(channels).must_be_kind_of Array | ||
|
||
channels.each do |channel| | ||
expect(channel).must_be_instance_of SlackCli::Channel | ||
end | ||
end | ||
end | ||
|
||
describe "details" do | ||
it "lists details for an instance of Channel" do | ||
channel = channel_list[1] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you create a channel = SlackCli::Channel.new('bad_id', 'channel_dne', '', 0)
channel.details Presumably this would work just fine, but it makes an interesting test case. |
||
expect(channel.details).must_be_kind_of String | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great use of a template method.