class Apivore::SwaggerChecker

Constants

PATH_TO_CHECKER_MAP

Attributes

mappings[R]
response[R]
swagger[R]
swagger_path[R]
untested_mappings[R]

Public Class Methods

instance_for(path) click to toggle source
# File lib/apivore/swagger_checker.rb, line 5
def self.instance_for(path)
  PATH_TO_CHECKER_MAP[path] ||= new(path)
end
new(swagger_path) click to toggle source
# File lib/apivore/swagger_checker.rb, line 62
def initialize(swagger_path)
  @swagger_path = swagger_path
  load_swagger_doc!
  validate_swagger!
  setup_mappings!
end

Public Instance Methods

base_path() click to toggle source
# File lib/apivore/swagger_checker.rb, line 48
def base_path
  @swagger.base_path
end
fragment(path, method, code) click to toggle source
# File lib/apivore/swagger_checker.rb, line 31
def fragment(path, method, code)
  path_fragment = mappings[path][method.to_s][code.to_s]
  path_fragment.dup unless path_fragment.nil?
end
has_matching_document_for(path, method, code, body) click to toggle source
# File lib/apivore/swagger_checker.rb, line 25
def has_matching_document_for(path, method, code, body)
  JSON::Validator.fully_validate(
    swagger, body, fragment: fragment(path, method, code)
  )
end
has_method_at_path?(path, method) click to toggle source
# File lib/apivore/swagger_checker.rb, line 13
def has_method_at_path?(path, method)
  mappings[path].has_key?(method)
end
has_path?(path) click to toggle source
# File lib/apivore/swagger_checker.rb, line 9
def has_path?(path)
  mappings.has_key?(path)
end
has_response_code_for_path?(path, method, code) click to toggle source
# File lib/apivore/swagger_checker.rb, line 17
def has_response_code_for_path?(path, method, code)
  mappings[path][method].has_key?(code.to_s)
end
remove_tested_end_point_response(path, method, code) click to toggle source
# File lib/apivore/swagger_checker.rb, line 36
def remove_tested_end_point_response(path, method, code)
  return if untested_mappings[path].nil? ||
    untested_mappings[path][method].nil?
  untested_mappings[path][method].delete(code.to_s)
  if untested_mappings[path][method].size == 0
    untested_mappings[path].delete(method)
    if untested_mappings[path].size == 0
      untested_mappings.delete(path)
    end
  end
end
response=(response) click to toggle source
# File lib/apivore/swagger_checker.rb, line 52
def response=(response)
  @response = response
end
response_codes_for_path(path, method) click to toggle source
# File lib/apivore/swagger_checker.rb, line 21
def response_codes_for_path(path, method)
  mappings[path][method].keys.join(", ")
end

Private Instance Methods

fetch_swagger!() click to toggle source
# File lib/apivore/swagger_checker.rb, line 73
def fetch_swagger!
  session = ActionDispatch::Integration::Session.new(Rails.application)
  begin
    session.get(swagger_path)
  rescue
    fail "Unable to perform GET request for swagger json: #{swagger_path} - #{$!}."
  end
   JSON.parse(session.response.body)
end
load_swagger_doc!() click to toggle source
# File lib/apivore/swagger_checker.rb, line 69
def load_swagger_doc!
  @swagger = Apivore::Swagger.new(fetch_swagger!)
end
setup_mappings!() click to toggle source
# File lib/apivore/swagger_checker.rb, line 92
def setup_mappings!
  @mappings = {}
  @swagger.each_response do |path, method, response_code, fragment|
    @mappings[path] ||= {}
    @mappings[path][method] ||= {}
    raise "duplicate" unless @mappings[path][method][response_code].nil?
    @mappings[path][method][response_code] = fragment
  end

  @untested_mappings = JSON.parse(JSON.generate(@mappings))
end
validate_swagger!() click to toggle source
# File lib/apivore/swagger_checker.rb, line 83
def validate_swagger!
  errors = swagger.validate
  unless errors.empty?
    msg = "The document fails to validate as Swagger #{swagger.version}:\n"
    msg += errors.join("\n")
    fail msg
  end
end