forked from AdaGold/slack-cli
-
Notifications
You must be signed in to change notification settings - Fork 27
Sockets - Erica and Kelly #8
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
Open
kdow
wants to merge
42
commits into
Ada-C11:master
Choose a base branch
from
kdow:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
97d7f3d
Setup environment
kdow 6e0dee7
More environment setup
norrise120 29cfeef
Add SlackCLI module and Recipient constructor test
kdow deb3634
Wrote constructor for Recepicient, fixed recipient spec
norrise120 58df5a4
Added test for self.get to Recipient spec and removed VCR from constr…
norrise120 166fb29
Add recipient self.get method
kdow 72304f9
Add recipient self.list
kdow c07db08
Change recipient to take kwargs and wrote constructor tests for User
kdow d9c962e
Wrote constructor for User class
norrise120 37e9abb
"Wrote test for self.list for User class"
norrise120 8f16cfc
Create user.list method
kdow 658fbc8
Added constructor test for channel class
norrise120 c81f7d5
Create Channel constructor
kdow 87c5bd3
Create channel.list test
kdow 7e66ef9
Created self.list for Channel class
norrise120 f436034
Add workspace constructor tests
kdow 85a195d
Wrote constructor for Workspace and updated test helper to include wo…
norrise120 ede491e
Display number of users and channels when program is launched
kdow 1a287b7
Updated Slack.rb to ask user for input and list possible options so f…
norrise120 112cd39
Add functionality to list users and channels
kdow a7138ae
Write tests for select_user and add additional options to main
kdow b5478f3
Created select_user and find_user for workspace class, passed tests
norrise120 a36f2b7
Added tests for select_channel and updated tests for select_user
norrise120 b62b045
Add select_channel and find_channel methods to workspace
kdow 4438a24
Create details method in recipient and details test for user
kdow adfeb09
Created details for user class
norrise120 a688cb8
Added tests for channel details method
norrise120 78628bd
Add details method to channel and added select user and channel funct…
kdow fd5dba1
Added tests for show_details
norrise120 f6ffe2d
Wrote method for show details and added its functionality into main
kdow 4180431
Added recipient send message test
norrise120 2ef85bd
Add send_message to recipient
kdow 9504122
Fix typos
kdow 5f8ef78
create SlackApiError class
kdow a21aff7
Added send message to Workspace class
norrise120 383341b
Update error handling for recipient send_message and add test for it
kdow c6c2086
Added error handling and test for self.get in recipient class
norrise120 0f8aa9e
Add tests for workspace send_message
kdow 1b7bbbb
Updated recipient for send message, updated slack to include send mes…
norrise120 d8e88aa
DRYed workspace.rb
norrise120 32b66ce
Format output with table_print
kdow f7a0ed8
Hide main code from SimpleCov
kdow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,3 +54,7 @@ build-iPhoneSimulator/ | |
|
|
||
| # Ignore cassette files | ||
| /specs/cassettes/ | ||
|
|
||
| # Ignore test file | ||
| test.rb | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require_relative "recipient" | ||
| require 'dotenv' | ||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(slack_id:, name:, topic:, member_count:) | ||
| super(slack_id: slack_id, name: name) | ||
|
|
||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.list | ||
| url = "https://slack.com/api/channels.list" | ||
| data = { | ||
| "token": ENV["SLACK_API_TOKEN"], | ||
| } | ||
| response = self.get(url, data) | ||
| channels = [] | ||
| response["channels"].each do |channel| | ||
| slack_id = channel["id"] | ||
| name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| member_count = channel["num_members"] | ||
| channel = self.new(slack_id: slack_id, name: name, topic: topic, member_count: member_count) | ||
| channels << channel | ||
| end | ||
| return channels | ||
| end | ||
|
|
||
| def details | ||
| return "Slack ID: #{slack_id}, Name: #{name}, Topic: #{topic}, Member count: #{member_count}" | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| require "httparty" | ||
|
|
||
| module SlackCLI | ||
| class Recipient | ||
| class SlackApiError < StandardError; end | ||
|
|
||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def self.get(url, params) | ||
| response = HTTParty.get(url, query: params) | ||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "Error when getting response, error: #{response.parsed_response["error"]}" | ||
| end | ||
| return response | ||
| end | ||
|
|
||
| def send_message(message) | ||
| url = "https://slack.com/api/chat.postMessage" | ||
| api_key = ENV["SLACK_API_TOKEN"] | ||
|
|
||
| body = { | ||
| token: api_key, | ||
| text: message, | ||
| channel: self.slack_id, | ||
| } | ||
| headers = {"Content-Type" => "application/x-www-form-urlencoded"} | ||
| response = HTTParty.post(url, body: body, headers: headers) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "Error when posting #{message} to #{self.slack_id}, error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def self.list | ||
| raise NotImplementedError, "Implement me in a child class!" | ||
| end | ||
|
|
||
| def details | ||
| raise NotImplementedError, "Implement me in a child class!" | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,90 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "workspace.rb" | ||
| require "table_print" | ||
|
|
||
| # :nocov: | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
|
|
||
| # TODO project | ||
| workspace = SlackCLI::Workspace.new | ||
| puts "There are #{workspace.channels.count} channels in this workspace" | ||
| puts "There are #{workspace.users.count} users in this workspace" | ||
|
|
||
| puts "You can: List Users, List Channels, Select User, Select Channel, Details, Send Message, or Quit." | ||
| puts "Please enter what option you would like to take. You can quit by entering 'quit'." | ||
| response = gets.chomp.downcase | ||
| while response != "quit" | ||
| if response == "list users" | ||
| list_users(workspace) | ||
| elsif response == "list channels" | ||
| list_channels(workspace) | ||
| elsif response == "select user" | ||
| select_user_by_input(workspace) | ||
| elsif response == "select channel" | ||
| select_channel_by_input(workspace) | ||
| elsif response == "details" | ||
| puts get_details(workspace) | ||
| elsif response == "send message" | ||
| send_message(workspace) | ||
| else | ||
| puts "Please make sure to select from the choices above. You can quit be entering 'quit'." | ||
| end | ||
| puts "\nPlease enter what you would like to do: \nList Users \nList Channels \nSelect User \nSelect Channel \nDetails \nSend Message \nQuit" | ||
| puts | ||
| response = gets.chomp.downcase | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| def list_users(workspace) | ||
| tp workspace.users, "slack_id", "name", "real_name" | ||
| end | ||
|
|
||
| def list_channels(workspace) | ||
| tp workspace.channels, "slack_id", "name", "topic", "member_count" | ||
| end | ||
|
|
||
| def select_user_by_input(workspace) | ||
| puts "What is the Slack ID or username? " | ||
| user_info = gets.chomp | ||
| user = workspace.select_user(user_info) | ||
| if user == nil | ||
| puts "We couldn't find that user" | ||
| else | ||
| puts "User has been selected" | ||
| end | ||
| end | ||
|
|
||
| def select_channel_by_input(workspace) | ||
| puts "What is the Slack ID or name? " | ||
| channel_info = gets.chomp | ||
| channel = workspace.select_channel(channel_info) | ||
| if channel == nil | ||
| puts "We couldn't find that channel" | ||
| else | ||
| puts "Channel has been selected" | ||
| end | ||
| end | ||
|
|
||
| def get_details(workspace) | ||
| detail_info = workspace.show_details | ||
| if detail_info == nil | ||
| puts "There is nothing selected to show details about. Please select a user or channel." | ||
| else | ||
| return detail_info | ||
| end | ||
| end | ||
|
|
||
| def send_message(workspace) | ||
| puts "What would you like to send?" | ||
| message = gets.chomp | ||
| if workspace.send_message(message) | ||
| puts "Message successfully sent" | ||
| else | ||
| puts "Please select a channel or user before sending message." | ||
| end | ||
| end | ||
| # :nocov: | ||
| main if __FILE__ == $PROGRAM_NAME |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| require_relative "recipient" | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
| attr_reader :real_name | ||
|
|
||
| def initialize(slack_id:, name:, real_name:) | ||
| super(slack_id: slack_id, name: name) | ||
|
|
||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.list | ||
| url = "https://slack.com/api/users.list" | ||
| data = { | ||
| "token": ENV["SLACK_API_TOKEN"], | ||
| } | ||
| response = self.get(url, data) | ||
| users = [] | ||
| response["members"].each do |member| | ||
| slack_id = member["id"] | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
| user = self.new(slack_id: slack_id, name: name, real_name: real_name) | ||
| users << user | ||
| end | ||
| return users | ||
| end | ||
|
|
||
| def details | ||
| return "Slack ID: #{slack_id}, Username: #{name}, Real Name: #{real_name}" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| module SlackCLI | ||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = SlackCLI::User.list | ||
| @channels = SlackCLI::Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_user(user_info) | ||
| @selected = find_user(user_info) | ||
| end | ||
|
|
||
| def select_channel(channel_info) | ||
| @selected = find_channel(channel_info) | ||
| end | ||
|
|
||
| def show_details | ||
| return @selected == nil ? nil : @selected.details | ||
| end | ||
|
|
||
| def send_message(message) | ||
| return false if @selected == nil | ||
| @selected.send_message(message) | ||
| return true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def find_user(user_info) | ||
| @users.each do |user| | ||
| if user_info == user.slack_id || user_info == user.name | ||
| return user | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| def find_channel(channel_info) | ||
| @channels.each do |channel| | ||
| if channel_info == channel.slack_id || channel_info == channel.name | ||
| return 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. This code and |
||
| end | ||
| end | ||
| return nil | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| describe SlackCLI::Channel do | ||
| describe "constructor" do | ||
| before do | ||
| @slack_id = "CH2SKTDBN" | ||
| @name = "Catssss" | ||
| @topic = "It's about cats, duh" | ||
| @member_count = "2,000,000" | ||
| @record = SlackCLI::Channel.new(slack_id: @slack_id, name: @name, topic: @topic, member_count: @member_count) | ||
| end | ||
|
|
||
| it "is an instance of Channel" do | ||
| expect(@record).must_be_kind_of SlackCLI::Channel | ||
| end | ||
|
|
||
| it "takes and saves a Slack id, name, topic, and member count" do | ||
| expect(@record.slack_id).must_equal @slack_id | ||
| expect(@record.name).must_equal @name | ||
| expect(@record.topic).must_equal @topic | ||
| expect(@record.member_count).must_equal @member_count | ||
| end | ||
| end | ||
|
|
||
| describe "self.list" do | ||
| it "returns an array of channels" do | ||
| VCR.use_cassette("channels") do | ||
| channels_array = SlackCLI::Channel.list | ||
|
|
||
| expect(channels_array).must_be_kind_of Array | ||
| expect(channels_array.first).must_be_kind_of SlackCLI::Channel | ||
| expect(channels_array.length).wont_equal 0 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "details" do | ||
| before do | ||
| @slack_id = "CH2SKTDBN" | ||
| @name = "Catssss" | ||
| @topic = "It's about cats, duh" | ||
| @member_count = "2,000,000" | ||
| @record = SlackCLI::Channel.new(slack_id: @slack_id, name: @name, topic: @topic, member_count: @member_count) | ||
| @detail = @record.details | ||
| end | ||
|
|
||
| it "returns a string" do | ||
| expect(@detail).must_be_kind_of String | ||
| end | ||
|
|
||
| it "returns the correct information" do | ||
| expect(@detail).must_equal "Slack ID: #{@slack_id}, Name: #{@name}, Topic: #{@topic}, Member count: #{@member_count}" | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Most of this data is going to be common to all subclasses of
Recipient, the only thing that's changing is the end of the URL (users.list). Could you move some or all of it toRecipient.get?