Skip to content

Commit

Permalink
Add Feedbin stats section to daily packet
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Nov 3, 2024
1 parent 18ef2bd commit 4b490dd
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 6 deletions.
35 changes: 35 additions & 0 deletions app/clients/feedbin_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class FeedbinApi
def self.generate_client
endpoint_url = Monolithium.config.feedbin_endpoint_url
password = Monolithium.config.feedbin_password
username = Monolithium.config.feedbin_username
return unless endpoint_url && password && username

Faraday.new(url: endpoint_url) do |f|
f.adapter Faraday.default_adapter
f.request :authorization, :basic, username, password
f.request :json
f.response :json
end
end

def self.client
@client ||= generate_client
end

def self.get_unread_entries
return unless client

url = "/v2/unread_entries.json"
response = client.get(url)
response.body
end

def self.get_entry(entry_id)
return unless client

url = "/v2/entries/#{entry_id}.json"
response = client.get(url)
response.body
end
end
10 changes: 10 additions & 0 deletions app/models/daily_packet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ class DailyPacket < ApplicationRecord
delegate :save_to_disk, :save_to_s3, to: :producer

validates :built_on, presence: true
validates :feedbin_oldest_ago, presence: true
validates :feedbin_unread_count, presence: true
validates :reading_list_pace, presence: true

def built_on_phrase
"#{built_on.to_fs}, week #{built_on.cweek}"
end

def feedbin_oldest_phrase
"oldest: #{feedbin_oldest_ago} days ago"
end

def feedbin_unread_phrase
"unread: #{feedbin_unread_count}"
end

def reading_list_phrase
"#{reading_list_pace} pages/day"
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/daily_packet/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ 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?

feedbin_unread_count, feedbin_oldest_ago = FeedbinStats.compute
reading_list = ReadingList.new
warm_fuzzy = WarmFuzzy.random

attributes = {
feedbin_oldest_ago: feedbin_oldest_ago,
feedbin_unread_count: feedbin_unread_count,
reading_list_pace: reading_list.pace,
warm_fuzzy: warm_fuzzy
}
Expand Down
12 changes: 8 additions & 4 deletions app/models/daily_packet/pdf_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ def draw_front_page

move_down 20

font_size(20) do
text "Reading Pace", style: :bold
text daily_packet.reading_list_phrase
end
text "Reading Pace", style: :bold, size: 20
text daily_packet.reading_list_phrase, size: 16

move_down 20

text "Feedbin Stats", style: :bold, size: 20
text daily_packet.feedbin_unread_phrase, size: 16
text daily_packet.feedbin_oldest_phrase, size: 16
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/models/feedbin_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class FeedbinStats
def self.compute
unread_entry_ids = FeedbinApi.get_unread_entries
first_entry_id = unread_entry_ids.min
oldest_entry = FeedbinApi.get_entry(first_entry_id)
oldest_published_on = Date.parse(oldest_entry["published"])
oldest_ago = (Date.today - oldest_published_on).to_i
[unread_entry_ids.count, oldest_ago]
end
end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pHjuU/ByM1jPqsmRZwCRgBfT5j7qznqc4t2j3zY+BzyKmQvWW+X09UNnlQXE0eJJ30g7KmMeTJ6pV7JibA7e2+CzYs+0ip3qnTyOkGNcdIom+V//vf5PoKk1KZ7UoxfV7sslSCOXnlDbjNfYhWYxT8OgHOQhQc/Z2jxeMasogzyxGi8kV4IcQpAH+QXmlqi3QjIsO4K1li2wCClaEQnZBHWSoy2Vz+1AZYo+Ug3LiGia4QRlpXUlJCNdit1rmoAIMfTZmVjsmu86N8PKMlu6zkhy/xOzyLDFglUW2Fy/ydL27JpwR9pAPN+ArkG+EF112m0NShhc9rQof1bvb8HlclxOFmwB0puAJJS/x6YfS9DORhE8h1jRu2ytP4C/gjTpYyPVFnaDp72UUnwkXD9ymIRTSok9pYGfvhqiRYURkMaRCWAcMDiaL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hA/q/IW+CN3+aGu7RCF0SflaWGNPxMPf7MiTsHLGwBsNsKyVgzy4X+KbINLDASVYRZ8oK8PsbXTukjLMr3/YE4AszuqIffuY4qThFWTwcrEmAgiXIF8/H2ANeZaxGLdJ2z57bP5kgU+sdGnWNpQn/4zgbdFz1sve7B/w3oHXn8tQO0gu04pgyi/U1hctifLDpTbJ7Jyya9u6aqPRyTkXsMllvM2Z8+oxFC6AT3Pdlf2dx3ylK3nuhbR2eT12p7M+CBwNLvUXQj0MaHY9UTCT4sw4CxhF17/DTGguNNgH2B5mw6eOWmHbp4tFBpWRXuT0XqgRL+mrqEWaTw0JNATPkLrInKcy3VPxQ7V8pe0D5795w02itVuNG3sKWulPFsD7sh8/Q3RU5G2RMuF17ECIDiYBpJ+uQeIaw==--D5xacH6f7UnVOulj--5Z5Jka+gZTCWSlUZS1g3ig==
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddFeedbinStatsToDailyPackets < ActiveRecord::Migration[7.2]
def change
add_column :daily_packets, :feedbin_unread_count, :integer, null: false, default: 0
add_column :daily_packets, :feedbin_oldest_ago, :integer, null: false, default: 0
end
end
2 changes: 2 additions & 0 deletions spec/factories/daily_packet.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FactoryBot.define do
factory :daily_packet do
built_on { Date.parse("2007-07-07") }
feedbin_oldest_ago { 14 }
feedbin_unread_count { 9 }
reading_list_pace { 7.7 }
warm_fuzzy
end
Expand Down
1 change: 1 addition & 0 deletions spec/models/daily_packet/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

context "without an existing DailyPacket record" do
it "creates and returns a new record" do
expect(FeedbinStats).to receive(:compute).and_return([0, 0])
FactoryBot.create(:warm_fuzzy)

expect do
Expand Down
5 changes: 4 additions & 1 deletion spec/models/daily_packet/pdf_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"Your haircut is adequate.",
"- Wife, 01/01/1970 12:00:00am",
"Reading Pace",
"7.7 pages/day"
"7.7 pages/day",
"Feedbin Stats",
"unread: 9",
"oldest: 14 days ago"
])

expect(page_two_strings).to eq([
Expand Down
4 changes: 4 additions & 0 deletions spec/models/daily_packet/producer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe DailyPacket::Producer do
describe ".save_to" do
before do
allow(FeedbinStats).to receive(:compute).and_return([0, 0])
end

context "with an invalid target" do
it "raises an ArgumentError" do
expect do
Expand Down

0 comments on commit 4b490dd

Please sign in to comment.