Sha256: c7bdf569477760af89ac18b188046c965c6139e11b299de57d62989b3126d87c

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'httpx'

module Rodbot
  class Plugins
    class WordOfTheDay
      class Schedule
        def initialize
          Clockwork.every(1.day, -> { Rodbot.say message }, at: time)
        end

        private

        def time
          Rodbot.config(:plugin, :word_of_the_day, :time) || '12:00'
        end

        def message
          Rodbot::Plugins::WordOfTheDay::Today.new.message
        end

      end

      class Today
        def initialize
          @response = HTTPX.with(timeout: { request_timeout: 60 }).get('https://www.merriam-webster.com/word-of-the-day')
        end

        def message
          if @response.status == 200
            "Word of the day: [#{word}](#{url})"
          else
            "Sorry, there was a problem fetching the word of the day."
          end
        end

        private

        def word
          @response.body.to_s.match(/<h2 class="word-header-txt">(.+?)</).captures.first
        end

        def url
          @response.body.to_s.match(/<meta property="og:url" content="(.+?)"/).captures.first
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rodbot-0.4.2 lib/rodbot/plugins/word_of_the_day/schedule.rb
rodbot-0.4.1 lib/rodbot/plugins/word_of_the_day/schedule.rb
rodbot-0.4.0 lib/rodbot/plugins/word_of_the_day/schedule.rb
rodbot-0.3.4 lib/rodbot/plugins/word_of_the_day/schedule.rb
rodbot-0.3.3 lib/rodbot/plugins/word_of_the_day/schedule.rb
rodbot-0.3.2 lib/rodbot/plugins/word_of_the_day/schedule.rb