Class: ApiTester::ResponseEvaluator
- Inherits:
-
Object
- Object
- ApiTester::ResponseEvaluator
- Defined in:
- lib/api-tester/util/response_evaluator.rb
Overview
Class for evaluating responses against what is expected
Instance Attribute Summary collapse
-
#expected_response ⇒ Object
Returns the value of attribute expected_response.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
Instance Method Summary collapse
- #expected_field_array(expected_fields) ⇒ Object
- #expected_fields ⇒ Object
- #expected_fields_hash ⇒ Object
- #extra_fields ⇒ Object
- #field_array(object) ⇒ Object
-
#initialize(actual_body:, expected_fields:) ⇒ ResponseEvaluator
constructor
A new instance of ResponseEvaluator.
- #inner_expected_field(expected_fields:, name:) ⇒ Object
- #missing_fields ⇒ Object
- #response_field_array ⇒ Object
- #seen_fields ⇒ Object
Constructor Details
#initialize(actual_body:, expected_fields:) ⇒ ResponseEvaluator
Returns a new instance of ResponseEvaluator.
9 10 11 12 |
# File 'lib/api-tester/util/response_evaluator.rb', line 9 def initialize(actual_body:, expected_fields:) self.response_body = actual_body self.expected_response = expected_fields end |
Instance Attribute Details
#expected_response ⇒ Object
Returns the value of attribute expected_response.
7 8 9 |
# File 'lib/api-tester/util/response_evaluator.rb', line 7 def expected_response @expected_response end |
#response_body ⇒ Object
Returns the value of attribute response_body.
6 7 8 |
# File 'lib/api-tester/util/response_evaluator.rb', line 6 def response_body @response_body end |
Instance Method Details
#expected_field_array(expected_fields) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/api-tester/util/response_evaluator.rb', line 44 def expected_field_array(expected_fields) fields = {} expected_fields.each do |field| fields[field.name] = field fields = fields.merge inner_expected_field(expected_fields: field.fields, name: field.name) end fields end |
#expected_fields ⇒ Object
18 19 20 |
# File 'lib/api-tester/util/response_evaluator.rb', line 18 def expected_fields expected_fields_hash.keys end |
#expected_fields_hash ⇒ Object
32 33 34 |
# File 'lib/api-tester/util/response_evaluator.rb', line 32 def expected_fields_hash expected_field_array expected_response.body end |
#extra_fields ⇒ Object
36 37 38 |
# File 'lib/api-tester/util/response_evaluator.rb', line 36 def extra_fields response_field_array - expected_fields end |
#field_array(object) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/api-tester/util/response_evaluator.rb', line 65 def field_array(object) fields = [] object.each do |key, value| if key.respond_to?('each') fields.concat(field_array(key)) elsif value == nil || value == 0 || value == false fields << key.to_s fields.concat(field_array(value).map { |i| "#{key}.#{i}" }) elsif value.to_s[0] == '[' && value.to_s[-1] == ']' && !value.to_s.include?('=>') fields << key.to_s elsif value fields << key.to_s fields.concat(field_array(value).map { |i| "#{key}.#{i}" }) else fields.concat(field_array(key)) end end fields rescue NoMethodError fields end |
#inner_expected_field(expected_fields:, name:) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/api-tester/util/response_evaluator.rb', line 54 def inner_expected_field(expected_fields:, name:) fields = {} expected_fields.each do |field| inner_name = "#{name}.#{field.name}" fields[inner_name] = field fields = fields.merge inner_expected_field(expected_fields: field.fields, name: inner_name) end fields end |
#missing_fields ⇒ Object
40 41 42 |
# File 'lib/api-tester/util/response_evaluator.rb', line 40 def missing_fields expected_fields - response_field_array end |
#response_field_array ⇒ Object
14 15 16 |
# File 'lib/api-tester/util/response_evaluator.rb', line 14 def response_field_array field_array response_body end |
#seen_fields ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/api-tester/util/response_evaluator.rb', line 22 def seen_fields seen = [] fields = response_field_array - extra_fields expected = expected_fields_hash fields.each do |field_key| seen << expected[field_key] end seen end |