Skip to content

Commit

Permalink
Introduce GoldContainer data value
Browse files Browse the repository at this point in the history
A `GoldContainer` wraps gold nuggets, and has some metadata about them (origin
and date of packing).

There was a hidden dependency between the explorer and the blog post date
(both using `Helpers::Time.last_friday`), but now their relation is explicit.
  • Loading branch information
MatheusRich committed Oct 6, 2023
1 parent 5d15efd commit ff2efa2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 23 deletions.
6 changes: 3 additions & 3 deletions exe/gold_miner
Expand Up @@ -16,9 +16,9 @@ t0 = Time.now

GoldMiner
.mine_in(channel)
.fmap { |messages|
debug "Found #{messages.size} messages in #{Time.now - t0} seconds."
puts GoldMiner.convert_messages_to_blogpost(channel, messages)
.fmap { |gold_container|
debug "Found #{gold_container.gold_nuggets.size} messages in #{Time.now - t0} seconds."
puts GoldMiner.convert_messages_to_blogpost(channel, container)
}
.or { |error| abort "[ERROR] #{error}" }

Expand Down
8 changes: 4 additions & 4 deletions lib/gold_miner.rb
Expand Up @@ -16,11 +16,11 @@ def mine_in(slack_channel, slack_client: GoldMiner::Slack::Client, env_file: ".e
.fmap { |client| explore(slack_channel, client) }
end

def convert_messages_to_blogpost(channel, gold_nuggets, blog_post_builder: GoldMiner::BlogPost)
def convert_messages_to_blogpost(channel, container, blog_post_builder: GoldMiner::BlogPost)
blog_post_builder.new(
slack_channel: channel,
gold_nuggets: gold_nuggets,
since: Helpers::Time.last_friday,
slack_channel: container.origin,
gold_nuggets: container.gold_nuggets,
since: container.packing_date,
writer: BlogPost::Writer.from_env
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gold_miner/blog_post.rb
Expand Up @@ -70,7 +70,7 @@ def highlight_from(gold_nugget)
end

def time_period
start_date = Helpers::Time.pretty_date(Date.parse(@since))
start_date = Helpers::Time.pretty_date(Date.parse(@since.to_s))

"(#{start_date})"
end
Expand Down
3 changes: 3 additions & 0 deletions lib/gold_miner/gold_container.rb
@@ -0,0 +1,3 @@
module GoldMiner
GoldContainer = Data.define(:gold_nuggets, :origin, :packing_date)
end
8 changes: 5 additions & 3 deletions lib/gold_miner/slack_explorer.rb
Expand Up @@ -8,14 +8,16 @@ def initialize(slack_client, author_config)
end

def explore(channel, start_on:)
start_on = Date.parse(start_on.to_s)
interesting_messages = interesting_messages_query(channel, start_on)

Sync do
til_messages_task = Async { search_messages(interesting_messages.with_topic("TIL")) }
tip_messages_task = Async { search_messages(interesting_messages.with_topic("tip")) }
hand_picked_messages_task = Async { search_messages(interesting_messages.with_reaction("rupee-gold")) }
nuggets = extract_nuggets(til_messages_task, tip_messages_task, hand_picked_messages_task)

merge_messages(til_messages_task, tip_messages_task, hand_picked_messages_task)
GoldContainer.new(gold_nuggets: nuggets, origin: channel, packing_date: start_on)
end
end

Expand All @@ -25,10 +27,10 @@ def interesting_messages_query(channel, start_on)
Slack::MessagesQuery
.new
.on_channel(channel)
.sent_after(Date.parse(start_on.to_s))
.sent_after(start_on)
end

def merge_messages(*search_tasks)
def extract_nuggets(*search_tasks)
search_tasks
.flat_map(&:wait)
.uniq { |message| message[:permalink] }
Expand Down
13 changes: 8 additions & 5 deletions spec/gold_miner/slack_explorer_spec.rb
Expand Up @@ -40,21 +40,24 @@
"user" => user3,
"permalink" => "https:///message-4-permalink.com"
)
slack_channel = "dev"
stub_slack_message_search_requests(slack_client, {
"TIL in:dev after:#{date}" => [msg1, msg2],
"tip in:dev after:#{date}" => [msg2, msg3],
"in:dev after:#{date} has::rupee-gold:" => [msg2, msg4]
"TIL in:#{slack_channel} after:#{date}" => [msg1, msg2],
"tip in:#{slack_channel} after:#{date}" => [msg2, msg3],
"in:#{slack_channel} after:#{date} has::rupee-gold:" => [msg2, msg4]
})

explorer = described_class.new(slack_client, author_config)
messages = explorer.explore("dev", start_on: date)
container = explorer.explore(slack_channel, start_on: date)

expect(messages).to match_array [
expect(container.gold_nuggets).to match_array [
TestFactories.create_gold_nugget(content: msg1.text, author: author1, source: msg1.permalink),
TestFactories.create_gold_nugget(content: msg2.text, author: author2, source: msg2.permalink),
TestFactories.create_gold_nugget(content: msg3.text, author: author2, source: msg3.permalink),
TestFactories.create_gold_nugget(content: msg4.text, author: author3, source: msg4.permalink)
]
expect(container.origin).to eq slack_channel
expect(container.packing_date).to eq Date.parse(date)
end
end

Expand Down
27 changes: 20 additions & 7 deletions spec/gold_miner_spec.rb
Expand Up @@ -23,8 +23,9 @@
slack_client_builder = double(GoldMiner::Slack::Client, build: Success(slack_client))

result = GoldMiner.mine_in("dev", slack_client: slack_client_builder, env_file: "./spec/fixtures/.env.test")
gold_nuggets = result.value!.gold_nuggets

expect(result.value!).to eq [
expect(gold_nuggets).to eq [
TestFactories.create_gold_nugget(
content: slack_message.text,
author: TestFactories.create_author(
Expand All @@ -40,21 +41,27 @@

describe ".convert_messages_to_blogpost" do
it "converts slack messages to a blogpost" do
travel_to "2022-10-07" do
date = "2022-10-07"
travel_to date do
with_env("OPEN_AI_API_TOKEN" => nil) do
channel = "dev"
gold_nuggets = [
TestFactories.create_gold_nugget,
TestFactories.create_gold_nugget
]
blog_post_builder = spy("BlogPost builder")
container = TestFactories.create_gold_container(
gold_nuggets: gold_nuggets,
origin: channel,
packing_date: Date.today
)

GoldMiner.convert_messages_to_blogpost(channel, gold_nuggets, blog_post_builder: blog_post_builder)
GoldMiner.convert_messages_to_blogpost(channel, container, blog_post_builder: blog_post_builder)

expect(blog_post_builder).to have_received(:new).with(
slack_channel: channel,
gold_nuggets: gold_nuggets,
since: "2022-09-30",
since: Date.parse(date),
writer: instance_of(GoldMiner::BlogPost::SimpleWriter)
)
end
Expand All @@ -63,18 +70,24 @@

context "when the OPEN_AI_API_TOKEN is set" do
it "uses the OpenAiWriter" do
travel_to "2022-10-07" do
date = "2022-10-07"
travel_to date do
with_env("OPEN_AI_API_TOKEN" => "test-token") do
channel = "dev"
gold_nuggets = []
blog_post_builder = spy("BlogPost builder")
container = TestFactories.create_gold_container(
gold_nuggets: gold_nuggets,
origin: channel,
packing_date: Date.today
)

GoldMiner.convert_messages_to_blogpost(channel, gold_nuggets, blog_post_builder: blog_post_builder)
GoldMiner.convert_messages_to_blogpost(channel, container, blog_post_builder: blog_post_builder)

expect(blog_post_builder).to have_received(:new).with(
slack_channel: channel,
gold_nuggets: gold_nuggets,
since: "2022-09-30",
since: Date.parse(date),
writer: instance_of(GoldMiner::BlogPost::OpenAiWriter)
)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/support/test_factories.rb
Expand Up @@ -10,6 +10,15 @@ def create_author(overriden_attributes = {})
GoldMiner::Author.new(**default_attributes.merge(overriden_attributes))
end

def create_gold_container(overriden_attributes = {})
default_attributes = {
gold_nuggets: [],
origin: "some-channel",
packing_date: Date.today
}
GoldMiner::GoldContainer.new(**default_attributes.merge(overriden_attributes))
end

def create_gold_nugget(overriden_attributes = {})
default_attributes = {
content: "TIL about the difference betweeen .size and .count in Rails",
Expand Down

0 comments on commit ff2efa2

Please sign in to comment.