-
Notifications
You must be signed in to change notification settings - Fork 27
Sockets-Grace & Bita #23
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
4b2d502
f502ab6
8c83fc0
3f9786f
ff7f7a2
145d312
e9f005c
af7056c
7bb84a6
6569872
604fd81
24c7fd2
b8d8051
9258eb5
4e04cc4
2742dd9
390fedb
093b250
fe50b30
8f059c8
1a3732a
a72b53a
32350f2
d6de455
21ee33b
eef74a4
5cc917a
d54ba41
8b4e25b
0304ab5
855498a
47933a2
4dd4e61
55e891f
2cc177f
017c3b1
18a0e8a
a420ef7
04f1abd
a2c704d
1d870f8
9e05aaf
9cb2224
b454049
282dd5c
d35ea14
478bc3b
2281f75
577f10c
501080d
a78220e
f2bd699
0db833f
cfce3ba
0151a13
791d92a
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 |
|---|---|---|
|
|
@@ -53,4 +53,4 @@ build-iPhoneSimulator/ | |
| .env | ||
|
|
||
| # Ignore cassette files | ||
| /specs/cassettes/ | ||
| /specs/cassette | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require_relative "recipient" | ||
|
|
||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(slack_id, name, topic, member_count) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.list | ||
| raw_data = self.get("channel") | ||
|
|
||
| unless raw_data.code == 200 | ||
| raise SlackApiError, "Improper request: #{raw_data.message}" | ||
| end | ||
| channel_list = [] | ||
| channels = raw_data["channels"] | ||
|
|
||
| channels.each do |channel| | ||
| slack_id = channel["id"] | ||
| name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| member_count = channel["members"].count | ||
|
|
||
| new_channel = Channel.new(slack_id, name, topic, member_count) | ||
| channel_list << new_channel | ||
| end | ||
| return channel_list | ||
| end | ||
|
|
||
| def details | ||
| return "#{name} #{topic} member count: #{member_count} slack id: #{slack_id}" | ||
| end | ||
| end | ||
|
|
||
| # puts Channel.list |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| require "dotenv" | ||
| require "httparty" | ||
| Dotenv.load | ||
|
|
||
| class Recipient | ||
| class SlackApiError < StandardError; end | ||
|
|
||
| attr_accessor :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| CHANNEL_URL = "https://slack.com/api/channels.list" | ||
| USER_URL = "https://slack.com/api/users.list" | ||
| POST_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| def self.get(type) | ||
| params = { | ||
| "token" => ENV["SLACK_TOKEN"], | ||
| } | ||
| if type == "user" | ||
| url = USER_URL | ||
| elsif type == "channel" | ||
| url = CHANNEL_URL | ||
| end | ||
|
|
||
| response = HTTParty.get(url, query: params) | ||
| return response | ||
| end | ||
|
|
||
| def send_msg(message) | ||
| params = { | ||
| "token" => ENV["SLACK_TOKEN"], | ||
| "channel" => @slack_id, | ||
| "text" => message, | ||
| "as_user" => true, | ||
| } | ||
|
|
||
| response = HTTParty.post( | ||
| POST_URL, | ||
| body: params, | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
| ) | ||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "Error: #{response.parsed_response["error"]}" | ||
| end | ||
| return response | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def details | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| def self.list | ||
| raise NotImplementedError | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,63 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "workspace" | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
|
|
||
| # TODO project | ||
| workspace = Workspace.new | ||
| input = "" | ||
|
|
||
| while input != "quit" | ||
| puts "Choose an option: | ||
| \n 1. List users | ||
| \n 2. List channels | ||
| \n 3. Select user | ||
| \n 4. Select channel | ||
| \n 5. Get details | ||
| \n 6. Send message | ||
| \n 7. Quit" | ||
|
|
||
| input = gets.chomp.downcase | ||
| case input | ||
| when "list users" | ||
| puts workspace.show_details("users") | ||
| when "list channels" | ||
| puts workspace.show_details("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. These should be calling |
||
| when "select user" | ||
| print "Enter the user name or Slack ID: " | ||
| input_user = gets.chomp | ||
| workspace.select_user(input_user) | ||
| if workspace.selected == nil | ||
| puts "User not found" | ||
| end | ||
| when "select channel" | ||
| print "Enter the channel name or Slack ID: " | ||
| input_channel = gets.chomp | ||
| workspace.select_channel(input_channel) | ||
| if workspace.selected == nil | ||
| puts "Channel not found" | ||
| end | ||
| when "details" | ||
| if workspace.selected == nil | ||
| puts "Please select a user or channel." | ||
| else | ||
| puts workspace.show_details | ||
| end | ||
| when "send message" | ||
|
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 have the wrong number of arguments to |
||
| if workspace.selected == nil | ||
| puts "Please select a user or channel." | ||
| else | ||
| print "Enter your message: " | ||
| text = gets.chomp | ||
| workspace.send_message(text) | ||
| end | ||
| when "quit" | ||
| else | ||
| puts "Please select a option from the menu." | ||
| end | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require "httparty" | ||
| require "pry" | ||
| require_relative "recipient" | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| class User < Recipient | ||
| attr_reader :real_name, :name, :slack_id, :status_text, :status_imoji | ||
|
|
||
| def initialize(slack_id, name, real_name, status_text, status_imoji) | ||
| super(slack_id, name) | ||
| @real_name = real_name | ||
| @slack_id = slack_id | ||
| @name = name | ||
| @status_text = status_text | ||
| @status_imoji = status_imoji | ||
| end | ||
|
|
||
| def self.list | ||
| raw_data = self.get("user") | ||
|
|
||
| unless raw_data.code == 200 | ||
| raise SlackApiError, "Improper request: #{raw_data.message}" | ||
| end | ||
| members_list = [] | ||
| members = raw_data["members"] | ||
| members.each do |member| | ||
| slack_id = member["id"] | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
| new_member = User.new(slack_id, name, real_name) | ||
| members_list << new_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 are missing two arguments in the call to |
||
| end | ||
| return members_list | ||
| end | ||
|
|
||
| def details | ||
| return "#{real_name}, slack user name: #{name}, slack_id: #{slack_id}" | ||
| end | ||
| end | ||
|
|
||
| # puts User.list | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = User.list | ||
| @channels = Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| # modify this | ||
| def select_channel(user_input) | ||
| selected = channels.select do |channel| | ||
| channel.name == user_input || channel.slack_id == user_input | ||
| end | ||
| @selected = selected.first | ||
|
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. Ruby's |
||
| end | ||
|
|
||
| def select_user(user_input) | ||
| @selected = users.select do |user| | ||
| user.name == user_input || user.slack_id == user_input | ||
| end | ||
| @selected = selected.first | ||
| end | ||
|
|
||
| # used to show details for selected user or channel | ||
| def show_details(selected) | ||
| return @selected.details | ||
| end | ||
|
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. Do you need the parameter here? You're not using it, and your CLI code doesn't expect to supply it. |
||
|
|
||
| # used to show details for all users or all channels | ||
| def print_details(recipients) | ||
| if recipients == "users" | ||
| return_array = [] | ||
| users.each do |user| | ||
| return_array << user.details | ||
| end | ||
| elsif recipients == "channels" | ||
| return_array = [] | ||
| channels.each do |channel| | ||
| return_array << channel.details | ||
| end | ||
| end | ||
| return return_array | ||
| end | ||
|
|
||
| def send_message(text) | ||
| @selected.send_msg(text) | ||
| 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.
Ideally the superclass would not need to know these details from the subclass. One of the benefits of inheritance is it allows you to add new subclasses without changing the parent class.
To do so, instead of using a constant for this, you could define a template method
list_urlwhich is overridden in the child classes to return the correct endpoint.That would also allow you to get rid of the
ifstatement inself.getbelow.