Class: LazyApiDoc::VariantsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_api_doc/variants_parser.rb

Constant Summary collapse

OPTIONAL =
:lazy_api_doc_optional

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variants) ⇒ VariantsParser

Returns a new instance of VariantsParser.



6
7
8
# File 'lib/lazy_api_doc/variants_parser.rb', line 6

def initialize(variants)
  @variants = variants.is_a?(Array) ? variants : [variants]
end

Instance Attribute Details

#variantsObject (readonly)

Returns the value of attribute variants.



4
5
6
# File 'lib/lazy_api_doc/variants_parser.rb', line 4

def variants
  @variants
end

Instance Method Details

#parse(variant, variants) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lazy_api_doc/variants_parser.rb', line 14

def parse(variant, variants)
  variants.delete(OPTIONAL)
  case variant
  when Array
    variant = variants.find(&:any?) || variants.first
    parse_array(variant, variants)
  when Hash
    parse_hash(variants)
  else
    types_template(variants).merge("example" => variant)
  end
end

#parse_array(variant, variants) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/lazy_api_doc/variants_parser.rb', line 84

def parse_array(variant, variants)
  first = variant.first
  types_template(variants).merge(
    "items"   => parse(first, variants.compact.map(&:first).compact),
    "example" => variant
  )
end

#parse_hash(variants) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lazy_api_doc/variants_parser.rb', line 70

def parse_hash(variants)
  result = types_template(variants)
  variant = variants.select { |v| v.is_a?(Hash) }.reverse_each
                    .with_object({}) { |v, res| res.merge!(v) }
  result["properties"] = variant.to_h do |key, val|
    [
      key.to_s,
      parse(val, variants.select { |v| v.is_a?(Hash) }.map { |v| v.fetch(key, OPTIONAL) })
    ]
  end
  result["required"] = variant.keys.select { |key| variants.select { |v| v.is_a?(Hash) }.all? { |v| v.key?(key) } }
  result
end

#resultObject



10
11
12
# File 'lib/lazy_api_doc/variants_parser.rb', line 10

def result
  @result ||= parse(variants.first, variants)
end

#type_of(variant) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lazy_api_doc/variants_parser.rb', line 44

def type_of(variant)
  case variant
  when Hash
    "object"
  when NilClass
    "null"
  when TrueClass, FalseClass
    "boolean"
  when String
    type_of_string(variant)
  when Float
    'number'
  else
    variant.class.name.downcase
  end
end

#type_of_string(variant) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/lazy_api_doc/variants_parser.rb', line 61

def type_of_string(variant)
  case variant
  when /\A\d+\.\d+\z/
    "decimal"
  else
    "string"
  end
end

#types_of(variants) ⇒ Object



40
41
42
# File 'lib/lazy_api_doc/variants_parser.rb', line 40

def types_of(variants)
  variants.map { |v| type_of(v) }.uniq
end

#types_template(variants) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lazy_api_doc/variants_parser.rb', line 27

def types_template(variants)
  types = types_of(variants)
  if types.count == 1
    {
      "type" => types.first
    }
  else
    {
      "oneOf" => types.map { |t| { "type" => t } }
    }
  end
end