Sha256: 45ffefcbae9be8ff74c7792b0e8498118d79cc19fa46578e55ce90c640acfacd

Contents?: true

Size: 1.69 KB

Versions: 25

Compression:

Stored size: 1.69 KB

Contents

require 'open-uri'
require 'time'
require 'rexml/document'

class Delicious
  include REXML

  attr_accessor :url, :items, :link, :title, :days

  # This object holds given information of an item
  class DeliciousItem < Struct.new(:link, :title, :description, :description_link, :date)
    def to_s; title end
  end

  # Pass the url to the RSS feed you would like to keep tabs on
  # by default this will request the rss from the server right away and
  # fill the items array
  def initialize(url, refresh = true)
    self.items  = []
    self.url    = url
    self.days   = {}
    self.refresh if refresh
  end

  # This method lets you refresh the items in the items array
  # useful if you keep the object cached in memory and
  def refresh
    open(@url) do |http|
      parse(http.read)
    end
  end

private

  def parse(body)

    xml = Document.new(body)

    self.items        = []
    self.link         = XPath.match(xml, "//channel/link/text()").first.value rescue ""
    self.title        = XPath.match(xml, "//channel/title/text()").first.value rescue ""

    XPath.each(xml, "//item/") do |elem|
      item = DeliciousItem.new
      item.title       = XPath.match(elem, "title/text()").first.value rescue ""
      item.link        = XPath.match(elem, "link/text()").first.value rescue ""
      item.description = XPath.match(elem, "description/text()").first.value rescue ""
      item.date        = Time.mktime(*ParseDate.parsedate(XPath.match(elem, "dc:date/text()").first.value)) rescue Time.now

      item.description_link = item.description
      item.description.gsub!(/<\/?a\b.*?>/, "") # remove all <a> tags
      items << item
    end

    self.items = items.sort_by { |item| item.date }.reverse
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
georgi-shinmun-0.3.1 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.10 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.2 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.3 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.4 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.5 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.6 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.7 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.8 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3.9 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.3 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.4.1 lib/shinmun/aggregations/delicious.rb
georgi-shinmun-0.4 lib/shinmun/aggregations/delicious.rb
shinmun-0.2 lib/shinmun/aggregations/delicious.rb
typo-3.99.0 app/models/aggregations/delicious.rb
typo-3.99.1 app/models/aggregations/delicious.rb
typo-3.99.2 app/models/aggregations/delicious.rb
typo-3.99.3 app/models/aggregations/delicious.rb
typo-4.0.2 app/models/aggregations/delicious.rb
typo-3.99.4 app/models/aggregations/delicious.rb