Class: Rain::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/parser.rb

Constant Summary collapse

@@title_regex =
/{title (.*?)}/m
@@route_regex =
/{route (.*?)}/m
@@header_regex =
/{header (.*?)}/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

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Parser



13
14
15
# File 'lib/parser.rb', line 13

def initialize(type)
  self.type = type
end

Instance Attribute Details

#current_lineObject

Returns the value of attribute current_line.



3
4
5
# File 'lib/parser.rb', line 3

def current_line
  @current_line
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/parser.rb', line 3

def type
  @type
end

Instance Method Details

#extract_header_name(line) ⇒ Object



193
194
195
# File 'lib/parser.rb', line 193

def extract_header_name(line)
  line[@@header_regex, 1]
end

#extract_method(line) ⇒ Object



145
146
147
# File 'lib/parser.rb', line 145

def extract_method(line)
  line[@@method_regex, 1]
end

#extract_param_default(line) ⇒ Object



185
186
187
# File 'lib/parser.rb', line 185

def extract_param_default(line)
  line[@@param_regex_default, 3]
end

#extract_param_name(line) ⇒ Object



173
174
175
# File 'lib/parser.rb', line 173

def extract_param_name(line)
  line[@@param_regex, 1]
end

#extract_param_type(line) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/parser.rb', line 177

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



161
162
163
# File 'lib/parser.rb', line 161

def extract_response_code(line)
  line[@@response_regex, 1]
end

#extract_response_id(line) ⇒ Object



165
166
167
# File 'lib/parser.rb', line 165

def extract_response_id(line)
  line[@@response_regex, 2]
end

#extract_route(line) ⇒ Object



153
154
155
# File 'lib/parser.rb', line 153

def extract_route(line)
  line[@@route_regex, 1]
end

#extract_title(line) ⇒ Object



137
138
139
# File 'lib/parser.rb', line 137

def extract_title(line)
  line[@@title_regex, 1]
end

#is_header?(line) ⇒ Boolean



189
190
191
# File 'lib/parser.rb', line 189

def is_header?(line)
  line.start_with?('{header') || line.start_with?('{/header')
end

#is_method?(line) ⇒ Boolean



141
142
143
# File 'lib/parser.rb', line 141

def is_method?(line)
  line.start_with? '{method'
end

#is_param?(line) ⇒ Boolean



169
170
171
# File 'lib/parser.rb', line 169

def is_param?(line)
  line.start_with?('{param') || line.start_with?('{/param')
end

#is_response?(line) ⇒ Boolean



157
158
159
# File 'lib/parser.rb', line 157

def is_response?(line)
  line.start_with?('{response') || line.start_with?('{/response')
end

#is_route?(line) ⇒ Boolean



149
150
151
# File 'lib/parser.rb', line 149

def is_route?(line)
  line.start_with? '{route'
end

#is_title?(line) ⇒ Boolean



133
134
135
# File 'lib/parser.rb', line 133

def is_title?(line)
  line.start_with? '{title'
end

#parse(line, parse_signatures = false) ⇒ Object

parses the current line, determining which tag the line contains and returning the result accordingly in a hash with the tag type



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/parser.rb', line 20

def parse(line, parse_signatures = false)

  # return nil if there is no # on the line for ruby.
  basic_stripped = line.strip().sub(' do', '')
  return nil if self.type == :RUBY && basic_stripped == ''

  # see if a signature part can be extracted if a signature is enabled
  if parse_signatures
    return nil if self.type == :RUBY && !basic_stripped.start_with?('#') && !(basic_stripped =~ /class|def|get|put|post|delete/)
  else
    return nil if self.type == :RUBY && !basic_stripped.start_with?('#')
  end

  # 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

  # param tag. must determine whether to open the tag
  # for extra docs or close it
  if is_header?(line)
    open = line.start_with?('{/header') ? false : true
    return {
      tag: :header,
      name: extract_header_name(line),
      open: open
    }
  end

  # return method signature if found
  if parse_signatures
    if line.start_with?('def ', 'class ', 'get "', "get '", 'put "', "put '", 'post "', "post '", 'delete "', "delete '")
      return {
        tag: :signature,
        text: line
      }
    end
  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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/parser.rb', line 121

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