Class: Nexmo::OAS::Renderer::Presenters::Groups

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo/oas/renderer/presenters/groups.rb

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Groups

Returns a new instance of Groups.



8
9
10
# File 'lib/nexmo/oas/renderer/presenters/groups.rb', line 8

def initialize(definition)
  @definition = definition
end

Instance Method Details

#groupsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nexmo/oas/renderer/presenters/groups.rb', line 12

def groups
  tags = @definition.raw['tags']
  # For now we only use the first tag in the list as an equivalent for the old x-group functionality
  @groups = @definition.endpoints.group_by do |endpoint|
    next nil unless tags

    endpoint.raw['tags']&.first
  end

  # We want to use the order in which the tags are defined in the definition, so iterate over the tags
  # and store the index against the tag name. We'll use this later for sorting
  ordering = {}
  tags&.each_with_index do |tag, index|
    ordering[tag['name'].capitalize] = index
  end

  # Sort by the order in which they're defined in the definition
  @groups = @groups.sort_by do |name, _|
    next -1 if name.nil?

    ordering[name.capitalize] || 999
  end
end