Sha256: 7a15108653e8b975c1658b81d2fa9c520b28d4d3f53b21c83ac16b35b5135ed4

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'time'
require_relative '../utils'

module Html2rss
  module AttributePostProcessors
    ##
    # Returns the {https://www.w3.org/Protocols/rfc822/ RFC822} representation of a time.
    #
    # Imagine this HTML structure:
    #
    #     <p>Published on <span>2019-07-02</span></p>
    #
    # YAML usage example:
    #
    #    selectors:
    #      description:
    #        selector: span
    #        post_process:
    #          name: 'parse_time'
    #          time_zone: 'Europe/Berlin'
    #
    # Would return:
    #    "Tue, 02 Jul 2019 00:00:00 +0200"
    #
    # It uses `Time.parse`.
    class ParseTime < Base
      def self.validate_args!(value, context)
        assert_type(value, String, :value, context:)
        assert_type(context[:config].time_zone, String, :time_zone, context:)
      end

      ##
      # Converts the provided time string to RFC822 format, taking into account the time_zone.
      #
      # @return [String] RFC822 formatted time
      # @raise [TZInfo::InvalidTimezoneIdentifier] if the configured time zone is invalid
      def get
        time_zone = context[:config].time_zone

        Utils.use_zone(time_zone) { Time.parse(value).rfc822 }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
html2rss-0.15.0 lib/html2rss/attribute_post_processors/parse_time.rb
html2rss-0.14.0 lib/html2rss/attribute_post_processors/parse_time.rb
html2rss-0.13.0 lib/html2rss/attribute_post_processors/parse_time.rb