Skip to content

Instantly share code, notes, and snippets.

@mirrec
Created March 4, 2014 12:26
Show Gist options
  • Save mirrec/9345599 to your computer and use it in GitHub Desktop.
Save mirrec/9345599 to your computer and use it in GitHub Desktop.
Code snippets for rails workflow
describe "Amazon bounce handling process" do
# We have subscriptions on email "[email protected]" in different clients
# When last email to him was returned with "permanent failure"
# Then all subscription on email "[email protected]" are marked as bounced
end
describe "Amazon bounce handling process" do
it "marks all subcriptions as bounced on permanent failure notifications" do
# Given
different_active_subscription_on("[email protected]")
# When
receive_permanent_failure_notification(on_receiver: "[email protected]")
# Then
all_subscriptions_on("[email protected]").should be_bounced
end
end
# custom rspec matchers
matcher :be_bounced do
match do |actual_subscriptions|
actual_subscriptions.all? do |subscription|
subscription.status == "bounced"
end
end
end
# integration spec
def different_active_subscription_on(email)
subscriber_1 = create(:subscriber, email: email, client: create(:client))
subscriber_2 = create(:subscriber, email: email, client: create(:client))
# ... subscription1, subscription2
end
def receive_permanent_failure_notification(options)
post "/amazon_sns/bounces", permanent_failure_raw_data(options), {
"CONTENT_TYPE" => 'text/plain'
}
end
def permanent_failure_raw_data(options)
'{
"notificationType":"Bounce",' # ....
end
# in routung
namespace :amazon_sns do
post "bounces", :to => "bounce_listeners#handle"
end
# basic controller
class AmazonSns::BounceListenersController < ApplicationController
def handle
render :nothing => true
end
end
class AmazonSns::BounceListenersController < ApplicationController
def handle
bounce_object = bounce_parser(request_data)
bounce_handler.handle(bounce_object)
# bounce_handler
bounce_object.emails.each do |email|
subscription_marker.mark_as_bounced(email)
end
render :nothing => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment