require 'sass'
require 'sinatra'
require 'slim'
require 'xednese'
class Seq::Paged
def page(num)
@block.call(num)
end
end
helpers do
def authenticate!
redirect '/login' unless session[:username] && session[:password]
end
def esendex
authenticate!
Esendex.new(session[:username], session[:password])
end
end
use Rack::Session::Cookie, secret: 'hey im a secret'
get '/' do
slim :index, locals: {messages: esendex.messages, accounts: esendex.accounts}
end
post '/send' do
esendex.account(params['reference']).dispatcher.send(params['body'], params['to'])
redirect '/'
end
get '/sent-messages' do
page = params['page'].to_i || 0
redirect '/sent-messages' if page < 0
slim :sent_messages, locals: {
messages: esendex.messages.sent.page(page),
accounts: esendex.accounts,
page: page
}
end
get '/received-messages' do
page = params['page'].to_i || 0
redirect '/received-messages' if page < 0
slim :received_messages, locals: {
messages: esendex.messages.received.page(page),
accounts: esendex.accounts,
page: page
}
end
get '/login' do
slim :login
end
post '/login' do
session[:username] = params['username']
session[:password] = params['password']
redirect '/'
end
get '/logout' do
session[:username] = nil
session[:password] = nil
redirect '/login'
end
get '/styles.css' do
sass :styles
end