-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves -- Janice and Emily #10
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
130b750
cf982c8
83d3e9f
09b1a4b
0d491c3
1028f18
7e51cfd
b728981
ad40cdc
3db3b79
e8936de
3e0f1a4
0d476c0
ce610fe
086ea9e
f58d9cf
f168335
d7799f6
85996b0
9a88e79
e4169c4
c48294f
81a787c
1c0620d
da46702
58805c1
7c725b6
d5f6628
56cae3a
435efba
0ca4685
f500726
6a7186c
b414036
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,27 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require_relative 'recipient' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| attr_reader :slack_id, :name, :topic, :member_count | ||
|
|
||
| def initialize(slack_id, name, topic, member_count) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| #factory method for producing individual channels from json | ||
| def self.json_parse(json) | ||
| channels = [] | ||
| json["channels"].each do|channel| | ||
| new_ch = SlackCLI::Channel.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"]) | ||
| channels << new_ch | ||
| end | ||
| return channels | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Recipient | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def send_message(msg) | ||
| post_parameters = { | ||
| token: ENV['SLACK_KEY'], | ||
| channel: self.slack_id, | ||
| text: msg | ||
| } | ||
| response = HTTParty.post('https://slack.com/api/chat.postMessage', query: post_parameters) | ||
| if response.code == 200 | ||
| puts "Message sent successfully." | ||
| else | ||
| puts "Message not sent, error code #{response.code}." | ||
| end | ||
| return response | ||
| end | ||
|
|
||
| def self.get(url, query) | ||
| response = HTTParty.get(url, query) | ||
| if response.code != 200 | ||
| puts "Something went wrong, error code #{response.code}." | ||
| end | ||
| return response | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| Dotenv.load | ||
|
|
||
| url = "https://slack.com/api/channels.list?" | ||
|
|
||
| query_parameters = { | ||
| token: ENV['SLACK_TOKEN'] | ||
| } | ||
|
|
||
| response = HTTParty.get(url, query: query_parameters) | ||
|
|
||
| response["channels"].each do |channel| | ||
| puts "This channel is called #{channel["name"]} and has #{channel["num_members"]} members." | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,80 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
| require_relative 'workspace' | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| Dotenv.load | ||
|
|
||
| def invalid_input_check(input) | ||
| if input == nil | ||
| print "\nSorry. Invalid selection." | ||
| return true | ||
| else | ||
| puts "\n#{input.name} has been selected." | ||
| end | ||
| end | ||
|
|
||
| # TODO project | ||
| def selected_is_nil(selected) | ||
| if selected == nil | ||
| puts "\nNo user or channel selected." | ||
| return true | ||
| end | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| tsu = SlackCLI::Workspace.new | ||
|
|
||
| loop do | ||
| print "\nWhat would you like to do? | ||
| - list users | ||
| - list channels | ||
| - select user | ||
| - select channel | ||
| - show details | ||
| - send message | ||
| - quit | ||
| \nYour choice: " | ||
| choice = gets.chomp.downcase | ||
| until ["list users", "list channels", "select user", "select channel", "show details", "send message", "quit"].include?(choice) | ||
| print "\nSorry. Please enter a valid choice. " | ||
| choice = gets.chomp.downcase | ||
| end | ||
|
|
||
| case choice | ||
| when "list users" | ||
| tp tsu.list_users | ||
| when "list channels" | ||
| tp tsu.list_channels | ||
| when "select user" | ||
| print "Please select a user (by Slack ID or Display Name): " | ||
| user_chosen = gets.chomp | ||
| tsu.selected = tsu.select_user(user_chosen) | ||
| invalid_input_check(tsu.selected) | ||
| when "select channel" | ||
| print "Please select a channel (by Slack ID or Name): " | ||
| channel_chosen = gets.chomp | ||
| tsu.selected = tsu.select_channel(channel_chosen) | ||
| invalid_input_check(tsu.selected) | ||
| when "show details" | ||
| if !selected_is_nil(tsu.selected) | ||
| tp tsu.print_details(tsu.selected) | ||
| end | ||
| when "send message" | ||
| if !selected_is_nil(tsu.selected) | ||
| print "What is your message? " | ||
| msg = gets.chomp | ||
| tsu.selected.send_message(msg) | ||
| end | ||
| when "quit" | ||
| puts "Thank you for using the Ada Slack CLI" | ||
| exit | ||
| end | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main | ||
|
|
||
| #main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require 'table_print' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| # https://slack.com/api/conversations.list includes private conversations | ||
| # CHANNELS_BASE_URL = "https://slack.com/api/channels.list" | ||
| # query_parameters = { | ||
| # token: ENV['SLACK_KEY'] | ||
| # } | ||
|
|
||
| # channels_response = HTTParty.get(CHANNELS_BASE_URL, query: query_parameters) | ||
| # channels_selected_info = [] | ||
| # channels_response["channels"].each do |channel| | ||
| # channels_selected_info << [channel["id"], channel["members"], channel["name"], channel["name_normalized"]] | ||
| # end | ||
| # tp channels_selected_info, "channels", "members", "name", "name normalized" | ||
|
|
||
| USERS_BASE_URL = "https://slack.com/api/users.list" | ||
| query_parameters = { | ||
| token: ENV['SLACK_KEY'] | ||
| } | ||
|
|
||
| users_response = HTTParty.get(USERS_BASE_URL, query: query_parameters) | ||
| user_array = [] | ||
| users_response["members"].each do |user| | ||
| user_array << user["profile"]["real_name"] | ||
| user_array << user["profile"]["display_name"] | ||
| end | ||
| tp user_array, "real name", "display name" | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # CHAT_POST_BASE_URL = "https://slack.com/api/chat.postMessage" | ||
| # query_parameters = { | ||
| # token: ENV['SLACK_KEY'] | ||
| # channel: | ||
| # text: | ||
| # } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require_relative 'recipient' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
|
|
||
| attr_reader :real_name, :status_text, :status_emoji | ||
| def initialize(slack_id, name, real_name, status_text, status_emoji = nil) | ||
| super(slack_id, name) | ||
| @real_name = real_name | ||
| @status_text = status_text | ||
| @status_emoji = status_emoji | ||
| end | ||
|
|
||
| #factory method for producing individual users from json | ||
| def self.json_parse(json) | ||
| users = [] | ||
| json["members"].each do |member| | ||
| new_user = SlackCLI::User.new(member["id"], member["profile"]["display_name"], member["profile"]["real_name"], member["profile"]["status_text"], member["profile"]["status_emoji"]) | ||
| users << new_user | ||
| end | ||
| return users | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require 'table_print' | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
| require_relative 'recipient' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Workspace | ||
| attr_accessor :users, :channels, :selected | ||
|
|
||
| CHANNEL_URL = 'https://slack.com/api/channels.list' | ||
| USER_URL = 'https://slack.com/api/users.list' | ||
| GET_PARAMETERS = { | ||
| token: ENV['SLACK_KEY'] | ||
| } | ||
|
|
||
| def initialize | ||
| @users = SlackCLI::User.json_parse(SlackCLI::User.get(USER_URL, query: GET_PARAMETERS)) | ||
| @channels = SlackCLI::Channel.json_parse(SlackCLI::Channel.get(CHANNEL_URL, query: GET_PARAMETERS)) | ||
|
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. What if these come back with errors? You don't give any useful error messaging so I know what needs to be fixed! |
||
| @selected = nil | ||
|
|
||
| puts "Workspace loaded! #{users.length} users and #{channels.length} channels available." | ||
| end | ||
|
|
||
| def list_users | ||
| users_hash_array = [] | ||
| @users.each do |user| | ||
| user_hash = { | ||
| "slack_id" => user.slack_id, | ||
| "display name" => user.name, | ||
| "real name" => user.real_name, | ||
| "status text" => user.status_text, | ||
| "status emoji" => user.status_emoji | ||
| } | ||
| users_hash_array << user_hash | ||
| end | ||
| return users_hash_array | ||
| end | ||
|
|
||
| def list_channels | ||
| channels_hash_array = [] | ||
| @channels.each do |channel| | ||
| channel_hash = { | ||
| "slack_id" => channel.slack_id, | ||
| "name" => channel.name, | ||
| "topic" => channel.topic, | ||
| "member_count" => channel.member_count | ||
| } | ||
| channels_hash_array << channel_hash | ||
| end | ||
| return channels_hash_array | ||
| end | ||
|
|
||
| def select_user(user_chosen) | ||
| selected_user = @users.find { |user| user.slack_id == user_chosen || user.name == user_chosen } | ||
|
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.
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. If this is a return value, be explicit about that, and don't instantiate a var that won't be used. |
||
| end | ||
|
|
||
| def select_channel(channel_chosen) | ||
| return @channels.find { |channel| channel.slack_id == channel_chosen || channel.name == channel_chosen } | ||
| end | ||
|
|
||
| def print_details(selected) | ||
| if selected.class == SlackCLI::User | ||
| user_hash = [ | ||
| { | ||
| "slack_id" => selected.slack_id, | ||
| "display name" => selected.name, | ||
| "real name" => selected.real_name, | ||
| "status text" => selected.status_text, | ||
| "status emoji" => selected.status_emoji | ||
| } | ||
| ] | ||
| return user_hash | ||
| elsif selected.class == SlackCLI::Channel | ||
| channel_hash = [ | ||
| { | ||
| "slack_id" => selected.slack_id, | ||
| "name" => selected.name, | ||
| "topic" => selected.topic, | ||
| "member_count" => selected.member_count | ||
| } | ||
| ] | ||
| return channel_hash | ||
| 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.
I'd put these urls in the respective classes, but that is just me being picky.