-
-
Save ShopifyEng/7e3542df1c5491ec491627747776f517 to your computer and use it in GitHub Desktop.
How to Build a Web App with and without Rails Libraries
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
# Rack app and Rack Requests: | |
# * Transform Mirth into an application that | |
# follows the Rack specifications and uses | |
# Rack::Request to handle requests | |
# * Use Puma as an application server | |
require 'yaml/store' | |
# Require the relevant libraries | |
require 'rack' | |
require 'rack/handler/puma' | |
app = -> environment { | |
request = Rack::Request.new(environment) | |
store = YAML::Store.new("mirth.yml") | |
if request.get? && request.path == "/show/birthdays" | |
status = 200 | |
content_type = "text/html" | |
response_message = "<ul>\n" | |
all_birthdays = {} | |
store.transaction do | |
all_birthdays = store[:birthdays] | |
end | |
all_birthdays.each do |birthday| | |
response_message << "<li> #{birthday[:name]}</b> was born on #{birthday[:date]}!</li>\n" | |
end | |
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> | |
</form> | |
STR | |
elsif request.post? && request.path == "/add/birthday" | |
status = 303 | |
content_type = "text/html" | |
response_message = "" | |
# Instead of decoding the body, we can | |
# use #params to get the decoded body | |
new_birthday = request.params | |
store.transaction do | |
store[:birthdays] << new_birthday.transform_keys(&:to_sym) | |
end | |
else | |
status = 200 | |
content_type = "text/plain" | |
response_message = "✅ Received a #{request.request_method} request to #{request.path}" | |
end | |
# Return 3-element Array | |
headers = { | |
'Content-Type' => "#{content_type}; charset=#{response_message.encoding.name}", | |
"Location" => "/show/birthdays" | |
} | |
body = [response_message] | |
[status, headers, body] | |
} | |
# Run the application with Puma | |
Rack::Handler::Puma.run(app, :Port => 1337, :Verbose => true) |
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
--- | |
:birthdays: | |
- :name: Gma | |
:date: 01/01/2021 | |
- :name: Tom | |
:date: 02/01/2021 | |
- :name: Sesame | |
:date: 03/01/2021 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment