From 7b20d076fbc5e09ba82c8040517b3c57ed592efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles?= Date: Sat, 23 Mar 2024 20:40:34 -0300 Subject: [PATCH] add examples --- .DS_Store | Bin 0 -> 6148 bytes app.rb | 1 - examples/001.ru | 19 ++++++++++++++ examples/002.ru | 28 ++++++++++++++++++++ examples/003.ru | 46 ++++++++++++++++++++++++++++++++ examples/004.ru | 48 ++++++++++++++++++++++++++++++++++ examples/gurupi_middleware.rb | 15 +++++++++++ 7 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 examples/001.ru create mode 100644 examples/002.ru create mode 100644 examples/003.ru create mode 100644 examples/004.ru create mode 100644 examples/gurupi_middleware.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4f8fc6307e6033defbdcb8506e079b52927a5439 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8-BN@e6nb3nTClCPAYMYPFJMFuDm5WNgK4%jsTE2gXMG``#OHBl zcLSF8;7P>Jz~-BspWU4gvOkP5K9~g^#u|(<0S%F(vO&d({3 zhJpT~3D+)Q&9BU3WA<(Reyn`(efX0&O0#zRlb6bso!zRain_RWpVZvVylj>Zyy-3W zu9S-XLihaZD4dTP`{ydjyeJ8$Iw1-|2)Vn7l0eM|YL*14&UKAL)I@F6Xf77LPPZky z{gY)&E>64cmh2t(m&=+sI6OML96iV5M7DZJG4MkMaDR}{5FLxTLA`ar2d~fQuOOm;j&BJ>Y0rRomD?uzQF3J9F`P;d*wkOC8R*W01bY05P!2K-qK~ zc>Z6&FSGHHznVflVt^Raq-D0{Z9m4|1og!TXp1>pnh^RB1HZt)7vzvl9RL6T literal 0 HcmV?d00001 diff --git a/app.rb b/app.rb index 3cf48fa..93fb1d2 100644 --- a/app.rb +++ b/app.rb @@ -1,7 +1,6 @@ require 'rack' require 'sqlite3' require 'erb' -require 'json' # Rack application # diff --git a/examples/001.ru b/examples/001.ru new file mode 100644 index 0000000..a90cd25 --- /dev/null +++ b/examples/001.ru @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'rack' +end + +require 'rack' + +app = + proc do |_env| + [200, { 'content-type' => 'text/html' }, ['Hello, World!']] + end + +run app + +# curl -i "http://localhost:9292" diff --git a/examples/002.ru b/examples/002.ru new file mode 100644 index 0000000..d90d5fa --- /dev/null +++ b/examples/002.ru @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'rack' +end + +require 'rack' + +class MyApp + def call(env) + request = Rack::Request.new(env) + + case [request.request_method, request.path] + in ['GET', '/'] + [200, { 'content-type' => 'text/plain' }, ['Home page']] + in ['POST', '/books'] + [201, { 'content-type' => 'text/plain' }, ['The book was created']] + else [404, { 'content-type' => 'text/html' }, ['Not Found']] + end + end +end + +run MyApp.new + +# curl -i -X POST http://localhost:9292/books diff --git a/examples/003.ru b/examples/003.ru new file mode 100644 index 0000000..11302d2 --- /dev/null +++ b/examples/003.ru @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'rack' +end + +require 'rack' + +class MyApp + def call(env) + request = Rack::Request.new(env) + @response = Rack::Response.new + + case [request.request_method, request.path] + in ['GET', '/'] + @response.status = 200 + @response['content-type'] = 'text/plain' + @response.write('Home page') + + response.finish + in ['POST', '/books'] + @response.status = 201 + @response['content-type'] = 'text/plain' + @response.write('The book was created') + + @response.finish + else not_found + end + end + + def not_found + @response.status = 404 + @response['content-type'] = 'text/plain' + @response.write('Not Found') + + @response.finish + end +end + +run MyApp.new + +# curl -i http://localhost:9292/ +# curl -i -X POST http://localhost:9292/books diff --git a/examples/004.ru b/examples/004.ru new file mode 100644 index 0000000..cff8e18 --- /dev/null +++ b/examples/004.ru @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'rack' +end + +require 'rack' +require_relative 'gurupi_middleware' + +class MyApp + def call(env) + request = Rack::Request.new(env) + @response = Rack::Response.new + + case [request.request_method, request.path] + in ['GET', '/'] + @response.status = 200 + @response['content-type'] = 'text/plain' + @response.write('Home page') + + response.finish + in ['POST', '/books'] + @response.status = 201 + @response['content-type'] = 'text/plain' + @response.write('The book was created') + + @response.finish + else not_found + end + end + + def not_found + @response.status = 404 + @response['content-type'] = 'text/plain' + @response.write('Not Found') + + @response.finish + end +end + +use GurupiMiddleware + +run MyApp.new + + diff --git a/examples/gurupi_middleware.rb b/examples/gurupi_middleware.rb new file mode 100644 index 0000000..94d984f --- /dev/null +++ b/examples/gurupi_middleware.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class GurupiMiddleware + def initialize(app) + @app = app + end + + def call(env) + status, headers, body = @app.call(env) + + headers['x-gurupi'] = 'Gurupi' + + [status, headers, body] + end +end