Sha256: ec5de3364abe3e5e5d4817c7c080b361724335945298bf7c5c91544273284ab4

Contents?: true

Size: 1015 Bytes

Versions: 2

Compression:

Stored size: 1015 Bytes

Contents

require "json-schema"

module Swagger
  # Casts a string value to the ruby datatype as definied in the swagger spec
  def self.cast(value, type = "string")
    raise ArgumentError, "#{value} is not a string" unless value.is_a?(String)
    case type
    when "string"
      value
    when "integer"
      raise ArgumentError, "#{value} is not an integer" unless value =~ /^-?\d+$/
      value.to_i
    when "number"
      raise ArgumentError, "#{value} is not a float" unless value =~ /^-?\d+(?:\.\d+)?$/
      value.to_f
    else
      raise NotImplementedError
    end
  end

  class Base
    # Pre-load JSON-schema files used for validation
    Dir.glob(File.join(__dir__, "../../schema/*-schema.json")) do |schema|
      data = JSON.parse(File.read(schema))
      JSON::Validator.add_schema(JSON::Schema.new(data, data['id']))
    end

    def initialize(filepath)
      @spec = YAML.load(open(filepath))
      JSON::Validator.validate!({ "$ref" => "http://swagger.io/v2/schema.json#" }, @spec)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinatra-swagger-0.1.1 lib/swagger/base.rb
sinatra-swagger-0.0.0 lib/swagger/base.rb