Class: Binocs::Swagger::PathMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/binocs/swagger/path_matcher.rb

Class Method Summary collapse

Class Method Details

.build_swagger_ui_url(operation) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
# File 'lib/binocs/swagger/path_matcher.rb', line 28

def build_swagger_ui_url(operation)
  return nil unless operation

  base_url = Binocs.configuration.swagger_ui_url
  return nil if base_url.blank?

  # Build full URL if needed
  unless base_url.start_with?('http://', 'https://')
    if defined?(Rails)
      host = Rails.application.routes.default_url_options[:host] || 'localhost'
      port = Rails.application.routes.default_url_options[:port] || 3000
      protocol = Rails.application.routes.default_url_options[:protocol] || 'http'
      base_url = "#{protocol}://#{host}:#{port}#{base_url}"
    else
      base_url = "http://localhost:3000#{base_url}"
    end
  end

  # Build anchor for Swagger UI
  # Format: #/{tag}/{operationId}
  # Example: #/Company%20Invitations/get_v1_companies__company_uuid__invitations
  tag = operation[:tags]&.first || 'default'
  encoded_tag = URI.encode_www_form_component(tag).gsub('+', '%20')

  if operation[:operation_id]
    "#{base_url}#/#{encoded_tag}/#{operation[:operation_id]}"
  else
    # Fallback: build operation ID from method and path
    # get /v1/companies/{company_uuid}/invitations -> get_v1_companies__company_uuid__invitations
    fallback_op_id = "#{operation[:method]}#{operation[:spec_path]}"
      .gsub('/', '_')
      .gsub(/[{}]/, '_')
      .gsub(/__+/, '__')
    "#{base_url}#/#{encoded_tag}/#{fallback_op_id}"
  end
end

.find_operation(request) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/binocs/swagger/path_matcher.rb', line 7

def find_operation(request)
  spec = Client.fetch_spec
  return nil unless spec && spec['paths']

  method = request.method.downcase
  path = normalize_path(request.path)

  spec['paths'].each do |spec_path, path_item|
    next unless path_item.is_a?(Hash)

    operation = path_item[method]
    next unless operation

    if path_matches?(path, spec_path)
      return build_operation_result(spec_path, method, operation, path_item, spec)
    end
  end

  nil
end