Class: Fitting::Report::Response::Macro

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/report/response/macro.rb

Instance Method Summary collapse

Constructor Details

#initialize(tests) ⇒ Macro

Returns a new instance of Macro.



5
6
7
# File 'lib/fitting/report/response/macro.rb', line 5

def initialize(tests)
  @json = responses(tests)
end

Instance Method Details

#find_index(response) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/fitting/report/response/macro.rb', line 59

def find_index(response)
  response['schemas'].size.times do |i|
    if response['fully_validates'][i] == []
      return i
    end
  end
end

#push(key, data, name, location) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/fitting/report/response/macro.rb', line 67

def push(key, data, name, location)
  data[key] = if data[key][name]
    data[key].merge(name => data[key][name] + [location])
  else
    data[key].merge(name => [location])
  end
end

#request_key(request_data) ⇒ Object



75
76
77
# File 'lib/fitting/report/response/macro.rb', line 75

def request_key(request_data)
  "#{request_data['method']} #{request_data['path']}"
end

#response_key(request_data, response_data) ⇒ Object



79
80
81
# File 'lib/fitting/report/response/macro.rb', line 79

def response_key(request_data, response_data)
  "#{request_key(request_data)} #{response_data['status']}"
end

#responses(tests) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fitting/report/response/macro.rb', line 9

def responses(tests)
  data = {
    'not_documented' => {},
    'invalid' => {},
    'valid' => {}
  }
  full_responses = {}

  tests.map do |location, test|
    request = MultiJson.load(test['request'])
    response = MultiJson.load(test['response'])
    if request['schema'].nil?
      data['not_documented'][response_key(request, response)] = [location]
    else
      if response['schemas'].nil?
        data['not_documented'][response_key(request['schema'], response)] = [location]
      else
        responses_documented(location, response['valid'], data, response_key(request['schema'], response), full_responses, response)
      end
    end
  end

  not_cover = {}
  full_responses.map do |response, date|
    date['cases'].map do |one_case|
      if not_cover["#{response} #{one_case}"]
        not_cover["#{response} #{one_case}"] = not_cover["#{response} #{one_case}"] + [date['test']]
      else
        not_cover["#{response} #{one_case}"] = [date['test']]
      end
    end
  end
  data['not_cover_where_either'] = not_cover
  data
end

#responses_documented(location, valid, data, name, full_responses, response) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fitting/report/response/macro.rb', line 45

def responses_documented(location, valid, data, name, full_responses, response)
  if valid
    unless full_responses[name]
      full_responses[name] = {}
      full_responses[name]['cases'] = (0..response['schemas'].size-1).to_a
    end
    full_responses[name]['test'] = location
    full_responses[name]['cases'].delete(find_index(response))
    push('valid', data, "#{name} #{find_index(response)}", location)
  else
    push('invalid', data, name, location)
  end
end

#to_hashObject



83
84
85
# File 'lib/fitting/report/response/macro.rb', line 83

def to_hash
  @json
end