forked from OperationCode/operationcode_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OperationCode#504 from juliantrueflynn/be-repair/i…
…nvite-slack-api-pybot WIP: API request to PyBot for Slack invite
- Loading branch information
Showing
9 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
class SlackJobs | ||
class InviterJob < SlackJobs | ||
include Sidekiq::Worker | ||
sidekiq_options retry: 5 | ||
|
||
def perform(email) | ||
# Invites user to general channel | ||
Raven.user_context email: email | ||
Raven.tags_context slack_invite: 'yes' | ||
slack_client.invite( | ||
email: email, | ||
channels: ['C1T66M57G'], | ||
extra_message: 'Welcome to Operation Code! After logging in, please introduce yourself in the #general channel - a bit about who you are, where you\'re coming from, and where you\'re interested in going next.' | ||
) | ||
|
||
SlackService::Client.new.invite(email) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module SlackService | ||
class Client | ||
include HTTParty | ||
|
||
base_uri ENV['SLACK_SERVICE_URL'] | ||
|
||
attr_reader :headers | ||
|
||
def initialize | ||
bearer_token = "Bearer #{ENV['SLACK_SERVICE_AUTH_TOKEN']}" | ||
@headers = { Authorization: bearer_token } | ||
end | ||
|
||
def verify(email) | ||
response = self.class.get( | ||
'/verify', | ||
headers: headers, | ||
query: { email: email } | ||
) | ||
|
||
Rails.logger.info "Slack service #verify response: #{response.inspect}" | ||
|
||
return_value_for(response) | ||
end | ||
|
||
def invite(email) | ||
response = self.class.post( | ||
'/invite', | ||
headers: headers, | ||
body: { email: email }.to_json | ||
) | ||
|
||
Rails.logger.info "Slack service #invite response: #{response.inspect}" | ||
|
||
return_value_for(response) | ||
end | ||
|
||
private | ||
|
||
def response_json(response) | ||
response.present? ? response.parsed_response : {} | ||
end | ||
|
||
def return_value_for(response) | ||
json = response_json(response) | ||
case response.code | ||
when 200 | ||
json | ||
else | ||
raise SlackService::Error, response.inspect | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module SlackService | ||
class Error < StandardError | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'test_helper' | ||
|
||
class ClientTest < Minitest::Test | ||
def test_invite_success | ||
VCR.use_cassette('slack_service/client/invite_success') do | ||
response = SlackService::Client.new.invite('[email protected]') | ||
assert response['ok'].present? | ||
json = JSON.parse(response) | ||
assert_equal true, json['ok'] | ||
end | ||
end | ||
|
||
def test_invite_failure | ||
VCR.use_cassette('slack_service/client/invite_failure') do | ||
assert_raises { SlackService::Client.new.invite('[email protected]') } | ||
end | ||
end | ||
end |