-
Notifications
You must be signed in to change notification settings - Fork 27
Sockets - Pauline & Niv #17
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
46d2104
5557af1
f54ce2d
e2c60a5
fc2f66e
5348b32
b1c5b02
d02f2be
91a050a
1cc5b1a
01193f9
8685784
5db8e83
042cfc9
32f0573
5a55aa0
832c4be
e91ecd4
e5a9dec
2695c6f
dd4ad23
86d6b0e
dd139ef
d8ed86e
c7da16c
499b62b
6d0150a
45e8404
febd4e4
da8db5f
72c3219
32fbd1a
0514ab3
f2c815c
678f83c
d78a5d7
4caf4cd
a0db0d6
d672f90
e62871e
333ddec
d2b1957
803b051
e2b178c
f639218
0c1c1ee
50eac82
ebb8293
891b75b
8fb6865
e521cd0
f21bd3e
5c4c034
1889248
22b432a
4437b76
3b8288a
937be60
2e4edef
b937f4a
372e031
958fa10
8af3f77
43c947c
4955e3b
373a3b5
ac38d87
2c0c328
ba7559b
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,49 @@ | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'table_print' | ||
| require_relative 'recipient' | ||
| Dotenv.load | ||
|
|
||
| module SlackAPI | ||
| class Channel < Recipient | ||
|
|
||
| attr_reader :topic, :member_count | ||
|
|
||
| URL = "https://slack.com/api/channels.list" | ||
| TOKEN = ENV['TOKEN'] | ||
| @@all = [] | ||
|
|
||
| def initialize(slack_id:, name:, topic:, member_count:) | ||
| super(slack_id: slack_id, name: name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def details | ||
| return "Channel name: #{name} | ||
| Slack ID: #{slack_id} | ||
| Topic: #{topic} | ||
| Member count: #{member_count}" | ||
| end | ||
|
|
||
| def self.list | ||
| return @@all | ||
| end | ||
|
|
||
| def self.load | ||
| query_parameters = { | ||
| token: TOKEN, | ||
| } | ||
| response = HTTParty.get(URL, query: query_parameters)['channels'] | ||
| response.each do |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. Forgot to validate |
||
| topic = channel['topic']['value'] | ||
| member_count = channel['num_members'] | ||
| slack_id = channel['id'] | ||
| name = channel['name'] | ||
| new_channel = Channel.new(topic: topic, member_count: member_count, slack_id: slack_id, name: name) | ||
| @@all << new_channel | ||
| end | ||
| return @@all | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| module SlackAPI | ||
| class Recipient | ||
|
|
||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def send_message(text:) | ||
| body = { | ||
| text: text, | ||
| channel: slack_id, | ||
| token: ENV["TOKEN"], | ||
| } | ||
|
|
||
| response = HTTParty.post("https://slack.com/api/chat.postMessage", | ||
| body: body, | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" }) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "Error when posting #{text} to #{name}, error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return response.code == 200 && response.parsed_response["ok"] | ||
|
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 would consider just returning |
||
| end | ||
|
|
||
| def self.load | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| def details | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| def self.list | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| require "dotenv" | ||
| require "httparty" | ||
| require "table_print" | ||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackAPI | ||
| class SlackApiError < StandardError; end | ||
|
|
||
| class User < Recipient | ||
|
|
||
| attr_reader :real_name | ||
|
|
||
| BASE_URL = "https://slack.com/api" | ||
| USERS_LIST_PATH = "/users.list" | ||
| CHAT_POST_MESSAGE_PATH = "/chat.postMessage" | ||
| TOKEN = ENV["TOKEN"] | ||
| @@all = [] | ||
|
|
||
| def initialize(real_name:, slack_id:, name:) | ||
| super(slack_id: slack_id, name: name) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def details | ||
| return "Name: #{name} | ||
| Slack ID: #{slack_id} | ||
| Real name: #{real_name}" | ||
| end | ||
|
|
||
| def self.list | ||
| return @@all | ||
| end | ||
|
|
||
| def self.load | ||
| query_parameters = { token: TOKEN } | ||
| response = HTTParty.get(BASE_URL << USERS_LIST_PATH, query: query_parameters) | ||
| response["members"].length.times do |i| | ||
|
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. Forgot to validate |
||
| real_name = response["members"][i]["real_name"] | ||
| slack_id = response["members"][i]["id"] | ||
| name = response["members"][i]["name"] | ||
| new_user = SlackAPI::User.new(real_name: real_name, slack_id: slack_id, name: name) | ||
| @@all.push(new_user) | ||
| end | ||
| return @@all | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'pry' | ||
| Dotenv.load | ||
|
|
||
| module SlackAPI | ||
| class Workspace | ||
|
|
||
| attr_reader :users, :channels | ||
| attr_accessor :selected | ||
|
|
||
| def initialize(users:, channels:, selected: nil) | ||
| @users = users | ||
| @channels = channels | ||
| @selected = selected | ||
| end | ||
|
|
||
| def select_channel(id_or_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. Tiny issue: it's a little odd to use a keyword argument here since you only have a single argument. |
||
| @channels.each do |channel| | ||
| if channel.name == id_or_name | ||
| @selected = channel | ||
| elsif channel.slack_id == id_or_name | ||
| @selected = channel | ||
| end | ||
| end | ||
| return @selected | ||
| end | ||
|
|
||
| def select_user(id_or_name:) | ||
| @users.each do |user| | ||
| if user.name == id_or_name | ||
| @selected = user | ||
| elsif user.slack_id == id_or_name | ||
| @selected = user | ||
| end | ||
| end | ||
| return @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. Ruby's |
||
| end | ||
|
|
||
| def show_details | ||
| @selected.details | ||
| end | ||
|
|
||
| def send_message(text:) | ||
| @selected.send_message(text: text) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require 'minitest' | ||
| require_relative 'test_helper.rb' | ||
|
|
||
| describe "Channel" do | ||
| before do | ||
| VCR.use_cassette("channel_list") do | ||
| @response = SlackAPI::Channel.load | ||
| end | ||
| end | ||
|
|
||
| describe "details" do | ||
|
|
||
| end | ||
|
|
||
| describe "self.list" do | ||
| it "can list details of all the channels" do | ||
| expect(SlackAPI::Channel.list).must_be_kind_of Array | ||
| end | ||
| end | ||
|
|
||
| describe "self.load" do | ||
| it "can find the list of channels" do | ||
| expect(@response).must_be_kind_of Array | ||
| expect(@response[0]).must_be_kind_of SlackAPI::Channel | ||
| expect(@response[0].name).must_equal "everyone" | ||
| expect(@response[0].slack_id).must_equal "CH2SBU69Y" | ||
| expect(@response[0].topic).must_equal "Company-wide announcements and work-based matters" | ||
| expect(@response[0].member_count).must_equal 2 | ||
| end | ||
| end | ||
|
|
||
| it "sends a message to selected channel" do | ||
| VCR.use_cassette("channel_send_message") do | ||
| channel = SlackAPI::Channel.list[0] | ||
| test2 = channel.send_message(text: "hello") | ||
| expect(test2).must_equal true | ||
| end | ||
| 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. I'd like to see some edge case tests here. What happens if you can't talk to the API or there's an error? |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'minitest' | ||
| require_relative 'test_helper.rb' | ||
|
|
||
| describe "Recipient" do | ||
|
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. Empty tests 🙁. |
||
| before do | ||
|
|
||
| end | ||
|
|
||
| describe "self.get" do | ||
|
|
||
| end | ||
|
|
||
| describe "details" do | ||
|
|
||
|
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 would like to see an assertion that this |
||
| end | ||
|
|
||
| describe "self.list" do | ||
|
|
||
|
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 here. |
||
| end | ||
| 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. I'd also like to see a test that verifies that |
||
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.
While it doesn't cause a problem here generally class variables should be your tool of last resort. I probably would have only stored them in a field in
Workspaceinstead of here and there.