Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aristotelesbr committed Mar 23, 2024
1 parent 0539f40 commit 7b20d07
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rack'
require 'sqlite3'
require 'erb'
require 'json'

# Rack application
#
Expand Down
19 changes: 19 additions & 0 deletions examples/001.ru
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 28 additions & 0 deletions examples/002.ru
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions examples/003.ru
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions examples/004.ru
Original file line number Diff line number Diff line change
@@ -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


15 changes: 15 additions & 0 deletions examples/gurupi_middleware.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7b20d07

Please sign in to comment.