Commit 73e6013c
Changed files (2)
app
spec
app/jobs/publish_to_twitter_job.rb
@@ -0,0 +1,25 @@
+class PublishToTwitterJob < ActiveJob::Base
+ queue_as :default
+
+ def perform(cake)
+ tweet_about(cake)
+ end
+
+ private
+
+ def twitter
+ @twitter ||= Spank::IOC.resolve(:twitter_publisher)
+ end
+
+ def tweet_about(cake)
+ twitter.tweet(tweet_for(cake)) if cake.published?
+ end
+
+ def tweet_for(cake)
+ "#{cake.name} By #{cake.user.name} on #{routes.cake_url(cake)}!"
+ end
+
+ def routes
+ Cake::Application.routes.url_helpers
+ end
+end
spec/jobs/publish_to_twitter_job_spec.rb
@@ -0,0 +1,41 @@
+require 'rails_helper'
+
+RSpec.describe PublishToTwitterJob, :type => :job do
+ subject { PublishToTwitterJob.new }
+
+ describe "#perform" do
+ let(:artist) { User.new(name: 'joe') }
+ let(:cake) { Creation.new(id: id, name: 'yummy') }
+ let(:id) { 88 }
+ let(:twitter) { double(tweet: '') }
+ let(:cakes) { double }
+
+ before :each do
+ allow(cake).to receive(:user).and_return(artist)
+ allow(cakes).to receive(:find).with(id).and_return(cake)
+ allow(subject).to receive(:twitter).and_return(twitter)
+ end
+
+ context "when the cake is published and safe for kids" do
+ before :each do
+ allow(cake).to receive(:published?).and_return(true)
+ end
+
+ it "tweets about the new cake" do
+ subject.perform(cake)
+ expect(twitter).to have_received(:tweet).with("yummy By joe on http://www.blah.com/cakes/88-yummy!")
+ end
+ end
+
+ context "when the cake is not published" do
+ before :each do
+ allow(cake).to receive(:published?).and_return(false)
+ end
+
+ it "should not publish any tweets" do
+ subject.perform(cake)
+ expect(twitter).not_to have_received(:tweet)
+ end
+ end
+ end
+end