diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..e1b0f84 --- /dev/null +++ b/.env.sample @@ -0,0 +1,4 @@ +export AWS_REGION= +export AWS_ACCESS_KEY_ID= +export AWS_SECRET_ACCESS_KEY= +export AWS_TOPIC= diff --git a/.gitignore b/.gitignore index 2de85d0..6100f70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ db/*.db config/app.yml +.env diff --git a/.travis.yml b/.travis.yml index 19789fa..3a25690 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 27543f3..78605a4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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* diff --git a/Gemfile b/Gemfile index 58343ad..ad74e44 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 6955f03..daf30c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app.rb b/app.rb index 4ab628e..c846d58 100644 --- a/app.rb +++ b/app.rb @@ -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' diff --git a/config/init.rb b/config/init.rb index c3d1df5..6257633 100644 --- a/config/init.rb +++ b/config/init.rb @@ -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' diff --git a/lib/sms_notifier.rb b/lib/sms_notifier.rb new file mode 100644 index 0000000..1044748 --- /dev/null +++ b/lib/sms_notifier.rb @@ -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 diff --git a/spec/features/admin/send_link_spec.rb b/spec/features/admin/send_link_spec.rb index 571a583..287d55e 100644 --- a/spec/features/admin/send_link_spec.rb +++ b/spec/features/admin/send_link_spec.rb @@ -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