Commit 73e6013c

mo khan <mo@mokhan.ca>
2015-01-17 19:27:50
add job to publish a cake to twitter.
1 parent cea3cd6
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