Class: Timet::DiscordNotifier
- Inherits:
-
Object
- Object
- Timet::DiscordNotifier
- Defined in:
- lib/timet/discord_notifier.rb
Overview
Handles sending notifications to Discord via webhooks for Pomodoro events.
This class provides methods to send structured messages to a Discord channel when a Pomodoro session starts, ends, or a break ends. It relies on a Discord webhook URL configured via the ‘DISCORD_WEBHOOK_URL` environment variable.
Constant Summary collapse
- DISCORD_WEBHOOK_URL =
ENV.fetch('DISCORD_WEBHOOK_URL', nil)
Class Method Summary collapse
-
.pomodoro_ended(duration) ⇒ void
Sends a notification indicating that a Pomodoro work session has ended.
-
.pomodoro_started(duration) ⇒ void
Sends a notification indicating that a Pomodoro work session has started.
-
.send_notification(message_content, embed_data = nil) ⇒ void
Sends a notification to the configured Discord webhook.
Class Method Details
.pomodoro_ended(duration) ⇒ void
This method returns an undefined value.
Sends a notification indicating that a Pomodoro work session has ended.
The notification includes a title, description, color, and fields for duration and the next scheduled event (work session).
Sends a notification indicating that a Pomodoro work session has ended.
The notification includes a title, description, color, and fields for duration and the next scheduled event (work session).
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/timet/discord_notifier.rb', line 84 def self.pomodoro_ended(duration) break_duration = (duration / 5).to_i # Assuming a 1/5th break duration break_duration = 5 if break_duration == 0 # Minimum 5 minute break = { title: 'Pomodoro Session Ended! 🎉', description: "Time for a #{break_duration} minute break!", color: 0xFFA500, # Orange fields: [ { name: 'Duration', value: "#{break_duration} minutes", inline: true }, { name: 'Next Up', value: 'Work Session', inline: true } ], timestamp: Time.now.utc.iso8601 } send_notification('Break time!', ) end |
.pomodoro_started(duration) ⇒ void
This method returns an undefined value.
Sends a notification indicating that a Pomodoro work session has started.
The notification includes a title, description, color, and fields for duration and the next scheduled event (short break).
Sends a notification indicating that a Pomodoro work session has started.
The notification includes a title, description, color, and fields for duration and the next scheduled event (short break).
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/timet/discord_notifier.rb', line 58 def self.pomodoro_started(duration) = { title: 'Pomodoro Session Started! 🍅', description: "Time to focus for #{duration} minutes!", color: 0x00FF00, # Green fields: [ { name: 'Duration', value: "#{duration} minutes", inline: true }, { name: 'Next Up', value: 'Short Break', inline: true } ], timestamp: Time.now.utc.iso8601 } send_notification('Focus time!', ) end |
.send_notification(message_content, embed_data = nil) ⇒ void
This method returns an undefined value.
Sends a notification to the configured Discord webhook.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/timet/discord_notifier.rb', line 20 def self.send_notification(, = nil) return unless DISCORD_WEBHOOK_URL uri = URI.parse(DISCORD_WEBHOOK_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' }) payload = { content: , username: 'Timet Pomodoro', avatar_url: 'https://gravatar.com/avatar/b4921f111e1d481e3f5f35101432bce5' } payload[:embeds] = [] if request.body = payload.to_json response = http.request(request) unless response.is_a?(Net::HTTPSuccess) puts "Failed to send Discord notification: #{response.code} - #{response.body}" end rescue StandardError => e puts "Error sending Discord notification: #{e.message}" end |