Skip to content

Commit

Permalink
Merge pull request OperationCode#504 from juliantrueflynn/be-repair/i…
Browse files Browse the repository at this point in the history
…nvite-slack-api-pybot

WIP: API request to PyBot for Slack invite
  • Loading branch information
juliantrueflynn authored Apr 27, 2019
2 parents 748691c + 29ee1f1 commit 1e3e9c4
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ REDIS_URL=redis://redis:6379/0
RAILS_ENV=development
POSTGRES_USER=postgres
POSTGRES_HOST=operationcode-psql
SLACK_SERVICE_URL=http://pybot.operationcode.org:8080/pybot/api/v1/slack
SLACK_SERVICE_AUTH_TOKEN=XXXXX
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ group :development, :test do
gem 'factory_girl_rails'
gem 'faker'
gem 'mocha'
gem 'vcr', '~> 3.0', '>= 3.0.3'
gem 'webmock', '~> 3.0', '>= 3.0.1'
gem 'vcr', '~> 4.0.0'
gem 'webmock', '~> 3.5.1'
end

group :development do
Expand Down
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ GEM
tzinfo (~> 1.1)
acts-as-taggable-on (5.0.0)
activerecord (>= 4.2.8)
addressable (2.5.2)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
arbre (1.1.1)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -119,7 +119,7 @@ GEM
has_scope (0.7.1)
actionpack (>= 4.1, < 5.2)
activesupport (>= 4.1, < 5.2)
hashdiff (0.3.7)
hashdiff (0.3.9)
httparty (0.14.0)
multi_xml (>= 0.5.2)
i18n (0.9.5)
Expand Down Expand Up @@ -189,7 +189,7 @@ GEM
method_source (~> 0.8.1)
pry-rails (0.3.6)
pry (>= 0.10.4)
public_suffix (3.0.0)
public_suffix (3.0.3)
puma (3.8.2)
raabro (1.1.6)
rack (2.0.6)
Expand Down Expand Up @@ -250,7 +250,7 @@ GEM
ruby-graphviz (1.2.3)
ruby-progressbar (1.9.0)
ruby_http_client (3.0.2)
safe_yaml (1.0.4)
safe_yaml (1.0.5)
sass (3.5.2)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand Down Expand Up @@ -286,10 +286,10 @@ GEM
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.3.2)
vcr (3.0.3)
vcr (4.0.0)
warden (1.2.8)
rack (>= 2.0.6)
webmock (3.1.0)
webmock (3.5.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
Expand Down Expand Up @@ -334,8 +334,8 @@ DEPENDENCIES
skylight
spring
spring-watcher-listen (~> 2.0.0)
vcr (~> 3.0, >= 3.0.3)
webmock (~> 3.0, >= 3.0.1)
vcr (~> 4.0.0)
webmock (~> 3.5.1)

BUNDLED WITH
1.17.1
1.17.2
9 changes: 3 additions & 6 deletions app/jobs/slack_jobs/inviter_job.rb
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
54 changes: 54 additions & 0 deletions app/lib/slack_service/client.rb
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
4 changes: 4 additions & 0 deletions app/lib/slack_service/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SlackService
class Error < StandardError
end
end
32 changes: 32 additions & 0 deletions test/cassettes/slack_service/client/invite_failure.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions test/cassettes/slack_service/client/invite_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions test/requests/slack_service/client_test.rb
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

0 comments on commit 1e3e9c4

Please sign in to comment.