Class: SBSM::AbstractTransHandler
- Inherits:
-
Object
- Object
- SBSM::AbstractTransHandler
show all
- Defined in:
- lib/sbsm/trans_handler.rb
Constant Summary
collapse
- CONFIG_PATH =
'../etc/trans_handler.yml'
- HANDLER_URI =
'/index.rbx'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AbstractTransHandler.
18
19
20
21
22
23
24
25
|
# File 'lib/sbsm/trans_handler.rb', line 18
def initialize(name)
@parser_name = name
@parser_method = "_#{name}_parser"
@grammar_path = File.expand_path("../../data/#{name}.grammar",
File.dirname(__FILE__).untaint)
@parser_path = File.expand_path("#{name}_parser.rb",
File.dirname(__FILE__).untaint)
end
|
Instance Attribute Details
#parser_name ⇒ Object
Returns the value of attribute parser_name.
12
13
14
|
# File 'lib/sbsm/trans_handler.rb', line 12
def parser_name
@parser_name
end
|
Instance Method Details
#config(request) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/sbsm/trans_handler.rb', line 26
def config(request)
config = Hash.new { {} }
begin
path = File.expand_path(CONFIG_PATH, request.server.document_root.untaint)
path.untaint
config.update(YAML.load(File.read(path)))
config
rescue StandardError => err
fmt = 'Unable to load url configuration: %s'
request.server.log_warn(fmt, err.message)
fmt = 'Hint: store configuration in a YAML-File at DOCUMENT_ROOT/%s'
request.server.log_notice(fmt, CONFIG_PATH)
config
end
end
|
#handle_shortcut(request, config) ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/sbsm/trans_handler.rb', line 41
def handle_shortcut(request, config)
if(notes = config['shortcut'][request.uri])
notes.each { |key, val|
request.notes.add(key, val)
}
request.uri = HANDLER_URI
end
end
|
#parse_uri(request) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/sbsm/trans_handler.rb', line 73
def parse_uri(request)
@uri_parser ||= self.uri_parser
ast = @uri_parser.parse(request.uri)
values = request.notes
ast.children_names.each { |name|
case name
when'language', 'flavor', 'event', 'zone'
values.add(name, ast.send(name).value)
when 'variables'
ast.variables.each { |pair|
key = pair.key.value
val = if(pair.children_names.include?('value'))
CGI.unescape(pair.value.value.to_s)
else
''
end
values.add(key, val)
}
end
}
end
|
#simple_parse(uri) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/sbsm/trans_handler.rb', line 49
def simple_parse(uri)
items = uri.split('/')
items.shift
values = {}
lang = items.shift
values.store(:language, lang) if lang
flavor = items.shift
values.store(:flavor, flavor) if flavor
event = items.shift
values.store(:event, event) if event
until items.empty?
key = items.shift
value = items.shift
values.store(key, value)
end
values
end
|
#simple_parse_uri(request) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/sbsm/trans_handler.rb', line 67
def simple_parse_uri(request)
values = request.notes
simple_parse(request.uri).each do |key, value|
values.add(key.to_s, CGI.unescape(value.to_s))
end
end
|
#translate_uri(request) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/sbsm/trans_handler.rb', line 94
def translate_uri(request)
@@empty_check ||= Regexp.new('^/?$')
@@lang_check ||= Regexp.new('^/[a-z]{2}(/|$)')
config = config(request)
handle_shortcut(request, config)
uri = request.uri
case uri
when @@empty_check
request.uri = config['redirect']['/'] || HANDLER_URI
when @@lang_check
begin
self.parse_uri(request)
rescue LoadError
simple_parse_uri(request)
end
request.uri = HANDLER_URI
end
Apache::DECLINED
end
|
#uri_parser(grammar_path = @grammar_path, parser_path = @parser_path) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/sbsm/trans_handler.rb', line 113
def uri_parser(grammar_path=@grammar_path, parser_path=@parser_path)
if(File.exist?(grammar_path))
oldpath = File.expand_path("_" << File.basename(grammar_path),
File.dirname(grammar_path))
src = File.read(grammar_path)
unless(File.exists?(oldpath) && File.read(oldpath)==src)
File.delete(oldpath) if File.exists?(oldpath)
Parse.generate_parser_from_file_to_file(grammar_path,
parser_path, @parser_method, 'SBSM')
File.open(oldpath, 'w') { |f| f << src }
end
end
require parser_path
SBSM.send(@parser_method)
end
|