Skip to content

Commit b5baf88

Browse files
committed
Subscribe to the Playplay.io mailing list.
1 parent 920c697 commit b5baf88

File tree

10 files changed

+125
-41
lines changed

10 files changed

+125
-41
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ AllCops:
66
Style:
77
Enabled: false
88

9+
Metrics:
10+
Enabled: false
11+
912
inherit_from: .rubocop_todo.yml

.rubocop_todo.yml

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2018-09-20 16:28:00 -0400 using RuboCop version 0.59.1.
3+
# on 2019-04-16 08:56:55 -0400 using RuboCop version 0.67.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 5
10-
Metrics/AbcSize:
11-
Max: 33
12-
13-
# Offense count: 12
14-
# Configuration parameters: CountComments, ExcludedMethods.
15-
# ExcludedMethods: refine
16-
Metrics/BlockLength:
17-
Max: 108
18-
19-
# Offense count: 2
20-
Metrics/CyclomaticComplexity:
21-
Max: 11
22-
23-
# Offense count: 5
24-
# Configuration parameters: CountComments.
25-
Metrics/MethodLength:
26-
Max: 23
27-
28-
# Offense count: 2
29-
Metrics/PerceivedComplexity:
30-
Max: 11
31-
329
# Offense count: 1
3310
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
3411
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
@@ -43,9 +20,3 @@ Naming/HeredocDelimiterNaming:
4320
Exclude:
4421
- 'slack-api-explorer/commands/help.rb'
4522
- 'slack-api-explorer/info.rb'
46-
47-
# Offense count: 61
48-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
49-
# URISchemes: http, https
50-
Metrics/LineLength:
51-
Max: 186

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ gem 'newrelic_rpm'
1313
gem 'rack-robotz'
1414
gem 'rack-server-pages'
1515
gem 'slack-ruby-bot-server'
16+
gem 'slack-ruby-bot-server-mailchimp'
1617
gem 'unicorn'
1718

1819
group :development, :test do

Gemfile.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ GEM
4848
descendants_tracker (0.0.4)
4949
thread_safe (~> 0.3, >= 0.3.1)
5050
diff-lcs (1.3)
51+
domain_name (0.5.20180417)
52+
unf (>= 0.0.5, < 1.0.0)
5153
equalizer (0.0.11)
5254
fabrication (2.20.1)
5355
faker (1.9.3)
@@ -89,6 +91,8 @@ GEM
8991
grape (>= 0.16.2)
9092
hashdiff (0.3.8)
9193
hashie (3.6.0)
94+
http-cookie (1.0.3)
95+
domain_name (~> 0.5)
9296
hyperclient (0.9.0)
9397
faraday (>= 0.9.0)
9498
faraday-digestauth
@@ -111,6 +115,11 @@ GEM
111115
kgio (2.11.2)
112116
launchy (2.4.3)
113117
addressable (~> 2.3)
118+
mailchimp_api_v3 (0.2.18)
119+
rest-client (~> 2)
120+
mime-types (3.2.2)
121+
mime-types-data (~> 3.2015)
122+
mime-types-data (3.2019.0331)
114123
mini_mime (1.0.1)
115124
mini_portile2 (2.4.0)
116125
minitest (5.11.3)
@@ -136,6 +145,7 @@ GEM
136145
mustermann-grape (1.0.0)
137146
mustermann (~> 1.0.0)
138147
net-http-digest_auth (1.4.1)
148+
netrc (0.11.0)
139149
newrelic-slack-ruby-bot (0.2.1)
140150
newrelic_rpm
141151
slack-ruby-bot (>= 0.10.5)
@@ -168,6 +178,10 @@ GEM
168178
declarative (< 0.1.0)
169179
declarative-option (< 0.2.0)
170180
uber (< 0.2.0)
181+
rest-client (2.0.2)
182+
http-cookie (>= 1.0.2, < 2.0)
183+
mime-types (>= 1.16, < 4.0)
184+
netrc (~> 0.8)
171185
roar (1.1.0)
172186
representable (~> 3.0.0)
173187
rspec (3.8.0)
@@ -211,6 +225,9 @@ GEM
211225
rack-rewrite
212226
rack-server-pages
213227
slack-ruby-bot (>= 0.12.0)
228+
slack-ruby-bot-server-mailchimp (0.2.0)
229+
mailchimp_api_v3
230+
slack-ruby-bot-server (>= 0.10.0)
214231
slack-ruby-client (0.14.2)
215232
activesupport
216233
faraday (>= 0.9)
@@ -225,6 +242,9 @@ GEM
225242
tzinfo (1.2.5)
226243
thread_safe (~> 0.1)
227244
uber (0.1.0)
245+
unf (0.1.4)
246+
unf_ext
247+
unf_ext (0.0.7.5)
228248
unicode-display_width (1.5.0)
229249
unicorn (5.5.0)
230250
kgio (~> 2.6)
@@ -273,6 +293,7 @@ DEPENDENCIES
273293
rubocop
274294
selenium-webdriver
275295
slack-ruby-bot-server
296+
slack-ruby-bot-server-mailchimp
276297
unicorn
277298
vcr
278299
webmock

config.ru

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SlackApiExplorer::App.instance.prepare!
1313
Thread.abort_on_exception = true
1414

1515
Thread.new do
16-
SlackApiExplorer::Service.instance.start_from_database!
16+
SlackRubyBotServer::Service.instance.start_from_database!
1717
end
1818

1919
run Api::Middleware.instance
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SlackRubyBotServer::Mailchimp.configure do |config|
2+
config.mailchimp_api_key = ENV['MAILCHIMP_API_KEY']
3+
config.mailchimp_list_id = ENV['MAILCHIMP_LIST_ID']
4+
config.additional_member_tags = ['slack-api-explorer']
5+
config.additional_merge_fields = { 'BOT' => 'SlackApiExplorer' }
6+
end

slack-api-explorer/api/endpoints/teams_endpoint.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,33 @@ class TeamsEndpoint < Grape::API
4545
)
4646

4747
token = rc['bot']['bot_access_token']
48+
bot_user_id = rc['bot']['bot_user_id']
49+
user_id = rc['user_id']
50+
access_token = rc['access_token']
4851
team = Team.where(token: token).first
4952
team ||= Team.where(team_id: rc['team_id']).first
50-
if team && !team.active?
53+
54+
if team
55+
team.update_attributes!(
56+
activated_user_id: user_id,
57+
activated_user_access_token: access_token,
58+
bot_user_id: bot_user_id
59+
)
60+
raise "Team #{team.name} is already registered." if team.active?
61+
5162
team.activate!(token)
52-
elsif team
53-
raise "Team #{team.name} is already registered."
5463
else
5564
team = Team.create!(
5665
token: token,
5766
team_id: rc['team_id'],
58-
name: rc['team_name']
67+
name: rc['team_name'],
68+
activated_user_id: user_id,
69+
activated_user_access_token: access_token,
70+
bot_user_id: bot_user_id
5971
)
6072
end
6173

62-
SlackApiExplorer::Service.instance.start!(team)
74+
SlackRubyBotServer::Service.instance.create!(team)
6375
present team, with: Api::Presenters::TeamPresenter
6476
end
6577
end

spec/api/endpoints/teams_endpoint_spec.rb

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,25 @@
5555

5656
context 'register' do
5757
before do
58-
oauth_access = { 'bot' => { 'bot_access_token' => 'token' }, 'team_id' => 'team_id', 'team_name' => 'team_name' }
58+
oauth_access = {
59+
'bot' => {
60+
'bot_access_token' => 'token',
61+
'bot_user_id' => 'bot_user_id'
62+
},
63+
'access_token' => 'access_token',
64+
'user_id' => 'activated_user_id',
65+
'team_id' => 'team_id',
66+
'team_name' => 'team_name'
67+
}
5968
ENV['SLACK_CLIENT_ID'] = 'client_id'
6069
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
70+
allow_any_instance_of(Slack::Web::Client).to receive(:im_open).with(
71+
user: 'activated_user_id'
72+
).and_return(
73+
'channel' => {
74+
'id' => 'C1'
75+
}
76+
)
6177
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(
6278
hash_including(
6379
code: 'code',
@@ -71,7 +87,7 @@
7187
ENV.delete('SLACK_CLIENT_SECRET')
7288
end
7389
it 'creates a team' do
74-
expect(SlackApiExplorer::Service.instance).to receive(:start!)
90+
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
7591
expect do
7692
team = client.teams._post(code: 'code')
7793
expect(team.team_id).to eq 'team_id'
@@ -81,7 +97,7 @@
8197
end.to change(Team, :count).by(1)
8298
end
8399
it 'reactivates a deactivated team' do
84-
expect(SlackApiExplorer::Service.instance).to receive(:start!)
100+
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
85101
existing_team = Fabricate(:team, token: 'token', active: false)
86102
expect do
87103
team = client.teams._post(code: 'code')
@@ -101,7 +117,7 @@
101117
end
102118
end
103119
it 'reactivates a deactivated team with a different code' do
104-
expect(SlackApiExplorer::Service.instance).to receive(:start!)
120+
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
105121
existing_team = Fabricate(:team, token: 'old', team_id: 'team_id', active: false)
106122
expect do
107123
team = client.teams._post(code: 'code')
@@ -113,6 +129,59 @@
113129
expect(team.active).to be true
114130
end.to_not change(Team, :count)
115131
end
132+
133+
context 'with mailchimp settings' do
134+
before do
135+
SlackRubyBotServer::Mailchimp.configure do |config|
136+
config.mailchimp_api_key = 'api-key'
137+
config.mailchimp_list_id = 'list-id'
138+
end
139+
end
140+
after do
141+
SlackRubyBotServer::Mailchimp.config.reset!
142+
end
143+
144+
let(:list) { double(Mailchimp::List, members: double(Mailchimp::List::Members)) }
145+
146+
it 'subscribes to the mailing list' do
147+
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
148+
149+
allow_any_instance_of(Slack::Web::Client).to receive(:users_info).with(
150+
user: 'activated_user_id'
151+
).and_return(
152+
user: {
153+
profile: {
154+
155+
first_name: 'First',
156+
last_name: 'Last'
157+
}
158+
}
159+
)
160+
161+
allow_any_instance_of(Mailchimp::Client).to receive(:lists).with('list-id').and_return(list)
162+
163+
expect(list.members).to receive(:where).with(email_address: '[email protected]').and_return([])
164+
165+
expect(list.members).to receive(:create_or_update).with(
166+
email_address: '[email protected]',
167+
merge_fields: {
168+
'FNAME' => 'First',
169+
'LNAME' => 'Last',
170+
'BOT' => 'SlackApiExplorer'
171+
},
172+
status: 'pending',
173+
name: nil,
174+
tags: %w[slack-api-explorer],
175+
unique_email_id: 'team_id-activated_user_id'
176+
)
177+
178+
client.teams._post(code: 'code')
179+
end
180+
after do
181+
ENV.delete('MAILCHIMP_API_KEY')
182+
ENV.delete('MAILCHIMP_LIST_ID')
183+
end
184+
end
116185
end
117186
end
118187
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Fabricator(:team) do
22
token { Fabricate.sequence(:team_token) { |i| "abc-#{i}" } }
33
team_id { Fabricate.sequence(:team_id) { |i| "T#{i}" } }
4+
activated_user_id { Fabricate.sequence(:activated_user_id) { |i| "U#{i}" } }
45
name { Faker::Lorem.word }
56
api { true }
67
end

spec/integration/teams_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
context 'oauth', vcr: { cassette_name: 'auth_test' } do
1313
it 'registers a team' do
1414
allow_any_instance_of(Team).to receive(:ping!).and_return(ok: true)
15-
expect(SlackApiExplorer::Service.instance).to receive(:start!)
15+
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
1616
oauth_access = { 'bot' => { 'bot_access_token' => 'token' }, 'team_id' => 'team_id', 'team_name' => 'team_name' }
1717
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(hash_including(code: 'code')).and_return(oauth_access)
1818
expect do

0 commit comments

Comments
 (0)