-
Notifications
You must be signed in to change notification settings - Fork 27
Ports - Margaret and Ari #5
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
base: master
Are you sure you want to change the base?
Changes from all commits
0690957
de7b750
3268e0c
92c9449
e27145f
8c63b50
4468323
e646028
806f6cd
a588b63
38cd541
53c7eac
dd1a21c
7d2956a
948b30b
6c9e142
e5099a6
8821a24
33211e7
ec223d6
a7cec70
6470849
6934c0c
63a8f96
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| require "pry" | ||
| require "httparty" | ||
| require_relative "recipient" | ||
|
|
||
| module SlackAPI | ||
| class SlackError < StandardError; end | ||
|
|
||
| class Channel < Recipient | ||
| BASE_URL = "https://slack.com/api/" | ||
|
|
||
| def self.list_channels | ||
| response = Recipient.get("channels.list") | ||
|
|
||
| channel_list = [] | ||
|
|
||
| if response["ok"] != true | ||
| raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}" | ||
| else | ||
| response["channels"].each do |channel| | ||
| channel_list << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } | ||
| end | ||
| end | ||
| return channel_list | ||
| end | ||
| end #end of class | ||
| end #end of module | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| module SlackAPI | ||
| class Recipient | ||
| BASE_URL = "https://slack.com/api/" | ||
|
|
||
| def self.get(sub_url) | ||
| query = { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| } | ||
|
|
||
| response = HTTParty.get("#{BASE_URL}#{sub_url}", query: query) | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def send_msg(recipient, text) | ||
| response = HTTParty.post("#{BASE_URL}chat.postMessage", | ||
| headers: {"Content-Type" => "application/x-www-form-urlencoded"}, | ||
| body: { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| channel: recipient, | ||
| text: text, | ||
| }) | ||
|
|
||
| if response["ok"] == false | ||
| raise SlackAPI::SlackError, "Error when posting #{text} to #{recipient}, error #{response["error"]}" | ||
| else | ||
| return true | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,74 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "user" | ||
| require_relative "channel" | ||
| require_relative "workspace" | ||
| require "httparty" | ||
| require "table_print" | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| puts "The workspace has #{SlackAPI::Channel.list_channels.length} channels and #{SlackAPI::User.list_users.length} users" | ||
| ask_again = "Select an option: \n1. list users \n2. list channels \n3. select user \n4. select channel \n5. details (select user or channel first)\n6. send message (select user or channel first) \n7. quit" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
|
|
||
| # TODO project | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| until response == "7" || response == "quit" | ||
|
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. Oh thank god you respond to the numbers! |
||
| if response == "list users" || response == "1" | ||
|
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 set of if-else blocks, really looks like it could be broken up into methods. |
||
| tp SlackAPI::User.list_users, "name", "real name", "slack id" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| elsif response == "list channels" || response == "2" | ||
| tp SlackAPI::Channel.list_channels, "name", "topic", "member count", "id" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| elsif response == "select user" || response == "3" | ||
| select_response = "select user" | ||
| puts "What is the user id or Slack name?" | ||
| identifier = gets.chomp.downcase | ||
| user = SlackAPI::Workspace.new | ||
| selected_recipient = user.select_user(identifier) | ||
| if selected_recipient == "" | ||
| puts "There is no user with that identifier" | ||
| end | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| elsif response == "select channel" || response == "4" | ||
| select_response = "select channel" | ||
| puts "What is the user id or Slack name?" | ||
| identifier = gets.chomp.downcase | ||
| channel = SlackAPI::Workspace.new | ||
| selected_recipient = channel.select_channel(identifier) | ||
| if selected_recipient == "" | ||
| puts "There is no channel with that identifier" | ||
| end | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| elsif response == "details" || response == "5" | ||
| if select_response == "select user" | ||
| details = user.see_user_details(selected_recipient) | ||
| tp details, "name", "real name", "slack id" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| elsif select_response == "select channel" | ||
| details = channel.see_channel_details(selected_recipient) | ||
| tp details, "name", "topic", "member count", "id" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| else | ||
| puts "Select a user or channel first" | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| end | ||
| elsif response == "send message" || response == "6" | ||
| puts "What would you like to say?" | ||
| text = gets.chomp.downcase | ||
| msg = SlackAPI::Recipient.new.send_msg(selected_recipient, text) | ||
| puts ask_again | ||
| response = gets.chomp.downcase | ||
| end | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| require "pry" | ||
| require "httparty" | ||
| require_relative "recipient" | ||
|
|
||
| module SlackAPI | ||
| class SlackError < StandardError; end | ||
|
|
||
| class User < Recipient | ||
| BASE_URL = "https://slack.com/api/" | ||
|
|
||
| def self.list_users | ||
| response = Recipient.get("users.list") | ||
|
|
||
| user_list = [] | ||
|
|
||
| if response["ok"] | ||
| response["members"].each do |member| | ||
| user_list << {"name" => member["name"], "real name" => member["real_name"], "slack id" => member["id"]} | ||
|
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 method would make more sense if it returned a list of |
||
| end | ||
| else | ||
| raise SlackAPI::SlackError, "There was an error. The error message is #{response["error"]}" | ||
| end | ||
| return user_list | ||
| end | ||
| end # end of class | ||
| end # end of module | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| require "httparty" | ||
| require "pry" | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| BASE_URL = "https://slack.com/api/channels.list" | ||
| query = { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| } | ||
|
|
||
| response = HTTParty.get(BASE_URL, query: query) | ||
|
|
||
| response["channels"].each do |channel| | ||
| print channel["name"] | ||
| end | ||
| # code snips to save: | ||
| # puts channel["topic"]["value"] # gets topic | ||
| # puts channel["members"].length # gets # of members | ||
| # puts channel["id"] # gets slack id for channel |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| require_relative "recipient" | ||
| require "httparty" | ||
|
|
||
| module SlackAPI | ||
| class Workspace | ||
| def select_channel(identifier) | ||
| response = Recipient.get("channels.list") | ||
|
|
||
| selected_channel = "" | ||
| response["channels"].each do |channel| | ||
| if channel["id"] == identifier | ||
| selected_channel = identifier | ||
| elsif channel["name"] == identifier | ||
| selected_channel = identifier | ||
| end | ||
| end | ||
| if selected_channel == "" | ||
| raise SlackAPI::SlackError, "That is not a vaild channel" | ||
| end | ||
| return selected_channel | ||
| end | ||
|
|
||
| def see_channel_details(identifier) | ||
| response = Recipient.get("channels.list") | ||
|
|
||
| channel_details = [] | ||
|
|
||
| response["channels"].each do |channel| | ||
| if channel["id"] == identifier || channel["name"] == identifier | ||
| channel_details << { "name" => channel["name"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length, "id" => channel["id"] } | ||
| return channel_details | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def select_user(identifier) | ||
| response = Recipient.get("users.list") | ||
|
|
||
| selected_user = "" | ||
|
|
||
| response["members"].each do |member| | ||
|
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 could also use the |
||
| if member["id"] == identifier | ||
| selected_user = member["id"] | ||
| elsif member["name"] == identifier | ||
| selected_user = member["id"] | ||
| end | ||
| end | ||
| if selected_user == "" | ||
| raise SlackAPI::SlackError, "That is not a valid user" | ||
| end | ||
| return selected_user | ||
| end | ||
|
|
||
| def see_user_details(identifier) | ||
| response = Recipient.get("users.list") | ||
|
|
||
| user_details = [] | ||
|
|
||
| response["members"].each do |member| | ||
| if member["id"] == identifier || member["name"] == identifier | ||
| user_details << | ||
| { "name" => member["name"], | ||
| "real name" => member["real_name"], | ||
| "slack id" => member["id"] } | ||
|
|
||
| return user_details | ||
| end | ||
| end | ||
| end | ||
| end # end of class | ||
| end # end of module | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| describe "self.list" do | ||
| it "list all channels" do | ||
| VCR.use_cassette("list_channels") do | ||
| response = SlackAPI::Channel.list_channels | ||
|
|
||
| expect(response).wont_be_nil | ||
| expect(response[0]).must_equal "name" => "general", "topic" => "Company-wide announcements", "member count" => 2, "id" => "CH2SKTKPC" | ||
| expect(response.length).must_equal 3 | ||
| end | ||
| end | ||
| it "will raise an error if given an invalid key" do | ||
|
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. 👍 |
||
| real_token = ENV["SLACK_API_TOKEN"] | ||
| ENV["SLACK_API_TOKEN"] = "NOT_REAL_TOKEN" | ||
|
|
||
| VCR.use_cassette("list_channels_2") do | ||
| error = expect { | ||
| SlackAPI::Channel.list_channels | ||
| }.must_raise SlackAPI::SlackError | ||
| expect(error.message).must_equal "There was an error. The error message is invalid_auth" | ||
| end | ||
|
|
||
| ENV["SLACK_API_TOKEN"] = real_token | ||
| end | ||
| end | ||
|
|
||
| describe "send_msg" do | ||
| it "sends a message to selected channel" do | ||
| channel = SlackAPI::Channel.new() | ||
| VCR.use_cassette("send_msg") do | ||
| msg = channel.send_msg("CH2SKTKPC", "This is a test message!") | ||
| expect(msg).must_equal true | ||
| end | ||
| end | ||
|
|
||
| it "generates an error if given an invalid channel" do | ||
| VCR.use_cassette("send_msg") do | ||
| expect { | ||
| SlackAPI::Channel.new.send_msg("bogus", "Test message") | ||
| }.must_raise SlackAPI::SlackError | ||
| end | ||
| end | ||
|
|
||
| it "will raise an error if given an empty message" do | ||
| VCR.use_cassette("send_msg") do | ||
| error = expect { | ||
| SlackAPI::Channel.new.send_msg("general", | ||
| "") | ||
| }.must_raise SlackAPI::SlackError | ||
|
|
||
| # expect(error.message).must_equal "Error when posting to ports-api-testing, error: no_text" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,33 @@ | ||
| require 'simplecov' | ||
| require "simplecov" | ||
| SimpleCov.start | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require 'minitest/reporters' | ||
| require 'minitest/skip_dsl' | ||
| require 'vcr' | ||
| require "httparty" | ||
| require "minitest" | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
| require "minitest/skip_dsl" | ||
| require "webmock/minitest" | ||
| require "vcr" | ||
|
|
||
| require_relative "../lib/channel.rb" | ||
| require_relative "../lib/slack.rb" | ||
| require_relative "../lib/user.rb" | ||
| require_relative "../lib/recipient.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, # 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 |
Uh oh!
There was an error while loading. Please reload this page.
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.
You have missed an opportunity to create an array of
Channelinstances instead of an array of hashes.