fortythree.rb

来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 63 行

RB
63
字号
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  endprivate  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  endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?