Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, the `GoldMiner` module (now a class) had methods for each part of the mining process: exploration, smithing, and distribution. The orchestration of when and how these methods were called was done in the `exe/gold_miner` script. This wasn't ideal since there's no way to guarantee the order in which the methods are called, and a script is arguably not the best place to put this logic. Furthermore, that arrangement wasn't flexible, as there was no way to specify different explorers, smiths, or distributors. This commit rewrites the `GoldMiner` class to have an `initialize` method that receives the explorer, smith, and distributor as arguments. It also adds a `#mine` method that orchestrates the mining process.
- Loading branch information
1 parent
d9cf295
commit 9f6301d
Showing
5 changed files
with
73 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dotenv" | ||
require "dry/monads" | ||
require "zeitwerk" | ||
require "openai" | ||
|
||
Zeitwerk::Loader.for_gem.setup | ||
|
||
class GoldMiner | ||
class << self | ||
def mine_in(slack_channel, slack_client: GoldMiner::Slack::Client, env_file: ".env") | ||
Dotenv.load!(env_file) | ||
include Dry::Monads[:result] | ||
|
||
prepare(slack_client) | ||
.fmap { |client| explore(slack_channel, client) } | ||
end | ||
def initialize(explorer:, smith:, distributor:, env_file: ".env") | ||
@explorer = explorer | ||
@smith = smith | ||
@distributor = distributor | ||
@env_file = env_file | ||
end | ||
|
||
def smith_blog_post(gold_container, ...) | ||
BlogPostSmith.new(...).smith(gold_container) | ||
end | ||
def mine(location, start_on:) | ||
explore(location, start_on:) | ||
.bind { |gold_container| smith(gold_container) } | ||
.bind { |blog_post| distribute(blog_post) } | ||
end | ||
|
||
def distribute(blog_post) | ||
TerminalDistributor.new.distribute(blog_post) | ||
end | ||
private | ||
|
||
private | ||
def explore(location, start_on:) | ||
Success(@explorer.explore(location, start_on:)) | ||
end | ||
|
||
def prepare(slack_client) | ||
slack_client.build(api_token: ENV["SLACK_API_TOKEN"]) | ||
end | ||
def smith(gold_container) | ||
Success(@smith.smith(gold_container)) | ||
end | ||
|
||
def explore(slack_channel, slack_client) | ||
SlackExplorer | ||
.new(slack_client, AuthorConfig.default) | ||
.explore(slack_channel, start_on: Helpers::Time.last_friday) | ||
end | ||
def distribute(blog_post) | ||
Success(@distributor.distribute(blog_post)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dry/monads" | ||
require "slack-ruby-client" | ||
|
||
class GoldMiner | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters