-
Notifications
You must be signed in to change notification settings - Fork 31
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
Water - Kayla and Mackenzie #15
base: master
Are you sure you want to change the base?
Changes from all commits
6bfc7ff
45892c2
b281d0e
93cbf4f
307b8c8
842c464
b2217b5
ff4662c
f2201eb
7bf2314
361172c
d031112
135458b
7a314b2
9a25e58
a1e3f00
bc83915
46991e7
035321a
0034a2a
c3fd363
10cca2a
e4984ba
bb7073b
3df54a5
ed32b26
686116b
6788196
20a3c13
5308f94
ffadce4
853a12b
a7ac6f2
659d6fc
a257b60
7cd655c
0412387
f40d1ec
4383d44
6509699
e4c732f
9fbca53
47b6075
3e7bf22
5c13f20
65a03c9
89389bd
e457e14
57d3fbc
c5086b3
02d2307
6d8a0cf
2a694ec
1220e25
776c135
23bfc2b
83bfb59
6aca832
440c366
e02e66b
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,42 @@ | ||
require 'httparty' | ||
require 'dotenv' | ||
require 'table_print' | ||
|
||
require_relative 'recipient' | ||
|
||
Dotenv.load | ||
|
||
class Channel < Recipient | ||
|
||
attr_reader :topic, :member_count, :slack_id, :name | ||
|
||
def initialize (slack_id, name, topic, member_count) | ||
super(slack_id, name) | ||
@topic = topic | ||
@member_count = member_count | ||
end | ||
|
||
def self.list | ||
response = self.get("https://slack.com/api/conversations.list") | ||
|
||
channel_array = [] | ||
|
||
response["channels"].each do |channel| | ||
slack_id = channel["id"] | ||
name = channel["name"] | ||
purpose = channel["purpose"]["value"] | ||
member_count = channel["num_members"] | ||
|
||
temp_channel = self.new(slack_id, name, purpose, member_count) | ||
channel_array << temp_channel | ||
end | ||
|
||
return channel_array | ||
end | ||
|
||
def details | ||
return "Channel found, here's the scoop!\n Slack ID: #{slack_id}\n Name: #{name}\n Topic: #{topic}\n Member Count: #{member_count}" | ||
end | ||
|
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'httparty' | ||
require 'dotenv' | ||
|
||
Dotenv.load | ||
|
||
class Recipient | ||
attr_reader :slack_id, :name | ||
|
||
def initialize(slack_id, name) | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
|
||
def self.get(url) | ||
response = HTTParty.get(url, query: { | ||
token: ENV["SLACK_TOKEN"] | ||
} | ||
) | ||
|
||
unless response.code == 200 && response.parsed_response["ok"] | ||
raise ArgumentError, "SlackApiError. Reason: #{response["error"]}" | ||
end | ||
|
||
return response | ||
end | ||
|
||
def self.send_msg(message, id) | ||
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. Consider making |
||
response = HTTParty.post("https://slack.com/api/chat.postMessage", | ||
body: { | ||
token: ENV["SLACK_TOKEN"], | ||
text: message, | ||
channel: id | ||
}, | ||
headers: { 'Content-Type' => 'application/x-www-form-urlencoded'} | ||
) | ||
|
||
unless response.code == 200 && response.parsed_response["ok"] | ||
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. Nice work error handling with a custom exception. |
||
raise ArgumentError, "SlackApiError. Reason: #{response["error"]}" | ||
end | ||
|
||
return response | ||
end | ||
|
||
|
||
private | ||
|
||
def self.list | ||
raise NotImplementedError, 'Implement me in a child class' | ||
end | ||
|
||
def self.details | ||
raise NotImplementedError, 'Implement me in a child class' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,59 @@ | ||
#!/usr/bin/env ruby | ||
require 'table_print' | ||
require_relative 'workspace' | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
puts "Welcome to the Ada Slack CLI!\n\n" | ||
workspace = Workspace.new | ||
|
||
# TODO project | ||
puts "There are #{workspace.channels.count} channels in this workspace" | ||
puts "There are #{workspace.users.count} users in this workspace." | ||
puts | ||
puts "*" * 40 | ||
Comment on lines
-4
to
+12
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. Consider wrapping this set-up functionality into its own method. |
||
|
||
user_input = nil | ||
|
||
until user_input == "7" || user_input == "quit" | ||
puts options | ||
user_input = gets.chomp.downcase | ||
|
||
case user_input | ||
when "1", "list users" | ||
workspace.list_users | ||
when "2", "list channels" | ||
workspace.list_channels | ||
when "3", "select user" | ||
puts "Please enter User Name or ID" | ||
user_identifier = gets.chomp.downcase | ||
workspace.select_user(user_identifier) | ||
when "4", "select channel" | ||
puts "Please enter Chanel Name or ID" | ||
channel_identifier = gets.chomp.downcase | ||
workspace.select_channel(channel_identifier) | ||
when "5", "details" | ||
if workspace.selected_recipient == nil | ||
puts "Please select a channel or user first to view details" | ||
else | ||
puts workspace.find_details | ||
end | ||
when "6", "send message" | ||
if workspace.selected_recipient == nil | ||
puts "Please select a channel or user first to send message" | ||
else | ||
puts "Send a message!" | ||
message = gets.chomp | ||
workspace.send_msg(message) | ||
puts "\nMessage sent!" | ||
end | ||
end | ||
end | ||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
Comment on lines
+16
to
51
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. Consider wrapping this functionality into its own method. |
||
|
||
def options | ||
puts "\nWhat would you like to do?\n" | ||
puts "Choose the number that corresponds to the following options:" | ||
puts "[1]'List Users'\n[2]'List Channels'\n[3]'Select User'\n[4]'Select Channel'\n[5]'Details'\n[6]'Send Message'\n[7]'Quit'" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'httparty' | ||
require 'dotenv' | ||
require 'table_print' | ||
|
||
require_relative 'recipient' | ||
|
||
Dotenv.load | ||
|
||
class User < Recipient | ||
|
||
attr_reader :slack_id, :username, :real_name, :status_emoji | ||
|
||
def initialize (slack_id, username, real_name, status_emoji, name) | ||
super(slack_id, name) | ||
@real_name = real_name | ||
@status_emoji = status_emoji | ||
@username = username #for table print | ||
end | ||
|
||
def self.list | ||
response = self.get("https://slack.com/api/users.list") | ||
|
||
user_array = [] | ||
response["members"].each do |user| | ||
slack_id = user["id"] | ||
username = user["name"] #for table print | ||
real_name = user["profile"]["real_name"] | ||
status_emoji = user["profile"]["status_emoji"] | ||
name = user["name"] | ||
|
||
temp_user = self.new(slack_id, username, real_name, status_emoji, name) | ||
user_array << temp_user | ||
end | ||
return user_array | ||
end | ||
|
||
def details | ||
return "User found, here's the scoop!\n Username: #{name} \n Slack ID: #{slack_id}\n Real Name: #{real_name}\n Status Emoji: #{status_emoji}" | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'dotenv' | ||
|
||
require_relative 'user' | ||
require_relative 'channel' | ||
|
||
Dotenv.load | ||
|
||
class Workspace | ||
attr_reader :users, :channels | ||
attr_accessor :selected_recipient | ||
|
||
def initialize | ||
@users = User.list | ||
@channels = Channel.list | ||
@selected_recipient = nil | ||
end | ||
|
||
def list_users | ||
return tp @users, "slack_id", "username", "real_name" | ||
end | ||
|
||
def list_channels | ||
return tp @channels, "name", "topic", "member_count", "slack_id" | ||
end | ||
|
||
def select_user(id) | ||
@users.each do |user| | ||
if user.slack_id.downcase == id || user.username.downcase == id | ||
@selected_recipient = user | ||
puts "Valid user selected" | ||
return @selected_recipient | ||
end | ||
end | ||
puts "User does not exist" | ||
end | ||
|
||
def select_channel(id) | ||
@channels.each do |channel| | ||
if channel.slack_id.downcase == id || channel.name.downcase == id | ||
@selected_recipient = channel | ||
puts "Valid channel selected" | ||
return @selected_recipient | ||
end | ||
end | ||
puts "Channel does not exist" | ||
end | ||
|
||
def find_details | ||
return @selected_recipient.details | ||
end | ||
|
||
def send_msg(message) | ||
Recipient.send_msg(message, @selected_recipient.slack_id) | ||
end | ||
|
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative 'test_helper' | ||
|
||
describe Channel do | ||
before do | ||
slack_id = "111" | ||
name = "random" | ||
topic = "random" | ||
member_count = "79" | ||
@channel= Channel.new(slack_id, name, topic, member_count) | ||
@detail = @channel.details | ||
@slack_id = slack_id | ||
@name = name | ||
@topic = topic | ||
@member_count = member_count | ||
|
||
VCR.use_cassette("random_channel") do | ||
@response = Channel.list | ||
end | ||
end | ||
|
||
it 'is an instance of Channel' do | ||
expect(@channel).must_be_kind_of Channel | ||
end | ||
|
||
it 'channel.list is an instance of an array' do | ||
expect(@response).must_be_kind_of Array | ||
expect(@response.length).must_be_kind_of Integer | ||
expect(@response.length).must_be_close_to 49 #passes at time of submission | ||
end | ||
|
||
it 'returns the correct info for members' do | ||
expect _(@response[1].slack_id).must_equal "C0165NC8LHH" | ||
expect _(@response[3].name).must_equal "csmemes" | ||
end | ||
|
||
describe 'channel details' do | ||
|
||
it 'returns a description of a selected channel' do | ||
expect(@detail).must_equal "Channel found, here's the scoop!\n Slack ID: #{@slack_id}\n Name: #{@name}\n Topic: #{@topic}\n Member Count: #{@member_count}" | ||
end | ||
|
||
it 'description returns a string' do | ||
expect(@detail).must_be_kind_of String | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require_relative 'test_helper' | ||
require 'httparty' | ||
|
||
describe Recipient do | ||
before do | ||
@slack_id = "U0179312KV3" | ||
@name = "kayla.elizabeth89" | ||
@recipient = Recipient.new(@slack_id, @name) | ||
end | ||
|
||
describe "instantiation" do | ||
it "creates a new Recipient instance" do | ||
expect(@recipient).must_be_instance_of Recipient | ||
end | ||
end | ||
|
||
it "can send a valid message" do | ||
VCR.use_cassette("messages take 4") do | ||
response = Recipient.send_msg("I can post messages!", "U0179312KV3") | ||
expect(response.code).must_equal 200 | ||
end | ||
end | ||
|
||
it "can send a valid complicated message" do | ||
VCR.use_cassette("messages take 4") do | ||
response = Recipient.send_msg("Kayla! I'm testing a longer message with emojis :party_blob: :confused-dog: I'm sorry if this comes through to you multiple times but if it does I at least hope you are able to enjoy the emojis being sent through our bot!! :gem: :unicorn_face: :zap: **FIST BUMP** :right-facing_fist::left-facing_fist: ", "U0179312KV3") | ||
expect(response.code).must_equal 200 | ||
end | ||
end | ||
|
||
it "will raise error for invalid address" do | ||
VCR.use_cassette("messages take 4") do | ||
expect{Recipient.send_msg("I can post messages!", "mvlofthus")}.must_raise ArgumentError | ||
end | ||
end | ||
|
||
it "will raise error for empty message" do | ||
VCR.use_cassette("messages take 4") do | ||
expect{Recipient.send_msg("", "U0179312KV3")}.must_raise ArgumentError | ||
end | ||
end | ||
|
||
describe 'private methods' do | ||
|
||
it "will raise Argument error if Recipient.list is called" do | ||
VCR.use_cassette("messages take 4") do | ||
expect{Recipient.list}.must_raise NotImplementedError | ||
end | ||
end | ||
|
||
it "will raise Argument error if Recipient.details is called" do | ||
VCR.use_cassette("messages take 4") do | ||
expect{Recipient.details}.must_raise NotImplementedError | ||
end | ||
end | ||
|
||
describe 'failed SlackAPI request/post' do | ||
|
||
it "Will raise an Argument error if Token is bogus" do #this will work if you give a bogus token in .env | ||
VCR.use_cassette("SlackAPI failed") do | ||
expect{Recipient.get("https://slack.com/api/users.list")}.must_raise ArgumentError | ||
end | ||
end | ||
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.
Good work error handling.