forked from AdaGold/slack-cli
-
Notifications
You must be signed in to change notification settings - Fork 27
Sockets - Carla && Shubha #2
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
shubha-rajan
wants to merge
54
commits into
Ada-C11:master
Choose a base branch
from
shubha-rajan: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
54 commits
Select commit
Hold shift + click to select a range
11cf327
Verified slack token on my machine! -Shubha
shubha-rajan d115731
Set up test environment with VCR
carlabosco 01724b4
created wrapper class for channels.list endpoint
shubha-rajan cee8153
resolving merge conflict
shubha-rajan 48168f7
added tests for user class
shubha-rajan 5baef6d
Created test file for channel
carlabosco 3f76128
Solving merge conflicts
carlabosco 8bbb51b
added driver code to main.rb and test for User#display_details
shubha-rajan a9563a5
Added tests for channel
carlabosco 0c0da23
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco 574e045
channel passes all tests in spec!
shubha-rajan 35c7ad7
added test for display_details method in channel
shubha-rajan a98abe3
added display_details method to channel
shubha-rajan 168e55b
Created User class
carlabosco 5c8cd29
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco 9d32a17
added test to workspace_spec.rb
shubha-rajan f220a3c
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco 3533a24
added display details method to User class
shubha-rajan 3fca687
Created Workspace class and initialize method
carlabosco 3221395
Fixed merge conflicts
carlabosco 73ae2d8
created instance of workspace in slack.rb
shubha-rajan e40c523
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan 0040ee8
moved creation of new workspace to inside main method
shubha-rajan ef3bba4
Added SlackApiError class
carlabosco 5d604cb
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
carlabosco f3d9dac
added tests for Workspace#display_users and Workspace#display_channels
shubha-rajan b9142ef
added display_users and display_channels methods to workspace
shubha-rajan b6512eb
fixed bugs in main driver code
shubha-rajan 4bf29db
fixed another bug where == was used instead of =
shubha-rajan 3589eb6
Added table_print format to users and channels
carlabosco e3b305e
fine, github. i will commit my deleted whitespace before i pull.
shubha-rajan 1bd94ab
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan 7b37d1e
refactored tests to use before block for VCR. removed tests for displ…
shubha-rajan 8a99248
Refactored tests
carlabosco ab10a31
Solving merge conflicts
carlabosco aed77af
Added tests for select_user and select_channel
carlabosco 34db870
wrote methods for select user and select channel that pass tests. ref…
shubha-rajan 5f36162
Refactored options menu
carlabosco 1734b3e
Added show_details method
carlabosco 33a79bf
added display_details back to channel and user classes
shubha-rajan 003e8c1
added recipient class and set up inheritance
shubha-rajan 5296aa5
took out inherited readers from user class
shubha-rajan ca18852
Added test for send_message
carlabosco cc72d9a
send message method works with names! need to fix bug with id lookup
shubha-rajan 44831fa
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan 56e8a0c
Added error for invalid recipient
carlabosco 28bb9a0
added test for sending message when selected recipient is nil
shubha-rajan 43deaef
Merge branch 'master' of https://github.com/shubha-rajan/slack-cli
shubha-rajan a0e7f7f
added tests for send_message method
shubha-rajan 4455918
created display_selected_menu method in slack.rb to DRY code
shubha-rajan 5e6222f
reverted changes in slack.rb. drying up code broke something!
shubha-rajan 04ac604
all tests passing. fixed bug where it failed to select by id!!!
shubha-rajan 5c39d8d
Added colors to menu
carlabosco 0509249
refactored driver code in lib/slack.rb
shubha-rajan 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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| BASE_URL = "https://slack.com/api/channels.list" | ||
|
|
||
| attr_reader :topic, :members | ||
|
|
||
| def initialize(slack_id, name, topic, members) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @members = members | ||
| end | ||
|
|
||
| def self.get_from_api | ||
| query_parameters = { | ||
| token: ENV["OAUTH_ACCESS_TOKEN"], | ||
| } | ||
|
|
||
| response = get(BASE_URL, query_parameters) | ||
|
|
||
| if (response.code == 200) | ||
| channels = response["channels"].map do |channel| | ||
| slack_id = channel["id"] | ||
| channel_name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| members = channel["members"].length | ||
| new(slack_id, channel_name, topic, members) | ||
| end | ||
| return channels | ||
| else | ||
| raise SlackApiError, "Error #{response.code} : #{response["message"]}" | ||
| end | ||
| end | ||
|
|
||
| def display_details | ||
| info_string = "\nSlack ID : #{slack_id}" + | ||
| "\nChannel name : #{name}" + | ||
| "\nTopic : #{topic}" + | ||
| "\nMember count: #{members}" | ||
| return info_string | ||
| 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,38 @@ | ||
| module SlackCLI | ||
| class Recipient | ||
| CHAT_ENDPOINT = "https://slack.com/api/chat.postMessage" | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name) | ||
| @name = name | ||
| @slack_id = slack_id | ||
| end | ||
|
|
||
| def self.get(url, params) | ||
| return HTTParty.get(url, query: params) | ||
| end | ||
|
|
||
| def send_message(message) | ||
| query_parameters = { | ||
| token: ENV["OAUTH_ACCESS_TOKEN"], | ||
| channel: slack_id, | ||
| text: message, | ||
| } | ||
| response = HTTParty.post( | ||
| CHAT_ENDPOINT, | ||
| body: query_parameters, | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
| ) | ||
|
|
||
| return response.code == 200 && response.parsed_response["ok"] | ||
| end | ||
|
|
||
| def display_details | ||
| raise NotImplementedError, "Implement me in a child class!" | ||
| end | ||
|
|
||
| def self.get_from_api | ||
| 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,85 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "channel" | ||
| require_relative "user" | ||
| require_relative "workspace" | ||
|
|
||
| require "httparty" | ||
| require "dotenv" | ||
| require "awesome_print" | ||
| require "colorize" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| def set_main_menu_input | ||
| puts "What would you like to do?".colorize(:yellow) | ||
| puts "\nOptions: \n1.list users \n2.list channels \n3.select user \n4.select channel \n5.quit".colorize(:blue) | ||
| input = gets.chomp.downcase | ||
| return input | ||
| end | ||
|
|
||
| def set_selected_input | ||
| puts "\nOptions: \ndetails \nsend message \nreturn to main menu".colorize(:blue) | ||
| input = gets.chomp.downcase | ||
| return input | ||
| end | ||
|
|
||
| def selected_loop(input, workspace) | ||
| until (input == "return to main menu") | ||
| case input | ||
| when "details" | ||
| puts workspace.show_details.colorize(:green) | ||
| when "send message" | ||
| puts "Type a message to send:".colorize(:blue) | ||
| message = gets.chomp | ||
| workspace.send_message(message) | ||
| when "return to main menu" | ||
| break | ||
| else | ||
| puts "That wasn't one of the options!".colorize(:red) | ||
| end | ||
| input = set_selected_input | ||
| end | ||
| end | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| users = SlackCLI::User.get_from_api | ||
| channels = SlackCLI::Channel.get_from_api | ||
| workspace = SlackCLI::Workspace.new(users: users, channels: channels) | ||
|
|
||
| puts "Welcome to the Ada Slack CLI!".colorize(:yellow) | ||
| input = set_main_menu_input | ||
|
|
||
| until (input == "quit") | ||
| case input | ||
| when "list users" | ||
| workspace.display_users | ||
| when "list channels" | ||
| workspace.display_channels | ||
| when "select user" | ||
| puts "Enter username or slack id:".colorize(:yellow) | ||
|
|
||
| name_or_id = gets.chomp | ||
| workspace.select_user(name_or_id) | ||
|
|
||
| input = set_selected_input | ||
| selected_loop(input, workspace) | ||
| when "select channel" | ||
| puts "Enter channel name or id:".colorize(:blue) | ||
| name_or_id = gets.chomp | ||
| workspace.select_channel(name_or_id) | ||
|
|
||
| input = set_selected_input | ||
|
|
||
| # TODO project | ||
| selected_loop(input, workspace) | ||
| when "quit" | ||
| break | ||
| else | ||
| puts "That wasn't one of the options!".colorize(:red) | ||
| end | ||
| input = set_main_menu_input | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| puts "Thank you for using the Ada Slack CLI.".colorize(:yellow) | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| 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,46 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "pry" | ||
| require "table_print" | ||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
| BASE_URL = "https://slack.com/api/users.list" | ||
| attr_reader :real_name | ||
|
|
||
| def initialize(name, real_name, slack_id) | ||
| super(slack_id, name) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.get_from_api | ||
| query_parameters = { | ||
| token: ENV["OAUTH_ACCESS_TOKEN"], | ||
| } | ||
|
|
||
| response = get(BASE_URL, query_parameters) | ||
|
|
||
| if (response.code == 200) | ||
| users = response["members"].map do |member| | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
| slack_id = member["id"] | ||
| new(name, real_name, slack_id) | ||
| end | ||
| return users | ||
| else | ||
| raise SlackApiError, "Error #{response.code} : #{response["message"]}" | ||
| end | ||
| end | ||
|
|
||
| def display_details | ||
| info_string = "\nSlack ID : #{slack_id}" + | ||
| "\nUsername : #{name}" + | ||
| "\nReal name : #{real_name}" | ||
| return info_string | ||
| 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,56 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "pry" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class SlackApiError < Exception; end | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize(users:, channels:) | ||
| @users = users | ||
| @channels = channels | ||
| end | ||
|
|
||
| def display_users | ||
| return tp users, :name, :real_name, :slack_id | ||
| end | ||
|
|
||
| def display_channels | ||
| return tp channels, :slack_id, :name, :members, :topic => { :width => 120 } | ||
| end | ||
|
|
||
| def select_user(name_or_id) | ||
| @selected = users.find { |user| | ||
| user.name == name_or_id | ||
| } | ||
|
|
||
| @selected ||= users.find { |user| | ||
| user.slack_id == name_or_id | ||
| } | ||
|
|
||
| return @selected | ||
| end | ||
|
|
||
| def select_channel(name_or_id) | ||
| @selected = channels.find { |channel| channel.name == name_or_id } | ||
| @selected ||= channels.find { |channel| channel.slack_id == name_or_id } | ||
| return @selected | ||
| end | ||
|
|
||
| def show_details | ||
| selected.display_details | ||
| end | ||
|
|
||
| def send_message(message) | ||
| if selected == nil | ||
| raise SlackApiError, "Invalid recipient" | ||
| else | ||
| selected.send_message(message) | ||
| end | ||
| 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,46 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| describe "Channel" do | ||
| let (:channel_data) { | ||
| { | ||
| id: "CH2P2QWMR", | ||
| channel_name: "everyone", | ||
| topic: "Company-wide announcements and work-based matters", | ||
| members: 2, | ||
| } | ||
| } | ||
| it "instantiate a channel object" do | ||
| new_channel = SlackCLI::Channel.new( | ||
| channel_data[:id], | ||
| channel_data[:channel_name], | ||
| channel_data[:topic], | ||
| channel_data[:members] | ||
| ) | ||
|
|
||
| expect(new_channel).must_be_instance_of SlackCLI::Channel | ||
| end | ||
|
|
||
| it "has working reader methods" do | ||
| new_channel = SlackCLI::Channel.new( | ||
| channel_data[:id], | ||
| channel_data[:channel_name], | ||
| channel_data[:topic], | ||
| channel_data[:members] | ||
| ) | ||
|
|
||
| expect(new_channel.slack_id).must_equal channel_data[:id] | ||
| expect(new_channel.name).must_equal channel_data[:channel_name] | ||
| expect(new_channel.topic).must_equal channel_data[:topic] | ||
| expect(new_channel.members).must_equal channel_data[:members] | ||
| end | ||
|
|
||
| it "loads array of channels from Slack's API" do | ||
| VCR.use_cassette("list_channels") do | ||
| channels = SlackCLI::Channel.get_from_api | ||
| expect(channels).must_be_instance_of Array | ||
| channels.each do |channel| | ||
| expect(channel).must_be_instance_of SlackCLI::Channel | ||
| end | ||
| 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,15 +1,30 @@ | ||
| require 'simplecov' | ||
| SimpleCov.start | ||
|
|
||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require 'minitest/reporters' | ||
| require 'minitest/skip_dsl' | ||
| require 'vcr' | ||
| require "simplecov" | ||
| SimpleCov.start do | ||
| add_filter %r{^/specs?/} | ||
| add_filter "slack.rb" | ||
| end | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
| require "vcr" | ||
| require "webmock/minitest" | ||
| require "dotenv" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| require_relative "../lib/slack.rb" | ||
|
|
||
| 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("<OAUTH_ACCESS_TOKEN>") do | ||
| ENV["OAUTH_ACCESS_TOKEN"] | ||
| 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.
Good use of
||=here.