rowdy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +65 -0
- data/LICENSE.txt +21 -0
- data/README.md +194 -0
- data/Rakefile +10 -0
- data/config.ru +33 -0
- data/lib/rowdy/version.rb +5 -0
- data/lib/rowdy.rb +167 -0
- data/rowdy.gemspec +39 -0
- data/sig/rowdy.rbs +4 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9623d2965f89d64e509afc01ea669df1176fe67446d5f7b48178eb98cdb1eae0
|
4
|
+
data.tar.gz: 6fa39ae3152fcdb3fac39d7b8fb0ac3f06812afa8017d32269df6e8dfda28677
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 180a1c59223070e1dd979e16b53c15f9d9f4035ac80687f71f711d4ea590141c0e506613ac892a863bb7f5f29f244237540087e7bd15b9440d2c550c49105245
|
7
|
+
data.tar.gz: 8a8aa8c42dd476e787a032b22d6e16b6174d6365b0a58fd13d57a26a8198482eef1f8227bffa89342c56e028ae1c4b17336fb81df4f5b5ceca49e9494dda6441
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at bradgessler@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rowdy (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
json (2.6.3)
|
12
|
+
language_server-protocol (3.17.0.2)
|
13
|
+
parallel (1.22.1)
|
14
|
+
parser (3.2.0.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
rainbow (3.1.1)
|
17
|
+
rake (13.0.6)
|
18
|
+
regexp_parser (2.7.0)
|
19
|
+
rexml (3.2.5)
|
20
|
+
rspec (3.12.0)
|
21
|
+
rspec-core (~> 3.12.0)
|
22
|
+
rspec-expectations (~> 3.12.0)
|
23
|
+
rspec-mocks (~> 3.12.0)
|
24
|
+
rspec-core (3.12.1)
|
25
|
+
rspec-support (~> 3.12.0)
|
26
|
+
rspec-expectations (3.12.2)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.12.0)
|
29
|
+
rspec-mocks (3.12.3)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.12.0)
|
32
|
+
rspec-support (3.12.0)
|
33
|
+
rubocop (1.42.0)
|
34
|
+
json (~> 2.3)
|
35
|
+
parallel (~> 1.10)
|
36
|
+
parser (>= 3.1.2.1)
|
37
|
+
rainbow (>= 2.2.2, < 4.0)
|
38
|
+
regexp_parser (>= 1.8, < 3.0)
|
39
|
+
rexml (>= 3.2.5, < 4.0)
|
40
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
41
|
+
ruby-progressbar (~> 1.7)
|
42
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
43
|
+
rubocop-ast (1.24.1)
|
44
|
+
parser (>= 3.1.1.0)
|
45
|
+
rubocop-performance (1.15.2)
|
46
|
+
rubocop (>= 1.7.0, < 2.0)
|
47
|
+
rubocop-ast (>= 0.4.0)
|
48
|
+
ruby-progressbar (1.11.0)
|
49
|
+
standard (1.22.0)
|
50
|
+
language_server-protocol (~> 3.17.0.2)
|
51
|
+
rubocop (= 1.42.0)
|
52
|
+
rubocop-performance (= 1.15.2)
|
53
|
+
unicode-display_width (2.4.2)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
arm64-darwin-22
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
rake (~> 13.0)
|
60
|
+
rowdy!
|
61
|
+
rspec (~> 3.0)
|
62
|
+
standard (~> 1.3)
|
63
|
+
|
64
|
+
BUNDLED WITH
|
65
|
+
2.4.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Brad Gessler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# Rowdy
|
2
|
+
|
3
|
+
Rowdy is a gem that takes a PORO approach to Ruby web routing using modern Ruby features, like pattern matching. Here's what Rowdy looks like in action.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
Blog = Data.define(:title, :description)
|
7
|
+
Post = Data.define(:title, :body)
|
8
|
+
|
9
|
+
class Application
|
10
|
+
include Rowdy::Routing
|
11
|
+
|
12
|
+
def initialize(model:)
|
13
|
+
@model = model
|
14
|
+
end
|
15
|
+
|
16
|
+
def route(http)
|
17
|
+
http.response.headers["Content-Type"] = "text/plain"
|
18
|
+
http.response.status = 200
|
19
|
+
|
20
|
+
case http.request
|
21
|
+
in root: true
|
22
|
+
"Hello from Rowdy!"
|
23
|
+
in path: [ "blogs", id, *_ ]
|
24
|
+
Rowdy::Controller::Resources.new(scope: Blog).route(http)
|
25
|
+
in path: [ "posts", id, *_ ]
|
26
|
+
Rowdy::Controller::Resources.new(scope: Post).route(http)
|
27
|
+
else
|
28
|
+
http.response.status = 404
|
29
|
+
http.response.write "Not Found"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
If you tried to do this in Rails, you'd have to generate a controller per resource even though they probably do the same thing. Since Rowdy embraces a full PORO approach to building web applications, you can compose classes in a very object-oriented way.
|
36
|
+
|
37
|
+
Rowdy is composed if the three follow concepts:
|
38
|
+
|
39
|
+
* **Application** - This is the main routing file, as depicted above.
|
40
|
+
|
41
|
+
* **Controller** - Controllers are a collection of similar Actions, but not like you're thinking. In Rails, a controller has various methods that are actions. If you want to do things before and after the action, you're going to get stuck in callback soup. Rowdy is different in that action action is a class, so you can create a `ProtectedAction` subclass that requires a login, then use that subclass in the controller.
|
42
|
+
|
43
|
+
* **Action** - An action is a plain 'ol Ruby object that includes `Rowdy::Routing`.
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
|
47
|
+
Install the gem and add to the application's Gemfile by executing:
|
48
|
+
|
49
|
+
$ bundle add rowdy
|
50
|
+
|
51
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
52
|
+
|
53
|
+
$ gem install rowdy
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
Create something like this in `app.rb`
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Blog = Data.define(:title, :description)
|
61
|
+
Post = Data.define(:title, :body)
|
62
|
+
|
63
|
+
class Application
|
64
|
+
include Rowdy::Routing
|
65
|
+
|
66
|
+
def initialize(model:)
|
67
|
+
@model = model
|
68
|
+
end
|
69
|
+
|
70
|
+
def route(http)
|
71
|
+
http.response.headers["Content-Type"] = "text/plain"
|
72
|
+
http.response.status = 200
|
73
|
+
|
74
|
+
case http.request
|
75
|
+
in root: true
|
76
|
+
"Hello from Rowdy!"
|
77
|
+
in path: [ "blogs", id, *_ ]
|
78
|
+
Rowdy::Controller::Resources.new(scope: Blog).route(http)
|
79
|
+
in path: [ "posts", id, *_ ]
|
80
|
+
Rowdy::Controller::Resources.new(scope: Post).route(http)
|
81
|
+
else
|
82
|
+
http.response.status = 404
|
83
|
+
http.response.write "Not Found"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
Then mount it in the config.ru file:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
require_relative "./app"
|
93
|
+
|
94
|
+
run Application.new
|
95
|
+
```
|
96
|
+
|
97
|
+
The run it.
|
98
|
+
|
99
|
+
```
|
100
|
+
rackup config.ru
|
101
|
+
```
|
102
|
+
|
103
|
+
## Prior art
|
104
|
+
|
105
|
+
The idea of PORO Ruby web frameworks have been around forever, starting with [camping](https://github.com/camping/camping). Since then Ruby has a *ton* of really amazing web & routing frameworks. This section is intended to answer the question, "why Rowdy?". It is **not** intended to dunk on any other Ruby web or routing frameworks.
|
106
|
+
|
107
|
+
### Camping
|
108
|
+
|
109
|
+
Camping get's the closest to what I want. Consider the example code on the [camping Github repo](https://github.com/camping/camping).
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
require 'camping'
|
113
|
+
|
114
|
+
Camping.goes :Blog
|
115
|
+
|
116
|
+
module Blog::Models
|
117
|
+
class Post < Base; belongs_to :user; end
|
118
|
+
class Comment < Base; belongs_to :user; end
|
119
|
+
class User < Base; end
|
120
|
+
end
|
121
|
+
|
122
|
+
module Blog::Controllers
|
123
|
+
class Index
|
124
|
+
def get
|
125
|
+
@posts = Post.find :all
|
126
|
+
render :index
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
module Blog::Views
|
132
|
+
def layout
|
133
|
+
html do
|
134
|
+
head { title "My Blog" }
|
135
|
+
body do
|
136
|
+
h1 "My Blog"
|
137
|
+
self << yield
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def index
|
143
|
+
@posts.each do |post|
|
144
|
+
h1 post.title
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
Everything looks fine at first glance, but if you try to extend `Blog::Controllers`, you can't because it's a module. Instead you'd have to do something like this:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
module ExtendedBlog::Controllers
|
154
|
+
Index = Blog::Controllers::Index
|
155
|
+
# Example above leaves out all the other actions you'd have to manually extend...
|
156
|
+
Show = Blog::Controllers::Show
|
157
|
+
Edit = Blog::Controllers::Edit
|
158
|
+
Delete = Blog::Controllers::Delete
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
Zoiks! That's no fun.
|
163
|
+
|
164
|
+
Rowdy gets around that with the concept of a `Rowdy::Controller` class being a very loose association of `Rowdy::Action` classes. The `Rowdy::Controller` has the methods you'd expect, `#index`, `#show`, etc., but they create instances of `Rowdy:Action` classes with the context you'd expect.
|
165
|
+
|
166
|
+
### Roda
|
167
|
+
|
168
|
+
TODO
|
169
|
+
|
170
|
+
### Sinatra
|
171
|
+
|
172
|
+
TODO
|
173
|
+
|
174
|
+
### Rails
|
175
|
+
|
176
|
+
TODO
|
177
|
+
|
178
|
+
## Development
|
179
|
+
|
180
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
181
|
+
|
182
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
183
|
+
|
184
|
+
## Contributing
|
185
|
+
|
186
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rocketshipio/rowdy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rocketshipio/rowdy/blob/main/CODE_OF_CONDUCT.md).
|
187
|
+
|
188
|
+
## License
|
189
|
+
|
190
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
191
|
+
|
192
|
+
## Code of Conduct
|
193
|
+
|
194
|
+
Everyone interacting in the Rowdy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rocketshipio/rowdy/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative "./lib/rowdy.rb"
|
2
|
+
|
3
|
+
module Model
|
4
|
+
class Person < Data.define(:name, :email)
|
5
|
+
def self.all
|
6
|
+
10.times.map { |n| find n }
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.find(id)
|
10
|
+
new name: "Person #{id}", email: "person-#{id}@example.com"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Application code (dev sees this)
|
16
|
+
class Application < Rowdy::Server
|
17
|
+
def route(http)
|
18
|
+
http.response.headers["Content-Type"] = "text/plain"
|
19
|
+
http.response.status = 200
|
20
|
+
|
21
|
+
case http.request
|
22
|
+
in root: true
|
23
|
+
http.response.write "Hello world!"
|
24
|
+
in path: ["people", *_ ]
|
25
|
+
Rowdy::Controller::Resources.new(scope: Model::Person).route(http)
|
26
|
+
else
|
27
|
+
http.response.write "Not Found"
|
28
|
+
http.response.status = 404
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
run Application.new
|
data/lib/rowdy.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rowdy/version"
|
4
|
+
|
5
|
+
module Rowdy
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
module Routing
|
9
|
+
def call(env)
|
10
|
+
dispatch Transaction.from_rack env
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def dispatch(http)
|
16
|
+
route http
|
17
|
+
http.response.to_a
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Transaction < Data.define(:request, :response)
|
22
|
+
class Request < Rack::Request
|
23
|
+
def request_method
|
24
|
+
super.downcase.to_sym
|
25
|
+
end
|
26
|
+
|
27
|
+
def path_segments
|
28
|
+
path.split("/")[1..-1]
|
29
|
+
end
|
30
|
+
|
31
|
+
def domain_segments
|
32
|
+
host.split(".")
|
33
|
+
end
|
34
|
+
|
35
|
+
def deconstruct_keys(keys)
|
36
|
+
{
|
37
|
+
fullpath: path,
|
38
|
+
path: path_segments,
|
39
|
+
host: host,
|
40
|
+
domain: domain_segments,
|
41
|
+
scheme: scheme,
|
42
|
+
port: port,
|
43
|
+
params: params,
|
44
|
+
method: request_method,
|
45
|
+
ip: ip,
|
46
|
+
root: fullpath == "/"
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Response < Rack::Response
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.from_rack(env)
|
55
|
+
new request: Request.new(env), response: Response.new
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Server
|
60
|
+
include Routing
|
61
|
+
end
|
62
|
+
|
63
|
+
module Action
|
64
|
+
class Base
|
65
|
+
include Routing
|
66
|
+
|
67
|
+
def route(http)
|
68
|
+
http.response.write self.public_send http.request.request_method
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Singular < Base
|
73
|
+
def initialize(model:)
|
74
|
+
@model = model
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Collection < Base
|
79
|
+
def initialize(scope:)
|
80
|
+
@scope = scope
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Index < Collection
|
85
|
+
def get
|
86
|
+
@scope.all
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_json
|
90
|
+
get.to_json
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Show < Singular
|
95
|
+
def get
|
96
|
+
@model
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Edit < Singular
|
101
|
+
def get
|
102
|
+
"Editing #{@model}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
module Controller
|
108
|
+
class Resource
|
109
|
+
include Routing
|
110
|
+
|
111
|
+
def initialize(scope:, id:)
|
112
|
+
@scope = scope
|
113
|
+
@id = id
|
114
|
+
@model = @scope.find(@id)
|
115
|
+
end
|
116
|
+
|
117
|
+
def show
|
118
|
+
Action::Show.new(model: @model)
|
119
|
+
end
|
120
|
+
|
121
|
+
def edit
|
122
|
+
Action::Edit.new(model: @model)
|
123
|
+
end
|
124
|
+
|
125
|
+
def destroy
|
126
|
+
@model.destroy
|
127
|
+
end
|
128
|
+
|
129
|
+
def route(http)
|
130
|
+
case http.request
|
131
|
+
in path: [ _, id ], method: :get
|
132
|
+
show.route http
|
133
|
+
in path: [ _, id, "edit" ], method: :get
|
134
|
+
edit.route http
|
135
|
+
in path: [ _, id ], method: :delete
|
136
|
+
destroy
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class Resources
|
142
|
+
include Routing
|
143
|
+
|
144
|
+
def initialize(scope:, path: "people")
|
145
|
+
@scope = scope
|
146
|
+
@path = path
|
147
|
+
end
|
148
|
+
|
149
|
+
def index
|
150
|
+
Action::Index.new(scope: @scope)
|
151
|
+
end
|
152
|
+
|
153
|
+
def resource(id)
|
154
|
+
Resource.new(scope: @scope, id: id)
|
155
|
+
end
|
156
|
+
|
157
|
+
def route(http)
|
158
|
+
case http.request
|
159
|
+
in path: [ ^@path ], method: :get
|
160
|
+
index.route http
|
161
|
+
in path: [ ^@path, id, *_ ]
|
162
|
+
resource(id).route http
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
data/rowdy.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/rowdy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rowdy"
|
7
|
+
spec.version = Rowdy::VERSION
|
8
|
+
spec.authors = ["Brad Gessler"]
|
9
|
+
spec.email = ["bradgessler@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Route web requests to classes"
|
12
|
+
spec.description = "Routes Rack::Requests to classes"
|
13
|
+
spec.homepage = "https://github.com/rocketshipio/rowdy"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
# Uncomment to register a new dependency of your gem
|
35
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
36
|
+
|
37
|
+
# For more information and examples about making a new gem, check out our
|
38
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
39
|
+
end
|
data/sig/rowdy.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rowdy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Gessler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Routes Rack::Requests to classes
|
14
|
+
email:
|
15
|
+
- bradgessler@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".standard.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- config.ru
|
30
|
+
- lib/rowdy.rb
|
31
|
+
- lib/rowdy/version.rb
|
32
|
+
- rowdy.gemspec
|
33
|
+
- sig/rowdy.rbs
|
34
|
+
homepage: https://github.com/rocketshipio/rowdy
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
allowed_push_host: https://rubygems.org/
|
39
|
+
homepage_uri: https://github.com/rocketshipio/rowdy
|
40
|
+
source_code_uri: https://github.com/rocketshipio/rowdy
|
41
|
+
changelog_uri: https://github.com/rocketshipio/rowdy
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.6.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.4.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Route web requests to classes
|
61
|
+
test_files: []
|