Class: Praxis::Docs::OpenApi::ParameterObject

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/docs/open_api/parameter_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:, name:, is_required:, info:) ⇒ ParameterObject

Returns a new instance of ParameterObject.



12
13
14
15
16
17
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 12

def initialize(location:, name:, is_required:, info:)
  @location = location
  @name = name
  @info = info
  @is_required = is_required
end

Instance Attribute Details

#infoObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 10

def info
  @info
end

#is_requiredObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 10

def is_required
  @is_required
end

#locationObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 10

def location
  @location
end

#nameObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 10

def name
  @name
end

Class Method Details

.process_parameters(action) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 43

def self.process_parameters(action)
  output = []
  # An array, with one hash per param inside
  if action.headers
    (action.headers.attributes || {}).each_with_object(output) do |(name, info), out|
      out << ParameterObject.new(location: 'header', name: name, is_required: info.options[:required], info: info).dump
    end
  end

  if action.params
    route_params = \
      if action.route
        action.route.path.named_captures.keys.collect(&:to_sym)
      else
        warn "Warning: No routes defined for action #{action.name}"
        []
      end
    (action.params.attributes || {}).each_with_object(output) do |(name, info), out|
      in_type = route_params.include?(name) ? :path : :query
      is_required = in_type == :path ? true : info.options[:required]
      out << ParameterObject.new(location: in_type, name: name, is_required: is_required, info: info).dump
    end
  end

  output
end

Instance Method Details

#dumpObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/praxis/docs/open_api/parameter_object.rb', line 19

def dump
  # Fixed fields
  h = { name: name, in: location }
  h[:description] = info.options[:description] if info.options[:description]
  h[:required] = is_required if is_required
  # h[:deprecated] = false
  # h[:allowEmptyValue] ??? TODO: support in Praxis

  # Other supported attributes
  # style
  # explode
  # allowReserved

  # Now merge the rest schema and example
  # schema
  # example
  # examples (Example and Examples are mutually exclusive)
  schema = SchemaObject.new(info: info)
  h[:schema] = schema.dump_schema
  # NOTE: we do not support the 'content' key...we always use schema
  h[:example] = schema.dump_example
  h
end