Module: DocMyRoutes::Annotatable

Defined in:
lib/doc_my_routes/doc/mixins/annotatable.rb

Overview

Logic to help with the REST API documentation. This module provides methods to “decorate” sinatra routes as follows:

summary 'Short definition of the route'
notes 'More detailed explanation of the operation'
status_codes [200, 401, 500]
get '/api/example' do
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object

When a class is extended with this module documentation specific features are enabled



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 19

def extended(mod)
  # Wrap sinatra's route method to register the defined routes
  mod.define_singleton_method(:route) do |*args, &block|
    result = super(*args, &block)
    
    options = args.last.is_a?(Hash) ? args.pop : {}
    routes = [*args.pop].map(&:to_s) # ensure routes are strings
    verbs = args.map { |verb| verb.is_a?(Symbol) ? verb.to_s.upcase : verb }
    
    verbs.each do |verb|
      routes.each do |route_pattern|
        reset_doc = verb.equal?(verbs.last) && route_pattern.equal?(routes.last)

        track_route(self, verb, route_pattern, options, reset_doc, &block)
      end
    end

    result
  end

  extract_url_map if Mapping.mapping_used?
end

Instance Method Details

#examples_regex(value) ⇒ Object

Match interaction examples to this route



91
92
93
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 91

def examples_regex(value)
  route_documentation.examples_regex = value
end

#no_docObject



70
71
72
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 70

def no_doc
  route_documentation.hidden = true
end

#notes(value) ⇒ Object



82
83
84
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 82

def notes(value)
  route_documentation.notes = value
end

#notes_ref(value) ⇒ Object

It’s possible to provide the route notes using a file to avoid adding too much text in the route definition



97
98
99
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 97

def notes_ref(value)
  route_documentation.notes_ref = value
end

#parameter(value, options = {}) ⇒ Object



101
102
103
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 101

def parameter(value, options = {})
  route_documentation.add_parameter(value, options)
end

#produces(*value) ⇒ Object



74
75
76
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 74

def produces(*value)
  route_documentation.produces = value
end

#route_documentationObject



64
65
66
67
68
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 64

def route_documentation
  @route_documentation ||= begin
    RouteDocumentation.new
  end
end

#status_codes(value) ⇒ Object



86
87
88
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 86

def status_codes(value)
  route_documentation.status_codes = value
end

#summary(value) ⇒ Object



78
79
80
# File 'lib/doc_my_routes/doc/mixins/annotatable.rb', line 78

def summary(value)
  route_documentation.summary = value
end