Class: DocMyRoutes::Route

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/doc_my_routes/doc/route.rb

Overview

Simple object representing a route

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#conditionsObject

Returns the value of attribute conditions.



11
12
13
# File 'lib/doc_my_routes/doc/route.rb', line 11

def conditions
  @conditions
end

#documentationObject (readonly)

Returns the value of attribute documentation.



12
13
14
# File 'lib/doc_my_routes/doc/route.rb', line 12

def documentation
  @documentation
end

#namespaceObject

Returns the value of attribute namespace.



12
13
14
# File 'lib/doc_my_routes/doc/route.rb', line 12

def namespace
  @namespace
end

#resourceObject

Returns the value of attribute resource.



11
12
13
# File 'lib/doc_my_routes/doc/route.rb', line 11

def resource
  @resource
end

#route_patternObject

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

#verbObject

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_infoObject

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

#pathObject



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_hashObject

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_sObject



60
61
62
# File 'lib/doc_my_routes/doc/route.rb', line 60

def to_s
  "#{verb} #{namespace}#{route_pattern} #{conditions}"
end