Class: Configuration::Handler

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

Defined Under Namespace

Classes: HandlerConfiguration

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



123
124
125
126
127
# File 'lib/httpimagestore/configuration/handler.rb', line 123

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

.parse(configuration, node) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/httpimagestore/configuration/handler.rb', line 133

def self.parse(configuration, node)
	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', "#{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', "<key>|#{default}") do
				->{captures.push(req.GET[name.to_s] || default)}
			end
		when /^\&:(.+)$/# &:foo
			name = $1.to_sym
			Matcher.new([name], 'QueryKey', "<key>") do
				->{req.GET[name.to_s] && captures.push(req.GET[name.to_s])}
			end
		# Literal URI segment matcher
		else # foobar
			Matcher.new([], matcher, nil) do
				Regexp.escape(matcher)
			end
		end
	end

	handler_configuration = HandlerConfiguration.new(configuration, node.name, uri_matchers)

	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



214
215
216
# File 'lib/httpimagestore/configuration/handler.rb', line 214

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

.pre(configuration) ⇒ Object



129
130
131
# File 'lib/httpimagestore/configuration/handler.rb', line 129

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