lib/weneedfeed/item.rb in weneedfeed-0.8.0 vs lib/weneedfeed/item.rb in weneedfeed-0.9.0

- old
+ new

@@ -1,7 +1,9 @@ # frozen_string_literal: true +require 'mimemagic' + module Weneedfeed class Item class << self # @param [String] string # @return [Time, nil] @@ -14,24 +16,27 @@ end end end # @param [String, nil] description_selector + # @param [String, nil] image_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:, + image_selector:, link_selector:, node:, time_selector:, title_selector:, url: ) @description_selector = description_selector + @image_selector = image_selector @link_selector = link_selector @node = node @time_selector = time_selector @title_selector = title_selector @url = url @@ -40,9 +45,32 @@ # @return [String, nil] def description return unless @description_selector @node.at(@description_selector)&.inner_html + end + + # @return [String, nil] + def image_mime_type + return unless image_url + + ::MimeMagic.by_path(image_url)&.type + end + + # @return [String, nil] + def image_path_or_url + return unless @image_selector + + @node.at(@image_selector)&.[]('src') + end + + def image_url + return unless image_path_or_url + + ::URI.join( + @url, + image_path_or_url + ).to_s end # @return [String] def link ::URI.join(