sbsm 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +2 -3
- data/Gemfile +1 -10
- data/History.txt +14 -0
- data/README.md +7 -2
- data/Rakefile +29 -2
- data/lib/cgi/drbsession.rb +4 -2
- data/lib/sbsm.rb +31 -0
- data/lib/sbsm/app.rb +112 -0
- data/lib/sbsm/cgi.rb +2 -52
- data/lib/sbsm/drb.rb +2 -1
- data/lib/sbsm/drbserver.rb +20 -16
- data/lib/sbsm/exception.rb +2 -2
- data/lib/sbsm/index.rb +2 -1
- data/lib/sbsm/logger.rb +46 -0
- data/lib/sbsm/lookandfeel.rb +2 -2
- data/lib/sbsm/lookandfeelfactory.rb +2 -2
- data/lib/sbsm/lookandfeelwrapper.rb +2 -1
- data/lib/sbsm/redirector.rb +4 -3
- data/lib/sbsm/session.rb +61 -67
- data/lib/sbsm/state.rb +16 -21
- data/lib/sbsm/time.rb +2 -0
- data/lib/sbsm/trans_handler.rb +82 -49
- data/lib/sbsm/turing.rb +2 -1
- data/lib/sbsm/user.rb +2 -0
- data/lib/sbsm/validator.rb +2 -0
- data/lib/sbsm/version.rb +1 -1
- data/lib/sbsm/viralstate.rb +2 -0
- data/sbsm.gemspec +4 -2
- data/test/simple_sbsm.rb +207 -0
- data/test/suite.rb +5 -2
- data/test/test_application.rb +122 -0
- data/test/test_logger.rb +121 -0
- data/test/test_session.rb +26 -17
- data/test/test_trans_handler.rb +167 -386
- metadata +67 -3
- data/lib/sbsm/request.rb +0 -234
data/lib/sbsm/time.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
+
#--
|
3
4
|
#
|
4
5
|
# State Based Session Management
|
5
6
|
# Copyright (C) 2004 Hannes Wyss
|
@@ -22,6 +23,7 @@
|
|
22
23
|
# hwyss@ywesee.com
|
23
24
|
#
|
24
25
|
# Time -- sbsm -- 20.11.2002 -- hwyss@ywesee.com
|
26
|
+
#++
|
25
27
|
|
26
28
|
class Time
|
27
29
|
def rfc1123
|
data/lib/sbsm/trans_handler.rb
CHANGED
@@ -1,58 +1,87 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
+
#--
|
3
4
|
# SBSM::TransHandler -- sbsm -- 25.01.2012 -- mhatakeyama@ywesee.com
|
4
5
|
# SBSM::TransHandler -- sbsm -- 23.09.2004 -- hwyss@ywesee.com
|
6
|
+
#++
|
5
7
|
|
6
8
|
$USING_STRSCAN = true
|
7
9
|
require 'cgi'
|
8
10
|
require 'singleton'
|
9
11
|
require 'yaml'
|
12
|
+
require 'rack'
|
13
|
+
module Rack
|
14
|
+
class Request
|
15
|
+
attr_accessor :uri # monkey patch for Rack::Request
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
module Apache
|
21
|
+
DECLINED = -1 unless defined?(DECLINED)
|
22
|
+
end
|
10
23
|
|
11
24
|
module SBSM
|
12
25
|
class AbstractTransHandler
|
13
|
-
|
14
|
-
|
26
|
+
# * +parser_name+ - Name defaults to 'uri'
|
27
|
+
attr_reader :parser_name
|
28
|
+
# * +config_file+ - Full path of YAML configuration file for shortcuts
|
29
|
+
attr_reader :config_file
|
30
|
+
# * +handler_uri+ - The handler file to be used
|
31
|
+
attr_reader :handler_uri
|
32
|
+
@@empty_check ||= nil
|
33
|
+
@@lang_check ||= nil
|
34
|
+
@@uri_parser ||= nil
|
15
35
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
36
|
+
#
|
37
|
+
# === arguments
|
38
|
+
#
|
39
|
+
# * +name+ - Used for checking whether a 'name'.grammar or
|
40
|
+
# 'name'_parser.rb can be loaded
|
41
|
+
# * +config_file+ - Where the config file can be found. Default to 'etc/trans_handler.yml'
|
42
|
+
# * +handler_ur+ - The handler to be invoked. defaults to '/index.rbx'
|
43
|
+
#
|
44
|
+
def initialize(name: nil, config_file: nil, handler_uri: nil)
|
45
|
+
@handler_uri = handler_uri ||= '/index.rbx'
|
46
|
+
config_file ||= 'etc/trans_handler.yml'
|
47
|
+
@config_file = File.expand_path(config_file).untaint
|
48
|
+
@parser_name = name ||= 'uri'
|
49
|
+
@parser_method = "_#{name}_parser"
|
50
|
+
@grammar_path = File.expand_path("../../data/#{name}.grammar",
|
51
|
+
File.dirname(__FILE__).untaint)
|
52
|
+
@parser_path = File.expand_path("#{name}_parser.rb",
|
53
|
+
File.dirname(__FILE__).untaint)
|
54
|
+
end
|
28
55
|
def config(request)
|
29
|
-
config = Hash.new { {} }
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
config.update(YAML.load(File.read(path)))
|
56
|
+
config = Hash.new { {} }
|
57
|
+
begin
|
58
|
+
return config unless @config_file && File.exist?(@config_file)
|
59
|
+
config.update(YAML.load(File.read(@config_file)))
|
34
60
|
config
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
fmt = 'Hint: store configuration in a YAML-File at DOCUMENT_ROOT/%s'
|
39
|
-
request.server.log_notice(fmt, CONFIG_PATH)
|
61
|
+
rescue StandardError => err
|
62
|
+
fmt = 'Unable to load url configuration: %s', @config_file
|
63
|
+
fmt = 'Hint: store configuration in a YAML-File'
|
40
64
|
config
|
41
|
-
|
65
|
+
end
|
42
66
|
end
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
67
|
+
# * +request+ - A Rack::Request with our monkey patch to access :uri
|
68
|
+
# * +config+ - config values for shortcuts (Hash of Hash)
|
69
|
+
def handle_shortcut(request, config)
|
70
|
+
if(params = config['shortcut'][request.path])
|
71
|
+
params.each do |key, val|
|
72
|
+
request.params[key.to_s] = val ? val : ''
|
73
|
+
end
|
74
|
+
request.uri = @handler_uri
|
75
|
+
end
|
76
|
+
end
|
77
|
+
# * +uri+ - uri to be parsed, e.g. /language/flavor/event/key/value/key/value/...
|
78
|
+
#
|
79
|
+
# language, flavor and event are added as special keys, else
|
80
|
+
# values are inserted as string
|
51
81
|
def simple_parse(uri)
|
52
|
-
|
82
|
+
values = {}
|
53
83
|
items = uri.split('/')
|
54
84
|
items.shift
|
55
|
-
values = {}
|
56
85
|
lang = items.shift
|
57
86
|
values.store(:language, lang) if lang
|
58
87
|
flavor = items.shift
|
@@ -66,16 +95,21 @@ module SBSM
|
|
66
95
|
end
|
67
96
|
values
|
68
97
|
end
|
98
|
+
# * +request+ - A Rack::Request with our monkey patch to access :uri
|
69
99
|
def simple_parse_uri(request)
|
70
|
-
values = request.
|
71
|
-
simple_parse(request.
|
72
|
-
|
100
|
+
# values = request.params
|
101
|
+
simple_parse(request.path).each do |key, value|
|
102
|
+
request.env[key.to_s] = value ? value : ''
|
103
|
+
request.update_param(key.to_s, CGI.unescape(value.to_s))
|
73
104
|
end
|
74
105
|
end
|
106
|
+
# The default parser for an URI
|
107
|
+
# === arguments
|
108
|
+
# * +request+ - A Rack::Request with our monkey patch to access :uri
|
75
109
|
def parse_uri(request)
|
76
|
-
@uri_parser ||= self.uri_parser
|
77
|
-
ast = @uri_parser.parse(request.
|
78
|
-
values = request.
|
110
|
+
@uri_parser ||= self.uri_parser
|
111
|
+
ast = @uri_parser.parse(request.path)
|
112
|
+
values = request.params
|
79
113
|
ast.children_names.each { |name|
|
80
114
|
case name
|
81
115
|
when'language', 'flavor', 'event', 'zone'
|
@@ -88,27 +122,29 @@ module SBSM
|
|
88
122
|
else
|
89
123
|
''
|
90
124
|
end
|
91
|
-
|
125
|
+
request.update_param(key, val)
|
92
126
|
}
|
93
127
|
end
|
94
128
|
}
|
95
129
|
end
|
130
|
+
# * +request+ - A Rack::Request with our monkey patch to access :uri
|
96
131
|
def translate_uri(request)
|
97
132
|
@@empty_check ||= Regexp.new('^/?$')
|
98
133
|
@@lang_check ||= Regexp.new('^/[a-z]{2}(/|$)')
|
134
|
+
uri = request.path
|
135
|
+
request.uri ||= uri
|
99
136
|
config = config(request)
|
100
137
|
handle_shortcut(request, config)
|
101
|
-
uri = request.uri
|
102
138
|
case uri
|
103
139
|
when @@empty_check
|
104
|
-
request.uri = config['redirect']['/']
|
140
|
+
request.uri = config['redirect']['/'] ||= @handler_uri
|
105
141
|
when @@lang_check
|
106
142
|
begin
|
107
143
|
self.parse_uri(request)
|
108
144
|
rescue LoadError
|
109
|
-
simple_parse_uri(request)
|
145
|
+
res = simple_parse_uri(request)
|
110
146
|
end
|
111
|
-
request.uri =
|
147
|
+
request.uri = @handler_uri
|
112
148
|
end
|
113
149
|
Apache::DECLINED
|
114
150
|
end
|
@@ -130,8 +166,5 @@ module SBSM
|
|
130
166
|
end
|
131
167
|
class TransHandler < AbstractTransHandler
|
132
168
|
include Singleton
|
133
|
-
def initialize
|
134
|
-
super('uri')
|
135
|
-
end
|
136
169
|
end
|
137
170
|
end
|
data/lib/sbsm/turing.rb
CHANGED
data/lib/sbsm/user.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
+
#--
|
3
4
|
#
|
4
5
|
# State Based Session Management
|
5
6
|
# Copyright (C) 2004 Hannes Wyss
|
@@ -22,6 +23,7 @@
|
|
22
23
|
# hwyss@ywesee.com
|
23
24
|
#
|
24
25
|
# User -- sbsm -- 20.11.2002 -- hwyss@ywesee.com
|
26
|
+
#++
|
25
27
|
|
26
28
|
module SBSM
|
27
29
|
class User
|
data/lib/sbsm/validator.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
+
#--
|
3
4
|
#
|
4
5
|
# State Based Session Management
|
5
6
|
# Copyright (C) 2004 Hannes Wyss
|
@@ -22,6 +23,7 @@
|
|
22
23
|
# hwyss@ywesee.com
|
23
24
|
#
|
24
25
|
# Validator -- sbsm -- 15.11.2002 -- hwyss@ywesee.com
|
26
|
+
#++
|
25
27
|
|
26
28
|
require 'digest/md5'
|
27
29
|
require 'mail'
|
data/lib/sbsm/version.rb
CHANGED
data/lib/sbsm/viralstate.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
+
#--
|
3
4
|
#
|
4
5
|
# State Based Session Management
|
5
6
|
# Copyright (C) 2004 Hannes Wyss
|
@@ -22,6 +23,7 @@
|
|
22
23
|
# hwyss@ywesee.com
|
23
24
|
#
|
24
25
|
# ViralState -- sbsm -- 16.04.2004 -- hwyss@ywesee.com
|
26
|
+
#++
|
25
27
|
|
26
28
|
module SBSM
|
27
29
|
module ViralState
|
data/sbsm.gemspec
CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_dependency 'rack'
|
24
24
|
spec.add_dependency 'mail'
|
25
25
|
spec.add_dependency 'hpricot'
|
26
|
+
spec.add_dependency 'chrono_logger'
|
26
27
|
|
27
28
|
spec.add_development_dependency "bundler"
|
28
29
|
spec.add_development_dependency "rake"
|
@@ -33,7 +34,8 @@ Gem::Specification.new do |spec|
|
|
33
34
|
spec.add_development_dependency "simplecov"
|
34
35
|
spec.add_development_dependency "watir"
|
35
36
|
spec.add_development_dependency "watir-webdriver"
|
36
|
-
|
37
|
-
|
37
|
+
spec.add_development_dependency "pry-byebug"
|
38
|
+
spec.add_development_dependency "nokogiri"
|
39
|
+
spec.add_development_dependency "rack-test"
|
38
40
|
end
|
39
41
|
|
data/test/simple_sbsm.rb
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
# Niklaus Giger, November 2016
|
3
|
+
# A simple example on how to use SBSM with webrick
|
4
|
+
require 'drb/drb'
|
5
|
+
require 'sbsm/logger'
|
6
|
+
require 'sbsm/session'
|
7
|
+
require 'sbsm/validator'
|
8
|
+
require 'sbsm/drbserver'
|
9
|
+
require 'sbsm/state'
|
10
|
+
|
11
|
+
SERVER_URI = 'druby://localhost:9876'
|
12
|
+
SERVER_NAME = 'localhost:9878'
|
13
|
+
|
14
|
+
# Some string shared with the unit test
|
15
|
+
HOME_HTML_CONTENT = 'Überall zu Hause' # mit UTF-8!
|
16
|
+
ABOUT_HTML_CONTENT = 'About SBSM: TDD ist great!'
|
17
|
+
FEEDBACK_HTML_CONTENT = 'Give us your feedback about SBSM'
|
18
|
+
CONFIRM_HTML_CONTENT = 'Please confirm your feedback'
|
19
|
+
SENT_HTML_CONTENT = 'Thanks for you feedback! Hope to see you soon'
|
20
|
+
|
21
|
+
|
22
|
+
module Demo
|
23
|
+
class GlobalState < SBSM::State
|
24
|
+
end
|
25
|
+
class Validator < SBSM::Validator
|
26
|
+
EVENTS = %i{
|
27
|
+
home
|
28
|
+
about
|
29
|
+
feedback
|
30
|
+
}
|
31
|
+
STRINGS = %i{
|
32
|
+
anrede
|
33
|
+
name
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
class HomeState < GlobalState
|
38
|
+
EVENTS = %i{
|
39
|
+
home
|
40
|
+
about
|
41
|
+
feedback
|
42
|
+
}
|
43
|
+
@@class_counter = 0
|
44
|
+
def initialize(session, user)
|
45
|
+
SBSM.info "HomeState #{session} DRb.front is #{DRb.front}"
|
46
|
+
@session = session
|
47
|
+
@member_counter = 0
|
48
|
+
super(session, user)
|
49
|
+
end
|
50
|
+
def to_html(cgi)
|
51
|
+
@@class_counter += 1
|
52
|
+
@member_counter += 1
|
53
|
+
SBSM.info "@member_counter now #{@member_counter}"
|
54
|
+
info = ["State is Home" ,
|
55
|
+
"pid is #{Process.pid}",
|
56
|
+
"request_path is #{@request_path}" ,
|
57
|
+
"@member_counter is #{@member_counter}",
|
58
|
+
"@@class_counter is #{@@class_counter}",
|
59
|
+
HOME_HTML_CONTENT,
|
60
|
+
]
|
61
|
+
info.join("\n")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
class AboutState < GlobalState
|
65
|
+
def initialize(session, user)
|
66
|
+
SBSM.info "AboutState #{session}"
|
67
|
+
super(session, user)
|
68
|
+
end
|
69
|
+
def to_html(cgi)
|
70
|
+
'About SBSM: TDD ist great!'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
class FeedbackMail
|
74
|
+
attr_accessor :errors
|
75
|
+
attr_reader :email, :anrede, :name, :vorname, :firma, :adresse, :ort,
|
76
|
+
:telefon, :bestell_diss, :bestell_pedi, :text
|
77
|
+
|
78
|
+
def initialize(session)
|
79
|
+
@errors = []
|
80
|
+
@session = session
|
81
|
+
@email = @session.user_input(:email)
|
82
|
+
@anrede = @session.user_input(:anrede)
|
83
|
+
@name = @session.user_input(:name)
|
84
|
+
@vorname = @session.user_input(:vorname)
|
85
|
+
@firma = @session.user_input(:firma)
|
86
|
+
@adresse = @session.user_input(:adresse)
|
87
|
+
@ort = @session.user_input(:ort)
|
88
|
+
@telefon = @session.user_input(:telefon)
|
89
|
+
@bestell_diss = @session.user_input(:bestell_diss)
|
90
|
+
@bestell_pedi = @session.user_input(:bestell_pedi)
|
91
|
+
@text = @session.user_input(:text)
|
92
|
+
end
|
93
|
+
|
94
|
+
def body
|
95
|
+
width = 25
|
96
|
+
body = []
|
97
|
+
body << 'Email-Adresse:'.ljust(width) + @email
|
98
|
+
body << 'Anrede:'.ljust(width) + @anrede
|
99
|
+
body << 'Name:'.ljust(width) + @name
|
100
|
+
body << 'Vorname:'.ljust(width) + @vorname
|
101
|
+
body << 'Firma:'.ljust(width) + @firma
|
102
|
+
body << 'Adresse:'.ljust(width) + @adresse
|
103
|
+
body << 'Ort:'.ljust(width) + @ort
|
104
|
+
body << 'Telefon:'.ljust(width) + @telefon
|
105
|
+
body << 'Bestellung Dissertion:'.ljust(width) + @bestell_diss
|
106
|
+
body << 'Bestellung Pädiatrie:'.ljust(width) + @bestell_pedi
|
107
|
+
body << 'Ihre Mitteilung:'.ljust(width) + @text
|
108
|
+
body.join("\n")
|
109
|
+
end
|
110
|
+
|
111
|
+
def error?(key)
|
112
|
+
@errors.include?(key)
|
113
|
+
end
|
114
|
+
|
115
|
+
def ready?
|
116
|
+
unless @email
|
117
|
+
false
|
118
|
+
else
|
119
|
+
true
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def do_sendmail
|
124
|
+
smtp = Net::SMTP.new(Steinwies.config.mailer['server'])
|
125
|
+
smtp.start(
|
126
|
+
Steinwies.config.mailer['domain'],
|
127
|
+
Steinwies.config.mailer['user'],
|
128
|
+
Steinwies.config.mailer['pass'],
|
129
|
+
Steinwies.config.mailer['auth']
|
130
|
+
)
|
131
|
+
smtp.ready(@email, Steinwies.config.mailer['to']) { |a|
|
132
|
+
a.write("Content-Type: text/plain; charset='UTF-8'\n")
|
133
|
+
a.write("Subject: Email von Deiner Webseite.\n")
|
134
|
+
a.write("\n")
|
135
|
+
a.write(body)
|
136
|
+
}
|
137
|
+
end
|
138
|
+
end
|
139
|
+
class SentState < GlobalState
|
140
|
+
def to_html(cgi)
|
141
|
+
SENT_HTML_CONTENT
|
142
|
+
end
|
143
|
+
end
|
144
|
+
class ConfirmState < GlobalState
|
145
|
+
DIRECT_EVENT = :confirm
|
146
|
+
|
147
|
+
def sendmail
|
148
|
+
@model.do_sendmail
|
149
|
+
SentState.new(@session, nil)
|
150
|
+
end
|
151
|
+
|
152
|
+
def back
|
153
|
+
KontaktState.new(@session, @model)
|
154
|
+
end
|
155
|
+
def to_html(cgi)
|
156
|
+
CONFIRM_HTML_CONTENT
|
157
|
+
end
|
158
|
+
end
|
159
|
+
class FeedbackState < GlobalState
|
160
|
+
DIRECT_EVENT = :feedback
|
161
|
+
|
162
|
+
def initialize(session, model)
|
163
|
+
SBSM.info "state/feedback.rb #{__LINE__} Steinwies init #{session.class} model #{model.class}"
|
164
|
+
super(session, model)
|
165
|
+
end
|
166
|
+
def to_html(cgi)
|
167
|
+
FEEDBACK_HTML_CONTENT
|
168
|
+
end
|
169
|
+
def confirm
|
170
|
+
mail = FeedbackMail.new(@session)
|
171
|
+
puts "state/feedback.rb #{__LINE__} confirm #{mail.inspect} #{mail.ready?.inspect}"
|
172
|
+
if mail.ready?
|
173
|
+
ConfirmState.new(@session, mail)
|
174
|
+
else
|
175
|
+
puts "Pushed error"
|
176
|
+
mail.errors.push(:email)
|
177
|
+
@model = mail
|
178
|
+
self
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
class GlobalState < SBSM::State
|
183
|
+
GLOBAL_MAP = {
|
184
|
+
:home => Demo::HomeState,
|
185
|
+
:about => Demo::AboutState,
|
186
|
+
:feedback => Demo::FeedbackState,
|
187
|
+
}
|
188
|
+
DIRECT_EVENT = nil
|
189
|
+
# VIEW = ::Demo::Home
|
190
|
+
end
|
191
|
+
class Session < SBSM::Session
|
192
|
+
DEFAULT_STATE = HomeState
|
193
|
+
def initialize(key, app, validator=Validator.new)
|
194
|
+
SBSM.info "Session #{app}"
|
195
|
+
super(key, app, validator)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
class SimpleSBSM < SBSM::App
|
199
|
+
SESSION = Session
|
200
|
+
attr_reader :drb_uri
|
201
|
+
def initialize
|
202
|
+
@drb_uri = SERVER_URI
|
203
|
+
SBSM.info "SimpleSBSM.new"
|
204
|
+
super(:app => self, :validator => Validator.new, :trans_handler => SBSM::TransHandler.instance)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|