Class: UDPRest::UHTTPRequest

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUHTTPRequest

Returns a new instance of UHTTPRequest.



13
14
15
16
17
# File 'lib/udp_rest/uhttp.rb', line 13

def initialize
    self.req_method = 'GET'
    self.protocol = 'UHTTP/1.0'
    self.path ='/'
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/udp_rest/uhttp.rb', line 9

def path
  @path
end

#protocolObject

Returns the value of attribute protocol.



11
12
13
# File 'lib/udp_rest/uhttp.rb', line 11

def protocol
  @protocol
end

#queryObject

Returns the value of attribute query.



10
11
12
# File 'lib/udp_rest/uhttp.rb', line 10

def query
  @query
end

#req_methodObject

Returns the value of attribute req_method.



8
9
10
# File 'lib/udp_rest/uhttp.rb', line 8

def req_method
  @req_method
end

Class Method Details

.from_packet(p) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#paramsObject



49
50
51
52
53
54
55
56
57
58
59
# 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_queryObject



41
42
43
44
45
46
47
# File 'lib/udp_rest/uhttp.rb', line 41

def path_and_query
    if query.nil? || query.strip == ''
        path
    else
        path + '?' + query
    end
end

#to_sObject



36
37
38
39
# 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