Class: OasRails::OasRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_rails/oas_route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOasRoute

Returns a new instance of OasRoute.



6
# File 'lib/oas_rails/oas_route.rb', line 6

def initialize; end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def controller
  @controller
end

#controller_actionObject

Returns the value of attribute controller_action.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def controller_action
  @controller_action
end

#controller_classObject

Returns the value of attribute controller_class.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def controller_class
  @controller_class
end

#controller_pathObject

Returns the value of attribute controller_path.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def controller_path
  @controller_path
end

#docstringObject

Returns the value of attribute docstring.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def docstring
  @docstring
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def method
  @method
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def path
  @path
end

#rails_routeObject

Returns the value of attribute rails_route.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def rails_route
  @rails_route
end

#source_stringObject

Returns the value of attribute source_string.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def source_string
  @source_string
end

#verbObject

Returns the value of attribute verb.



3
4
5
# File 'lib/oas_rails/oas_route.rb', line 3

def verb
  @verb
end

Class Method Details

.new_from_rails_route(rails_route: ActionDispatch::Journey::Route) ⇒ Object



8
9
10
11
12
13
# File 'lib/oas_rails/oas_route.rb', line 8

def self.new_from_rails_route(rails_route: ActionDispatch::Journey::Route)
  instance = new
  instance.rails_route = rails_route
  instance.extract_rails_route_data
  instance
end

Instance Method Details

#controller_path_extractor(controller) ⇒ Object



41
42
43
# File 'lib/oas_rails/oas_route.rb', line 41

def controller_path_extractor(controller)
  Rails.root.join("app/controllers/#{controller}_controller.rb").to_s
end

#extract_docstringObject



27
28
29
30
31
# File 'lib/oas_rails/oas_route.rb', line 27

def extract_docstring
  ::YARD::Docstring.parser.parse(
    controller_class.constantize.instance_method(method).comment.lines.map { |line| line.sub(/^#\s*/, '') }.join
  ).to_docstring
end

#extract_rails_route_dataObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/oas_rails/oas_route.rb', line 15

def extract_rails_route_data
  @controller_action = "#{@rails_route.defaults[:controller].camelize}Controller##{@rails_route.defaults[:action]}"
  @controller_class = "#{@rails_route.defaults[:controller].camelize}Controller"
  @controller = @rails_route.defaults[:controller]
  @controller_path = controller_path_extractor(@rails_route.defaults[:controller])
  @method = @rails_route.defaults[:action]
  @verb = @rails_route.verb
  @path = Extractors::RouteExtractor.clean_route(@rails_route.path.spec.to_s)
  @docstring = extract_docstring
  @source_string = extract_source_string
end

#extract_source_stringObject



33
34
35
# File 'lib/oas_rails/oas_route.rb', line 33

def extract_source_string
  @controller_class.constantize.instance_method(method).source
end

#path_paramsObject



37
38
39
# File 'lib/oas_rails/oas_route.rb', line 37

def path_params
  @rails_route.path.spec.to_s.scan(/:(\w+)/).flatten.reject! { |e| e == 'format' }
end