Skip to content

Commit

Permalink
Merge pull request #40 from thoughtbot/mr/introduce-smiths
Browse files Browse the repository at this point in the history
Introduce Smiths
  • Loading branch information
MatheusRich authored Oct 20, 2023
2 parents 9702a02 + e7a7af5 commit 784be5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion exe/gold_miner
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ 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, gold_container)

puts GoldMiner.smith_blog_post(gold_container)
}
.or { |error| abort "[ERROR] #{error}" }

Expand Down
9 changes: 2 additions & 7 deletions lib/gold_miner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/gold_miner/blog_post_smith.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 14 additions & 12 deletions spec/gold_miner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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),
Expand Down

0 comments on commit 784be5b

Please sign in to comment.