class APIReaper::Checker

This is an API response and data structure checker.

Public Class Methods

new(method, url, opts) click to toggle source

Class constructor method

# File lib/apireaper/checker.rb, line 27
def initialize(method, url, opts)
  @requester = APIReaper::Requester.new(method, url, opts)
  @opts = opts
end

Public Instance Methods

check() click to toggle source

Main method to run the check

# File lib/apireaper/checker.rb, line 33
def check
  res = @requester.request
  check_response_code(res.code)
  check_data_structure(res.body)
  File.write(@opts['outfile'], res.body) unless @opts['outfile'].nil?
  puts 'All checks passed' unless @opts['quiet']
end
check_data_structure(data) click to toggle source

Check data structure

# File lib/apireaper/checker.rb, line 42
def check_data_structure(data)
  JSON::Validator.validate!(find_schema_source, data)
  puts 'Response body is valid' unless @opts['quiet']
rescue JSON::Schema::ValidationError => e
  exit_with_error(2, e.message)
end
check_response_code(code) click to toggle source

Check respose code

# File lib/apireaper/checker.rb, line 50
def check_response_code(code)
  if code.to_i.between?(200, 299)
    puts "Response code is valid: #{code}" unless @opts['quiet']
  else
    exit_with_error(2, "Response code is invalid: #{code}")
  end
end
exit_with_error(errno, message) click to toggle source

Exit with the specified errno and message

# File lib/apireaper/checker.rb, line 59
def exit_with_error(errno, message)
  puts message unless @opts['quiet']
  exit errno
end
find_schema_source() click to toggle source

Get the data structure schema source

# File lib/apireaper/checker.rb, line 65
def find_schema_source
  if @opts['schema_file'].nil?
    JSON.parse(
      @opts['schema'].gsub(/:([a-zA-z]+)/, '"\\1"').gsub('=>', ': ')
    )
  else
    @opts['schema_file']
  end
end