Weasel Diesel Sinatra
Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra apps using the Weasel Diesel DSL
$ gem install 'wd_sinatra'
Once the gem is installed, you can use the generator to create a new
app. Go to the location where you want to generate a new app and type
the following command (replace
<app_name> by the name you want to
application to have):
$ wd_sinatra <app_name>
Check the newly generated app
$ cd <app_name>
You'll need bundler to install the dependencies:
$ gem install bundler $ bundle install
Starting the server
The app is now ready to use, to start it you can use rack:
$ bundle exec rackup
This will start the server on port 9292 and the default GET
/hello_world service will be available at: `http://localhost:9292/hello_world'.
Note that the code won't be reloading automatically in the server when you make a modification to the source code. For that, you might want to use Puma + Guard or another tool that allows you to do that. While it's a nice feature to have, a lot of developers like to do that differently and it seems more sensitive to let them pick the way they like the most.
$ bundle exec bin/console
The console mode is like the server mode but without the server only concerns such as the sinatra routes config and Rack middleware.
$ rake doc:services
To generate documentation for the APIs you created in the api folder.
Writing a service
TODO see Weasel Diesel for now.
Config and hooks
config/app.rb file is being required after the environment is set
but before the models are loaded. This is the perfect place to load
custom libraries and set your datastore.
This is where you will for instance load ActiveRecord and set your DB connection.
The files in
be used to set environment specific configuration or other.
If you add a new environment such as staging, you can add a
file in the environments folder that will only get required when running
in this env mode.
Whatever the environment is, the
being required before the specific env file.
The request dispatcher offers 3 hooks which you can see demonstrated in
The two first hooks are used to process the params and when implemented are expected to return the params that will be used in the request.
pre_dispatch_hook is called just before the request is dispatched
to the service implementation. This is where you might want to implement
an authentication verification system for instance.
These 3 hooks have access to the entire request context, including the
service being called. You can use the service
extra option to set
some custom settings that can then be used in this pre dispatch hook.
In the default generated application, a body parser is provided to parse
JSON requests when the HTTP verb is PUT, POST or DELETE. The json parser is set by default to use the
JSON module but you might want to change it to use Yajl for instance.
To do that, edit the
config/hooks.rb file and change the following:
BodyParser.json_parser = JSON
BodyParser.json_parser = Yajl::Parser
Of course, you'll need to require
Yajl first and add it to your
Gemfile if you want to use Bundler.
Using an ORM
TODO: see https://github.com/mattetti/sinatra-web-api-example/ for now for an example of setting up ActiveRecord. Eventually the generator will take an option to generate an AR, DM or other ORM template.
To update your app, just update your gem dependency on
can also compare the difference between your app and a freshly generated
app by trying to generate a new app named the same as your old app.
The generator will detect conflicts and let you pick an action (diff,
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Added some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request