Skip to content
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

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4f365ad
Tried out API call to channels.list API. Added VCR to test_helper. Cr…
Faiza1987 Mar 19, 2019
fde573e
removed ap ENV token from method, just put it in to test if it works.
Faiza1987 Mar 19, 2019
e1223ed
Finished writing the self.list method for Channel class.
Faiza1987 Mar 19, 2019
0d44ca4
Added self.list method to user.rb
Faiza1987 Mar 19, 2019
d36f0d7
Added second test to chanel_spec.rb. It's failing
Faiza1987 Mar 20, 2019
d4ddbab
Modified list_channels method in channel.rb
Faiza1987 Mar 20, 2019
9d667d0
Officially done with list_users and list_channels
Faiza1987 Mar 20, 2019
e5e14b9
Added list in workspace
Faiza1987 Mar 20, 2019
8e9df34
Added menu and quit to workspace.rb
Faiza1987 Mar 20, 2019
f67d3d7
Modiefied menu to remove extra information
Faiza1987 Mar 20, 2019
36a799a
Added select_user method
Faiza1987 Mar 20, 2019
1a9e525
Fixed select_user and select_channel methods in User.rb and Channel.rb
Faiza1987 Mar 21, 2019
6dd2a5c
added pseudocode for channel spec tests
smarchante1 Mar 21, 2019
1bcc9d7
Merge branch 'master' of https://github.com/Faiza1987/slack-cli
smarchante1 Mar 21, 2019
6bbc0ce
added pseudocode for user spec
smarchante1 Mar 21, 2019
e359815
added pseudocode for show details methods
smarchante1 Mar 21, 2019
9238e5b
working on tests
smarchante1 Mar 21, 2019
8ccb32d
Modified the format of Menu slightly.
Faiza1987 Mar 21, 2019
4f1d840
Merge branch 'master' of https://github.com/Faiza1987/slack-cli
Faiza1987 Mar 21, 2019
f711768
starting the show details method
smarchante1 Mar 21, 2019
0256dfe
Merge branch 'master' of https://github.com/Faiza1987/slack-cli
Faiza1987 Mar 21, 2019
589b2b4
Refactored list_all and select methods in User.rb and Channel.rb
Faiza1987 Mar 21, 2019
3bb46ac
Added show_details method to Channel.
Faiza1987 Mar 22, 2019
6a7acfa
added show details for user
smarchante1 Mar 22, 2019
41511e5
refactored everythinggggg
smarchante1 Mar 22, 2019
a30277b
fixed show details method for user
smarchante1 Mar 22, 2019
3db37b5
added test for recipient class
smarchante1 Mar 22, 2019
7883428
finished user spec tests
smarchante1 Mar 22, 2019
b18f5cd
working on channel spec tests
smarchante1 Mar 22, 2019
e91d03a
working on workspace test
smarchante1 Mar 22, 2019
b29733f
workspace select channel method
smarchante1 Mar 22, 2019
6f5a9e1
Fixed show details methiod for Channel
Faiza1987 Mar 22, 2019
f55b1fe
made adjustments to channel class and fixed associated tests
smarchante1 Mar 22, 2019
d9a8ea4
successfully sent message to user and channel
smarchante1 Mar 22, 2019
014d8fe
working on fixing test errors
smarchante1 Mar 22, 2019
6b04020
Uncommited changes.
Faiza1987 Mar 22, 2019
e47b865
Uncommited changes
Faiza1987 Mar 22, 2019
abccb37
finished tests and ready for pull request
smarchante1 Mar 22, 2019
4d610e6
Add uncommitted changes
Faiza1987 Mar 22, 2019
6fc399e
Fixed one of the tests in Channel_spec.rb
Faiza1987 Mar 25, 2019
e77e043
Commented out puts statement in test
Faiza1987 Mar 25, 2019
4260dec
Removed some puts statements
Faiza1987 Mar 25, 2019
d7f3e2a
Made some minor changes and added table print
Faiza1987 Apr 1, 2019
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
31 changes: 31 additions & 0 deletions lib/channel.rb
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")

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 a channel_details method. This method should be named details.

end
end
end
55 changes: 55 additions & 0 deletions lib/recipient.rb
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)

Choose a reason for hiding this comment

The 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"]

Choose a reason for hiding this comment

The 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
11 changes: 0 additions & 11 deletions lib/slack.rb

This file was deleted.

78 changes: 78 additions & 0 deletions lib/slack_cli.rb
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
28 changes: 28 additions & 0 deletions lib/user.rb
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
70 changes: 70 additions & 0 deletions lib/workspace.rb
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|

Choose a reason for hiding this comment

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

You can use users.find here to simplify it.

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)

Choose a reason for hiding this comment

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

Do you notice how this and select_user are almost identical... that's a good sign you can dry it up with a helper method.

@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
51 changes: 51 additions & 0 deletions specs/channel_spec.rb
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
14 changes: 14 additions & 0 deletions specs/recipient_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative "test_helper"

Choose a reason for hiding this comment

The 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
32 changes: 23 additions & 9 deletions specs/test_helper.rb
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
Loading