Class: MemoriClient::Swagger::ProcessSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/memori_client/swagger/process_specification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, kind:) ⇒ ProcessSpecification

Returns a new instance of ProcessSpecification.



5
6
7
8
9
10
11
12
# File 'lib/memori_client/swagger/process_specification.rb', line 5

def initialize(uri:, kind:)
  @uri = uri
  @kind = kind
  @result = {}
  @endpoints = {}
  @methods = {}
  @methods_collisions = []
end

Instance Attribute Details

#swagger_specObject (readonly)

Returns the value of attribute swagger_spec.



4
5
6
# File 'lib/memori_client/swagger/process_specification.rb', line 4

def swagger_spec
  @swagger_spec
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/memori_client/swagger/process_specification.rb', line 14

def call
  data = URI.open(@uri).read
  parsed = JSON.parse(data)
  @swagger_spec = parsed
  paths = parsed['paths']
  # routes = collect_routes(paths)
  # puts JSON.pretty_generate(routes)
  paths.each do |path, path_data|
    path_data.each do |method, endpoint|
      module_name, info = analyze_path(method, path, endpoint)
      unless module_name.nil?
        @result[module_name] ||= []
        @result[module_name] << info
      end
    end
  end

  # Verify collisions
  @methods.keys.sort.each do |m|
    @methods_collisions << m if @methods[m].size > 1
  end

  if @methods_collisions.any?
    details = @methods_collisions.map do |m|
      endpoints = @methods[m].map { |e| "      #{e}" }.join("\n")
      "  - #{m} (#{@methods[m].size} endpoints):\n#{endpoints}"
    end.join("\n")

    raise MemoriClient::Swagger::MethodCollisionError, <<~MSG
      Cannot process '#{@kind}' spec from #{@uri}: #{@methods_collisions.size} method collision(s) detected.
      Two or more endpoints generate the same method name. Add an override entry in
      '#{@kind}_overrides.jsonc' with key "<http_method> <path>" and value "Module#method"
      to disambiguate one of the conflicting endpoints.
      Collisions detected:
      #{details}
    MSG
  end

  @result
end