Class: Configuration::Handler

Inherits:
Scope
  • Object
show all
Defined in:
lib/httpimagestore/configuration/handler.rb

Class Method Summary collapse

Methods inherited from Scope

#initialize, node_parsers, #parse, register_node_parser

Constructor Details

This class inherits a constructor from Configuration::Scope

Class Method Details

.match(node) ⇒ Object



278
279
280
281
282
# File 'lib/httpimagestore/configuration/handler.rb', line 278

def self.match(node)
	node.name == 'put' or
	node.name == 'post' or
	node.name == 'get'
end

.parse(configuration, node) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/httpimagestore/configuration/handler.rb', line 288

def self.parse(configuration, node)
	handler_configuration =
		Struct.new(
			:global,
			:http_method,
			:uri_matchers,
			:sources,
			:processors,
			:stores,
			:output
		).new

	handler_configuration.global = configuration
	handler_configuration.http_method = node.name
	handler_configuration.uri_matchers = node.values.map do |matcher|
		case matcher
		# URI matchers
		when %r{^:([^/]+)/(.*)/$} # :foobar/.*/
			name = $1.to_sym
			_regexp = Regexp.new($2)
			regexp = Regexp.new("(#{$2})")
			Matcher.new([name], 'Regexp', _regexp) do
				regexp
			end
		when %r{^/(.*)/$} # /.*/
			regexp = $1
			_regexp = Regexp.new($1)
			names = Regexp.new($1).names.map{|n| n.to_sym}
			Matcher.new(names, 'Regexp', _regexp) do
				-> {
					matchdata = env["PATH_INFO"].match(/\A\/(?<_match_>#{regexp})(?<_tail_>(?:\/|\z))/)

					next false unless matchdata

					path, *vars = matchdata.captures

					env["SCRIPT_NAME"] += "/#{path}"
					env["PATH_INFO"] = "#{vars.pop}#{matchdata.post_match}"

					captures.push(*vars)
				}
			end
		when /^:(.+)\?(.*)$/ # :foo?bar
			name = $1.to_sym
			default = $2
			Matcher.new([name], 'SegmentDefault', "<segment>|#{default}") do
				->{match(name) || captures.push(default)}
			end
		when /^:(.+)$/ # :foobar
			name = $1.to_sym
			Matcher.new([name], 'Segment', '<segment>') do
				name
			end
		# Query string matchers
		when /^\&([^=]+)=(.+)$/# ?foo=bar
			name = $1.to_sym
			value = $2
			Matcher.new([name], 'QueryKeyValue', "#{name}=#{value}") do
				->{req[name] && req[name] == value && captures.push(req[name])}
			end
		when /^\&:(.+)\?(.*)$/# &:foo?bar
			name = $1.to_sym
			default = $2
			Matcher.new([name], 'QueryKeyDefault', "#{name}=<key>|#{default}") do
				->{captures.push(req[name] || default)}
			end
		when /^\&:(.+)$/# &:foo
			name = $1.to_sym
			Matcher.new([name], 'QueryKey', "#{name}=<key>") do
				->{req[name] && captures.push(req[name])}
			end
		# Literal URI segment matcher
		else # foobar
			Matcher.new([], "Literal", matcher) do
				Regexp.escape(matcher)
			end
		end
	end
	handler_configuration.sources = []
	handler_configuration.processors = []
	handler_configuration.stores = []
	handler_configuration.output = nil

	node.grab_attributes

	if handler_configuration.http_method != 'get'
		handler_configuration.sources << InputSource.new
	end

	configuration.handlers << handler_configuration

	self.new(handler_configuration).parse(node)

	handler_configuration.output = OutputOK.new unless handler_configuration.output
end

.post(configuration) ⇒ Object



384
385
386
# File 'lib/httpimagestore/configuration/handler.rb', line 384

def self.post(configuration)
	log.warn 'no handlers configured' if configuration.handlers.empty?
end

.pre(configuration) ⇒ Object



284
285
286
# File 'lib/httpimagestore/configuration/handler.rb', line 284

def self.pre(configuration)
	configuration.handlers ||= []
end