Class: Rain::Parser
- Inherits:
-
Object
- Object
- Rain::Parser
- Defined in:
- lib/parser.rb
Constant Summary collapse
- @@title_regex =
/{title (.*?)}/m- @@route_regex =
/{route (.*?)}/m- @@response_regex =
/{response (.*?) (.*?)}/m- @@param_regex =
/{param (.*?) (.*?)}/m- @@param_regex_default =
/{param (.*?) (.*?) (.*?)}/m- @@method_regex =
/(GET|PUT|POST|DELETE|PATCH|HEAD)/
Instance Attribute Summary collapse
-
#current_line ⇒ Object
Returns the value of attribute current_line.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #extract_method(line) ⇒ Object
- #extract_param_default(line) ⇒ Object
- #extract_param_name(line) ⇒ Object
- #extract_param_type(line) ⇒ Object
- #extract_response_code(line) ⇒ Object
- #extract_response_id(line) ⇒ Object
- #extract_route(line) ⇒ Object
- #extract_title(line) ⇒ Object
-
#initialize(type) ⇒ Parser
constructor
A new instance of Parser.
- #is_method?(line) ⇒ Boolean
- #is_param?(line) ⇒ Boolean
- #is_response?(line) ⇒ Boolean
- #is_route?(line) ⇒ Boolean
- #is_title?(line) ⇒ Boolean
-
#parse(line) ⇒ Object
parses the current line, determining which tag the line contains and returning the result accordingly in a hash with the tag type.
-
#strip_current_line(line) ⇒ Object
remove any extra spaces from the current line and remove the comma # at the start of the line if the parser is for a ruby file.
Constructor Details
#initialize(type) ⇒ Parser
Returns a new instance of Parser.
12 13 14 |
# File 'lib/parser.rb', line 12 def initialize(type) self.type = type end |
Instance Attribute Details
#current_line ⇒ Object
Returns the value of attribute current_line.
3 4 5 |
# File 'lib/parser.rb', line 3 def current_line @current_line end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/parser.rb', line 3 def type @type end |
Instance Method Details
#extract_method(line) ⇒ Object
114 115 116 |
# File 'lib/parser.rb', line 114 def extract_method(line) line[@@method_regex, 1] end |
#extract_param_default(line) ⇒ Object
154 155 156 |
# File 'lib/parser.rb', line 154 def extract_param_default(line) line[@@param_regex_default, 3] end |
#extract_param_name(line) ⇒ Object
142 143 144 |
# File 'lib/parser.rb', line 142 def extract_param_name(line) line[@@param_regex, 1] end |
#extract_param_type(line) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/parser.rb', line 146 def extract_param_type(line) if line[@@param_regex_default, 2].nil? return line[@@param_regex, 2] end return line[@@param_regex_default, 2] end |
#extract_response_code(line) ⇒ Object
130 131 132 |
# File 'lib/parser.rb', line 130 def extract_response_code(line) line[@@response_regex, 1] end |
#extract_response_id(line) ⇒ Object
134 135 136 |
# File 'lib/parser.rb', line 134 def extract_response_id(line) line[@@response_regex, 2] end |
#extract_route(line) ⇒ Object
122 123 124 |
# File 'lib/parser.rb', line 122 def extract_route(line) line[@@route_regex, 1] end |
#extract_title(line) ⇒ Object
106 107 108 |
# File 'lib/parser.rb', line 106 def extract_title(line) line[@@title_regex, 1] end |
#is_method?(line) ⇒ Boolean
110 111 112 |
# File 'lib/parser.rb', line 110 def is_method?(line) line.start_with? '{method' end |
#is_param?(line) ⇒ Boolean
138 139 140 |
# File 'lib/parser.rb', line 138 def is_param?(line) line.start_with?('{param') || line.start_with?('{/param') end |
#is_response?(line) ⇒ Boolean
126 127 128 |
# File 'lib/parser.rb', line 126 def is_response?(line) line.start_with?('{response') || line.start_with?('{/response') end |
#is_route?(line) ⇒ Boolean
118 119 120 |
# File 'lib/parser.rb', line 118 def is_route?(line) line.start_with? '{route' end |
#is_title?(line) ⇒ Boolean
102 103 104 |
# File 'lib/parser.rb', line 102 def is_title?(line) line.start_with? '{title' end |
#parse(line) ⇒ Object
parses the current line, determining which tag the line contains and returning the result accordingly in a hash with the tag type
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/parser.rb', line 19 def parse(line) # return nil if there is no # on the line for ruby. return nil if self.type == :RUBY && !line.strip().start_with?('#') # strip blanks and # from the current line line = strip_current_line(line) # convert blank lines to new lines if line == "" line = "\n" end # title tag if is_title?(line) return { tag: :title, title: extract_title(line) } end # method tag if is_method?(line) return { tag: :method, method: extract_method(line) } end # route tag if is_route?(line) return { tag: :route, route: extract_route(line) } end # response tag. must determine whether to open the tag # for extra docs or close it if is_response?(line) open = line.start_with?('{/response') ? false : true return { tag: :response, code: extract_response_code(line), open: open, id: extract_response_id(line) } end # param tag. must determine whether to open the tag # for extra docs or close it if is_param?(line) open = line.start_with?('{/param') ? false : true return { tag: :param, name: extract_param_name(line), type: extract_param_type(line), default: extract_param_default(line), open: open } end # return simple doc line if no tags fit return { tag: :doc, text: line } end |
#strip_current_line(line) ⇒ Object
remove any extra spaces from the current line and remove the comma # at the start of the line if the parser is for a ruby file
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/parser.rb', line 90 def strip_current_line(line) line.strip! # check the current type and if ruby, remove the # if self.type == :RUBY line.sub!('#', '') line.strip! end return line end |