-
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 - Amy & Ngoc #11
base: master
Are you sure you want to change the base?
Changes from all commits
c7dfaa2
37abe63
a805089
2c55f9a
988df57
50fc8b5
4dbbb71
ef755a5
e51b54a
f4e8588
4d12414
f72471f
fb61b11
9e8dba7
d157d55
5cdf34f
4d5c5c6
3fc90cd
17a0657
3ff2cb3
0d8ecc5
204e4d3
3e47d87
2432233
0a02d51
0433674
57eb25a
f24cbe3
6733aa6
e213d76
b5acc5a
c262675
3546ff4
7d9e5e9
0eb8027
5fe7cce
91b31b2
5c8a313
388e9ad
1d18267
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 |
---|---|---|
|
@@ -51,6 +51,7 @@ build-iPhoneSimulator/ | |
|
||
# Ignore environemnt variables | ||
.env | ||
.DS_Store | ||
|
||
# Ignore cassette files | ||
/specs/cassettes/ |
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 | ||
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 |
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" | ||
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 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) | ||
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. 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 |
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 | ||
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. Similar questions to the method in |
||
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 |
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 |
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 |
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 |
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 |
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 |
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.
Instead of returning the hash about the channel, I suggest returning a new Channel instance.
Something like:
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?