-
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 - Faiza & Stephanie #19
base: master
Are you sure you want to change the base?
Changes from all commits
4f365ad
fde573e
e1223ed
0d44ca4
d36f0d7
d4ddbab
9d667d0
e5e14b9
8e9df34
f67d3d7
36a799a
1a9e525
6dd2a5c
1bcc9d7
6bbc0ce
e359815
9238e5b
8ccb32d
4f1d840
f711768
0256dfe
589b2b4
3bb46ac
6a7acfa
41511e5
a30277b
3db37b5
7883428
b18f5cd
e91d03a
b29733f
6f5a9e1
f55b1fe
d9a8ea4
014d8fe
6b04020
e47b865
abccb37
4d610e6
6fc399e
e77e043
4260dec
d7f3e2a
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,31 @@ | ||
require "awesome_print" | ||
require 'table_print' | ||
require_relative "recipient" | ||
|
||
module Slack | ||
class Channel < Recipient | ||
BASE_URL = "https://slack.com/api/channels.list" | ||
PARAMETERS = { token: ENV["SLACK_API_TOKEN"] } | ||
|
||
attr_reader :topic, :member_count | ||
|
||
def initialize(name, slack_id, topic, member_count) | ||
super(name, slack_id) | ||
@name = name | ||
@topic = topic | ||
@member_count = member_count | ||
end | ||
|
||
def self.list | ||
channel_data = self.get(BASE_URL, PARAMETERS) | ||
|
||
channel_data["channels"].map do |channel| | ||
self.new(channel["name"], channel["id"], channel["topic"]["value"], channel["members"].length) | ||
end | ||
end | ||
|
||
def channel_details | ||
super.push("topic", "member_count") | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require "httparty" | ||
require "awesome_print" | ||
require 'table_print' | ||
require "dotenv" | ||
Dotenv.load | ||
|
||
module Slack | ||
class ResponseError < StandardError; end | ||
|
||
class Recipient | ||
BASE_URL = "https://slack.com/api/chat.postMessage" | ||
|
||
attr_reader :slack_id, :name | ||
|
||
def initialize(slack_id, name) | ||
@slack_id = slack_id | ||
@name = name | ||
# raise error here if name isn't a string | ||
end | ||
|
||
def self.get(base_url, parameters) | ||
response = HTTParty.get(base_url, query: parameters) | ||
unless response.code == 200 && response.parsed_response["ok"] | ||
raise ResponseError, response["error"] | ||
end | ||
|
||
return response | ||
end | ||
|
||
def send_message(channel, text) | ||
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. Well done |
||
message_request = HTTParty.post(BASE_URL, | ||
headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
body: { | ||
token: ENV["SLACK_API_TOKEN"], | ||
channel: channel, | ||
text: text, | ||
# as_user: true, | ||
}) | ||
|
||
if message_request["ok"] | ||
return true | ||
else | ||
raise ResponseError, "There was an error sending your message" | ||
end | ||
end | ||
|
||
def self.list | ||
raise NotImplementedError, "Implement me in a child class!" | ||
end | ||
|
||
def details | ||
["name", "slack_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. This method just returns an array with the strings "name" and "slack_id" in it... This isn't useful data. I would suggest: def details
return "name: #{name}\nID: #{slack_id}"
end |
||
end | ||
end # class | ||
end # module |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require "awesome_print" | ||
require "table_print" | ||
require_relative "workspace" | ||
|
||
Dotenv.load | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Slack::Workspace.new | ||
# TODO project | ||
|
||
puts "This workspace has #{Slack::Channel.list.length} channels." | ||
puts "This workspace has #{Slack::User.list.length} users." | ||
|
||
loop do | ||
puts "\n\t\t~ MENU ~ | ||
\t|1|::List Channels | ||
\t|2|::List Users | ||
\t|3|::Select User | ||
\t|4|::Select Channel | ||
\t|5|::Send Message | ||
\t|6|::Quit" | ||
puts "\nPlease select from the Menu:" | ||
answer = gets.chomp.to_i | ||
|
||
# List Stuff | ||
case answer | ||
when 1 | ||
# ap Slack::Channel.list => {:width => 12} | ||
tp workspace.channels, "name", "slack_id", "topic", "member_count" | ||
when 2 | ||
# ap Slack::User.list => { :width => 12 } | ||
tp workspace.users, "slack_id", :Name => { :display_method => "real_name" }, | ||
:include => { :User_Name => { :display_method => "name" } } | ||
when 3 | ||
puts "Please enter a SlackID or full name:" | ||
name_or_id = gets.chomp | ||
|
||
workspace.select_user(name_or_id) | ||
# selection = workspace.select_user(name_or_id) | ||
|
||
puts "Show additional details for #{name_or_id}? (Y/N)" | ||
choice = gets.chomp.downcase | ||
|
||
if choice == "y" | ||
# puts "#{workspace.show_details}" | ||
tp workspace.show_details, "slack_id", :Name => { :display_method => "real_name" }, | ||
:include => { :User_Name => { :display_method => "name" } } | ||
# call show_details method and puts ap it | ||
end | ||
when 4 | ||
puts "Please enter a channel name or channel id:" | ||
name_or_id = gets.chomp | ||
|
||
workspace.select_channel(name_or_id) | ||
|
||
puts "Show additional details for #{name_or_id}? (Y/N)" | ||
choice = gets.chomp.downcase | ||
|
||
if choice == "y" | ||
# puts "#{workspace.show_details}" | ||
tp workspace.show_details, "name", "slack_id", "topic", "member_count" | ||
end | ||
when 5 | ||
puts "What would you like your message to say ?" | ||
message = gets.chomp | ||
workspace.send_message(message) | ||
puts "Your message has been sent! Yay!" | ||
when 6 | ||
break | ||
else | ||
puts "Please select from Menu Items." | ||
end | ||
end | ||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "awesome_print" | ||
require 'table_print' | ||
require_relative "recipient" | ||
|
||
module Slack | ||
class User < Recipient | ||
BASE_URL = "https://slack.com/api/users.list" | ||
PARAMETER = { token: ENV["SLACK_API_TOKEN"] } | ||
|
||
attr_reader :real_name, :status_text | ||
|
||
def initialize(name, slack_id, real_name) | ||
super(name, slack_id) | ||
@real_name = real_name | ||
end | ||
|
||
def self.list | ||
user_data = self.get(BASE_URL, PARAMETER) | ||
user_data["members"].map do |user| | ||
self.new(user["id"], user["name"], user["profile"]["real_name"]) | ||
end | ||
end | ||
|
||
def details | ||
super << "real_name" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require "dotenv" | ||
require "httparty" | ||
require "table_print" | ||
require "awesome_print" | ||
require_relative "user" | ||
require_relative "channel" | ||
|
||
Dotenv.load | ||
|
||
module Slack | ||
class ResponseError < StandardError; end | ||
|
||
class Workspace | ||
BASE_URL = "https://slack.com/api/users.list" | ||
attr_reader :users, :channels | ||
attr_accessor :selection | ||
|
||
def initialize | ||
@users = User.list | ||
@channels = Channel.list | ||
@selection = nil | ||
end | ||
|
||
def select_user(name_or_id) | ||
users.each do |user| | ||
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 can use |
||
if name_or_id == user.name || name_or_id == user.slack_id || name_or_id == user.real_name | ||
@selection = user | ||
end | ||
end | ||
if @selection == nil | ||
return "Sorry, #{name_or_id} is not a valid user." | ||
end | ||
return @selection | ||
end | ||
|
||
def select_channel(name_or_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. Do you notice how this and |
||
@channels.each do |channel| | ||
if name_or_id == channel.name || name_or_id == channel.slack_id | ||
@selection = channel | ||
end | ||
end | ||
|
||
if @selection == nil | ||
return "Sorry, #{name_or_id} is not a valid channel." | ||
end | ||
return @selection | ||
end | ||
|
||
def show_details | ||
return @selection, @selection.details | ||
# if @selection == nil | ||
# return false | ||
# ap @selection, @selection.details | ||
# end | ||
tp @selection, @selection.details | ||
end | ||
|
||
def send_message(text) | ||
if @selection == nil | ||
return false | ||
end | ||
|
||
if text == "" | ||
return nil | ||
end | ||
|
||
return @selection.send_message(@selection.slack_id, text) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require_relative "test_helper" | ||
|
||
describe "Channels" do | ||
describe "self get method" do | ||
it "returns a response from api for channels list" do | ||
VCR.use_cassette("connect to channel api") do | ||
url = "http://slack.com/api/channels.list" | ||
query = { token: ENV["SLACK_API_TOKEN"] } | ||
request = Slack::Channel.get(url, query) | ||
|
||
expect(request["ok"]).must_equal(true) | ||
end | ||
end | ||
end | ||
|
||
it "raises exception if get request fails" do | ||
VCR.use_cassette("connect to channel api") do | ||
url = "https://slack.com/api/channel.list" | ||
query = { token: "idontworkidontworkkkkkk" } | ||
|
||
expect { | ||
Slack::Channel.get(url, query) | ||
}.must_raise Slack::ResponseError | ||
end | ||
end | ||
|
||
describe "self list method" do | ||
it "returns channels array" do | ||
VCR.use_cassette("connect to channel api") do | ||
channels = Slack::Channel.list | ||
expect(channels).must_be_kind_of Array | ||
|
||
(0..channels.length - 1).each do |i| | ||
expect(channels[i]).must_be_kind_of Slack::Channel | ||
end | ||
end | ||
|
||
describe "channel details method" do | ||
it "returns an array that contains correct strings" do | ||
channel = Slack::Channel.new("i", "play", "electric", "bass") | ||
expect(channel.details).must_be_kind_of Array | ||
expect(channel.details[0]).must_equal "name" | ||
expect(channel.details[1]).must_equal "slack_id" | ||
expect(channel.details[2]).must_equal "topic" | ||
expect(channel.details[3]).must_equal "member_count" | ||
end | ||
end | ||
# # end end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative "test_helper" | ||
|
||
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. Incomplete testing here |
||
describe "Recipient Class" do | ||
describe "self list method" do | ||
it "raises an error if not implemented in a subclass" do | ||
expect { Slack::Recipient.list }.must_raise NotImplementedError | ||
end | ||
end | ||
|
||
describe "send message method" do | ||
it "raises error if parameters are incorrect" do | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
require 'simplecov' | ||
require "simplecov" | ||
SimpleCov.start | ||
|
||
require 'minitest' | ||
require 'minitest/autorun' | ||
require 'minitest/reporters' | ||
require 'minitest/skip_dsl' | ||
require 'vcr' | ||
require "minitest" | ||
require "minitest/autorun" | ||
require "minitest/reporters" | ||
require "minitest/skip_dsl" | ||
require "vcr" | ||
|
||
require_relative "../lib/user" | ||
require_relative "../lib/channel" | ||
require_relative "../lib/workspace" | ||
require_relative "../lib/recipient" | ||
require_relative "../lib/slack_cli" | ||
|
||
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
||
VCR.configure do |config| | ||
config.cassette_library_dir = "specs/cassettes" | ||
config.hook_into :webmock | ||
end | ||
config.cassette_library_dir = "specs/cassettes" # folder where casettes will be located | ||
config.hook_into :webmock # tie into this other tool called webmock | ||
config.default_cassette_options = { | ||
:record => :new_episodes, # record new data when we don't have it yet | ||
:match_requests_on => [:method, :uri, :body], # The http method, URI and body of a request all need to match | ||
} | ||
# Don't leave our token lying around in a cassette file. | ||
config.filter_sensitive_data("<SLACK_API_TOKEN>") do | ||
ENV["SLACK_API_TOKEN"] | ||
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.
Clever, except that the
Recipient
class doesn't have achannel_details
method. This method should be nameddetails
.