class UDPRest::Client

Attributes

host[RW]
port[RW]
socket[RW]
timeout[RW]

Public Class Methods

get(url) click to toggle source
# File lib/udp_rest.rb, line 134
def self.get(url)
        self.uhttp('GET', url)
end
new(host, port) click to toggle source
# File lib/udp_rest.rb, line 99
def initialize(host, port)
        @max_packet_size = 512

        self.host = host
        self.port = port
        self.socket = UDPSocket.new
        self.timeout = 5.0
end
uhttp(req_method, url) click to toggle source
# File lib/udp_rest.rb, line 121
def self.uhttp(req_method, url)
        uri = URI(url)
        client = self.new(uri.host, uri.port || 80)
        
        req = UHTTPRequest.new
        req.req_method = req_method
        req.path = uri.path
        req.query = uri.query

        packet = client.send_text(req.to_s)
        UHTTPResponse.parse(packet.text)
end

Public Instance Methods

send_text(text) click to toggle source
# File lib/udp_rest.rb, line 108
def send_text(text)
        thread = WorkerThread.new.start :timeout => self.timeout do
                self.socket.send(text, 0, self.host, self.port)
                response_data = self.socket.recvfrom(@max_packet_size)
                UDPPacket.new(response_data)
        end

        thread.join
        packet = thread.value
        raise "Request Timeout (#{host}:#{port})" if packet.nil?
        return packet
end