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
config/app.yml
.env

View File

@ -7,3 +7,5 @@ before_script:
- RACK_ENV=test bundle exec rake db:migrate
script:
- 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
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 \
&& rm -rf /var/cache/apk*

View File

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

View File

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

1
app.rb
View File

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

View File

@ -4,6 +4,7 @@ require 'bundler/setup'
require 'sinatra/config_file'
require 'sinatra/json'
Bundler.require
Dotenv.load
set :root, File.dirname('..')
@ -11,6 +12,7 @@ require_relative 'environments'
require_relative '../models/link'
require_relative '../helpers/application_helper'
require_relative '../lib/sms_notifier'
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
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
visit '/manage'
@ -30,5 +33,11 @@ describe 'Admin Send Links' do
expect(page).to_not have_content(link.title)
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