Commit f58764e2
Changed files (2)
app
services
application
handlers
spec
services
handlers
app/services/application/handlers/publish_cake_to_twitter.rb
@@ -9,7 +9,7 @@ class PublishCakeToTwitter
end
def handle(message)
- tweet_about(cakes.find(message[:cake_id]))
+ tweet_about(@cakes.find(message[:cake_id]))
end
private
@@ -19,7 +19,11 @@ class PublishCakeToTwitter
end
def tweet_for(cake)
- "#{cake.name} By #{cake.user.name} on https://www.cakeside.com/creations/#{cake.to_param}!"
+ "#{cake.name} By #{cake.user.name} on #{routes.creation_url(cake)}!"
+ end
+
+ def routes
+ Cake::Application.routes.url_helpers
end
handle_asynchronously :handle, run_at: Proc.new { 10.minutes.from_now }
spec/services/handlers/publish_cake_to_twitter_spec.rb
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe PublishCakeToTwitter do
+ let(:twitter) { double(tweet: '') }
+ let(:cakes) { double }
+
+ subject { PublishCakeToTwitter.new(twitter, cakes) }
+
+ describe "#handles?" do
+ it "handles cake_published" do
+ subject.handles?(:cake_published).should be_true
+ end
+ end
+
+ describe "#handle" do
+ context "when the cake is published and safe for kids" do
+ let(:artist) { User.new(name: 'joe') }
+ let(:cake) { Creation.new(id: id, name: 'yummy') }
+ let(:id) { 88 }
+
+ before :each do
+ Rails.application.routes.default_url_options[:host]= 'localhost:3000'
+ cake.stub(:is_safe_for_children?).and_return(true)
+ cake.stub(:published?).and_return(true)
+ cake.stub(:user).and_return(artist)
+ cakes.stub(:find).with(id).and_return(cake)
+ end
+
+ it "tweets new cakes" do
+ subject.handle(cake_id: id)
+ twitter.should have_received(:tweet).with("yummy By joe on http://localhost:3000/creations/88-yummy!")
+ end
+ end
+ end
+end