-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite GoldMiner
main entrypoint
#43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,26 +3,32 @@ | |||||||||||
$LOAD_PATH.unshift("#{__dir__}/../lib") | ||||||||||||
|
||||||||||||
require "gold_miner" | ||||||||||||
require "dotenv" | ||||||||||||
|
||||||||||||
def debug(msg) | ||||||||||||
return if ENV["DEBUG"].nil? | ||||||||||||
def load_env_file | ||||||||||||
Dotenv.load! | ||||||||||||
Dry::Monads::Success() | ||||||||||||
rescue Errno::ENOENT | ||||||||||||
Dry::Monads::Failure("Could not load env file #{env_file.inspect}") | ||||||||||||
end | ||||||||||||
|
||||||||||||
puts "[DEBUG] #{msg}" | ||||||||||||
def prepare | ||||||||||||
load_env_file | ||||||||||||
.bind { GoldMiner::Slack::Client.build(api_token: ENV["SLACK_API_TOKEN"]) } | ||||||||||||
.fmap { |slack_client| | ||||||||||||
GoldMiner::SlackExplorer.new(slack_client, GoldMiner::AuthorConfig.default) | ||||||||||||
} | ||||||||||||
Comment on lines
+17
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could have a
Suggested change
|
||||||||||||
end | ||||||||||||
|
||||||||||||
channel = ARGV.first || "dev" | ||||||||||||
|
||||||||||||
t0 = Time.now | ||||||||||||
|
||||||||||||
GoldMiner | ||||||||||||
.mine_in(channel) | ||||||||||||
.fmap { |gold_container| | ||||||||||||
debug "Found #{gold_container.gold_nuggets.size} messages in #{Time.now - t0} seconds." | ||||||||||||
|
||||||||||||
blog_post = GoldMiner.smith_blog_post(gold_container) | ||||||||||||
GoldMiner.distribute(blog_post) | ||||||||||||
} | ||||||||||||
prepare.bind { |slack_explorer| | ||||||||||||
GoldMiner | ||||||||||||
.new( | ||||||||||||
explorer: slack_explorer, | ||||||||||||
smith: GoldMiner::BlogPostSmith.new, | ||||||||||||
distributor: GoldMiner::TerminalDistributor.new | ||||||||||||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the previous refactor PRs had this in mind. Now that we can swap parts of the process and produce new behaviors. When we have a proper CLI, this will be handy. |
||||||||||||
) | ||||||||||||
.mine(channel, start_on: GoldMiner::Helpers::Time.last_friday) | ||||||||||||
} | ||||||||||||
.or { |error| abort "[ERROR] #{error}" } | ||||||||||||
|
||||||||||||
puts | ||||||||||||
debug "Done in #{Time.now - t0} seconds." |
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 |
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A sub-goal of mine with this rewrite was to remove logic from this file. While I think the current approach is much cleaner than before, this method is still something I want to improve later.