chespirito 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ea44622e48fc4daec6a855f893208c3a3d5a243a4ea38ff0077e63e29fffc6d
4
- data.tar.gz: a9717fab4ec5e1a1f2285c583f8e0ec85ce9188d6321e379f476a99e2a195ce0
3
+ metadata.gz: 5d3588983bf384783e2066d7bb71175b0fafca6a17c35212526dbbc46f6a5209
4
+ data.tar.gz: a9a8a41c4bbe2113112cc81428b302427861c5dafab63344a6bb54339a270384
5
5
  SHA512:
6
- metadata.gz: 92ba0c82376ec77860b8cda7b934da1a25bcd416a9827640957bafa074a317c411a3f39e39b7fd2e2e923c07ed28e624d5c03bd54262af1fe8650613333023bb
7
- data.tar.gz: b98940782b5be8d742d8c7750d86e576d8ce886f9d2697a9a52e0df7aeb3376497a8b1288a641d831e83c0d881fb4e5b75f30fdf90baebbdbc98aed746239679
6
+ metadata.gz: 85db7c0ac953324ed3c0102ec757af300384d1614a99446ed693f376444cf94dfa713ecca0a4cde330be3c0fd7cbc07628d542b571808e17434893fa6ece2a69
7
+ data.tar.gz: 0bf474e6e562247c38126250bf53b799c66f18a1aeeee451a43c6c5190c83ecc6e0b6156215fea01576269e67f22e9b42d1aa453f189bb8c3751f68fd9ec487f
data/README.md CHANGED
@@ -51,6 +51,52 @@ Usage: make <target>
51
51
  gem.yank Removes a specific version from the Rubygems
52
52
  ```
53
53
 
54
+ ## Boostrapping an application using Chespirito and Adelnor
55
+
56
+ 1. Install the gems:
57
+ ```bash
58
+ $ gem install chespirito adelnor
59
+ ```
60
+
61
+ 2. Register the Chespirito app and routes:
62
+
63
+ ```ruby
64
+ class MyApp
65
+ def self.application
66
+ Chespirito::App.configure do |app|
67
+ app.register_route('GET', '/', [HelloController, :index])
68
+ app.register_route('POST', '/', [HelloController, :create])
69
+ end
70
+ end
71
+ end
72
+ ```
73
+
74
+ 3. Create the Controller and action:
75
+
76
+ ```ruby
77
+ class HelloController < Chespirito::Controller
78
+ def index
79
+ response.status = 200
80
+
81
+ response.headers['Content-Type'] = 'text/html'
82
+
83
+ response.body = '<h1>Hello, world!</h1>'
84
+ end
85
+
86
+ def create
87
+ response.status = 204
88
+ end
89
+ end
90
+ ```
91
+
92
+ 4. Run the app using Adelnor (or you can choose other web server like Puma, Unicorn, etc):
93
+
94
+ ```ruby
95
+ Adelnor::Server.run MyApp.application, 3000
96
+ ```
97
+
98
+ 5. Open `localhost:3000` and cheers!
99
+
54
100
  ----
55
101
 
56
102
  [ASCII art generator](http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20)
data/chespirito.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'chespirito'
5
- spec.version = '0.0.1'
5
+ spec.version = '0.0.2'
6
6
  spec.summary = 'Chespirito Ruby web framework'
7
7
  spec.description = 'A dead simple, yet Rack-compatible, web framework written in Ruby'
8
8
  spec.authors = ['Leandro Proença']
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './request'
4
+ require_relative './router'
4
5
 
5
6
  module Chespirito
6
7
  class App
7
8
  def initialize
8
- @routes = {}
9
+ @router = ::Chespirito::Router.new
9
10
  end
10
11
 
11
12
  def self.configure
@@ -14,19 +15,13 @@ module Chespirito
14
15
  new.tap { |app| yield(app) }
15
16
  end
16
17
 
17
- def register_route(verb, path, trait)
18
- @routes[route_key(verb, path)] = trait
19
- end
20
-
21
- def lookup(request)
22
- controller_klass, action = @routes[route_key(request.verb, request.path)]
23
-
24
- return unless controller_klass
18
+ def register_route(*attrs)
19
+ attrs => [verb, path, trait]
25
20
 
26
- controller_klass.dispatch(action, request)
21
+ @router.register_route(verb, path, trait)
27
22
  end
28
23
 
29
- def route_key(verb, path) = "#{verb} #{path}"
24
+ def lookup(request) = @router.lookup(request)
30
25
 
31
26
  def call(env)
32
27
  request = ::Chespirito::Request.build(env)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './request'
4
+ require_relative './response'
5
+
6
+ module Chespirito
7
+ class Router
8
+ def initialize
9
+ @routes = {}
10
+ end
11
+
12
+ def register_route(verb, path, trait)
13
+ @routes[route_key(verb, path)] = trait
14
+ end
15
+
16
+ def lookup(request)
17
+ controller_klass, action = @routes[route_key(request.verb, request.path)]
18
+
19
+ return not_found_response unless controller_klass
20
+
21
+ controller_klass.dispatch(action, request)
22
+ end
23
+
24
+ def route_key(verb, path) = "#{verb} #{path}"
25
+
26
+ def not_found_response
27
+ ::Chespirito::Response.new.tap do |response|
28
+ response.status = 404
29
+ response.headers = {}
30
+ response.body = ''
31
+ end
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chespirito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Proença
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-22 00:00:00.000000000 Z
11
+ date: 2022-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -53,6 +53,7 @@ files:
53
53
  - lib/chespirito/controller.rb
54
54
  - lib/chespirito/request.rb
55
55
  - lib/chespirito/response.rb
56
+ - lib/chespirito/router.rb
56
57
  homepage: https://github.com/leandronsp/chespirito
57
58
  licenses:
58
59
  - MIT