Class: OneApi::DummyWebServer

Inherits:
Object
  • Object
show all
Defined in:
lib/oneapi-ruby/utils.rb

Overview

Web server, to be used only in examples

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_address, port) ⇒ DummyWebServer

Returns a new instance of DummyWebServer.



103
104
105
106
107
# File 'lib/oneapi-ruby/utils.rb', line 103

def initialize(ip_address, port)
    @webserver = TCPServer.new(ip_address, port)
    @session = nil
    @requests = []
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



101
102
103
# File 'lib/oneapi-ruby/utils.rb', line 101

def requests
  @requests
end

Instance Method Details

#parse_request_string(request_string) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/oneapi-ruby/utils.rb', line 131

def parse_request_string request_string
    lines = request_string.split(/\n/)
    method, url, http_version = lines[0].split(' ')
    headers = {}
    body = nil
    for line in lines[1,lines.length]
        if body == nil
            if line.strip == ''
                body = ''
            else
                index = line.index ':'
                key = line[0,index].strip
                value = line[index + 1, line.length].strip
                headers[key] = value
            end
        end

        if body != nil
            body += line + "\n"
        end
    end
    return method, url, headers, body.strip
end

#start(seconds) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/oneapi-ruby/utils.rb', line 109

def start(seconds)
    Thread.new {
        while (@session = @webserver.accept)
            request_string = @session.sysread 10000
            @session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
            @session.print("OK")
            @session.close
            @requests.push(parse_request_string request_string)
        end
    }

    sleep(seconds)

    @webserver.close
    if @session != nil
        begin
            @session.close
        rescue
        end
    end
end