Created
November 30, 2016 00:49
-
-
Save anonymous/8b9b25a158e5f4d7bf977ab423f30d6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' # Provides TCPServer and TCPSocket classes | |
require "json" | |
server = TCPServer.new('localhost', 2345) | |
loop do | |
socket = server.accept | |
request = socket.gets | |
puts request.split(" ")[1] | |
hydrant_data = [{:lat => "41.5573129", :long => "-82.6637969", :id => "45", :address => "3 Maple Lane", :distance => "71 Miles", :flow_rate => "5000 PSI", :status => "1"}, | |
{:lat => "41.2573129", :long => "-83.6637969", :id => "53", :address => "15 Charles Boulevard", :distance => "84 Miles", :flow_rate => "3200 PSI", :status => "1"}, | |
{:lat => "43.5573129", :long => "-86.6637969", :id => "97", :address => "22 Main Street", :distance => "320 Miles", :flow_rate => "650 PSI", :status => "0"}] | |
response = JSON.generate(hydrant_data) + "\n" | |
socket.print "HTTP/1.1 200 OK\r\n" + | |
"Content-Type: text/plain\r\n" + | |
"Content-Length: #{response.bytesize}\r\n" + | |
"Connection: close\r\n" | |
socket.print "\r\n" | |
socket.print response | |
# Close the socket, terminating the connection | |
socket.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment