Sha256: f7a0c7bde2d150e85e1ec11c5b7b5c74eab6df47332c6719988f0fb76a61c09f

Contents?: true

Size: 1.63 KB

Versions: 13

Compression:

Stored size: 1.63 KB

Contents

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

# Example:
#
# fortythree = Fortythree.new('http://www.43things.com/rss/uber/author?username=<user>')
# fortythree.things.each do |thing|
#   puts "#{thing.title} @ #{thing.link} updated at #{thing.date}"
# end
#
class Fortythree
  include REXML

  attr_accessor :url, :things, :link, :title, :description

  # This object holds given information of a thing
  class ThingItem < Struct.new(:link, :title, :date)
    def to_s; title end
    def date=(value); super(Time.parse(value)) 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 tasks array  def initialize(url, refresh = true)
  def initialize(url, refresh = true)
    self.things  = []
    self.url    = url
    self.refresh if refresh
  end

  # This method lets you refresh the things into the things 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.things        = []
    self.link         = XPath.match(xml, "//channel/link/text()").to_s
    self.title        = XPath.match(xml, "//channel/title/text()").to_s
    self.description  = XPath.match(xml, "//channel/description/text()").to_s

    XPath.each(xml, "//item/") do |elem|

      thing = ThingItem.new
      thing.title = XPath.match(elem, "title/text()").to_s

      thing.date  = XPath.match(elem, "pubDate/text()").to_s
      thing.link  = XPath.match(elem, "link/text()").to_s

      things << thing
    end
  end
end

Version data entries

13 entries across 11 versions & 1 rubygems

Version Path
typo-3.99.0 app/models/aggregations/fortythree.rb
typo-3.99.2 app/models/aggregations/fortythree.rb
typo-3.99.3 app/models/aggregations/fortythree.rb
typo-3.99.1 app/models/aggregations/fortythree.rb
typo-4.0.0 app/models/aggregations/fortythree.rb
typo-3.99.4 app/models/aggregations/fortythree.rb
typo-4.0.1 app/models/aggregations/fortythree.rb
typo-4.0.2 app/models/aggregations/fortythree.rb
typo-4.1.1 vendor/plugins/fortythreeplaces_sidebar/lib/fortythree.rb
typo-4.0.3 app/models/aggregations/fortythree.rb
typo-4.1.1 vendor/plugins/fortythree_sidebar/lib/fortythree.rb
typo-4.1 vendor/plugins/fortythree_sidebar/lib/fortythree.rb
typo-4.1 vendor/plugins/fortythreeplaces_sidebar/lib/fortythree.rb