Class: SoberSwag::Reporting::Input::InRange

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/input/in_range.rb

Overview

Specify that an item must be within a given range in ruby. This gets translated to minimum and maximum keys in swagger.

This works with endless ranges (Ruby 2.6+) and beginless ranges (Ruby 2.7+)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface

#add_schema_key, #call!, #described, #enum, #format, #in_range, #list, #mapped, #modify_schema, #multiple_of, #optional, #or, #referenced, #swagger_path_schema, #swagger_query_schema, #|

Constructor Details

#initialize(input, range) ⇒ InRange

Returns a new instance of InRange.



10
11
12
13
# File 'lib/sober_swag/reporting/input/in_range.rb', line 10

def initialize(input, range)
  @input = input
  @range = range
end

Instance Attribute Details

#inputInterface (readonly)

Returns:



17
18
19
# File 'lib/sober_swag/reporting/input/in_range.rb', line 17

def input
  @input
end

#rangeRange (readonly)

Returns:

  • (Range)


21
22
23
# File 'lib/sober_swag/reporting/input/in_range.rb', line 21

def range
  @range
end

Instance Method Details

#call(value) ⇒ Range

Returns:

  • (Range)


25
26
27
28
29
30
31
32
# File 'lib/sober_swag/reporting/input/in_range.rb', line 25

def call(value)
  res = input.call(value)

  return res if res.is_a?(Report::Base)
  return Report::Value.new(['was not in minimum/maximum range']) unless range.member?(res)

  res
end

#maximum_portionObject



47
48
49
50
51
# File 'lib/sober_swag/reporting/input/in_range.rb', line 47

def maximum_portion
  return {} unless range.end

  { maximum: range.end, exclusiveMaximum: range.exclude_end? }
end

#minimum_portionObject



53
54
55
56
57
# File 'lib/sober_swag/reporting/input/in_range.rb', line 53

def minimum_portion
  return {} unless range.begin

  { minimum: range.begin, exclusiveMinimum: false }
end

#swagger_schemaObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sober_swag/reporting/input/in_range.rb', line 34

def swagger_schema
  schema, found = input.swagger_schema

  merged =
    if schema.key?(:$ref)
      { allOf: [schema] }
    else
      schema
    end.merge(maximum_portion).merge(minimum_portion)

  [merged, found]
end