Class: UniversalAccessLogParser::ElementGroup

Inherits:
Array
  • Object
show all
Defined in:
lib/universal-access-log-parser.rb

Direct Known Subclasses

Integrating, Optional, Surrounding

Defined Under Namespace

Classes: Element, Integrating, Optional, Root, Surrounding

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, &block) ⇒ ElementGroup

Returns a new instance of ElementGroup.



120
121
122
123
# File 'lib/universal-access-log-parser.rb', line 120

def initialize(parent, &block)
	@parent = parent
	instance_eval &block
end

Class Method Details

.parser(name, &block) ⇒ Object

custom parser definition



126
127
128
# File 'lib/universal-access-log-parser.rb', line 126

def self.parser(name, &block)
	define_method(name, &block)
end

Instance Method Details

#date(name, format = '%d/%b/%Y:%H:%M:%S %z', options = {}) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/universal-access-log-parser.rb', line 208

def date(name, format = '%d/%b/%Y:%H:%M:%S %z', options = {})
	regex = Regexp.escape(format).gsub(/%./, '.+').gsub(/\//, '\\/') + '?'
	element(name, regex, options) do |match|
		DateTime.strptime(match, format).new_offset(0).instance_eval do
			Time.utc(year, mon, mday, hour, min, sec + sec_fraction)
		end
	end
end

#date_iis(name, options = {}) ⇒ Object



204
205
206
# File 'lib/universal-access-log-parser.rb', line 204

def date_iis(name, options = {})
	date(name, '%Y-%m-%d %H:%M:%S', options)
end

#date_ncsa(name, options = {}) ⇒ Object



200
201
202
# File 'lib/universal-access-log-parser.rb', line 200

def date_ncsa(name, options = {})
	date(name, '%d/%b/%Y:%H:%M:%S %z', options)
end

#double_quoted(&block) ⇒ Object



196
197
198
# File 'lib/universal-access-log-parser.rb', line 196

def double_quoted(&block)
	surrounded_by('"', '"', &block)
end

#element(name, regexp, options = {}, &parser) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/universal-access-log-parser.rb', line 172

def element(name, regexp, options = {}, &parser)
	nil_on = options[:nil_on]
	process = options[:process]
	if process
		p = lambda{|s| process.call(parser.call(s))}
	else
		p = parser 
	end
	push Element.new(name, regexp, nil_on, &p)
end

#float(name, options = {}) ⇒ Object



227
228
229
# File 'lib/universal-access-log-parser.rb', line 227

def float(name, options = {})
	element(name, '[\+|-]?\d+\.?\d*', options){|s| s.to_f}
end

#integer(name, options = {}) ⇒ Object



223
224
225
# File 'lib/universal-access-log-parser.rb', line 223

def integer(name, options = {})
	element(name, '[\+|-]?\d+', options){|s| s.to_i}
end

#integratin_group(separator, &block) ⇒ Object

core DSL



160
161
162
# File 'lib/universal-access-log-parser.rb', line 160

def integratin_group(separator, &block)
	push ElementGroup::Integrating.new(self, separator, &block)
end

#ip(name, options = {}) ⇒ Object



217
218
219
220
221
# File 'lib/universal-access-log-parser.rb', line 217

def ip(name, options = {})
	greedy = true
	greedy = options[:greedy] if options.member? :greedy
	element(name, ".*#{greedy ? '?' : ''}", options){|s| IP.new(s)}
end

#namesObject



139
140
141
142
143
144
145
146
147
# File 'lib/universal-access-log-parser.rb', line 139

def names
	map do |e|
		if e.kind_of? ElementGroup
			e.names
		else
			e.name
		end
	end.flatten
end

#optional(name, options = {}, &block) ⇒ Object



168
169
170
# File 'lib/universal-access-log-parser.rb', line 168

def optional(name, options = {}, &block)
	push ElementGroup::Optional.new(self, name, options, &block)
end

#parsersObject



149
150
151
152
153
154
155
156
157
# File 'lib/universal-access-log-parser.rb', line 149

def parsers
	map do |e|
		if e.kind_of? ElementGroup
			e.parsers
		else
			e.parser
		end
	end.flatten
end

#regexpObject



135
136
137
# File 'lib/universal-access-log-parser.rb', line 135

def regexp
	map{|e| e.regexp}.join(separator)
end

#separated_with(separator, &block) ⇒ Object

DSL



184
185
186
# File 'lib/universal-access-log-parser.rb', line 184

def separated_with(separator, &block)
	integratin_group(separator, &block)
end

#separatorObject

Raises:



130
131
132
133
# File 'lib/universal-access-log-parser.rb', line 130

def separator
	raise ParsingError, 'Integrating ElementGroup not defined in ElementGroup hierarhy' unless @parent
	@parent.separator
end

#single_quoted(&block) ⇒ Object



192
193
194
# File 'lib/universal-access-log-parser.rb', line 192

def single_quoted(&block)
	surrounded_by("'", "'", &block)
end

#string(name, options = {}) ⇒ Object



231
232
233
234
235
# File 'lib/universal-access-log-parser.rb', line 231

def string(name, options = {})
	greedy = true
	greedy = options[:greedy] if options.member? :greedy
	element(name, ".*#{greedy ? '?' : ''}", options){|s| s}
end

#surrounded_by(left, right, &block) ⇒ Object



188
189
190
# File 'lib/universal-access-log-parser.rb', line 188

def surrounded_by(left, right, &block)
	surrounding_group(left, right, &block)
end

#surrounding_group(left, right, &block) ⇒ Object



164
165
166
# File 'lib/universal-access-log-parser.rb', line 164

def surrounding_group(left, right, &block)
	push ElementGroup::Surrounding.new(self, left, right, &block)
end