-
Notifications
You must be signed in to change notification settings - Fork 31
Fire - Jing & Ren #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
base: master
Are you sure you want to change the base?
Changes from all commits
cae9773
0c8ba33
efd0d88
a530980
12d17ce
cfbfe8a
3680318
31009c8
84a2b1b
08dc9d1
87d3f06
2928c71
f003578
36959ae
c6a8fd1
286d659
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,5 @@ | |
|
|
||
| # Ignore environemnt variables | ||
| .env | ||
| lib/.env | ||
| coverage | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| class Channel < Recipient | ||
| attr_reader :name, :topic, :member_count, :slack_id | ||
|
|
||
| def initialize(slack_id, name, topic, member_count) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
|
|
||
| raise ArgumentError, "slack ID, name, topic, and member count are all required." unless topic || member_count | ||
| end | ||
|
|
||
| def self.list_all | ||
| response = self.get("https://slack.com/api/conversations.list") | ||
|
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. Nice use of a helper method in the parent class. |
||
| channels = [] | ||
| response["channels"].each do |channel| | ||
| channels << Channel.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"]) | ||
| end | ||
| return channels | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| require 'httparty' | ||
| #require_relative 'workspace' | ||
| require 'dotenv' | ||
| require 'table_print' | ||
|
|
||
| class Recipient | ||
| Dotenv.load ##inside or outside? | ||
| BASE_URL = "https://slack.com/api/" | ||
| SLACK_TOKEN = ENV['SLACK_TOKEN'] | ||
|
|
||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name)#should we get rid of name???? | ||
| @slack_id = slack_id | ||
| @name = name | ||
|
|
||
| raise ArgumentError, "slack ID and name are required." unless slack_id || name | ||
| end | ||
|
|
||
| def self.get(url) | ||
| return HTTParty.get(url, query: {token: SLACK_TOKEN}) | ||
| end | ||
|
Comment on lines
+20
to
+22
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. 👍 |
||
|
|
||
| def self.list_all | ||
| raise NotImplementedError, 'Implement me in a child class!' | ||
| end | ||
|
|
||
| def self.send_message(slack_id, message) | ||
| raise Exception.new("message is required") if message == "" | ||
| url = "https://slack.com/api/chat.postMessage" | ||
| response = HTTParty.post( | ||
| url, | ||
| body: { | ||
| token: SLACK_TOKEN, | ||
| text: message, | ||
| channel: slack_id | ||
| }, | ||
| headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } | ||
| ) | ||
| if response.code != 200 || response.parsed_response["ok"] == false | ||
| raise Exception.new("Http request code #{response.code} error #{response.parsed_response["error"]}") | ||
| end | ||
| return response | ||
| end | ||
|
|
||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,103 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'httparty' | ||
| require_relative 'workspace' | ||
| require 'dotenv' | ||
| require 'table_print' | ||
|
|
||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| workspace = Workspace.new | ||
| @workspace = Workspace.new | ||
|
|
||
| loop do | ||
| puts "(~ ̄▽ ̄)~ What would you like to do with CLI? ~( ̄▽ ̄~) \n1. list users \n2. list channels \n3. select user \n4. select channel \n5. detail\n (about the selected user or channel) \n6. send message \n7. quit" | ||
| input = gets.chomp | ||
|
|
||
| # TODO project | ||
| case input | ||
| when "list users", "1" | ||
| tp @workspace.users | ||
| puts "\n\n" | ||
| when "list channels", "2" | ||
| tp @workspace.channels | ||
| puts "\n\n" | ||
| when "select user", "3" | ||
| select_a_user | ||
| when "select channel", "4" | ||
| select_a_channel | ||
| when "detail", "5" | ||
| if @workspace.selected == nil | ||
| puts "ヽ(ˋДˊ)ノ" | ||
| puts "Please select a user or channel first!" | ||
| puts "\n\n" | ||
| else | ||
| tp @workspace.selected | ||
| puts "\n\n" | ||
| end | ||
| when "send message", "6" | ||
| write_message | ||
| puts "\n\n" | ||
| when "quit", "7" | ||
| puts "乀(ˉεˉ乀) bye~" | ||
| break | ||
| else | ||
| puts "ヽ(ˋДˊ)ノ" | ||
| puts "Please enter one of the menu items or their corresponding number values." | ||
| puts "\n\n" | ||
| end | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| def write_message | ||
| if @workspace.selected == nil | ||
| puts "ヽ(ˋДˊ)ノ \nPlease select a user or channel first!" | ||
| else | ||
| puts "What is your message?" | ||
| message = gets.chomp | ||
| if message == "" | ||
| puts "ヽ(ˋДˊ)ノ \nNo message was inputted." | ||
| return false | ||
| end | ||
| puts "ヽ(ˋ▽ˊ)ノ \nOk, we sent off your message!" if @workspace.send_message(message).parsed_response["ok"] == true | ||
| end | ||
| end | ||
|
|
||
| def select_a_user | ||
| loop do | ||
| puts "What's the user's slack id or username? enter 7 or quit to exit." | ||
| id_or_name = gets.chomp | ||
| if id_or_name == "7" || id_or_name == "quit" | ||
| break | ||
| end | ||
| begin | ||
| @workspace.select_user(id_or_name) | ||
| rescue Exception | ||
| puts "ヽ(ˋДˊ)ノ \nPlease enter a valid username or id.\n\n" | ||
| else | ||
| puts "ヽ(ˋ▽ˊ)ノ \nOk, #{id_or_name} is selected.\n\n" | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def select_a_channel | ||
| loop do | ||
| puts "What's the channel's slack id or username? enter 7 or quit to exit." | ||
| id_or_name = gets.chomp | ||
| if id_or_name == "7" || id_or_name == "quit" | ||
| break | ||
| end | ||
| begin | ||
| @workspace.select_channel(id_or_name) | ||
| rescue Exception | ||
| puts "ヽ(ˋДˊ)ノ \nPlease enter a valid channel name or id.\n\n" | ||
| else | ||
| puts "ヽ(ˋ▽ˊ)ノ \nOk, #{id_or_name} is selected.\n\n" | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
| main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| class User < Recipient | ||
|
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. 👍 |
||
| attr_reader :name, :real_name, :slack_id | ||
|
|
||
| def initialize(slack_id, name, real_name) | ||
| super(slack_id, name) #super(name)????? | ||
| @real_name = real_name | ||
|
|
||
| raise ArgumentError, "real name is required." unless real_name | ||
| end | ||
|
|
||
| def self.list_all | ||
| response = self.get("https://slack.com/api/users.list") | ||
| users = [] | ||
| response["members"].each do |member| | ||
| users << User.new(member["id"], member["name"], member["real_name"] || member["profile"]["real_name"]) | ||
| end | ||
| return users | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require_relative 'recipient' | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
| require 'table_print' | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels | ||
| attr_accessor :selected | ||
|
|
||
| def initialize(users: [], channels: [], selected: nil) | ||
| @users = User.list_all | ||
| @channels = Channel.list_all | ||
| @selected = selected | ||
|
|
||
| end | ||
|
|
||
| def select_user(name_or_id) | ||
| @selected = @users.find {|user| user.slack_id == name_or_id} | ||
| @selected = @users.find {|user| user.name == name_or_id} unless @selected | ||
| raise Exception.new("no user found.") unless @selected | ||
| return @selected | ||
| end | ||
|
|
||
| def select_channel(name_or_id) | ||
| @selected = @channels.find {|channel| channel.slack_id == name_or_id} | ||
| @selected = @channels.find {|channel| channel.name == name_or_id} unless @selected | ||
| raise Exception.new("no channel found.") unless @selected | ||
| return @selected | ||
| end | ||
|
|
||
| def send_message(message) | ||
| raise Exception.new("nothing selected, please select a user/channel to send to first!") if @selected == nil | ||
| channel = @selected.slack_id | ||
| Recipient.send_message(channel, message) | ||
| # return true | ||
| end | ||
| end | ||
|
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
👍