Class: GrapeSwagger::DocMethods::DataType

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-swagger/doc_methods/data_type.rb

Constant Summary collapse

PRIMITIVE_MAPPINGS =
{
  'integer' => %w[integer int32],
  'long' => %w[integer int64],
  'float' => %w[number float],
  'double' => %w[number double],
  'byte' => %w[string byte],
  'date' => %w[string date],
  'dateTime' => %w[string date-time],
  'binary' => %w[string binary],
  'password' => %w[string password],
  'email' => %w[string email],
  'uuid' => %w[string uuid]
}.freeze

Class Method Summary collapse

Class Method Details

.call(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 7

def call(value)
  raw_data_type = value.is_a?(Hash) ? value[:type] : value
  raw_data_type ||= 'String'
  raw_data_type = parse_multi_type(raw_data_type)

  case raw_data_type.to_s
  when 'Boolean', 'Date', 'Integer', 'String', 'Float', 'JSON', 'Array'
    raw_data_type.to_s.downcase
  when 'Hash'
    'object'
  when 'Rack::Multipart::UploadedFile', 'File'
    'file'
  when 'Grape::API::Boolean'
    'boolean'
  when 'BigDecimal'
    'double'
  when 'DateTime', 'Time'
    'dateTime'
  when 'Numeric'
    'long'
  when 'Symbol'
    'string'
  else
    parse_entity_name(raw_data_type)
  end
end

.collectionsObject



90
91
92
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 90

def collections
  %w[csv ssv tsv pipes multi brackets]
end

.mapping(value) ⇒ Object



86
87
88
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 86

def mapping(value)
  PRIMITIVE_MAPPINGS[value] || 'string'
end

.parse_entity_name(model) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 50

def parse_entity_name(model)
  if model.respond_to?(:entity_name)
    model.entity_name
  elsif model.to_s.end_with?('::Entity', '::Entities')
    model.to_s.split('::')[0..-2].join('_')
  elsif model.to_s.start_with?('Entity::', 'Entities::', 'Representable::')
    model.to_s.split('::')[1..-1].join('_')
  else
    model.to_s.split('::').join('_')
  end
end

.parse_multi_type(raw_data_type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 34

def parse_multi_type(raw_data_type)
  case raw_data_type
  when /\A\[.*\]\z/
    type_as_string = raw_data_type.gsub(/[\[\s+\]]/, '').split(',').first
    begin
      Object.const_get(type_as_string)
    rescue NameError
      type_as_string
    end
  when Array
    raw_data_type.first
  else
    raw_data_type
  end
end

.primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 66

def primitive?(type)
  primitives.include?(type.to_s.downcase)
end

.primitivesObject



74
75
76
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 74

def primitives
  PRIMITIVE_MAPPINGS.keys.map(&:downcase)
end

.query_array_primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 78

def query_array_primitive?(type)
  query_array_primitives.include?(type.to_s.downcase)
end

.query_array_primitivesObject



82
83
84
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 82

def query_array_primitives
  primitives << 'string'
end

.request_primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 62

def request_primitive?(type)
  request_primitives.include?(type.to_s.downcase)
end

.request_primitivesObject



70
71
72
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 70

def request_primitives
  primitives + %w[object string boolean file json array]
end