Class: TentValidator::Runner::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/tent-validator/runner/cli.rb

Constant Summary collapse

TRANSLATE_KEYS =
{
  :current_value => :actual
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



16
17
# File 'lib/tent-validator/runner/cli.rb', line 16

def initialize(options = {})
end

Class Method Details

.run(options = {}) ⇒ Object



11
12
13
14
# File 'lib/tent-validator/runner/cli.rb', line 11

def self.run(options = {})
  instance = self.new(options)
  instance.run
end

Instance Method Details

#blue(text) ⇒ Object



120
# File 'lib/tent-validator/runner/cli.rb', line 120

def blue(text); color(text, "\e[34m"); end

#color(text, color_code) ⇒ Object



122
123
124
# File 'lib/tent-validator/runner/cli.rb', line 122

def color(text, color_code)
  "#{color_code}#{text}\e[0m"
end

#green(text) ⇒ Object



117
# File 'lib/tent-validator/runner/cli.rb', line 117

def green(text); color(text, "\e[32m"); end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tent-validator/runner/cli.rb', line 41

def print_results(results, parent_names = [])
  results.each_pair do |name, children|
    next if name == :results
    child_results = children[:results]
    child_results.each do |r|
      id = r.object_id.to_s
      valid = result_valid?(r)
      if valid
        next if @valid.index(id)
        @valid << id
        print green(".")
      else
        next if @invalid.index(id)
        @invalid << id
        print red("F")
      end
    end
    print_results(children, parent_names + [name])
  end
end

#red(text) ⇒ Object



118
# File 'lib/tent-validator/runner/cli.rb', line 118

def red(text); color(text, "\e[31m"); end

#result_valid?(result) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/tent-validator/runner/cli.rb', line 101

def result_valid?(result)
  valid = result.as_json[:expected].inject(true) { |memo, (k,v)|
    memo = false if v.has_key?(:valid) && !v[:valid]
    memo
  }
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tent-validator/runner/cli.rb', line 19

def run
  @valid = []
  @invalid = []

  puts "Running Protocol Validations..."
  results = Runner.run do |results|
    print_results(results.as_json)
  end
  print "\n"
  validator_complete(results.as_json)

  print "\n"
  if @invalid.any?
    puts green("#{@valid.uniq.size} validations valid\t") + red("#{@invalid.uniq.size} failed")
  else
    puts green("#{@valid.uniq.size} validations valid\t0 failed")
  end
  print "\n"

  exit(1) unless @valid
end

#translate_keys(hash) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/tent-validator/runner/cli.rb', line 108

def translate_keys(hash)
  TRANSLATE_KEYS.each_pair do |from, to|
    next unless hash.has_key?(from)
    hash[to] = hash[from]
    hash.delete(from)
  end
  hash
end

#validator_complete(results, parent_names = []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tent-validator/runner/cli.rb', line 62

def validator_complete(results, parent_names = [])
  parent_names.reject! { |n| n == "" }
  results.each_pair do |name, children|
    next if name == :results
    child_results = children[:results]
    child_results.each do |r|
      next if result_valid?(r)

      print "\n"
      puts red((parent_names + [name]).join(" "))
      print "\n"

      actual = r.as_json[:actual]
      puts "REQUEST:"
      puts "#{actual[:request_method]} #{actual[:request_url]}"
      puts actual[:request_headers].inject([]) { |m, (k,v)| m << "#{k}: #{v}"; m }.join("\n")
      print "\n"
      puts actual[:request_body]
      print "\n"

      puts "RESPONSE:"
      puts actual[:response_status]
      puts actual[:response_headers].inject([]) { |m, (k,v)| m << "#{k}: #{v}"; m }.join("\n")
      print "\n"
      puts Yajl::Encoder.encode(actual[:response_body])
      print "\n"

      puts "DIFF:"
      r.as_json[:expected].each_pair do |key, val|
        next if val[:valid]

        puts key
        ap val[:diff].map { |i| translate_keys(i.dup) }
      end
    end
    validator_complete(children, parent_names + [name])
  end
end

#yellow(text) ⇒ Object



119
# File 'lib/tent-validator/runner/cli.rb', line 119

def yellow(text); color(text, "\e[33m"); end