-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Frédéric de Villamil
committed
Aug 30, 2008
0 parents
commit fcf1d3f
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the upcoming_sidebar plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the upcoming_sidebar plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'UpcomingSidebar' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'sidebar' | ||
require 'upcoming_sidebar' | ||
|
||
UpcomingSidebar.view_root = File.dirname(__FILE__) + '/views' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'open-uri' | ||
require 'rexml/document' | ||
|
||
# Example: | ||
# | ||
# upcoming = Upcoming.new('http://upcoming.org/syndicate/my_events/<your_user_id>') | ||
# upcoming.events.each do |event| | ||
# puts "#{event.title} @ #{event.timespan} #{event.link} : #{event.description}" | ||
# end | ||
# | ||
|
||
class Upcoming | ||
include REXML | ||
|
||
def choose(num) | ||
events.last(num) | ||
end | ||
|
||
attr_accessor :events, :link, :title, :description, :url | ||
|
||
# This object holds given information for an Event | ||
class Event < Struct.new(:link, :title, :description) | ||
def to_s; title end | ||
|
||
def timespan | ||
description.split(':')[0] | ||
end | ||
|
||
def info | ||
description.split(':')[1] | ||
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) | ||
self.events = [] | ||
self.url = url | ||
self.refresh if refresh | ||
end | ||
|
||
# This method lets you refresh the tasks int the tasks 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.events = [] | ||
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| | ||
|
||
event = Event.new | ||
event.title = XPath.match(elem, "title/text()").to_s | ||
event.link = XPath.match(elem, "link/text()").to_s | ||
event.description = XPath.match(elem, "description/text()").to_s | ||
event.inspect | ||
events << event | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class UpcomingSidebar < Sidebar | ||
description 'Events from <a href="http://www.upcoming.org">upcoming.org</a>' | ||
|
||
setting :feed, '', :label => 'Feed URL' | ||
setting :count, 4, :label => 'Items Limit' | ||
|
||
lifetime 6.hours | ||
|
||
def upcoming | ||
@upcoming ||= Upcoming.new(feed) | ||
rescue Exception => e | ||
logger.info e | ||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test/unit' | ||
require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
|
||
class UpcomingSidebarTest < Test::Unit::TestCase | ||
def test_sidebar_is_available | ||
assert Sidebar.available_sidebars.include?(UpcomingSidebar) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% if sidebar.upcoming -%> | ||
<h3>Upcoming Events</h3> | ||
<ul id="upcoming"> | ||
<% for event in sidebar.upcoming.choose(count.to_i) -%> | ||
<li><%= link_to event.title, event.link, {:title => event.description } %> - <%= event.timespan %></li> | ||
<% end -%> | ||
</ul> | ||
<% end -%> |