Class: R2OAS::Routing::PathComponent

Inherits:
BaseComponent show all
Defined in:
lib/r2-oas/routing/components/path_component.rb

Constant Summary collapse

FORMAT_PATH_PARAMETER_REGEXP =
/\(.+\)/.freeze
SYMBOL_PATH_PARAMETER_REGEXP =
/:(.\w+)/.freeze
BRACE_PATH_PARAMETER_REGEXP =
/\{(.\w+)\}/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PathComponent

Returns a new instance of PathComponent.



12
13
14
15
# File 'lib/r2-oas/routing/components/path_component.rb', line 12

def initialize(path)
  super()
  @path = path
end

Instance Method Details

#exist_path_parameters?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/r2-oas/routing/components/path_component.rb', line 49

def exist_path_parameters?
  path_parameters.present?
end

#path_excluded_path_parametersObject



42
43
44
45
46
47
# File 'lib/r2-oas/routing/components/path_component.rb', line 42

def path_excluded_path_parameters
  excluded_path_parameters = path_parameters.each_with_object(symbol_to_brace) do |path_parameter, result|
    result.gsub!("{#{path_parameter}}", '')
  end
  excluded_path_parameters.split('/').delete_if(&:empty?).join('/')
end

#path_parametersObject



53
54
55
56
# File 'lib/r2-oas/routing/components/path_component.rb', line 53

def path_parameters
  result = without_format.scan(SYMBOL_PATH_PARAMETER_REGEXP) + without_format.scan(BRACE_PATH_PARAMETER_REGEXP)
  result.flatten
end

#path_parameters_dataObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/r2-oas/routing/components/path_component.rb', line 29

def path_parameters_data
  return {} unless exist_path_parameters?

  path_parameters.each_with_object({}) do |path_parameter, data|
    type = (path_parameter =~ /id/ ? 'integer' : 'string')
    data.merge!(
      "#{path_parameter}": {
        type: type
      }
    )
  end
end

#symbol_to_braceObject



21
22
23
24
25
26
27
# File 'lib/r2-oas/routing/components/path_component.rb', line 21

def symbol_to_brace
  return without_format unless exist_path_parameters?

  path_parameters.each_with_object(without_format) do |path_parameter, result|
    result.gsub!(":#{path_parameter}", "{#{path_parameter}}")
  end
end

#to_sObject



17
18
19
# File 'lib/r2-oas/routing/components/path_component.rb', line 17

def to_s
  without_format.to_s
end