require 'open-uri' require 'time' require 'rexml/document' # Example: # # tada = Tada.new('http://.tadalist.com/lists/feed/?token=') # tada.items.each do |task| # puts "#{task.title} @ #{task.link} updated at #{task.date}" # end # class Tada include REXML # This object holds given information of a task # task.title => "Added: My tada task" # task.date => "2005-01-19..." # task.link => "http://..." class Task < Struct.new(:link, :title, :date, :status) def to_s title end def date=(value) super(Time.parse(value)) end end attr_accessor :url, :items # Pass the url to the tada RSS feed you would like to keep tabs on # by default this will request the rss from the tada server right away and # fill the items array def initialize(url, refresh = true) self.items = [] self.url = url self.refresh if refresh end # This method lets you refresh the tasks int the items array # useful if you keep the tada 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 = [] XPath.each(xml, "//item/") do |elem| title = XPath.match(elem, "title/text()").to_s # extract the status from the title status = if title =~ /^(.+): (.+)$/ title = $2 :"#{ $1.downcase }" else :unknown end task = Task.new task.title = title task.status = status task.date = XPath.match(elem, "pubDate/text()").to_s task.link = XPath.match(elem, "link/text()").to_s items << task end end end