https://github.com/katzer/mruby-r3
mruby bindings for libr3
https://github.com/katzer/mruby-r3
libr3 mruby-bindings mruby-gem path-dispatcher
Last synced: 5 months ago
JSON representation
mruby bindings for libr3
- Host: GitHub
- URL: https://github.com/katzer/mruby-r3
- Owner: katzer
- License: other
- Created: 2017-05-08T18:17:17.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-08-29T21:34:48.000Z (over 1 year ago)
- Last Synced: 2024-08-29T23:46:21.248Z (over 1 year ago)
- Topics: libr3, mruby-bindings, mruby-gem, path-dispatcher
- Language: C
- Homepage:
- Size: 269 KB
- Stars: 15
- Watchers: 4
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mruby-r3
[](https://travis-ci.com/katzer/mruby-r3) [](https://ci.appveyor.com/project/katzer/mruby-r3/branch/master)
mruby binding for [libr3][r3], a high-performance path dispatching library.
```ruby
tree << '/users/{user_id}/feeds/{feed_id}'
tree.match '/users/1/feeds/2'
# => { user_id: '1', feed_id: '2' }
```
## Installation
Add the line below to your `build_config.rb`:
```ruby
MRuby::Build.new do |conf|
# ... (snip) ...
conf.gem mgem: 'mruby-regexp-pcre' # Optional
conf.gem mgem: 'mruby-r3'
end
```
Or add this line to your aplication's `mrbgem.rake`:
```ruby
MRuby::Gem::Specification.new('your-mrbgem') do |spec|
# ... (snap) ...
spec.add_dependency 'mruby-regexp-pcre' # Optional
spec.add_dependency 'mruby-r3'
end
```
## Usage
First of all a tree object has to be created.
```ruby
tree = R3::Tree.new
```
The size grows dynamically. However the initializer accepts an optional initial size.
The default initial size is up to 5 routes.
```ruby
tree = R3::Tree.new(100)
```
The pattern syntax for routes is the following. Enhanced support for regular expression requires __mruby-regexp-pcre__ to be installed!
/blog/post/{id} use [^/]+ regular expression by default.
/blog/post/{id:\d+} use `\d+` regular expression instead of default.
Routes can be added to the tree at any time, however dont forget to call __compile__ before using them.
```ruby
tree << '/'
tree << '/blog/post'
tree << '/blog/post/{id}'
tree << '/user/{user_id}/feeds/{feed_id}'
```
Its also possible to specify the HTML method rather then to allow any.
```ruby
tree.add('/blog/post/{id:\\d+}', R3::DELETE)
```
Once the tree has been compiled he's ready for dispatching.
```ruby
tree << '/'
tree.add '/users', R3::ANY
tree.add '/posts', R3::GET
tree.add '/users/{user_id}/feeds/{feed_id}', R3::GET
tree.compile
tree.routes
# => ['GET /users/{user_id}/feeds/{feed_id}', ...]
tree.match? '/'
# => true
tree.match '/'
# => {}
tree.match? '/other'
# => false
tree.match '/other'
# => nil
tree.match? '/users', R3::POST
# => true
tree.match? '/posts', R3::POST
# => false
tree.match '/users/1/feeds/2'
# => { user_id: '1', feed_id: '2' }
tree.match '/users/1/feeds/2', R3::POST
# => nil
```
Before you're writing your own URL map, you can make use of the built-in feature to add any kind of data with the route.
```ruby
tree.add('/user/{name}', R3::ANY, -> { 'callback handler' })
tree.compile
params, handler = tree.match('/user/bernd')
handler.call
# => 'callback handler'
```
## Development
Clone the repo:
$ git clone https://github.com/katzer/mruby-r3.git && cd mruby-r3/
Compile the source:
$ rake compile
Run the tests:
$ rake test
## TODO
1. Add a route for multiple HTTP methods.
2. missing asprintf() for MingGW 32-bit compiler.
## Authors
- Sebastián Katzer, Fa. appPlant GmbH
## License
The mgem is available as open source under the terms of the [MIT License][license].
Made with :yum: in Leipzig
© 2017 [appPlant GmbH][appplant]
[r3]: https://github.com/c9s/r3
[license]: http://opensource.org/licenses/MIT
[appplant]: www.appplant.de