Sha256: c902f8d18f7af8c94b93173c97cf22d4d0a8615090411fc05c210e1863c93d2a

Contents?: true

Size: 998 Bytes

Versions: 9

Compression:

Stored size: 998 Bytes

Contents

module AutoResp

  module Parser

    require 'net/http'
    
    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

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


  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
auto_response-0.2.0 lib/ar/parser.rb
auto_response-0.1.8 lib/ar/parser.rb
auto_response-0.1.6 lib/ar/parser.rb
auto_response-0.1.5 lib/ar/parser.rb
auto_response-0.1.4 lib/ar/parser.rb
auto_response-0.1.3 lib/ar/parser.rb
auto_response-0.1.2 lib/ar/parser.rb
auto_response-0.1.1 lib/ar/parser.rb
auto_response-0.1.0 lib/ar/parser.rb