Class: DocMyRoutes::Route
- Inherits:
-
Object
- Object
- DocMyRoutes::Route
- Extended by:
- Forwardable
- Defined in:
- lib/doc_my_routes/doc/route.rb
Overview
Simple object representing a route
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#documentation ⇒ Object
readonly
Returns the value of attribute documentation.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#route_pattern ⇒ Object
Returns the value of attribute route_pattern.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
-
#initialize(resource, verb, route_pattern, conditions, documentation) ⇒ Route
constructor
A new instance of Route.
-
#param_info ⇒ Object
Return a list of parameters required by this route, if specified.
- #path ⇒ Object
-
#to_hash ⇒ Object
We need to use deep merge as param_info method will return a hash for parameters with extracted information from the route path, and documentation might also have parameters with some more documentation and we don’t want to loose extracted data from route path.
- #to_s ⇒ Object
Constructor Details
#initialize(resource, verb, route_pattern, conditions, documentation) ⇒ Route
Returns a new instance of Route.
16 17 18 19 20 21 22 23 24 |
# File 'lib/doc_my_routes/doc/route.rb', line 16 def initialize(resource, verb, route_pattern, conditions, documentation) @resource = resource @verb = verb @route_pattern = route_pattern # TODO: We could inherit this from the application mapping @namespace = nil @conditions = conditions @documentation = documentation end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
11 12 13 |
# File 'lib/doc_my_routes/doc/route.rb', line 11 def conditions @conditions end |
#documentation ⇒ Object (readonly)
Returns the value of attribute documentation.
12 13 14 |
# File 'lib/doc_my_routes/doc/route.rb', line 12 def documentation @documentation end |
#namespace ⇒ Object
Returns the value of attribute namespace.
12 13 14 |
# File 'lib/doc_my_routes/doc/route.rb', line 12 def namespace @namespace end |
#resource ⇒ Object
Returns the value of attribute resource.
11 12 13 |
# File 'lib/doc_my_routes/doc/route.rb', line 11 def resource @resource end |
#route_pattern ⇒ Object
Returns the value of attribute route_pattern.
11 12 13 |
# File 'lib/doc_my_routes/doc/route.rb', line 11 def route_pattern @route_pattern end |
#verb ⇒ Object
Returns the value of attribute verb.
11 12 13 |
# File 'lib/doc_my_routes/doc/route.rb', line 11 def verb @verb end |
Instance Method Details
#param_info ⇒ Object
Return a list of parameters required by this route, if specified.
Try to extract parameters from the route definition otherwise
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/doc_my_routes/doc/route.rb', line 67 def param_info path_parameters_array = route_pattern.split('/').map do |part| part.start_with?(':') ? part[1..-1].to_sym : nil end.compact path_parameters = HashHelpers.array_to_hash_keys(path_parameters_array, { in: :path, required: true }) condition_parameters = HashHelpers.array_to_hash_keys(conditions[:parameters] || []) HashHelpers.deep_merge(condition_parameters, path_parameters) end |
#path ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/doc_my_routes/doc/route.rb', line 38 def path @path ||= begin path = Mapping.mount_point_for_resource(resource) + route_pattern # Changing double slashes into single slash # Removing the trailing ? # Removing the trailing / only if it's not the only character # FROM /api/nodes/:id/? TO /api/nodes/:id # Change the path variable from Ruby style into brackets style # from /api/nodes/:id/ TO /api/nodes/{id}/ path.gsub(%r{//}, '/') .gsub(/(\?+)*$/, '') .gsub(%r{(.+)\/$}, '\\1') .gsub(/:(?<path_var>\w+)/, '{\k<path_var>}') end end |
#to_hash ⇒ Object
We need to use deep merge as param_info method will return a hash for parameters with extracted information from the route path, and documentation might also have parameters with some more documentation and we don’t want to loose extracted data from route path
30 31 32 33 34 35 36 |
# File 'lib/doc_my_routes/doc/route.rb', line 30 def to_hash HashHelpers.deep_merge({ http_method: verb, parameters: param_info, path: path }, documentation.to_hash) end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/doc_my_routes/doc/route.rb', line 60 def to_s "#{verb} #{namespace}#{route_pattern} #{conditions}" end |