class UDPRest::UHTTPRequest

Attributes

path[RW]
protocol[RW]
query[RW]
req_method[RW]

Public Class Methods

from_packet(p) click to toggle source
# File lib/udp_rest/uhttp.rb, line 19
def self.from_packet(p)
    text = p
    text = p.text if text.is_a? UDPPacket
    data = text.split(' ')

    raise 'invalid request' if data.length != 3
    req = self.new
    req.req_method = data[0]
    req.protocol = data[2]
    
    path_data = data[1].split('?')
    req.path = path_data[0]
    req.query = path_data[1] || '' if path_data.length > 1

    return req
end
new() click to toggle source
# File lib/udp_rest/uhttp.rb, line 13
def initialize
    self.req_method = 'GET'
    self.protocol = 'UHTTP/1.0'
    self.path ='/'
end

Public Instance Methods

params() click to toggle source
# File lib/udp_rest/uhttp.rb, line 49
def params
    return {} if query.nil? || query.strip == ''

    if @params.nil?
        @params = {}
        p = CGI.parse(self.query)
        p.each {|k,v| @params[k] = v.first }
    end

    @params
end
path_and_query() click to toggle source
# File lib/udp_rest/uhttp.rb, line 41
def path_and_query
    if query.nil? || query.strip == ''
        path
    else
        path + '?' + query
    end
end
to_s() click to toggle source
# File lib/udp_rest/uhttp.rb, line 36
def to_s
    self.path = '/' if path.nil? || path.empty?
    "#{req_method} #{path_and_query} #{protocol}\n"
end