Sha256: d46a0d310c1aeceee8d562b71d829038a2c1a0e9e20aa33767dcf05c0d9752cc

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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
        assert_type context[:config].time_zone, String, :time_zone
      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

1 entries across 1 versions & 1 rubygems

Version Path
html2rss-0.12.0 lib/html2rss/attribute_post_processors/parse_time.rb