Sha256: ebb02169c24df56fd33617aa9c8f31a3e5df32c486830c9193b7aa779235afac

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Weneedfeed
  class Item
    class << self
      # @param [String] string
      # @return [Time, nil]
      def parse_time(string)
        ::Time.parse(string)
      rescue ArgumentError, RangeError
        begin
          ::Time.strptime(string, '%Y年%m月%d日')
        rescue ArgumentError
        end
      end
    end

    # @param [String, nil] description_selector
    # @param [String, nil] link_selector
    # @param [Nokogiri::Node] node
    # @param [String] time_selector
    # @param [String] title_selector
    # @param [String] url
    def initialize(
      description_selector:,
      link_selector:,
      node:,
      time_selector:,
      title_selector:,
      url:
    )
      @description_selector = description_selector
      @link_selector = link_selector
      @node = node
      @time_selector = time_selector
      @title_selector = title_selector
      @url = url
    end

    # @return [String, nil]
    def description
      return unless @description_selector

      @node.at(@description_selector)&.inner_html
    end

    # @return [String]
    def link
      ::URI.join(
        @url,
        link_path_or_url
      ).to_s
    end

    # @return [Nokogiri::Node, nil]
    def link_node
      @node.at(@link_selector)
    end

    # @return [String, nil]
    def link_path_or_url
      node = link_node
      node['href'] || node.inner_text
    end

    # @return [Time, nil]
    def time
      return unless @time_selector

      string = time_string
      return unless string

      self.class.parse_time(string)
    end

    # @return [String, nil]
    def title
      @node.at(@title_selector)&.inner_text
    end

    private

    # @return [Nokogiri::Node, nil]
    def time_node
      @node.at(@time_selector)
    end

    # @return [String, nil]
    def time_string
      node = time_node
      return unless node

      node['datetime'] || node.inner_text
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
weneedfeed-0.8.0 lib/weneedfeed/item.rb
weneedfeed-0.7.2 lib/weneedfeed/item.rb