class Swagger::Grape::Type

Attributes

discovered_types[R]

Public Class Methods

new(type) click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 9
def initialize(type)
  @type = type.to_s || 'String'
end

Public Instance Methods

sub_types() click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 17
def sub_types
  Swagger::Grape::Entity.new(@type).sub_types
end
to_swagger(with_definition = true) click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 13
def to_swagger(with_definition = true)
  translate(with_definition)
end

Private Instance Methods

basic_type?() click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 55
def basic_type?
  basic_type_schemes.key? @type.downcase
end
basic_type_schemes() click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 69
def basic_type_schemes
  {
    'string' => {
      'type' => 'string'
    },
    'integer' => {
      'type' => 'integer'
    },
    'array' => {
      'type' => 'array',
      'items' => { 'type' => 'string' }
    },
    'hash' => {
      'type' => 'object',
      'properties' => {}
    },
    'boolean' => {
      'type' => 'boolean'
    },
    'virtus::attribute::boolean' => {
      'type' => 'boolean'
    },
    'symbol' => {
      'type' => 'string'
    },
    'float' => {
      'type' => 'number',
      'format' => 'float'
    },
    'rack::multipart::uploadedfile' => {
      'type' => 'string' # 'Warning - I have no idea how to handle the type file. Right now I will consider this a string, but we should probably handle it...'
    },
    'date' => {
      'type' => 'string',
      'format' => 'date'
    },
    'datetime' => {
      'type' => 'string',
      'format' => 'date-time'
    }

  }.freeze
end
short_hand_array?() click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 51
def short_hand_array?
  !(@type.downcase =~ /\[[a-zA-Z]+\]/).nil?
end
shorthand_array_scheme() click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 59
def shorthand_array_scheme
  match = @type.downcase.match(/\[(.*?)\]/)
  @swagger_type = {
    'type' => 'array',
    'items' => {
      'type' => match[1]
    }
  }
end
translate(with_definition) click to toggle source
# File lib/ruby-swagger/grape/type.rb, line 23
def translate(with_definition)
  swagger_type = {}

  # basic type
  if basic_type?
    swagger_type = basic_type_schemes[@type.downcase]

  # grape shorthand array eg. `Array[Integer]`
  elsif short_hand_array?
    swagger_type = shorthand_array_scheme

  # representer or entity object
  else
    if with_definition
      # I can just reference the name of the representer here
      swagger_type = {
        'type' => 'object',
        '$ref' => "#/definitions/#{@type}"
      }

    # grape-entity object
    else
      swagger_type = Swagger::Grape::Entity.new(@type).to_swagger
    end
  end
  swagger_type
end