class RSS2Mail::RSS::Item

Public Class Methods

new(item) click to toggle source
# File lib/rss2mail/rss.rb, line 97
def initialize(item)
  @item = item
end

Public Instance Methods

author() click to toggle source
# File lib/rss2mail/rss.rb, line 121
def author
  @author ||= value_for({ author: %w[contributor dc_creator] }, %w[name content])
end
body(tag = nil, encoding = nil) click to toggle source
# File lib/rss2mail/rss.rb, line 125
def body(tag = nil, encoding = nil)
  @body ||= get_body(tag, encoding)
end
date() click to toggle source
# File lib/rss2mail/rss.rb, line 115
def date
  @date ||= value_for({ date: %w[pubDate updated dc_date] }, :content) { |field, value|
    field == 'updated' && value.respond_to?(:content) ? Time.at(value.content.to_i) : value
  }
end
description(unescape_html = false) click to toggle source
# File lib/rss2mail/rss.rb, line 111
def description(unescape_html = false)
  @description ||= get_description(unescape_html)
end
subject() click to toggle source
# File lib/rss2mail/rss.rb, line 129
def subject
  @subject ||= title ? clean_subject(title.dup) : 'NO TITLE'
end
title() click to toggle source
# File lib/rss2mail/rss.rb, line 101
def title
  @title ||= value_for(:title, :content)
end

Private Instance Methods

clean_subject(str) click to toggle source
# File lib/rss2mail/rss.rb, line 196
def clean_subject(str)
  str.replace_diacritics!
  str.gsub!(SUB_RE, SUB)
  str.to_ascii
end
extract_body(expr, attribute = nil) click to toggle source
# File lib/rss2mail/rss.rb, line 191
def extract_body(expr, attribute = nil)
  load_feed(link) { |doc| elem = doc.at(expr)
    attribute ? elem[attribute] : elem.to_s }
end
get_body(tag, encoding) click to toggle source
# File lib/rss2mail/rss.rb, line 174
def get_body(tag, encoding)
  body = case tag
    when nil    then # nothing
    when true   then open_feed(link).read
    when String then extract_body(tag)
    when Array  then extract_body(*tag)
    else raise ArgumentError, "don't know how to handle tag of type #{tag.class}"
  end or return

  body.gsub!(/<\/?(.*?)>/) { |m| m if KEEP.include?($1.split.first.downcase) }
  body.gsub!(/<a\s+href=['"](?!http:).*?>(.*?)<\/a>/mi, '\1')

  body.encode!(encoding) if encoding
  body
rescue OpenURI::HTTPError, EOFError, SocketError
end
get_description(unescape_html) click to toggle source
# File lib/rss2mail/rss.rb, line 163
def get_description(unescape_html)
  description = value_for({ description: %w[summary content] }, :content)

  if description && unescape_html
    description.gsub!(/&lt;/, '<')
    description.gsub!(/&gt;/, '>')
  end

  description
end
get_value_for(fields, &block) click to toggle source
# File lib/rss2mail/rss.rb, line 148
def get_value_for(fields, &block)
  fields = fields.is_a?(Hash) ? fields.to_a.flatten : [*fields]

  fields.each { |field|
    begin
      value = @item.send(field)
      value = block[field, value] if block
      return value if value
    rescue NoMethodError
    end
  }

  nil
end
value_for(field, methods = nil, &block) click to toggle source
# File lib/rss2mail/rss.rb, line 135
def value_for(field, methods = nil, &block)
  value = get_value_for(field, &block)

  if methods
    [*methods].each { |method|
      break unless value.respond_to?(method)
      value = value.send(method)
    }
  end

  value.respond_to?(:strip) ? value.strip : value
end