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 - Amy & Ngoc #11

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c7dfaa2
project verification
amyesh Mar 19, 2019
37abe63
completed verification
amyesh Mar 19, 2019
a805089
updated files structure
amyesh Mar 19, 2019
2c55f9a
added ruby files for different classes
lebaongoc Mar 19, 2019
988df57
create list method for channel
lebaongoc Mar 19, 2019
50fc8b5
working through CLI for list channels
amyesh Mar 19, 2019
4dbbb71
minor changes
lebaongoc Mar 19, 2019
ef755a5
minor changes
lebaongoc Mar 19, 2019
e51b54a
CLI prints channel lists
amyesh Mar 19, 2019
f4e8588
removed slack_spec for CLI and added workspace files
amyesh Mar 19, 2019
4d12414
added files to test_helper
amyesh Mar 19, 2019
f72471f
minor changes
lebaongoc Mar 19, 2019
fb61b11
channel tests
amyesh Mar 19, 2019
9e8dba7
minor changes
lebaongoc Mar 20, 2019
d157d55
added test to channel_spec
lebaongoc Mar 20, 2019
5cdf34f
added requires to test_helper
amyesh Mar 20, 2019
4d5c5c6
added test
lebaongoc Mar 20, 2019
3fc90cd
minor changes
lebaongoc Mar 20, 2019
17a0657
added channel_spec tests
amyesh Mar 20, 2019
3ff2cb3
added tests for list users
amyesh Mar 20, 2019
0d8ecc5
added quit function
amyesh Mar 20, 2019
204e4d3
added workspace pseudocode
amyesh Mar 20, 2019
3e47d87
refactoring list methods
amyesh Mar 20, 2019
2432233
refactored list users method
amyesh Mar 20, 2019
0a02d51
select_user code passes tests
amyesh Mar 21, 2019
0433674
add select_channel tests and code
amyesh Mar 21, 2019
57eb25a
added test for get method
amyesh Mar 21, 2019
f24cbe3
refactored model classes to return class objects
amyesh Mar 21, 2019
6733aa6
added table_print functionality to Slack CLI
amyesh Mar 22, 2019
e213d76
added select channel/user and details functions
lebaongoc Mar 22, 2019
b5acc5a
debugged
lebaongoc Mar 22, 2019
c262675
added show details method and test
amyesh Mar 22, 2019
3546ff4
Merge branch 'master' of https://github.com/amyesh/slack-cli
lebaongoc Mar 22, 2019
7d9e5e9
merged changes on show details method in workspace
lebaongoc Mar 22, 2019
0eb8027
added test for workspace details and message function
lebaongoc Mar 22, 2019
5fe7cce
added rescue block to slack.rb
lebaongoc Mar 22, 2019
91b31b2
Added test on empty message, debugged
lebaongoc Mar 22, 2019
5c8a313
Delete recipient.rb
lebaongoc Mar 24, 2019
388e9ad
Delete recipient_spec.rb
lebaongoc Mar 24, 2019
1d18267
Update slack.rb
lebaongoc Mar 24, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ build-iPhoneSimulator/

# Ignore environemnt variables
.env
.DS_Store

# Ignore cassette files
/specs/cassettes/
37 changes: 37 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "dotenv"
Dotenv.load
require "httparty"

class Channel
attr_reader :channel_name, :id, :topic, :members, :member_count
BASE_URL = "https://slack.com/api/channels.list"
TOKEN = ENV["SLACK_API_TOKEN"]

def initialize(channel_name, id, topic, members)
@channel_name = channel_name
@id = id
@topic = topic
@members = members
@member_count = members.length
end

def self.get
query = {
token: TOKEN,
}
channel_info = HTTParty.get(BASE_URL, query: query)
if channel_info["ok"] == false
raise ArgumentError, "The error code is #{channel_info.code} and the reason is: #{channel_info.message}"
end
return channel_info

Choose a reason for hiding this comment

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

Instead of returning the hash about the channel, I suggest returning a new Channel instance.

Something like:

return Channel.new(channel_info["name", channel_info["id"], channel_info["topic"], channel_info["members"])

Also is this method necessary? You already have a method to get a list of Channels, so why have a method to get the details about one?

end

def self.list
channel_info = Channel.get
channel_list = channel_info["channels"]
channels = channel_list.map do |channel|
Channel.new(channel["name"], channel["id"], channel["topic"]["value"], channel["members"])
end
return channels
end
end
68 changes: 62 additions & 6 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
#!/usr/bin/env ruby
require "dotenv"
Dotenv.load
require "table_print"
require "httparty"
require "colorize"
require_relative "workspace"

Choose a reason for hiding this comment

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

You end up having some circular references here. It's important that you try to avoid having file A require file B and then file B requiring file A. That will give you a warning and it's bad form as if the interpreter wasn't well written it could lead to an infinite loop.

require_relative "user"
require_relative "channel"

def main
puts "Welcome to the Ada Slack CLI!"
work_space = Workspace.new
puts "Welcome to the Ada Slack CLI!".colorize(:color => :blue, :mode => :bold)
puts "\nPlease Choose from the following options:\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Send Message\n7. Quit".colorize(:color => :blue, :mode => :bold)

Choose a reason for hiding this comment

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

Just a note, if you put numbers in front of the options, people want to use the number to select the option.

choice = gets.chomp.upcase

# TODO project

puts "Thank you for using the Ada Slack CLI"
until choice == "QUIT"
if choice == "LIST USERS"
puts "\n"
tp work_space.users, "username", "real_name", "id"
elsif choice == "LIST CHANNELS"
puts "\n"
tp work_space.channels, "channel_name", "id", "topic"
elsif choice == "SELECT USER"
puts "Here is a list of users:".colorize(:color => :magenta)
tp work_space.users, "username", "id"
begin
puts "Please enter a username or Slack ID:".colorize(:color => :blue, :mode => :bold)
user_input = gets.chomp
user_recipient = work_space.select_user(user_input)
rescue ArgumentError
puts "The username or SlackID is invalid."
end
elsif choice == "SELECT CHANNEL"
puts "Here is a list of channels:".colorize(:color => :magenta)
tp work_space.channels, "channel_name", "id", "topic", "members"
begin
puts "Please enter channel name or Slack ID:".colorize(:color => :blue, :mode => :bold)
channel_input = gets.chomp
channel_recipient = work_space.select_channel(channel_input)
rescue ArgumentError
puts "The channel name or SlackID is invalid."
end
elsif choice == "DETAILS"
begin
tp work_space.show_details
rescue ArgumentError
puts "There is no recipient selected. Please select a user or a channel."
end
elsif choice == "SEND MESSAGE"
puts "Please enter your message below:"
user_message = gets.chomp
begin
work_space.send_message(user_message)
rescue ArgumentError
puts "The message is invalid. Please try again."
rescue NoMethodError
puts "Message failed to send. Please select a recipient and try again."
end
puts "Your message was successfully sent to the recipient!"
end
puts "\nWhat would you like to do next?\n1. List Users\n2. List Channels\n3. Select User\n4. Select Channel\n5. Details\n6. Send Message\n7. Quit".colorize(:color => :blue, :mode => :bold)
choice = gets.chomp.upcase
end
puts "Thank you for using the Ada Slack CLI!".colorize(:color => :green, :mode => :bold)
end

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
36 changes: 36 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "dotenv"
Dotenv.load
require "httparty"

class User
attr_reader :username, :real_name, :id
BASE_URL = "https://slack.com/api/users.list"
TOKEN = ENV["SLACK_API_TOKEN"]

def initialize(real_name, username, id)
@username = username
@real_name = real_name
@id = id
@user_info = User.get
end

def self.get
query = {
token: TOKEN,
}
@user_info = HTTParty.get(BASE_URL, query: query)
if @user_info["ok"] == false
raise ArgumentError, "The error code is #{@user_info.code} and the reason is: #{@user_info["error"]}"
end
return @user_info

Choose a reason for hiding this comment

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

Similar questions to the method in channel.rb

end

def self.list
user_info = User.get
user_list = user_info["members"]
users = user_list.map do |user|
User.new(user["real_name"], user["name"], user["id"])
end
return users
end
end
15 changes: 15 additions & 0 deletions lib/verification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "dotenv"
require "httparty"
Dotenv.load
require "ap"

BASE_URL = "https://slack.com/api/channels.list"
query = {
token: ENV["SLACK_API_TOKEN"],
}
channel_info = HTTParty.get(BASE_URL, query: query)
channel_list = channel_info["channels"]
channel_names = channel_list.map do |channel|
channel["name"]
end
ap channel_names
76 changes: 76 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require_relative "user"
require_relative "channel"
require_relative "slack"
require "dotenv"
require "httparty"
require "table_print"
tp.set User, :username, :id
tp.set Channel, :channel_name, :id, :members, :topic
Dotenv.load

class Workspace
attr_reader :selected, :users, :channels

def initialize
@users = User.list
@channels = Channel.list
@selected = nil
end

def select_user(user_identifier)
selected_user = nil
@users.each do |user|
if user.username == user_identifier || user.id == user_identifier
selected_user = user
end
end
if selected_user == nil
raise ArgumentError, "That user is invalid"
else
@selected = selected_user
return @selected
end
end

def select_channel(channel_identifier)
selected_channel = nil
@channels.each do |channel|
if channel.channel_name == channel_identifier || channel.id == channel_identifier
selected_channel = channel
end
end
if selected_channel == nil
raise ArgumentError, "That channel is invalid"
else
@selected = selected_channel
return @selected
end
end

def show_details
if @selected == nil
raise ArgumentError
else
return @selected
end
end

def send_message(message)
post_url = "https://slack.com/api/"
token = ENV["SLACK_API_TOKEN"]
response = HTTParty.post(
"#{post_url}/chat.postMessage",
body: {
token: token,
text: message,
channel: @selected.id,
},
headers: {"Content-Type" => "application/x-www-form-urlencoded"},
)
if response["ok"]
return response["ok"]
else
raise ArgumentError
end
end
end
39 changes: 39 additions & 0 deletions specs/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "test_helper"
require "pry"

describe "Channel class" do
describe "Channel#initialize" do
it "initializes a Channel object" do
VCR.use_cassette("Channel") do
response = Channel.new("fur_babes", "1234", "pets", ["AUAIWFS"])
expect(response).must_be_instance_of Channel
end
end
end

describe "Channel#get" do
it "returns information from the API" do
VCR.use_cassette("Channel") do
response = Channel.get
expect(response["ok"]).must_equal true
end
end
end

describe "Channel#list" do
it "includes a known channel" do
VCR.use_cassette("Channel") do
response = Channel.list
expect(response[0]).must_be_instance_of Channel
expect(response[0].member_count).must_be_instance_of Integer
end
end

it "returns correct number of channels" do
VCR.use_cassette("Channel") do
response = Channel.list.length
expect(response).must_equal 3
end
end
end
end
30 changes: 23 additions & 7 deletions specs/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
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 "webmock/minitest"
require "httparty"
require "dotenv"
Dotenv.load

require_relative "../lib/slack.rb"
require_relative "../lib/channel.rb"
require_relative "../lib/user.rb"
require_relative "../lib/workspace.rb"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

VCR.configure do |config|
config.cassette_library_dir = "specs/cassettes"
config.hook_into :webmock
end
config.default_cassette_options = {
:record => :new_episodes,
:match_requests_on => [:method, :uri, :body],
}
config.filter_sensitive_data("<SLACK_API_TOKEN>") do
ENV["SLACK_API_TOKEN"]
end
end
44 changes: 44 additions & 0 deletions specs/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative "test_helper"

describe "User class" do
describe "User#initialize" do
it "initializes a User object" do
VCR.use_cassette("User") do
response = User.new("Larry", "Truck", "1234")
expect(response).must_be_instance_of User
end
end
end

describe "User#get" do
it "returns information from the API" do
VCR.use_cassette("User") do
response = User.get
expect(response["ok"]).must_equal true
end
end
end

describe "User#list" do
it "includes a known User" do
VCR.use_cassette("User") do
response = User.list
expect(response[0]).must_be_instance_of User
end
end

it "User details are correct" do
VCR.use_cassette("User") do
response = User.list
expect(response[1].username).must_equal "amyesh08"
end
end

it "returns correct number of Users" do
VCR.use_cassette("User") do
response = User.list.length
expect(response).must_equal 3
end
end
end
end
Loading