Skip to content

Commit

Permalink
Migrate service to associated objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Nov 3, 2024
1 parent 31c63be commit 18ef2bd
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/jobs/one_day_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class OneDayJob < ApplicationJob
def perform
Hook.delete_all
RawHook.delete_all
DailyPacketService.save_to_s3
DailyPacket::Producer.save_to(:s3)
end
end
15 changes: 2 additions & 13 deletions app/models/daily_packet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class DailyPacket < ApplicationRecord

has_object :pdf_view

delegate :pdf_data, to: :pdf_view
has_object :producer
delegate :save_to_disk, :save_to_s3, to: :producer

validates :built_on, presence: true
validates :reading_list_pace, presence: true
Expand All @@ -15,16 +16,4 @@ def built_on_phrase
def reading_list_phrase
"#{reading_list_pace} pages/day"
end

def local_path
"tmp/daily_packet.pdf"
end

def s3_key
"daily-packets/#{built_on.strftime("%Y-%m-%d")}.pdf"
end

def save_locally
pdf_view.save_as(local_path)
end
end
17 changes: 17 additions & 0 deletions app/models/daily_packet/builder.rb
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
25 changes: 25 additions & 0 deletions app/models/daily_packet/producer.rb
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
24 changes: 0 additions & 24 deletions app/models/daily_packet_service.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/tasks/daily_packet.rake
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
26 changes: 26 additions & 0 deletions spec/models/daily_packet/builder_spec.rb
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
36 changes: 36 additions & 0 deletions spec/models/daily_packet/producer_spec.rb
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
30 changes: 0 additions & 30 deletions spec/models/daily_packet_service_spec.rb

This file was deleted.

0 comments on commit 18ef2bd

Please sign in to comment.