class MiniApivore::SwaggerChecker

Constants

PATH_TO_CHECKER_MAP

Attributes

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

Public Class Methods

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

Public Instance Methods

base_path() click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 50
def base_path
  @swagger.base_path
end
fragment(path, verb, code) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 33
def fragment(path, verb, code)
  path_fragment = mappings[path][verb.to_s][code.to_s]
  path_fragment.dup unless path_fragment.nil?
end
has_matching_document_for(path, verb, code, body) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 27
def has_matching_document_for(path, verb, code, body)
  JSON::Validator.fully_validate(
    swagger, body, fragment: fragment(path, verb, code)
  )
end
has_method_at_path?(path, verb) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 15
def has_method_at_path?(path, verb)
  mappings[path].has_key?(verb)
end
has_path?(path) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 11
def has_path?(path)
  mappings.has_key?(path)
end
has_response_code_for_path?(path, verb, code) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 19
def has_response_code_for_path?(path, verb, code)
  mappings[path][verb].has_key?(code.to_s)
end
remove_tested_end_point_response(path, verb, code) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 38
def remove_tested_end_point_response(path, verb, code)
  return if untested_mappings[path].nil? ||
    untested_mappings[path][verb].nil?
  untested_mappings[path][verb].delete(code.to_s)
  if untested_mappings[path][verb].size == 0
    untested_mappings[path].delete(verb)
    if untested_mappings[path].size == 0
      untested_mappings.delete(path)
    end
  end
end
response=(response) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 54
def response=(response)
  @response = response
end
response_codes_for_path(path, verb) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 23
def response_codes_for_path(path, verb)
  mappings[path][verb].keys.join(", ")
end
untested_mappings() click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 60
def untested_mappings; @untested_mappings end
untested_mappings=( other ) click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 61
def untested_mappings=( other ); @untested_mappings = other end

Private Instance Methods

fetch_swagger!() click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 80
def fetch_swagger!
  return @schema unless @schema.empty?
  
  if File.exist?( swagger_path )
    JSON.parse( File.read(swagger_path) )
  else
    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
end
load_swagger_doc!() click to toggle source

сюда можно поставить замену для загрузки из файла данных, а не из рельс.

# File lib/mini_apivore/swagger_checker.rb, line 76
def load_swagger_doc!
  @swagger = MiniApivore::Swagger.new(fetch_swagger!)
end
setup_mappings!() click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 105
def setup_mappings!
  @mappings = {}
  @swagger.each_response do |path, verb, response_code, fragment|
    @mappings[path] ||= {}
    @mappings[path][verb] ||= {}
    raise "duplicate" unless @mappings[path][verb][response_code].nil?
    @mappings[path][verb][response_code] = fragment
  end

  self.untested_mappings = JSON.parse(JSON.generate(@mappings))
end
validate_swagger!() click to toggle source
# File lib/mini_apivore/swagger_checker.rb, line 96
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