-
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
Ports - Heather and Mina #12
base: master
Are you sure you want to change the base?
Changes from all commits
904bd3f
0afb27b
49e3afa
f6b7471
7c9d55b
37897a0
b8a1fa8
8115f60
8e47965
7e223d5
da766d5
e611d21
573b950
f277f55
389301d
2096753
0be2280
e8ed772
8cbafb2
a9aaaa6
41c015d
7c51117
f3436e8
a7a0cc6
b63fb6c
9039c1e
f4ee723
8e924df
3764283
f58feb9
acd65e2
8249ccd
ad6efe6
4ecb3fb
b14f42d
5a0cd37
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 |
---|---|---|
|
@@ -54,3 +54,5 @@ build-iPhoneSimulator/ | |
|
||
# Ignore cassette files | ||
/specs/cassettes/ | ||
|
||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
// 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}" | ||
} | ||
] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative "recipient" | ||
|
||
module Slack | ||
class Channel < Recipient | ||
attr_reader :slack_id, :name, :num_members, :topic | ||
|
||
def initialize(slack_id, name, num_members, topic) | ||
super(slack_id, name) | ||
@num_members = num_members | ||
@topic = topic | ||
end | ||
|
||
def self.get | ||
url = "https://slack.com/api/channels.list" | ||
params = { | ||
token: ENV["KEY"], | ||
} | ||
|
||
response_from_get = HTTParty.get(url, query: params) | ||
|
||
if response_from_get["ok"] == false | ||
raise ArgumentError, "Request is unsuccessful" | ||
else | ||
return response_from_get | ||
end | ||
end | ||
|
||
def self.list_all | ||
all_channels = Channel.get["channels"].map do |channel| | ||
self.new(channel["id"], channel["name"], channel["num_members"], channel["topic"]["value"]) | ||
end | ||
return all_channels | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Slack | ||
class Recipient | ||
attr_reader :slack_id, :name | ||
|
||
def initialize(slack_id, name) | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
|
||
def post_message(message) | ||
url = "https://slack.com/api/chat.postMessage" | ||
params = { | ||
token: ENV["KEY"], | ||
channel: @slack_id || @name, | ||
text: message, | ||
} | ||
|
||
message_request = HTTParty.post(url, query: params) | ||
if message_request["ok"] == false | ||
raise ArgumentError, "Request is unsuccessful" | ||
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. You should also include the error message. |
||
else | ||
return message_request | ||
end | ||
end | ||
|
||
# def self.get(url, params) | ||
# HTTParty.GET(url, query: params) | ||
# end | ||
|
||
def self.list_all | ||
raise NotImplementedError, "Please implement in my child." | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,96 @@ | ||
#!/usr/bin/env ruby | ||
# require_relative "../spec/test_helper" | ||
require_relative "channel" | ||
require_relative "user" | ||
require_relative "recipient" | ||
require_relative "workspace" | ||
require "dotenv" | ||
require "httparty" | ||
Dotenv.load | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
|
||
# TODO project | ||
print "Channels loaded: ", Slack::Workspace.channel_list_all.length | ||
|
||
print "\nUsers loaded: ", Slack::Workspace.user_list_all.length | ||
|
||
puts "\nPlease select from the following options: \n list channels, select channel, list users, select user, details, send message, or quit" | ||
selection = gets.chomp.downcase | ||
|
||
until selection == "quit" | ||
case selection | ||
when "list channels" | ||
puts Slack::Workspace.all_channels_details | ||
when "select channel" | ||
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. The way you have this here means the channel doesn't stay selected. It also means you have a lot of duplicate code in selecting users and channels and sending messages etc. |
||
puts "Which channel would you like to select?" | ||
channel_identifier = gets.chomp.to_s | ||
selected_channel = Slack::Workspace.select_channel(channel_identifier) | ||
if selected_channel.nil? | ||
puts "This channel doesn't exist. Exiting to main menu" | ||
else | ||
puts "You have selected #{selected_channel.name}. What would you like to do: details, send message, or quit" | ||
answer = gets.chomp.downcase | ||
case answer | ||
when "details" | ||
puts Slack::Workspace.selected_channel_details(selected_channel) | ||
when "send message" | ||
puts "Type message: " | ||
message = gets.chomp | ||
if message == "" | ||
puts "Can't send a blank message. \nType message :" | ||
message = gets.chomp | ||
else | ||
Slack::Recipient.new(selected_channel.slack_id, selected_channel.name).post_message(message) | ||
end | ||
when "quit" | ||
else | ||
puts "Please select a valid input. Returning to main!" | ||
end | ||
end | ||
when "list users" | ||
puts Slack::Workspace.all_users_details | ||
when "select user" | ||
puts "Which user would you like to select?" | ||
user_identifier = gets.chomp.to_s | ||
selected_user = Slack::Workspace.select_user(user_identifier) | ||
if selected_user.nil? | ||
puts "This user doesn't exist. Exiting to main menu" | ||
else | ||
puts "You have selected #{selected_user.name}. What would you like to do: details, send message, or quit" | ||
answer = gets.chomp.downcase | ||
case answer | ||
when "details" | ||
puts Slack::Workspace.selected_user_details(selected_user) | ||
when "send message" | ||
puts "Type message: " | ||
message = gets.chomp | ||
if message == "" | ||
puts "Can't send a blank message. \nType message :" | ||
message = gets.chomp | ||
else | ||
Slack::Recipient.new(selected_user.slack_id, selected_user.name).post_message(message) | ||
end | ||
when "quit" | ||
else | ||
puts "Please select a valid input. Returning to main!" | ||
end | ||
end | ||
when "details" | ||
puts "You haven't selected a user or channel. Exiting to main prompt." | ||
when "send message" | ||
puts "You haven't selected a user or channel. Exiting to main prompt." | ||
when "quit" | ||
else | ||
puts "Please enter a valid selection." | ||
end | ||
puts "What would you like to do next? (list users, select user, list channels, select channel, details, send message, or quit)" | ||
selection = gets.chomp.downcase | ||
end | ||
|
||
# Should we use a case statement here for the 3 options? | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME | ||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_relative "recipient" | ||
|
||
module Slack | ||
class User < Recipient | ||
attr_reader :slack_id, :name, :real_name | ||
|
||
def initialize(slack_id, name, real_name) | ||
super(slack_id, name) | ||
@real_name = real_name | ||
end | ||
|
||
def self.get | ||
url = "https://slack.com/api/users.list" | ||
params = { | ||
token: ENV["KEY"], | ||
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. In general don't name your environmental variable Also both the token and the URL would make great constants. |
||
} | ||
|
||
response_from_get = HTTParty.get(url, query: params) | ||
|
||
return response_from_get | ||
end | ||
|
||
def self.list_all | ||
users = User.get["members"].map do |user| | ||
self.new(user["id"], user["name"], user["profile"]["real_name"]) | ||
end | ||
return users | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require_relative "channel" | ||
require_relative "user" | ||
require_relative "recipient" | ||
require "dotenv" | ||
require "httparty" | ||
require "pry" | ||
Dotenv.load | ||
|
||
module Slack | ||
class Workspace | ||
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 works as a collection of class methods, but if you're using all class methods, you might as well just make |
||
def self.channel_list_all | ||
channels = Slack::Channel.list_all | ||
name_channels = channels.map do |channel| | ||
channel.name | ||
end | ||
return name_channels | ||
end | ||
|
||
def self.all_channels_details | ||
channels = Slack::Channel.list_all.map do |channel| | ||
"Channel ID: #{channel.slack_id}\nChannel name: #{channel.name} \nMembers: #{channel.num_members} \nChannel Topic: #{channel.topic}" | ||
end | ||
return channels | ||
end | ||
|
||
def self.select_channel(channel_identifier) | ||
find_channel = Slack::Channel.list_all.find do |channel| | ||
channel.name.downcase == channel_identifier.downcase || | ||
channel.slack_id.downcase == channel_identifier.downcase | ||
end | ||
return find_channel | ||
end | ||
|
||
def self.selected_channel_details(chosen_channel) | ||
details = "Channel ID: #{chosen_channel.slack_id}\nChannel name: #{chosen_channel.name} \nMembers: #{chosen_channel.num_members} \nChannel Topic: #{chosen_channel.topic}" | ||
|
||
return details | ||
end | ||
|
||
def self.user_list_all | ||
users = Slack::User.list_all | ||
name_users = users.map do |user| | ||
user.name | ||
end | ||
return name_users | ||
end | ||
|
||
def self.all_users_details | ||
users = Slack::User.list_all.map do |user| | ||
"User ID: #{user.slack_id}: \nReal name: #{user.real_name} \nDisplay name: #{user.name}." | ||
end | ||
return users | ||
end | ||
|
||
def self.select_user(user_identifier) | ||
find_user = Slack::User.list_all.find { | ||
|user| | ||
user.name.downcase == user_identifier.downcase || | ||
user.slack_id.downcase == user_identifier.downcase | ||
} | ||
|
||
return find_user | ||
end | ||
|
||
def self.selected_user_details(chosen_user) | ||
details = "User ID: #{chosen_user.slack_id}: \nReal name: #{chosen_user.real_name} \nDisplay name: #{chosen_user.name}." | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require_relative "test_helper.rb" | ||
|
||
require "pry" | ||
|
||
describe "Channel class" do | ||
it "Creates an instance of the Channel class" do | ||
new_channel = Slack::Channel.new("id", "name", 3, "topic") | ||
expect(new_channel).must_be_instance_of Slack::Channel | ||
end | ||
end | ||
|
||
describe "Get method" do | ||
it "Successfully GETs response from Slack API" do | ||
VCR.use_cassette("channel_find") do | ||
response = Slack::Channel.get | ||
|
||
expect(response["ok"]).must_equal true | ||
end | ||
end | ||
end | ||
|
||
describe "list_all method" do | ||
it "gives a list of all user information from the API and returns a value of a 'general' channel name" do | ||
VCR.use_cassette("channel_find") do | ||
list_response = Slack::Channel.list_all | ||
ap list_response | ||
|
||
expect(list_response.length).must_equal 3 | ||
expect(list_response[0].name).must_equal "general" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require_relative "test_helper.rb" | ||
require "pry" | ||
|
||
describe "Recipient class" do | ||
it "creates an instance of Recipient class" do | ||
receiver = Slack::Recipient.new("id", "name") | ||
expect(receiver).must_be_instance_of Slack::Recipient | ||
end | ||
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. You should also test that your abstract method raises an error. |
||
describe "Post message method" do | ||
it "will return a true value for 'ok'" do | ||
VCR.use_cassette("message_find") do | ||
recipient_id = "USLACKBOT" | ||
name = "slackbot" | ||
|
||
message = "Hi Slackbot! - Heather and Mina" | ||
|
||
new_message = Slack::Recipient.new(recipient_id, name).post_message(message) | ||
expect(new_message["ok"]).must_equal true | ||
expect(new_message["message"]["text"]).must_match "- Heather and Mina" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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. ?? |
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.
You could implement this method in
Recipient
and inherit the behavior.