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



317
318
319
320
321
# File 'lib/httpimagestore/configuration/handler.rb', line 317

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

.parse(configuration, node) ⇒ Object



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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/httpimagestore/configuration/handler.rb', line 327

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.GET[name.to_s] && req.GET[name.to_s] == value && captures.push(req.GET[name.to_s])}
      end
    when /^\&:(.+)\?(.*)$/# &:foo?bar
      name = $1.to_sym
      default = $2
      Matcher.new([name], 'QueryKeyDefault', "#{name}=<key>|#{default}") do
        ->{captures.push(req.GET[name.to_s] || default)}
      end
    when /^\&:(.+)$/# &:foo
      name = $1.to_sym
      Matcher.new([name], 'QueryKey', "#{name}=<key>") do
        ->{req.GET[name.to_s] && captures.push(req.GET[name.to_s])}
      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



423
424
425
# File 'lib/httpimagestore/configuration/handler.rb', line 423

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

.pre(configuration) ⇒ Object



323
324
325
# File 'lib/httpimagestore/configuration/handler.rb', line 323

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