Hack in AWS SNS

This commit is contained in:
Andrew Tomaka 2016-10-05 20:56:09 -04:00
parent 160c775464
commit 8af0fec538
10 changed files with 46 additions and 2 deletions

4
.env.sample Normal file
View file

@ -0,0 +1,4 @@
export AWS_REGION=
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_TOPIC=

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
db/*.db db/*.db
config/app.yml config/app.yml
.env

View file

@ -7,3 +7,5 @@ before_script:
- RACK_ENV=test bundle exec rake db:migrate - RACK_ENV=test bundle exec rake db:migrate
script: script:
- RACK_ENV=test bundle exec rspec - RACK_ENV=test bundle exec rspec
env:
- AWS_REGION=us-east-1

View file

@ -5,7 +5,7 @@ RUN export LANG=en_US.UTF-8 && \
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
RUN apk update \ RUN apk update \
&& apk add build-base ruby-dev sqlite-dev \ && apk add build-base ruby-dev sqlite-dev ca-certificates \
&& apk add ruby ruby-bundler ruby-io-console \ && apk add ruby ruby-bundler ruby-io-console \
&& rm -rf /var/cache/apk* && rm -rf /var/cache/apk*

View file

@ -11,6 +11,9 @@ gem 'validate_url'
gem 'slim' gem 'slim'
gem 'aws-sdk', '~> 2'
gem 'dotenv'
gem 'bigdecimal' gem 'bigdecimal'
# alpine linux does not include a method to determine the timezone # alpine linux does not include a method to determine the timezone
gem 'tzinfo-data' gem 'tzinfo-data'

View file

@ -16,6 +16,12 @@ GEM
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.3.8) addressable (2.3.8)
arel (6.0.2) arel (6.0.2)
aws-sdk (2.2.3)
aws-sdk-resources (= 2.2.3)
aws-sdk-core (2.2.3)
jmespath (~> 1.0)
aws-sdk-resources (2.2.3)
aws-sdk-core (= 2.2.3)
backports (3.6.6) backports (3.6.6)
bigdecimal (1.2.7) bigdecimal (1.2.7)
builder (3.2.2) builder (3.2.2)
@ -33,11 +39,13 @@ GEM
coderay (1.1.0) coderay (1.1.0)
database_cleaner (1.4.1) database_cleaner (1.4.1)
diff-lcs (1.2.5) diff-lcs (1.2.5)
dotenv (2.1.0)
factory_girl (4.5.0) factory_girl (4.5.0)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
ffi (1.9.10) ffi (1.9.10)
hitimes (1.2.2) hitimes (1.2.2)
i18n (0.7.0) i18n (0.7.0)
jmespath (1.1.3)
json (1.8.3) json (1.8.3)
launchy (2.4.3) launchy (2.4.3)
addressable (~> 2.3) addressable (~> 2.3)
@ -123,9 +131,11 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
activerecord activerecord
aws-sdk (~> 2)
bigdecimal bigdecimal
capybara-webkit capybara-webkit
database_cleaner database_cleaner
dotenv
factory_girl factory_girl
launchy launchy
pry pry

1
app.rb
View file

@ -36,6 +36,7 @@ get '/send' do
@link = Link.find(params[:id]) @link = Link.find(params[:id])
@link.mark_sent @link.mark_sent
SmsNotifier.notify(@link)
flash[:success] = 'Link has been marked as sent' flash[:success] = 'Link has been marked as sent'
redirect '/manage' redirect '/manage'

View file

@ -4,6 +4,7 @@ require 'bundler/setup'
require 'sinatra/config_file' require 'sinatra/config_file'
require 'sinatra/json' require 'sinatra/json'
Bundler.require Bundler.require
Dotenv.load
set :root, File.dirname('..') set :root, File.dirname('..')
@ -11,6 +12,7 @@ require_relative 'environments'
require_relative '../models/link' require_relative '../models/link'
require_relative '../helpers/application_helper' require_relative '../helpers/application_helper'
require_relative '../lib/sms_notifier'
config_file 'config/app.yml' config_file 'config/app.yml'

12
lib/sms_notifier.rb Normal file
View file

@ -0,0 +1,12 @@
class SmsNotifier
cattr_reader :sns, instance_accessor: false do
Aws::SNS::Client.new
end
def self.notify(link)
sns.publish({
topic_arn: ENV['AWS_TOPIC'],
message: "#{link.title}: #{link.url}",
})
end
end

View file

@ -10,7 +10,10 @@ describe 'Admin Send Links' do
context 'when logged in' do context 'when logged in' do
let!(:links) { 10.times.collect { create(:link) } } let!(:links) { 10.times.collect { create(:link) } }
before(:each) { basic_auth 'admin', 'password' } before(:each) do
basic_auth 'admin', 'password'
allow(SmsNotifier).to receive(:notify)
end
it 'should allow sending of a link' do it 'should allow sending of a link' do
visit '/manage' visit '/manage'
@ -30,5 +33,11 @@ describe 'Admin Send Links' do
expect(page).to_not have_content(link.title) expect(page).to_not have_content(link.title)
end end
it 'attempts to send an SMS message' do
visit '/manage'
first('a', text: 'Send').click
expect(SmsNotifier).to have_received(:notify)
end
end end
end end