Class: CoverallsMulti::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/coveralls-multi/validator.rb

Overview

checks that a payload is valid before sending to Coveralls

Constant Summary collapse

TOP_LEVEL_KEYS =
%w[source_files].freeze
SOURCE_FILE_KEYS =
%w[name source_digest coverage source].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Validator

Returns a new instance of Validator.



10
11
12
# File 'lib/coveralls-multi/validator.rb', line 10

def initialize(payload)
  @payload = payload
end

Instance Attribute Details

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/coveralls-multi/validator.rb', line 6

def payload
  @payload
end

Instance Method Details

#check_required_keys(required_keys, message = 'Missing required key(s)', payload = @payload) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/coveralls-multi/validator.rb', line 39

def check_required_keys(required_keys, message = 'Missing required key(s)', payload = @payload)
  missing_keys = []

  required_keys.each do |key|
    missing_keys.push key unless payload[key]
  end

  error = "#{message} - #{missing_keys.join(', ')}"

  raise error unless missing_keys.empty?
end

#json?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/coveralls-multi/validator.rb', line 18

def json?
  parsed_json = JSON.dump(@payload)
  parsed_json
rescue JSON::UnparserError => e
  puts '[CoverallsMulti] Payload could not be parsed to JSON!'
  raise e
end

#runObject



14
15
16
# File 'lib/coveralls-multi/validator.rb', line 14

def run
  return true if json? && valid_coveralls_payload?
end

#valid_coveralls_payload?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coveralls-multi/validator.rb', line 26

def valid_coveralls_payload?
  raise 'Payload is empty!' if !@payload || @payload.empty?
  raise 'Payload should be a hash!' unless @payload.is_a?(Hash)

  check_required_keys(TOP_LEVEL_KEYS, 'Missing required top-level key')
  @payload['source_files'].each do |src_file|
    check_required_keys(
      SOURCE_FILE_KEYS, 'Missing required source file key(s)', src_file
    )
  end
  @payload
end