Class: SBSM::AbstractTransHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sbsm/trans_handler.rb

Direct Known Subclasses

TransHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, config_file: nil, handler_uri: nil) ⇒ AbstractTransHandler

arguments

  • name - Used for checking whether a ‘name’.grammar or ‘name’_parser.rb can be loaded

  • config_file - Where the config file can be found. Default to ‘etc/trans_handler.yml’

  • handler_ur - The handler to be invoked. defaults to ‘/index.rbx’



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sbsm/trans_handler.rb', line 44

def initialize(name: nil, config_file: nil, handler_uri: nil)
  @handler_uri = handler_uri ||=  '/index.rbx'
  config_file ||=  'etc/trans_handler.yml'
  @config_file = File.expand_path(config_file)
  @parser_name = name ||= 'uri'
  @parser_method = "_#{name}_parser"
  @grammar_path = File.expand_path("../../data/#{name}.grammar",
    File.dirname(__FILE__))
  @parser_path = File.expand_path("#{name}_parser.rb",
    File.dirname(__FILE__))
end

Instance Attribute Details

#config_fileObject (readonly)

  • config_file - Full path of YAML configuration file for shortcuts



29
30
31
# File 'lib/sbsm/trans_handler.rb', line 29

def config_file
  @config_file
end

#handler_uriObject (readonly)

  • handler_uri - The handler file to be used



31
32
33
# File 'lib/sbsm/trans_handler.rb', line 31

def handler_uri
  @handler_uri
end

#parser_nameObject (readonly)

  • parser_name - Name defaults to ‘uri’



27
28
29
# File 'lib/sbsm/trans_handler.rb', line 27

def parser_name
  @parser_name
end

Instance Method Details

#config(request) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sbsm/trans_handler.rb', line 55

def config(request)
  config = Hash.new { {} }
  begin
    return config unless @config_file && File.exist?(@config_file)
    config.update(YAML.load(File.read(@config_file)))
    config
  rescue StandardError => err
    fmt = 'Unable to load url configuration: %s', @config_file
    fmt = 'Hint: store configuration in a YAML-File'
    config
  end
end

#handle_shortcut(request, config) ⇒ Object

  • request - A Rack::Request with our monkey patch to access :uri

  • config - config values for shortcuts (Hash of Hash)



69
70
71
72
73
74
75
76
# File 'lib/sbsm/trans_handler.rb', line 69

def handle_shortcut(request, config)
  if(params = config['shortcut'][request.path])
    params.each do |key, val|
      request.params[key.to_s] = val ? val : ''
    end
    request.uri = @handler_uri
  end
end

#parse_uri(request) ⇒ Object

The default parser for an URI

arguments

  • request - A Rack::Request with our monkey patch to access :uri



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sbsm/trans_handler.rb', line 109

def parse_uri(request)
	@uri_parser ||= self.uri_parser
	ast = @uri_parser.parse(request.path)
	values = request.params
	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
				request.update_param(key, val)
			}
		end
	}
end

#simple_parse(uri) ⇒ Object

  • uri - uri to be parsed, e.g. /language/flavor/event/key/value/key/value/…

language, flavor and event are added as special keys, else values are inserted as string



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sbsm/trans_handler.rb', line 81

def simple_parse(uri)
  values = {}
  items = uri.split('/')
  items.shift
  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

  • request - A Rack::Request with our monkey patch to access :uri



99
100
101
102
103
104
105
# File 'lib/sbsm/trans_handler.rb', line 99

def simple_parse_uri(request)
			# values = request.params
  simple_parse(request.path).each do |key, value|
      request.env[key.to_s] = value ? value : ''
    request.update_param(key.to_s, CGI.unescape(value.to_s))
  end
end

#translate_uri(request) ⇒ Object

  • request - A Rack::Request with our monkey patch to access :uri



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sbsm/trans_handler.rb', line 131

def translate_uri(request)
	@@empty_check ||= Regexp.new('^/?$')
	@@lang_check ||= Regexp.new('^/[a-z]{2}(/|$)')
    uri = request.path
    request.uri ||= uri
    config = config(request)
    handle_shortcut(request, config)
    case uri
    when @@empty_check
      request.uri = config['redirect']['/'] ||= @handler_uri
    when @@lang_check
      begin
        self.parse_uri(request)
      rescue LoadError # this happens often
        res = simple_parse_uri(request)
      end
      request.uri = @handler_uri
    end
    Apache::DECLINED
end

#uri_parser(grammar_path = @grammar_path, parser_path = @parser_path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sbsm/trans_handler.rb', line 151

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.exist?(oldpath) && File.read(oldpath)==src)
			File.delete(oldpath) if File.exist?(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