Sha256: 0b01435a1dd642b3f99778939f32150620fd5f481121994e20f2dbab68f3c2d6

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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 {https://ruby-doc.org/stdlib-2.5.3/libdoc/time/rdoc/Time.html#method-c-parse Time.parse}.
    class ParseTime
      ##
      # @param value [String] the time to parse
      # @param env [Item::Context] Context object providing additional environment details
      def initialize(value, env)
        @value = value.to_s
        @time_zone = env[:config].time_zone
      end

      ##
      # Converts the provided time string to RFC822 format, taking into account the configured time zone.
      #
      # @return [String] RFC822 formatted time
      def get
        Utils.use_zone(@time_zone) { Time.parse(@value).rfc822 }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
html2rss-0.11.0 lib/html2rss/attribute_post_processors/parse_time.rb
html2rss-0.10.0 lib/html2rss/attribute_post_processors/parse_time.rb