Skip to content

Instantly share code, notes, and snippets.


ShopifyEng/mirth-3.rb Secret

Created Mar 25, 2021
What would you like to do?
How to Build a Web App with and without Rails Libraries
# mirth-3.rb
# HTTP Responses:
# Respond to HTTP GET and POST request
# with a HTTP response
require 'socket'
server =
# Default data that are not persistent
birthdays = [
{ name: 'Gma', date: '01/01/2021' },
{ name: 'Tom', date: '02/01/2021' },
{ name: 'Sesame', date: '03/01/2021' },
loop do
client = server.accept
request_line = client.readline
method_token, target, version_number = request_line.split
case [method_token, target]
when ["GET", "/show/birthdays"]
response_status_code = "200 OK"
content_type = "text/html"
response_message = ""
response_message << "<ul>\n"
birthdays.each do |birthday|
response_message << "<li> #{birthday[:name]}</b> was born on #{birthday[:date]}!</li>\n"
response_message << "</ul>\n"
response_message << <<~STR
<form action="/add/birthday" method="post" enctype="application/x-www-form-urlencoded">
<p><label>Name <input type="text" name="name"></label></p>
<p><label>Birthday <input type="date" name="date"></label></p>
<p><button>Submit birthday</button></p>
when ["POST", "/add/birthday"]
response_status_code = "303 See Other"
content_type = "text/html"
response_message = ""
# Break apart header fields to get the
# Content-Length which will help us get the body
# of the message
all_headers = {}
while true
line = client.readline
break if line == "\r\n"
header_name, value = line.split(": ")
all_headers[header_name] = value
body =['Content-Length'].to_i)
# Use Ruby's built in decoder library
# to decode the body into a Hash object
require 'uri'
new_birthday = URI.decode_www_form(body).to_h
birthdays << new_birthday.transform_keys(&:to_sym)
response_status_code = "200 OK"
response_message = "✅ Received a #{method_token} request to #{target} with #{version_number}"
content_type = "text/plain"
puts response_message
# Construct the HTTP Response
http_response = <<~MSG
#{version_number} #{response_status_code}
Content-Type: #{content_type}; charset=#{}
Location: /show/birthdays
client.puts http_response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment