-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate service to associated objects
- Loading branch information
1 parent
31c63be
commit 18ef2bd
Showing
9 changed files
with
109 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class DailyPacket::Builder | ||
def self.find_or_build_for(built_on) | ||
daily_packet = DailyPacket.find_or_initialize_by(built_on: built_on) | ||
return daily_packet if daily_packet.persisted? | ||
|
||
reading_list = ReadingList.new | ||
warm_fuzzy = WarmFuzzy.random | ||
|
||
attributes = { | ||
reading_list_pace: reading_list.pace, | ||
warm_fuzzy: warm_fuzzy | ||
} | ||
|
||
daily_packet.update(attributes) | ||
daily_packet | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class DailyPacket::Producer < ActiveRecord::AssociatedObject | ||
def self.save_to(target, date = Date.today) | ||
daily_packet = DailyPacket::Builder.find_or_build_for(date) | ||
|
||
if target == :disk | ||
daily_packet.save_to_disk | ||
elsif target == :s3 | ||
daily_packet.save_to_s3 | ||
else | ||
raise ArgumentError | ||
end | ||
end | ||
|
||
def save_to_disk | ||
local_path = "tmp/daily_packet.pdf" | ||
daily_packet.pdf_view.save_as(local_path) | ||
end | ||
|
||
def save_to_s3 | ||
timestamp = daily_packet.built_on.strftime("%Y-%m-%d") | ||
s3_key = "daily-packets/#{timestamp}.pdf" | ||
pdf_data = daily_packet.pdf_view.pdf_data | ||
S3Api.write(s3_key, pdf_data) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
namespace :daily_packet do | ||
desc "Produce today's packet and save locally." | ||
task save_locally: :environment do | ||
DailyPacketService.save_locally | ||
DailyPacket::Producer.save_to(:disk) | ||
end | ||
|
||
desc "Produce today's packet and save to S3" | ||
task save_to_s3: :environment do | ||
DailyPacketService.save_to_s3 | ||
DailyPacket::Producer.save_to(:s3) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "rails_helper" | ||
|
||
describe DailyPacket::Builder do | ||
describe ".find_or_build_for" do | ||
let(:built_on) { Date.parse("2007-07-07") } | ||
|
||
context "with an existing DailyPacket record" do | ||
it "returns that existing record" do | ||
existing_daily_packet = FactoryBot.create(:daily_packet, built_on: built_on) | ||
returned_daily_packet = DailyPacket::Builder.find_or_build_for(built_on) | ||
expect(returned_daily_packet.id).to eq existing_daily_packet.id | ||
expect(DailyPacket.count).to eq 1 | ||
end | ||
end | ||
|
||
context "without an existing DailyPacket record" do | ||
it "creates and returns a new record" do | ||
FactoryBot.create(:warm_fuzzy) | ||
|
||
expect do | ||
DailyPacket::Builder.find_or_build_for(built_on) | ||
end.to change(DailyPacket, :count).from(0).to(1) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "rails_helper" | ||
|
||
describe DailyPacket::Producer do | ||
describe ".save_to" do | ||
context "with an invalid target" do | ||
it "raises an ArgumentError" do | ||
expect do | ||
DailyPacket::Producer.save_to(:invalid) | ||
end.to raise_error(ArgumentError) | ||
end | ||
end | ||
context "when the target is disk" do | ||
it "renders and writes the pdf data locally" do | ||
mock_view = double(:mock_view) | ||
expect(DailyPacket::PdfView).to receive(:new).and_return(mock_view) | ||
expect(mock_view).to receive(:save_as).with("tmp/daily_packet.pdf").and_return(nil) | ||
DailyPacket::Producer.save_to(:disk) | ||
end | ||
end | ||
|
||
context "when the target is disk" do | ||
it "renders and writes the pdf data to s3" do | ||
mock_view = double(:mock_view, pdf_data: "PDF GOES HERE") | ||
expect(DailyPacket::PdfView).to receive(:new).and_return(mock_view) | ||
|
||
expect(S3Api).to receive(:write).with( | ||
"daily-packets/2001-02-03.pdf", | ||
"PDF GOES HERE" | ||
) | ||
|
||
date = Date.parse("2001-02-03") | ||
DailyPacket::Producer.save_to(:s3, date) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.