Module: AutoResp::Parser

Included in:
ProxyServer
Defined in:
lib/ar/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ar/parser.rb', line 7

def parse( str )

  headers, body = {}, []
  to_find_header, body_start = true, false

  str.lines.each_with_index do |line, idx|
    if to_find_header and not body_start
      if line =~ /^\s*$/
        if idx == 0
          to_find_header = false
          body << line
        else
          body_start = true
          body = []
        end
      else
        body << line
        name, value = parse_header_item(line)
        if name
          headers[name] = value
        else
          to_find_header = false
        end
      end
    else
      body << line
    end
  end

  headers = nil unless to_find_header and body_start
  res_body = body.join

  [headers, res_body]
end

#parse_header_item(str) ⇒ Object



42
43
44
45
# File 'lib/ar/parser.rb', line 42

def parse_header_item(str)
  mtc = str.match /^\s*(\S+)\s*:\s*(\S.*)$/
  [mtc[1], mtc[2]] if mtc
end