summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
Diffstat (limited to 'example')
-rw-r--r--example/http-srp.rb42
-rw-r--r--example/models/log.rb31
-rw-r--r--example/models/user.rb25
-rw-r--r--example/views/verify.erb7
4 files changed, 77 insertions, 28 deletions
diff --git a/example/http-srp.rb b/example/http-srp.rb
index c0c9360..4faa149 100644
--- a/example/http-srp.rb
+++ b/example/http-srp.rb
@@ -1,36 +1,11 @@
require 'sinatra'
require 'pp'
-class User
-
- def self.current
- # p "getting #{@current ? @current.login : 'nil'}"
- @current ||= User.new
- end
-
- attr_accessor :login
- attr_accessor :password
- attr_accessor :active
-
- def signup!(params)
- self.login = params.delete('login')
- p "signing up as #{login}"
- self.password = params.delete('password')
- self.active = false
- end
-
- def login!(params)
- self.active = valid_login?(params[:login], params[:password])
- end
-
- def valid_login?(login, password)
- (self.login == login) and (self.password == password)
- end
-end
+require 'models/user'
+require 'models/log'
get '/' do
@user = User.current
- p "visiting / as #{@user.login}"
erb :index
end
@@ -39,6 +14,8 @@ get '/signup' do
end
post '/signup' do
+ Log.clear
+ Log.log(:signup, params)
@user = User.current
@user.signup!(params)
redirect '/'
@@ -49,11 +26,20 @@ get '/login' do
end
post '/login' do
+ Log.log(:login, params)
@user = User.current
- @user.login!(params)
+ if @user.login!(params)
+ Log.log(:response, "Login succeeded")
+ else
+ Log.log(:response, "Login failed")
+ end
redirect '/'
end
+get '/verify' do
+ erb :verify
+end
+
helpers do
def button_link(action, options = {})
action = action.to_s
diff --git a/example/models/log.rb b/example/models/log.rb
new file mode 100644
index 0000000..9e700a8
--- /dev/null
+++ b/example/models/log.rb
@@ -0,0 +1,31 @@
+require 'yaml'
+
+class Log
+
+ def self.current
+ @current ||= Log.new
+ end
+
+ def self.log(*args)
+ self.current.log(*args)
+ end
+
+ def self.clear
+ @current = nil
+ end
+
+ attr_accessor :actions
+
+ def initialize
+ @actions = []
+ end
+
+ def log(action, params)
+ @actions << {action => params.dup}
+ end
+
+ def pretty
+ @actions.to_yaml
+ end
+
+end
diff --git a/example/models/user.rb b/example/models/user.rb
new file mode 100644
index 0000000..f3f86ba
--- /dev/null
+++ b/example/models/user.rb
@@ -0,0 +1,25 @@
+class User
+
+ def self.current
+ # p "getting #{@current ? @current.login : 'nil'}"
+ @current ||= User.new
+ end
+
+ attr_accessor :login
+ attr_accessor :password
+ attr_accessor :active
+
+ def signup!(params)
+ self.login = params.delete('login')
+ self.password = params.delete('password')
+ self.active = false
+ end
+
+ def login!(params)
+ self.active = valid_login?(params[:login], params[:password])
+ end
+
+ def valid_login?(login, password)
+ (self.login == login) and (self.password == password)
+ end
+end
diff --git a/example/views/verify.erb b/example/views/verify.erb
new file mode 100644
index 0000000..4bbe1c6
--- /dev/null
+++ b/example/views/verify.erb
@@ -0,0 +1,7 @@
+<h2>3. Verify</h2>
+<pre>
+ <%= Log.current.pretty %>
+</pre>
+<div class="form-actions">
+ <a href="/" class="btn btn-primary">Back</a>
+</div>