class Weneedfeed::Item

Public Class Methods

new( description_selector:, image_selector:, link_selector:, node:, time_selector:, title_selector:, url: ) click to toggle source

@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

# File lib/weneedfeed/item.rb, line 28
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
end
parse_time(string) click to toggle source

@param [String] string @return [Time, nil]

# File lib/weneedfeed/item.rb, line 11
def parse_time(string)
  ::Time.parse(string)
rescue ArgumentError, RangeError
  begin
    ::Time.strptime(string, '%Y年%m月%d日')
  rescue ArgumentError
  end
end

Public Instance Methods

description() click to toggle source

@return [String, nil]

# File lib/weneedfeed/item.rb, line 47
def description
  return unless @description_selector

  @node.at(@description_selector)&.inner_html
end
image_mime_type() click to toggle source

@return [String, nil]

# File lib/weneedfeed/item.rb, line 54
def image_mime_type
  return unless image_url

  uri = ::URI.parse(image_url)
  ::MimeMagic.by_path(uri.path)&.type
end
image_path_or_url() click to toggle source

@return [String, nil]

# File lib/weneedfeed/item.rb, line 62
def image_path_or_url
  return unless @image_selector

  node = @node.at(@image_selector)
  return unless node

  if node.name == 'img'
    node['src']
  else
    node.content
  end
end
image_url() click to toggle source
# File lib/weneedfeed/item.rb, line 75
def image_url
  return unless image_path_or_url

  ::URI.join(
    @url,
    image_path_or_url
  ).to_s
end
time() click to toggle source

@return [Time, nil]

# File lib/weneedfeed/item.rb, line 104
def time
  return unless @time_selector

  string = time_string
  return unless string

  self.class.parse_time(string)
end
title() click to toggle source

@return [String, nil]

# File lib/weneedfeed/item.rb, line 114
def title
  @node.at(@title_selector)&.inner_text
end

Private Instance Methods

time_node() click to toggle source

@return [Nokogiri::Node, nil]

# File lib/weneedfeed/item.rb, line 121
def time_node
  @node.at(@time_selector)
end
time_string() click to toggle source

@return [String, nil]

# File lib/weneedfeed/item.rb, line 126
def time_string
  node = time_node
  return unless node

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