-
Notifications
You must be signed in to change notification settings - Fork 31
Earth - Ana Karina - Denise - SlackCli #24
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
3f460c9
c7bca70
2d1ac1a
7eb4e48
4db308a
245c176
bdb6854
3235880
c797ca6
442250b
4b9abe1
9840519
d538c37
423b8c0
ad62bdc
f3d33b6
9d7c0ef
b558342
e847294
1308b3e
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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ | |
|
|
||
| # Ignore environemnt variables | ||
| .env | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| require_relative 'recipient.rb' | ||
| require 'table_print' | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| attr_reader :channel_name, :slack_id, :member_count, :topic | ||
|
|
||
| def initialize(channel_name, slack_id, member_count, topic) | ||
| super(slack_id) | ||
|
|
||
| @channel_name = channel_name | ||
| @member_count = member_count | ||
| @topic = topic | ||
| end | ||
|
|
||
| def details | ||
| return "Channel name: #{@channel_name}\nSlack ID: #{@slack_id}\nTopic: #{@topic}\nMember count: #{@member_count}" | ||
| end | ||
|
|
||
| # Will retrieve the data from the API using the URL | ||
| def self.list_all | ||
| response = super('conversations.list') | ||
| channels = [] | ||
| response["channels"].each do |channel| | ||
| channels << Channel.new(channel["name"], channel["id"], channel["num_members"].to_i, channel["topic"]["value"]) | ||
| end | ||
| return channels | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| TOKEN = ENV['SLACK_TOKEN'] | ||
| URL = "https://slack.com/api/" | ||
|
|
||
| module SlackCLI | ||
| class Recipient | ||
|
|
||
| attr_reader :slack_id | ||
|
|
||
| def initialize(slack_id) | ||
| @slack_id = slack_id | ||
| end | ||
|
|
||
| def self.list_all(list) | ||
| url = "#{URL}"<<"#{list}" | ||
| query_params = {token: TOKEN} | ||
| response = HTTParty.get(url, query: query_params) | ||
| return response | ||
| end | ||
|
|
||
| def send_message(message) | ||
| url = "#{URL}chat.postMessage" | ||
|
|
||
| response = HTTParty.post(url, | ||
| headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, | ||
| body: { | ||
| token: TOKEN, | ||
| channel: @slack_id, | ||
| text: message | ||
| }) | ||
| unless response.code == 200 | ||
| raise ArgumentError.new("Request Error") | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,80 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'table_print' | ||
| require_relative 'workspace' | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| workspace = Workspace.new | ||
| workspace = SlackCLI::Workspace.new | ||
|
|
||
| # TODO project | ||
| options_list | ||
| choice = get_user_choice | ||
| execute_choice(workspace, choice) | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| def options_list | ||
| print "Choose from the following:\n" | ||
| puts "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit" | ||
| end | ||
|
|
||
| def get_user_choice | ||
| user_choice = gets.chomp | ||
| options = ["list users", "list channels", "quit", "select user", "select channel", "details", "send message"] | ||
|
|
||
| until options.include?(user_choice) | ||
| puts "Invalid option. Please type: list users, list channels, select user, select channel, details, send message or quit" | ||
| user_choice = gets.chomp | ||
| end | ||
|
|
||
| return user_choice | ||
| end | ||
|
|
||
| def execute_choice(workspace, choice) | ||
| given_data = nil | ||
| until choice == "quit" | ||
| if choice == "list users" | ||
| tp workspace.users.list, "username", "slack_id", "real_name" | ||
|
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. Your code breaks here, because |
||
| puts "\n" | ||
| elsif choice == "list channels" | ||
| tp workspace.channels_list, "channel_name", "slack_id", "topic", "member_count" | ||
| elsif choice == "select user" | ||
| print "Please enter username or Slack ID: " | ||
| user_info = gets.chomp | ||
| given_data = workspace.select_user(user_info) | ||
| if given_data.nil? | ||
| puts "User not found" | ||
| end | ||
| elsif choice == "select channel" | ||
| print "Please select channel name or Slack ID: " | ||
| channel_info = gets.chomp | ||
| given_data = workspace.select_channel(channel_info) | ||
| if given_data.nil? | ||
| puts "Channel not found." | ||
| end | ||
| elsif choice == "details" | ||
| if given_data != nil | ||
| puts given_data.details | ||
| elsif given_data != nil | ||
| puts given_data.details | ||
| else | ||
| puts "There is no user or channel that matches your search" | ||
| end | ||
| elsif choice == "send message" | ||
| if given_data.nil? | ||
| puts "Cannot send message. Please choose a recipient first." | ||
| else | ||
| puts "Please type your message:" | ||
| user_msg = gets.chomp | ||
| given_data.send_message(user_msg) | ||
| puts "Success!" | ||
| end | ||
| end | ||
|
|
||
| puts "*****\n\n" | ||
| options_list | ||
| choice = get_user_choice | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require_relative 'recipient' | ||
| require 'httparty' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
| attr_reader :username, :slack_id, :real_name | ||
|
|
||
| def initialize(username, slack_id, real_name) | ||
| super (slack_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. Just a very small nitpick: It'll be clearer that this is a |
||
|
|
||
| @username = username | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def details | ||
| return "username: #{@username}\nSlack ID: #{@slack_id}\nreal name: #{@real_name}\n" | ||
| end | ||
|
|
||
| #will retrieve users data from the API using URL | ||
| def self.list_all | ||
| response = super('users.list') | ||
| users = [] | ||
| response["members"].each do |user| | ||
| users << User.new(user["name"], user["id"], user["real_name"]) | ||
| end | ||
| return users | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| require_relative 'channel.rb' | ||
| require_relative 'user.rb' | ||
|
|
||
| module SlackCLI | ||
| class Workspace | ||
|
|
||
| attr_reader :users, :channels | ||
|
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. I don't think It might be worth conforming to either attr_reader OR the |
||
|
|
||
| def initialize | ||
| @users = User.list_all | ||
| @channels= Channel.list_all | ||
| end | ||
|
|
||
| def users_list | ||
| return @users | ||
| end | ||
|
Comment on lines
+14
to
+16
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. The methods |
||
|
|
||
| def channels_list | ||
| return @channels | ||
| end | ||
|
|
||
| def select_user(user_info) | ||
| name_search = @users.find{|user| user.username == user_info} | ||
| id_search = @users.find{|user| user.slack_id == user_info} | ||
|
|
||
| if name_search.nil? | ||
| @selected_user = id_search | ||
| elsif id_search.nil? | ||
| @selected_user = name_search | ||
| else | ||
| puts "Invalid User name or 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.
Let me know if you have questions on this! |
||
| end | ||
| return @selected_user | ||
| end | ||
|
|
||
| def select_channel(channel_info) | ||
| name_search = @channels.find{|channel| channel.channel_name == channel_info} | ||
| id_search = @channels.find{|channel| channel.slack_id == channel_info} | ||
|
|
||
| if name_search.nil? | ||
| @selected_channel = id_search | ||
| elsif id_search.nil? | ||
| @selected_channel = name_search | ||
| else | ||
| puts "Invalid Channel name or 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. Same comment as above for |
||
| end | ||
| return @selected_channel | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||
| require_relative 'test_helper.rb' | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| describe "class Channel" do | ||||||||||||||||||||||||||
| before do | ||||||||||||||||||||||||||
| @channel = SlackCLI::Channel.new("seattle-stuff", "U786YWDVC3D", 71, "coffee shops") | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it "creates an instance of channel" do | ||||||||||||||||||||||||||
| expect(@channel).must_be_kind_of SlackCLI::Channel | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it "returns channel details correctly" do | ||||||||||||||||||||||||||
| expect(@channel.details).must_be_kind_of String | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it "returns a list of channels" do | ||||||||||||||||||||||||||
| VCR.use_cassette "channels_list" do | ||||||||||||||||||||||||||
| response = SlackCLI::Channel.list_all | ||||||||||||||||||||||||||
| expect(response).must_be_kind_of Array | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
Comment on lines
+16
to
+21
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. It's true that a request-response cycle happens during the However, this line in this test reads a little weirdly. What gets returned from the It might read better overall to see this test as...
Suggested change
Especially when you read it with your well-written |
||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require_relative 'test_helper.rb' | ||
|
|
||
| describe "Recipient class" do | ||
|
|
||
| it "stores id correctly" do | ||
| recipient_id = "GTB29AAZ" | ||
| recipient = SlackCLI::Recipient.new(recipient_id) | ||
| expect(recipient.slack_id).must_equal recipient_id | ||
| end | ||
|
|
||
| it "retrieves data from chosen list" do | ||
| VCR.use_cassette "list_all" do | ||
| response = SlackCLI::Recipient.list_all("conversations.list") | ||
| expect(response.code).must_equal 200 | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| require_relative 'test_helper' | ||
|
|
||
| describe "User Class" do | ||
| before do | ||
| @users = SlackCLI::User.new("ruby", "PM15QINBXFZ", "ruby mine") | ||
| end | ||
|
|
||
| it "Creates an instance of Users" do | ||
| expect(@users).must_be_kind_of SlackCLI::User | ||
| end | ||
|
|
||
| it "Return user details correctly" do | ||
| expect(@users.details).must_be_kind_of String | ||
| expect(@users.username).must_equal "ruby" | ||
| expect(@users.slack_id).must_equal "PM15QINBXFZ" | ||
| end | ||
|
|
||
| it "Returns a list of users" do | ||
| VCR.use_cassette "user_list" do | ||
| result = SlackCLI::User.list_all | ||
| expect(result).must_be_kind_of Array | ||
| 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.
This syntax works, as we can shovel strings into other strings.
Just for fun, I want to provide these other syntaxes you could use, in case you wanted something a little more straightforward:
url = "#{URL}#{list}"url = URL + list