Class: ApiTester::ResponseEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/api-tester/util/response_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual_response_body, expected_response_fields) ⇒ ResponseEvaluator

Returns a new instance of ResponseEvaluator.



6
7
8
9
# File 'lib/api-tester/util/response_evaluator.rb', line 6

def initialize(actual_response_body, expected_response_fields)
    self.response_body = actual_response_body
    self.expected_response = expected_response_fields
end

Instance Attribute Details

#expected_responseObject

Returns the value of attribute expected_response.



4
5
6
# File 'lib/api-tester/util/response_evaluator.rb', line 4

def expected_response
  @expected_response
end

#response_bodyObject

Returns the value of attribute response_body.



3
4
5
# File 'lib/api-tester/util/response_evaluator.rb', line 3

def response_body
  @response_body
end

Instance Method Details

#expected_field_array(expected_fields) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/api-tester/util/response_evaluator.rb', line 41

def expected_field_array expected_fields
    fields = {}
    expected_fields.each do |field|
        fields[field.name] = field
        fields = fields.merge inner_expected_field(field.fields, field.name)
    end
    fields
end

#expected_fieldsObject



15
16
17
# File 'lib/api-tester/util/response_evaluator.rb', line 15

def expected_fields
    expected_fields_hash.keys
end

#expected_fields_hashObject



29
30
31
# File 'lib/api-tester/util/response_evaluator.rb', line 29

def expected_fields_hash
    expected_field_array self.expected_response.body
end

#extra_fieldsObject



33
34
35
# File 'lib/api-tester/util/response_evaluator.rb', line 33

def extra_fields
    response_field_array - expected_fields
end

#field_array(object) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/api-tester/util/response_evaluator.rb', line 60

def field_array object
    fields = []
    object.each do |key, value|
        if(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



50
51
52
53
54
55
56
57
58
# File 'lib/api-tester/util/response_evaluator.rb', line 50

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(field.fields, inner_name)
    end
    fields
end

#missing_fieldsObject



37
38
39
# File 'lib/api-tester/util/response_evaluator.rb', line 37

def missing_fields
    expected_fields - response_field_array
end

#response_field_arrayObject



11
12
13
# File 'lib/api-tester/util/response_evaluator.rb', line 11

def response_field_array
    field_array self.response_body
end

#seen_fieldsObject



19
20
21
22
23
24
25
26
27
# File 'lib/api-tester/util/response_evaluator.rb', line 19

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