-
Notifications
You must be signed in to change notification settings - Fork 27
Sockets - Evelynn and Shirley #7
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
7444d73
c037b15
d11b5e1
6d4c6ad
2ff0a71
d34a78e
fde2c26
36fa1a5
d2d8222
12aae99
606145b
1404aca
272b7c1
7c9a866
d4d6443
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,15 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "Debug Local File", | ||
| "type": "Ruby", | ||
| "request": "launch", | ||
| "cwd": "${workspaceRoot}", | ||
| "program": "${file}" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require "dotenv" | ||
| require "HTTParty" | ||
| require "pry" | ||
|
|
||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| class Channel < Recipient | ||
| CHANNEL_URL = "https://slack.com/api/channels.list" | ||
| @query = { token: KEY } | ||
|
|
||
| attr_reader :slack_id, :name, :topic, :member_count | ||
|
|
||
| def initialize(slack_id:, name:, topic:, member_count:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.list | ||
| response = self.get(CHANNEL_URL, query: @query) | ||
| # binding.pry | ||
| channels = response["channels"].map do |channel| | ||
| id = channel["id"] | ||
| name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| member_count = channel["num_members"] | ||
| self.new(slack_id: id, name: name, topic: topic, member_count: member_count) | ||
| end | ||
| return channels | ||
| end | ||
|
|
||
| def details | ||
| return "slack_id: #{slack_id}, name: #{name}, topic: #{topic}, member_count: #{member_count}" | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| require "dotenv" | ||
| require "HTTParty" | ||
| require "pry" | ||
|
|
||
| Dotenv.load | ||
| KEY = ENV["SLACK_TOKEN"] | ||
| MESSAGE_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| module Slack | ||
| class SlackApiError < StandardError; end | ||
|
|
||
| class Recipient | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def self.get(url, query:) | ||
| response = HTTParty.get(url, query: query) | ||
| if response["ok"] == false | ||
| raise SlackApiError.new("Invalid request, error #{response.code}: #{response.message}") | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def send_message(message) | ||
| body = { | ||
| token: KEY, | ||
| channel: slack_id, | ||
| text: message, | ||
| } | ||
| response = HTTParty.post(MESSAGE_URL, body: body) | ||
|
|
||
| if response["ok"] == false | ||
| raise SlackApiError.new("Invalid request, error #{response.code}: #{response.message}") | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| # private | ||
|
|
||
| def self.list | ||
| raise NotImplementedError, "implement in child class" | ||
| end | ||
|
|
||
| def details | ||
| raise NotImplementedError, "implement in child class" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,69 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require_relative "workspace" | ||
| require "table_print" | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| workspace = Slack::Workspace.new | ||
| puts "Welcome to the Ada Slack CLI! \n | ||
| There are #{workspace.channels.length} channels and #{workspace.users.length} users\n" | ||
|
|
||
| commands = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"] | ||
|
|
||
| # TODO project | ||
| while true | ||
| puts "\nWhat would you like to do?" | ||
| puts | ||
| puts commands | ||
| puts | ||
| input = gets.chomp.downcase | ||
|
|
||
| case input | ||
| when "list users" | ||
| tp workspace.users, :name, :slack_id, :real_name | ||
| when "list channels" | ||
| tp workspace.channels, :name, :topic, :slack_id, :member_count | ||
| when "select user" | ||
| puts "Please type in username or Slack ID" | ||
| user = gets.chomp | ||
| selected_user = workspace.select_user(user) | ||
| if selected_user | ||
| puts "You selected user #{selected_user.name}" | ||
| else | ||
| puts "This user does not exist" | ||
| end | ||
| when "select channel" | ||
| puts "Please type in channel name or Slack ID" | ||
| channel = gets.chomp | ||
| selected_channel = workspace.select_channel(channel) | ||
| if selected_channel | ||
| puts "You selected the channel called \'#{selected_channel.name}\'" | ||
| else | ||
| puts "This channel does not exist" | ||
| end | ||
| when "details" | ||
| if workspace.show_details == nil | ||
| puts "No user or channel is selected" | ||
| else | ||
| puts workspace.show_details | ||
| end | ||
| when "send message" | ||
| if workspace.selected == nil | ||
| puts "No user or channel selected. Try again" | ||
| else | ||
| puts "Write your message here" | ||
| message = gets.chomp | ||
| workspace.send_message(message) | ||
| end | ||
| when "quit" | ||
| break | ||
| else | ||
| puts "Command not valid. Please type one of the following" | ||
| end | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| # Make sure all user input gets checked/converted for casing issues (.upcase for usernames, etc.) | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| SlackApiError < StandardError |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require "dotenv" | ||
| require "HTTParty" | ||
| require "pry" | ||
|
|
||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| class User < Recipient | ||
| USER_URL = "https://slack.com/api/users.list" | ||
| @query = { token: KEY } | ||
|
|
||
| attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji | ||
|
|
||
| def initialize(slack_id:, name:, real_name:, status_text:, status_emoji:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| @real_name = real_name | ||
| @status_text = status_text | ||
| @status_emoji = status_emoji | ||
| end | ||
|
|
||
| def self.list | ||
| response = self.get(USER_URL, query: @query) | ||
| users = response["members"].map do |user| | ||
| id = user["id"] | ||
| name = user["name"] | ||
| real_name = user["real_name"] | ||
| status_text = user["profile"]["status_text"] | ||
| status_emoji = user["profile"]["status_emoji"] | ||
| self.new(slack_id: id, name: name, real_name: real_name, status_text: status_text, status_emoji: status_emoji) | ||
| end | ||
| return users | ||
| end | ||
|
|
||
| def details | ||
| return "slack_id: #{slack_id}, name: #{name}, real_name: #{real_name}, status_text: #{status_text}, status_emoji: #{status_emoji}" | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Use the dotenv gem to load environment variables | ||
| # Use HTTParty to send a GET request to the channels.list endpoint | ||
| # Check that the request completed successfully, and print relevant information to the console if it didn't | ||
| # Loop through the results and print out the name of each channel | ||
|
|
||
| require "dotenv" | ||
| require "HTTParty" | ||
| require "pry" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| KEY = ENV["SLACK_TOKEN"] | ||
| url = "https://slack.com/api/channels.list" | ||
| query = { token: KEY } | ||
|
|
||
| response = HTTParty.get(url, query: query) | ||
| binding.pry | ||
| if response.code != 200 | ||
| puts response.reason | ||
| puts response.code | ||
| else | ||
| response["channels"].each do |channel| | ||
| puts channel["name"] | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| module Slack | ||
| class Workspace | ||
| attr_reader :users, :channels | ||
| attr_accessor :selected | ||
|
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 think an |
||
|
|
||
| def initialize | ||
| @users = Slack::User.list | ||
| @channels = Slack::Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_user(value) | ||
| @users.each do |user| | ||
| if user.name == value | ||
| @selected = user | ||
| return user | ||
| elsif user.slack_id == value | ||
| @selected = user | ||
| return user | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| def select_channel(value) | ||
| @channels.each do |channel| | ||
| if channel.name == value | ||
| @selected = channel | ||
|
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 a lot of repeated code between this method and |
||
| return channel | ||
| elsif channel.slack_id == value | ||
| @selected = channel | ||
| return channel | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| def show_details | ||
| return selected.details if selected | ||
| end | ||
|
|
||
| def send_message(message) | ||
| if selected | ||
| selected.send_message(message) | ||
| else | ||
| return nil | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| describe "Channel" do | ||
| describe "self.list" do | ||
| before do | ||
| VCR.use_cassette("make channels list") do | ||
| query_params = { | ||
| token: KEY, | ||
| } | ||
| @channels = Slack::Channel.list | ||
| @channel = @channels.first | ||
| end | ||
| end | ||
|
|
||
| it "will return an array of Channels" do | ||
| expect(@channels).must_be_kind_of Array | ||
| expect(@channel).must_be_kind_of Slack::Channel | ||
| end | ||
|
|
||
| it "will assign the correct attributes to each Channel" do | ||
| expect(@channel.slack_id).must_equal "CH2V3FHEY" | ||
| expect(@channel.name).must_equal "general" | ||
| expect(@channel.topic).must_equal "Company-wide announcements and work-based matters" | ||
| expect(@channel.member_count).must_equal 2 | ||
| end | ||
|
|
||
| it "has the correct data tyes for each attribute" do | ||
| expect(@channel.slack_id).must_be_kind_of String | ||
| expect(@channel.name).must_be_kind_of String | ||
| expect(@channel.topic).must_be_kind_of String | ||
| expect(@channel.member_count).must_be_kind_of Integer | ||
| end | ||
| end | ||
|
|
||
| describe "details" do | ||
| before do | ||
| VCR.use_cassette("show channel details") do | ||
| query_params = { | ||
| token: KEY, | ||
| } | ||
| @channels = Slack::Channel.list | ||
| @channel = @channels.first | ||
| end | ||
| end | ||
|
|
||
| it "returns the correct data" do | ||
| expect(@channel.details).must_be_kind_of String | ||
| 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.
Why is
privatecommented out?