Skip to content

Commit

Permalink
Refactor away SlackArena::Service.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Apr 4, 2019
1 parent b77f82d commit a79dac6
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SlackArena::App.instance.prepare!
Thread.abort_on_exception = true

Thread.new do
SlackArena::Service.instance.start_from_database!
SlackRubyBotServer::Service.instance.start_from_database!
SlackArena::App.instance.after_start!
end

Expand Down
17 changes: 12 additions & 5 deletions slack-arena/api/endpoints/teams_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,31 @@ class TeamsEndpoint < Grape::API
token = rc['bot']['bot_access_token']
bot_user_id = rc['bot']['bot_user_id']
user_id = rc['user_id']
access_token = rc['access_token']
team = Team.where(token: token).first
team ||= Team.where(team_id: rc['team_id']).first
if team && !team.active?

if team
team.update_attributes!(
activated_user_id: user_id,
activated_user_access_token: access_token,
bot_user_id: bot_user_id
)
raise "Team #{team.name} is already registered." if team.active?

team.activate!(token)
team.update_attributes!(activated_user_id: user_id, bot_user_id: bot_user_id)
elsif team
raise "Team #{team.name} is already registered."
else
team = Team.create!(
token: token,
team_id: rc['team_id'],
name: rc['team_name'],
activated_user_id: user_id,
activated_user_access_token: access_token,
bot_user_id: bot_user_id
)
end

SlackArena::Service.instance.start!(team)
SlackRubyBotServer::Service.instance.create!(team)
present team, with: Api::Presenters::TeamPresenter
end
end
Expand Down
2 changes: 1 addition & 1 deletion slack-arena/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def deactivate_asleep_teams!

begin
team.deactivate!
team.inform_everyone!(text: "Your subscription expired more than 2 weeks ago, deactivating. Reactivate at #{SlackArena::Service.url}. Your data will be purged in another 2 weeks.")
team.inform_everyone!(text: "Your subscription expired more than 2 weeks ago, deactivating. Reactivate at #{SlackRubyBotServer::Service.url}. Your data will be purged in another 2 weeks.")
rescue StandardError => e
logger.warn "Error informing team #{team}, #{e.message}."
end
Expand Down
2 changes: 1 addition & 1 deletion slack-arena/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SlackArena
© 2018 Daniel Doubrovkine & Contributors, MIT License
https://twitter.com/dblockdotorg
Service at #{SlackArena::Service.url}
Service at #{SlackRubyBotServer::Service.url}
Open-Source at https://github.com/dblock/slack-arena
EOS
end
4 changes: 2 additions & 2 deletions slack-arena/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def subscribe_text
end

def update_cc_text
"Update your credit card info at #{SlackArena::Service.url}/update_cc?team_id=#{team_id}."
"Update your credit card info at #{SlackRubyBotServer::Service.url}/update_cc?team_id=#{team_id}."
end

def subscribed_text
Expand Down Expand Up @@ -193,7 +193,7 @@ def trial_expired_text
end

def subscribe_team_text
"Subscribe your team for $4.99 a year at #{SlackArena::Service.url}/subscribe?team_id=#{team_id} to continue receiving Are.na channels in Slack."
"Subscribe your team for $4.99 a year at #{SlackRubyBotServer::Service.url}/subscribe?team_id=#{team_id} to continue receiving Are.na channels in Slack."
end

def inform_subscribed_changed!
Expand Down
2 changes: 1 addition & 1 deletion slack-arena/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def connected_to_arena?
end

def connect_redirect_url
"#{ENV['APP_URL'] || SlackArena::Service.url}/connect"
"#{ENV['APP_URL'] || SlackRubyBotServer::Service.url}/connect"
end

def connect_to_arena_url(channel_id)
Expand Down
4 changes: 2 additions & 2 deletions slack-arena/service.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SlackArena
class Service < SlackRubyBotServer::Service
module SlackRubyBotServer
class Service
def self.url
ENV['URL'] || (ENV['RACK_ENV'] == 'development' ? 'http://localhost:5000' : 'https://arena.playplay.io')
end
Expand Down
8 changes: 5 additions & 3 deletions spec/api/endpoints/teams_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'bot_access_token' => 'token',
'bot_user_id' => 'bot_user_id'
},
'access_token' => 'access_token',
'user_id' => 'activated_user_id',
'team_id' => 'team_id',
'team_name' => 'team_name'
Expand All @@ -62,7 +63,7 @@
ENV.delete('SLACK_CLIENT_SECRET')
end
it 'creates a team' do
expect(SlackArena::Service.instance).to receive(:start!)
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(
text: "Welcome to Are.na!\nInvite <@bot_user_id> to a channel to publish are.na channels to it.\n",
channel: 'C1',
Expand All @@ -79,7 +80,7 @@
}.to change(Team, :count).by(1)
end
it 'reactivates a deactivated team' do
expect(SlackArena::Service.instance).to receive(:start!)
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(
text: "Welcome to Are.na!\nInvite <@bot_user_id> to a channel to publish are.na channels to it.\n",
channel: 'C1',
Expand All @@ -99,14 +100,15 @@
}.to_not change(Team, :count)
end
it 'returns a useful error when team already exists' do
expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage)
existing_team = Fabricate(:team, token: 'token')
expect { client.teams._post(code: 'code') }.to raise_error Faraday::ClientError do |e|
json = JSON.parse(e.response[:body])
expect(json['message']).to eq "Team #{existing_team.name} is already registered."
end
end
it 'reactivates a deactivated team with a different code' do
expect(SlackArena::Service.instance).to receive(:start!)
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(
text: "Welcome to Are.na!\nInvite <@bot_user_id> to a channel to publish are.na channels to it.\n",
channel: 'C1',
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/teams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
context 'oauth', vcr: { cassette_name: 'auth_test' } do
it 'registers a team' do
allow_any_instance_of(Team).to receive(:ping!).and_return(ok: true)
expect(SlackArena::Service.instance).to receive(:start!)
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
oauth_access = { 'bot' => { 'bot_access_token' => 'token' }, 'team_id' => 'team_id', 'team_name' => 'team_name' }
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(hash_including(code: 'code')).and_return(oauth_access)
expect {
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-arena/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
let!(:subscribed_team_a_month_ago) { Fabricate(:team, created_at: 1.month.ago, subscribed: true) }
it 'destroys teams inactive for two weeks' do
expect_any_instance_of(Team).to receive(:inform_everyone!).with(
text: "Your subscription expired more than 2 weeks ago, deactivating. Reactivate at #{SlackArena::Service.url}. Your data will be purged in another 2 weeks."
text: "Your subscription expired more than 2 weeks ago, deactivating. Reactivate at #{SlackRubyBotServer::Service.url}. Your data will be purged in another 2 weeks."
).once
subject.send(:deactivate_asleep_teams!)
expect(active_team.reload.active).to be true
Expand Down
4 changes: 2 additions & 2 deletions spec/slack-arena/commands/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
let!(:team) { Fabricate(:team) }
it 'is a subscription feature' do
expect(message: "#{SlackRubyBot.config.user} subscription", user: 'user').to respond_with_slack_message(
"Your trial subscription has expired and we will no longer send your Are.na channels to Slack. Subscribe your team for $4.99 a year at #{SlackArena::Service.url}/subscribe?team_id=#{team.team_id} to continue receiving Are.na channels in Slack."
"Your trial subscription has expired and we will no longer send your Are.na channels to Slack. Subscribe your team for $4.99 a year at #{SlackRubyBotServer::Service.url}/subscribe?team_id=#{team.team_id} to continue receiving Are.na channels in Slack."
)
end
end
context 'team without a customer ID' do
let!(:team) { Fabricate(:team, subscribed: true, stripe_customer_id: nil) }
it 'errors' do
expect(message: "#{SlackRubyBot.config.user} subscription", user: 'user').to respond_with_slack_message(
"Not a subscriber. Subscribe your team for $4.99 a year at #{SlackArena::Service.url}/subscribe?team_id=#{team.team_id} to continue receiving Are.na channels in Slack."
"Not a subscriber. Subscribe your team for $4.99 a year at #{SlackRubyBotServer::Service.url}/subscribe?team_id=#{team.team_id} to continue receiving Are.na channels in Slack."
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/slack-arena/service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe SlackArena::Service do
describe SlackRubyBotServer::Service do
context '#url' do
before do
@rack_env = ENV['RACK_ENV']
Expand All @@ -9,14 +9,14 @@
ENV['RACK_ENV'] = @rack_env
end
it 'defaults to playplay.io in production' do
expect(SlackArena::Service.url).to eq 'https://arena.playplay.io'
expect(SlackRubyBotServer::Service.url).to eq 'https://arena.playplay.io'
end
context 'in development' do
before do
ENV['RACK_ENV'] = 'development'
end
it 'defaults to localhost' do
expect(SlackArena::Service.url).to eq 'http://localhost:5000'
expect(SlackRubyBotServer::Service.url).to eq 'http://localhost:5000'
end
end
context 'when set' do
Expand All @@ -27,7 +27,7 @@
ENV.delete('URL')
end
it 'defaults to ENV' do
expect(SlackArena::Service.url).to eq 'updated'
expect(SlackRubyBotServer::Service.url).to eq 'updated'
end
end
end
Expand Down

0 comments on commit a79dac6

Please sign in to comment.