Method: Beaver::Dam#build

Defined in:
lib/beaver/dam.rb

#build(matchers) ⇒ Object

Parses and checks the validity of the matching options passed to the Dam. XXX Yikes this is long and ugly…



130
131
132
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/beaver/dam.rb', line 130

def build(matchers)
  # Match path
  if matchers[:path].respond_to? :===
    @match_path = matchers[:path]
  else
    raise ArgumentError, "Path must respond to the '===' method; try a String or a Regexp (it's a #{matchers[:path].class.name})"
  end if matchers[:path]

  # Match HTTP referer
  referer = matchers[:referer] || matchers[:referrer]
  if referer.respond_to? :===
    @match_referer = referer
  else
    raise ArgumentError, "Referrer must respond to the '===' method; try a String or a Regexp (it's a #{referer.class.name})"
  end if referer

  # Match request method
  case
    when matchers[:method].is_a?(Symbol) then @match_method_s = matchers[:method].to_s.downcase.to_sym
    when matchers[:method].is_a?(Array) then @match_method_a = matchers[:method].map { |m| m.to_s.downcase.to_sym }
    else raise ArgumentError, "Method must be a Symbol or an Array (it's a #{matchers[:method].class.name})"
  end if matchers[:method]

  # Match Rails controller
  if matchers[:controller].respond_to? :===
    @match_controller = matchers[:controller]
  else
    raise ArgumentError, "Controller must respond to the '===' method; try a String or a Regexp (it's a #{matchers[:controller].class.name})"
  end if matchers[:controller]

  # Match Rails controller action
  if matchers[:action].respond_to? :=== or matchers[:action].is_a? Symbol
    @match_action = matchers[:action]
  else
    raise ArgumentError, "Action must respond to the '===' method or be a Symbol; try a String, Symbol or a Regexp (it's a #{matchers[:action].class.name})"
  end if matchers[:action]

  # Match response status
  case matchers[:status].class.name
    when Fixnum.name, Range.name then @match_status = matchers[:status]
    else raise ArgumentError, "Status must be a Fixnum or a Range (it's a #{matchers[:status].class.name})"
  end if matchers[:status]

  # Match request IP
  if matchers[:ip].respond_to? :===
    @match_ip = matchers[:ip]
  else
    raise ArgumentError, "IP must respond to the '===' method; try a String or a Regexp (it's a #{matchers[:ip].class.name})"
  end if matchers[:ip]

  # Match Rails' response format
  case
    when matchers[:format].is_a?(Symbol) then @match_format_s = matchers[:format].to_s.downcase.to_sym
    when matchers[:format].is_a?(Array) then @match_format_a = matchers[:format].map { |f| f.to_s.downcase.to_sym }
    else raise ArgumentError, "Format must be a Symbol or an Array (it's a #{matchers[:format].class.name})"
  end if matchers[:format]

  # Match Rails' response time (at least)
  case matchers[:longer_than].class.name
    when Fixnum.name then @match_longer = matchers[:longer_than]
    else raise ArgumentError, "longer_than must be a Fixnum (it's a #{matchers[:longer_than].class.name})"
  end if matchers[:longer_than]

  # Match Rails' response time (at most)
  case matchers[:shorter_than].class.name
    when Fixnum.name then @match_shorter = matchers[:shorter_than]
    else raise ArgumentError, "shorter_than must be a Fixnum (it's a #{matchers[:shorter_than].class.name})"
  end if matchers[:shorter_than]

  # Match HTTP response size
  case matchers[:size].class.name
    when Fixnum.name then @match_size = matchers[:size]
    else raise ArgumentError, "size must be a Fixnum (it's a #{matchers[:size].class.name})"
  end if matchers[:size]

  # Match HTTP response size (at most)
  case matchers[:smaller_than].class.name
    when Fixnum.name then @match_size_lt = matchers[:smaller_than]
    else raise ArgumentError, "size must be a Fixnum (it's a #{matchers[:smaller_than].class.name})"
  end if matchers[:smaller_than]

  # Match HTTP response size (at least)
  case matchers[:larger_than].class.name
    when Fixnum.name then @match_size_gt = matchers[:larger_than]
    else raise ArgumentError, "size must be a Fixnum (it's a #{matchers[:larger_than].class.name})"
  end if matchers[:larger_than]

  # Match before a request date
  if matchers[:before].is_a? Time
    @match_before_time = matchers[:before]
  elsif matchers[:before].is_a? Date
    @match_before_date = matchers[:before]
  else
    raise ArgumentError, "before must be a Date or Time (it's a #{matchers[:before].class.name})"
  end if matchers[:before]

  # Match after a request date or datetime
  if matchers[:after].is_a? Time
    @match_after_time = matchers[:after]
  elsif matchers[:after].is_a? Date
    @match_after_date = matchers[:after]
  else
    raise ArgumentError, "after must be a Date or Time (it's a #{matchers[:after].class.name})"
  end if matchers[:after]

  # Match a request date
  if matchers[:on].is_a? Date
    @match_on = matchers[:on]
  else
    raise ArgumentError, "on must be a Date (it's a #{matchers[:on].class.name})"
  end if matchers[:on]

  # Match request URL parameters string
  case matchers[:params_str].class.name
    when Regexp.name then @match_params_str = matchers[:params_str]
    else raise ArgumentError, "Params String must be a Regexp (it's a #{matchers[:params_str].class.name})"
  end if matchers[:params_str]

  # Match request URL parameters Hash
  case matchers[:params].class.name
    when Hash.name then @match_params = matchers[:params]
    else raise ArgumentError, "Params must be a String or a Regexp (it's a #{matchers[:params].class.name})"
  end if matchers[:params]

  # Match Rails request tags
  if matchers[:tagged]
    @match_tags = parse_tag_matchers(matchers[:tagged])
    @deep_tag_match = @match_tags.any? { |t| t.is_a? Array }
  end

  # Match HTTP user agent string
  if matchers[:user_agent].respond_to? :===
    @match_user_agent = matchers[:user_agent]
  else
    raise ArgumentError, "User Agent must respond to the '===' method; try a String or a Regexp (it's a #{matchers[:user_agent].class.name})"
  end if matchers[:user_agent]

  # Match the entire log entry string
  case matchers[:match].class.name
    when Regexp.name then @match_r = matchers[:match]
    else raise ArgumentError, "Match must be a Regexp (it's a #{matchers[:match].class.name})"
  end if matchers[:match]

  self
end