Sha256: 1a435d2aae5a073cff5458197dcfe9ab976e13c1be7ab345036d411e773152b5

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Opensaz
  class HTTPRequest

    CRLF = "\r\n"
    SEPERATOR = ": "

    def initialize(content)
      raise "request_str couldn't be nil" if content == nil
      @content = content
    end

    def headers
      first_line = headers_str.split(CRLF)[0]
      following_lines = headers_str.split(CRLF)[1..-1]
      get_request_line(first_line).merge(get_headers(following_lines))
    end

    def body
      @content.split(CRLF * 2)[1]
    end

    private

    def headers_str
      @content.split(CRLF * 2)[0]
    end

    def get_request_line(str)
      # turn first line of headers into hash
      a = str.split(" ")
      {method: a[0], path: a[1], version: a[2]}
    end

    def get_headers(lines)
      # turn following lines of headers into hash
      lines.map do |x|
        a = x.split(SEPERATOR)
        [symbolize_it(a[0]), a[1]]
      end.to_h
    end

    def symbolize_it(str)
      # make it lower case
      # sub '-'' with '_'
      str.downcase.gsub('-', '_').to_sym
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opensaz-0.1.0 lib/opensaz/http_request.rb