Class: Rain::DocPart
- Inherits:
-
Object
- Object
- Rain::DocPart
- Defined in:
- lib/doc_part.rb
Overview
the doc part is a model to store information about
Instance Attribute Summary collapse
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#responses ⇒ Object
Returns the value of attribute responses.
-
#route ⇒ Object
Returns the value of attribute route.
-
#signature ⇒ Object
Returns the value of attribute signature.
Instance Method Summary collapse
-
#append_doc(text) ⇒ Object
appends the text to the current documentation for the part.
-
#append_header(name, description) ⇒ Object
adds a http header with name and description of the header.
-
#append_param(name, description, type, default = nil) ⇒ Object
adds a parameter with the specified type.
-
#append_response(code, id, text) ⇒ Object
add or append a response with the specified code and the line of text passed in.
-
#get_doc ⇒ Object
joins all of the text in the doc property of the part with spaces.
-
#get_header(name) ⇒ Object
gets a header’s description from the store.
-
#get_param(name) ⇒ Object
gets a parameter by name.
-
#get_response(code, id) ⇒ Object
gets a response part by code and id and joins the parts of the text.
-
#get_route ⇒ Object
gets the current route for the doc part.
-
#get_signature ⇒ Object
gets the method signature.
-
#initialize ⇒ DocPart
constructor
A new instance of DocPart.
-
#set_method(method) ⇒ Object
sets the http method for the documentation part.
-
#set_route(path) ⇒ Object
sets the current route for the doc part.
-
#set_signature(sig) ⇒ Object
sets a method signature.
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ DocPart
Returns a new instance of DocPart.
8 9 10 11 12 13 14 |
# File 'lib/doc_part.rb', line 8 def initialize self.responses = [] self.doc = [] self.route = '//' self.params = [] self.headers = [] end |
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def doc @doc end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def headers @headers end |
#http_method ⇒ Object
Returns the value of attribute http_method.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def http_method @http_method end |
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def params @params end |
#responses ⇒ Object
Returns the value of attribute responses.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def responses @responses end |
#route ⇒ Object
Returns the value of attribute route.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def route @route end |
#signature ⇒ Object
Returns the value of attribute signature.
6 7 8 |
# File 'lib/doc_part.rb', line 6 def signature @signature end |
Instance Method Details
#append_doc(text) ⇒ Object
appends the text to the current documentation for the part
75 76 77 |
# File 'lib/doc_part.rb', line 75 def append_doc(text) self.doc << text end |
#append_header(name, description) ⇒ Object
adds a http header with name and description of the header
114 115 116 117 118 119 |
# File 'lib/doc_part.rb', line 114 def append_header(name, description) self.headers << { name: name, text: description } end |
#append_param(name, description, type, default = nil) ⇒ Object
adds a parameter with the specified type. also sets a default value if specified
97 98 99 100 101 102 103 104 |
# File 'lib/doc_part.rb', line 97 def append_param(name, description, type, default = nil) self.params << { name: name, text: description, type: type, default: default } end |
#append_response(code, id, text) ⇒ Object
add or append a response with the specified code and the line of text passed in
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/doc_part.rb', line 18 def append_response(code, id, text) # check that the response code is a number begin code.to_i rescue Exception => e raise ArgumentError, 'You can only use integer codes for HTTP response examples.' end # try and find the current response and id in the array current_response = self.responses.select { |resp| resp[:code] == code && resp[:id] == id }.first # add to array if nil if current_response.nil? self.responses << { code: code, id: id, text: [text] } else # otherwise append to the current response self.responses.each do |resp| if resp[:code] == code && resp[:id] == id resp[:text] << text end end end end |
#get_doc ⇒ Object
joins all of the text in the doc property of the part with spaces
81 82 83 |
# File 'lib/doc_part.rb', line 81 def get_doc self.doc.join(' ') end |
#get_header(name) ⇒ Object
gets a header’s description from the store
122 123 124 125 126 127 128 |
# File 'lib/doc_part.rb', line 122 def get_header(name) header = self.headers.select { |h| h[:name] == name }.first return nil if header.nil? return header[:text] end |
#get_param(name) ⇒ Object
gets a parameter by name. will return nil if the parameter does not exist
108 109 110 |
# File 'lib/doc_part.rb', line 108 def get_param(name) self.params.select { |param| param[:name] == name }.first end |
#get_response(code, id) ⇒ Object
gets a response part by code and id and joins the parts of the text
50 51 52 53 54 55 56 |
# File 'lib/doc_part.rb', line 50 def get_response(code, id) response = self.responses.select { |resp| resp[:code] == code && resp[:id] == id }.first raise 'Response code and id reference does not exist.' if response.nil? response[:text].join end |
#get_route ⇒ Object
gets the current route for the doc part
91 92 93 |
# File 'lib/doc_part.rb', line 91 def get_route self.route end |
#get_signature ⇒ Object
gets the method signature
136 137 138 |
# File 'lib/doc_part.rb', line 136 def get_signature self.signature end |
#set_method(method) ⇒ Object
sets the http method for the documentation part
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/doc_part.rb', line 60 def set_method(method) # capitalize and convert to a symbol if not already a symbol method.upcase! if !method.kind_of? Symbol # check if the http method is valid valid_methods = [:GET, :PUT, :POST, :PATCH, :DELETE] if !valid_methods.include?(method.to_sym) raise ArgumentError, "HTTP method must be valid (#{valid_methods.join(', ')})" end self.http_method = method end |
#set_route(path) ⇒ Object
sets the current route for the doc part
86 87 88 |
# File 'lib/doc_part.rb', line 86 def set_route(path) self.route = path end |
#set_signature(sig) ⇒ Object
sets a method signature
131 132 133 |
# File 'lib/doc_part.rb', line 131 def set_signature(sig) self.signature = sig end |
#to_hash ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/doc_part.rb', line 140 def to_hash return { route: self.route, params: self.params, headers: self.headers, doc: self.doc, responses: self.responses, http_method: self.http_method, signature: self.signature } end |