Class: Resat::Filter
- Inherits:
-
Object
- Object
- Resat::Filter
- Includes:
- Kwalify::Util::HashLike
- Defined in:
- lib/filter.rb
Instance Attribute Summary collapse
-
#failures ⇒ Object
Returns the value of attribute failures.
Instance Method Summary collapse
-
#extract ⇒ Object
Extract elements from response.
- #prepare(variables) ⇒ Object
-
#run(request) ⇒ Object
Run filter on given response.
-
#validate ⇒ Object
Validate response.
Instance Attribute Details
#failures ⇒ Object
Returns the value of attribute failures.
12 13 14 |
# File 'lib/filter.rb', line 12 def failures @failures end |
Instance Method Details
#extract ⇒ Object
Extract elements from response
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/filter.rb', line 72 def extract @extractors.each do |ex| @variables.substitute!(ex.field) if @request.has_response_field?(ex.field, @target) field = @request.get_response_field(ex.field, @target) if ex.pattern @variables.substitute!(ex.pattern) Regexp.new(ex.pattern).match(field) if Regexp.last_match @variables[ex.variable] = Regexp.last_match(1) else Log.warn("Extraction from response #{@target} field '#{ex.field}' ('#{field}') with pattern '#{ex.pattern}' failed.") end else @variables[ex.variable] = field end @variables.mark_for_save(ex.variable) if ex.save @variables.export(ex.variable) if ex.export else Log.warn("Extraction from response #{@target} field '#{ex.field}' failed: field not found.") end end if @extractors end |
#prepare(variables) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/filter.rb', line 14 def prepare(variables) @is_empty ||= false @failures = [] @variables = variables Log.info("Running filter '#{@name}'") end |
#run(request) ⇒ Object
Run filter on given response
22 23 24 25 26 27 |
# File 'lib/filter.rb', line 22 def run(request) @request = request @response = request.response validate extract end |
#validate ⇒ Object
Validate response
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/filter.rb', line 30 def validate unless @response @failures << "No response to validate." return end # 1. Check emptyness if @target == 'header' if @is_empty != (@response.size == 0) @failures << "Response header #{'not ' if @is_empty}empty." end else if @is_empty != (@response.body.nil? || @response.body.size <= 1) @failures << "Response body #{'not ' if @is_empty}empty." end end # 2. Check required fields @required_fields.each do |field| unless @request.has_response_field?(field, target) @failures << "Missing #{target} field '#{field}'." end end if @required_fields # 3. Evaluate validators @validators.each do |v| if @request.has_response_field?(v.field, @target) field = @request.get_response_field(v.field, @target) is_ok = v.is_empty && field.empty? is_ok ||= v.pattern && v.pattern.empty? && field.empty? if v.pattern && !v.pattern.empty? @variables.substitute!(v.pattern) is_ok ||= Regexp.new(v.pattern).match(field) end @failures << "Validator /#{v.pattern} failed on '#{field}' from #{@target} field '#{v.field}'." unless is_ok else @failures << "Missing #{@target} field '#{v.field}'." end end if @validators end |