From 6be363fb3dd4f2fa70d659ac8378c1c07fecfd3c Mon Sep 17 00:00:00 2001 From: Matheus Richard Date: Fri, 6 Oct 2023 22:24:15 -0300 Subject: [PATCH] Introduce Smiths Smiths are objects that take a `GoldContainer` and build something with it. We currently have a `BlogPostSmith`, but we could have a `EmailSmith`, `HtmlSmith`, `PdfSmith`, etc. --- exe/gold_miner | 2 +- lib/gold_miner.rb | 9 ++------- lib/gold_miner/blog_post_smith.rb | 17 +++++++++++++++++ spec/gold_miner_spec.rb | 26 ++++++++++++++------------ 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 lib/gold_miner/blog_post_smith.rb diff --git a/exe/gold_miner b/exe/gold_miner index 09849e2..395f60c 100755 --- a/exe/gold_miner +++ b/exe/gold_miner @@ -18,7 +18,7 @@ GoldMiner .mine_in(channel) .fmap { |gold_container| debug "Found #{gold_container.gold_nuggets.size} messages in #{Time.now - t0} seconds." - puts GoldMiner.convert_messages_to_blogpost(channel, container) + puts GoldMiner.smith_blog_post(gold_container) } .or { |error| abort "[ERROR] #{error}" } diff --git a/lib/gold_miner.rb b/lib/gold_miner.rb index 4a47b75..b861346 100644 --- a/lib/gold_miner.rb +++ b/lib/gold_miner.rb @@ -16,13 +16,8 @@ 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, container, blog_post_builder: GoldMiner::BlogPost) - blog_post_builder.new( - slack_channel: container.origin, - gold_nuggets: container.gold_nuggets, - since: container.packing_date, - writer: BlogPost::Writer.from_env - ) + def smith_blog_post(gold_container, ...) + BlogPostSmith.new(...).smith(gold_container) end private diff --git a/lib/gold_miner/blog_post_smith.rb b/lib/gold_miner/blog_post_smith.rb new file mode 100644 index 0000000..81195f4 --- /dev/null +++ b/lib/gold_miner/blog_post_smith.rb @@ -0,0 +1,17 @@ +module GoldMiner + class BlogPostSmith + def initialize(blog_post_class: BlogPost, blog_post_writer: BlogPost::Writer.from_env) + @blog_post_class = blog_post_class + @blog_post_writer = blog_post_writer + end + + def smith(gold_container) + @blog_post_class.new( + slack_channel: gold_container.origin, + gold_nuggets: gold_container.gold_nuggets, + since: gold_container.packing_date, + writer: @blog_post_writer + ) + end + end +end diff --git a/spec/gold_miner_spec.rb b/spec/gold_miner_spec.rb index 791b343..71112d9 100644 --- a/spec/gold_miner_spec.rb +++ b/spec/gold_miner_spec.rb @@ -39,8 +39,8 @@ end end - describe ".convert_messages_to_blogpost" do - it "converts slack messages to a blogpost" do + describe ".smith_blog_post" do + it "creates a blog post from a gold container" do date = "2022-10-07" travel_to date do with_env("OPEN_AI_API_TOKEN" => nil) do @@ -49,16 +49,15 @@ TestFactories.create_gold_nugget, TestFactories.create_gold_nugget ] - blog_post_builder = spy("BlogPost builder") + blog_post_class = spy("BlogPost class") container = TestFactories.create_gold_container( gold_nuggets: gold_nuggets, origin: channel, packing_date: Date.today ) + GoldMiner.smith_blog_post(container, blog_post_class:) - GoldMiner.convert_messages_to_blogpost(channel, container, blog_post_builder: blog_post_builder) - - expect(blog_post_builder).to have_received(:new).with( + expect(blog_post_class).to have_received(:new).with( slack_channel: channel, gold_nuggets: gold_nuggets, since: Date.parse(date), @@ -71,20 +70,23 @@ context "when the OPEN_AI_API_TOKEN is set" do it "uses the OpenAiWriter" do date = "2022-10-07" + token = "test-token" travel_to date do - with_env("OPEN_AI_API_TOKEN" => "test-token") do + with_env("OPEN_AI_API_TOKEN" => token) do channel = "dev" - gold_nuggets = [] - blog_post_builder = spy("BlogPost builder") + gold_nuggets = [ + TestFactories.create_gold_nugget, + TestFactories.create_gold_nugget + ] + blog_post_class = spy("BlogPost class") container = TestFactories.create_gold_container( gold_nuggets: gold_nuggets, origin: channel, packing_date: Date.today ) + GoldMiner.smith_blog_post(container, blog_post_class:) - GoldMiner.convert_messages_to_blogpost(channel, container, blog_post_builder: blog_post_builder) - - expect(blog_post_builder).to have_received(:new).with( + expect(blog_post_class).to have_received(:new).with( slack_channel: channel, gold_nuggets: gold_nuggets, since: Date.parse(date),