Skip to content

Commit

Permalink
Catch errors better.
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-github committed Mar 2, 2009
1 parent 152d413 commit 3121238
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
47 changes: 30 additions & 17 deletions lib/vpim/agent/app.rb
Expand Up @@ -11,6 +11,8 @@
require 'vpim/repo'
require 'vpim/view'

require 'cgi'

configure do
server = Sinatra::Application.server
set :server, Proc.new {
Expand Down Expand Up @@ -44,33 +46,27 @@ def self.atomize(caluri, feeduri)
end
end

def from_is_ics(from)
true
end

def from_is_empty(from)
return from.empty?
not from or from =~ /^\s*$/
end

get '/ics' do
from = env['QUERY_STRING']

url = URI.parse(request.url)
url.query = nil
url_base = url.to_s
url_atom = nil

@url_ics = from # ics from here
@url_atom = nil

if not from.empty?
# Error out if we can't atomize the feed
Vpim::Agent::App.atomize(from, "http://example.com")

if from.empty?
from = nil
url_atom = nil
else
url = URI.parse(request.url)
url.path << "/atom"
url_atom = url.to_s
end

@url_base = url_base # clean input form
@url_ics = from # ics from here
@url_atom = url_atom # atomized ics from here

haml :"ics.haml"
Expand All @@ -91,7 +87,11 @@ def from_is_empty(from)
port = env["SERVER_PORT"].to_i
here = request.url

# if from is empty, redirect to /ics
if from.empty?
url = URI.parse(here)
url.path.sub(/atom$/, "")
redirect here.to_s
end

xml, xmltype = Vpim::Agent::App.atomize(from, here)

Expand All @@ -104,6 +104,11 @@ def from_is_empty(from)
sass :"ics.sass"
end

error do
@url_error = CGI.escapeHTML(env['sinatra.error'].inspect)
haml :"ics.haml"
end

use_in_file_templates!

# FIXME - hard-coded :action paths below, bad!
Expand All @@ -116,6 +121,8 @@ def from_is_empty(from)
a
:color black
:font-style italic
a:hover
:color darkred

#header
:border-bottom 3px solid darkred
Expand Down Expand Up @@ -165,7 +172,7 @@ def from_is_empty(from)
%form#form{:method => 'POST', :action => '/ics'}
%input#url{:name => 'url', :value => params[:url]}
%input#button{:type => 'submit', :value => 'Submit'}
- if @url_ics
- if @url_atom
#subscribe
.text
Subscribe to
Expand All @@ -174,8 +181,14 @@ def from_is_empty(from)
%ul.feed
%a{:href => @url_atom}= @url_atom
(atom feed)
- if @url_error
#error.text
#preamble Sorry, trying to access:
#source= @url_ics
#transition resulted in:
#destination= @url_error
#footer
.text
:textile
Coming from the "Octet Cloud":http://octetcloud.com/ using "vPim":http://vpim.rubyforge.org/, with the aid of cloud monkey "Sam Roberts":mailto:vieuxtech@gmail.com
Coming from the "Octet Cloud":http://octetcloud.com/ using "vPim":http://vpim.rubyforge.org/, piloted by cloud monkey "Sam Roberts":mailto:vieuxtech@gmail.com

9 changes: 8 additions & 1 deletion test/common.rb
Expand Up @@ -19,7 +19,14 @@ def data_on_port(data, port)
Thread.abort_on_exception = true
thrd = Thread.new do
require "webrick"
server = WEBrick::HTTPServer.new( :Port => port )
l = WEBrick::Log.new(STDOUT, WEBrick::BasicLog::WARN)
server = WEBrick::HTTPServer.new(
:Port => port,
:Logger => l,
:AccessLog => l
)
#server.logger.level = WEBrick::BasicLog::WARN
#pp server.logger
begin
server.mount_proc("/") do |req,resp|
resp.body = data.to_str
Expand Down
14 changes: 13 additions & 1 deletion test/test_agent_app.rb
Expand Up @@ -55,8 +55,20 @@ def test_ics_query
end

def test_ics_atom
# TODO redirect to ics
get '/ics/atom'
assert_equal(302, @response.status)
end

=begin
WTF? Sinatra doesn't run it's error catcher in unit test mode?
def test_ics_atom_query_bad
get '/ics/atom?http://example.com'
assert_equal(500, @response.status)
assert(@response.body =~ Regexp.new(
Regexp.quote("error")), @response.body)
end
=end

end

6 changes: 3 additions & 3 deletions test/test_agent_atomize.rb
Expand Up @@ -26,7 +26,7 @@ def test_minimal

assert_equal(feed.entries.size, 1)
assert_equal("http://example.com/feed", feed.id)
assert_equal("http://example.com/calendar - atomized!", feed.title)
assert_equal("http://example.com/calendar", feed.title)
assert(feed.to_xml.to_str)
assert_equal(nil, feed.entries.first.title)
assert_equal(nil, feed.entries.first.content)
Expand All @@ -47,7 +47,7 @@ def test_small

assert_equal(feed.entries.size, 1)
assert_equal("http://example.com/feed", feed.id)
assert_equal("http://example.com/calendar - atomized!", feed.title)
assert_equal("http://example.com/calendar", feed.title)
assert_equal("I am summarized", feed.entries.first.title)
assert_equal("And I am described", feed.entries.first.content)
assert(feed.to_xml.to_str)
Expand All @@ -73,7 +73,7 @@ def test_recurring
puts feed.to_xml
assert_equal(1, feed.entries.size)
assert_equal("http://example.com/feed", feed.id)
assert_equal("http://example.com/calendar - atomized!", feed.title)
assert_equal("http://example.com/calendar", feed.title)
assert_equal("I am summarized", feed.entries.first.title)
assert_equal("And I am described", feed.entries.first.content)
assert(feed.to_xml.to_str)
Expand Down

0 comments on commit 3121238

Please sign in to comment.