Skip to content

Instantly share code, notes, and snippets.

@AlexTalker
Created February 4, 2014 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTalker/8796935 to your computer and use it in GitHub Desktop.
Save AlexTalker/8796935 to your computer and use it in GitHub Desktop.
Hmmm, server crashed with error: TypeError at /stream/1000000.25 exception class/object expected file: app.rb location: raise line: 35
#!/usr/bin/env ruby
require 'rubygems'
require 'erb'
require 'sinatra'
enable :sessions
$msgs = Array.new
# added a explot guard
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
error 404 do
redirect('/views/404.html')
end
# class Stream
# def each
# # $msgs.each_with_index { |item, index| yield "#{item[0]}<br/>#{item[1]}<br/>#{item[2]}<br/>---------------------------<br\>"}
# "<a href='/stream/1'>Go to first stream page!</a>"
# end
# end
def page_messages(n)
n = n.to_i
s = String.new
if (n.integer?) and (n>0) and ($msgs.length >= ((n-1) * 10))
s << (erb :message, :locals => {:n => n, :page => :message,:title => "Page #{n}"})
else
raise 404
end
end
get '/' do
# draw a form to input message
erb :notification, :locals => {:title => "Home page."} do
erb :form
end
end
post '/post' do
session[:notification] = (erb :post, :locals => {:time => Time.now}, :layout => false).to_s
redirect to('/')
end
get '/stream/:page' do
page_messages(params[:page])
end
# Browse message in array.
get('/stream/?') { redirect to('/stream/1') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment