Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring all the docs. Aiming to make README more concise, and pro…
…vide links to further documentation.
- Loading branch information
Showing
6 changed files
with
286 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
### Adapters | ||
|
||
Webmachine includes adapters for [WEBrick][webrick], [Reel][reel], and | ||
[HTTPkit][httpkit]. Additionally, the [Rack][rack] adapter lets it | ||
run on any webserver that provides a Rack interface. It also lets it run on | ||
[Shotgun][shotgun] ([example][shotgun_example]). | ||
|
||
#### A Note about Rack | ||
|
||
In order to be compatible with popular deployment stacks, | ||
Webmachine has a [Rack](https://github.com/rack/rack) adapter (thanks to Jamis Buck). | ||
**n.b.:** We recommend that NO middleware is used. The | ||
behaviors that are encapsulated in Webmachine assume that no modifications | ||
are done to requests or response outside of Webmachine. | ||
|
||
## A Note about MRI 1.9 | ||
|
||
The [Reel][reel] and [HTTPkit][httpkit] | ||
adapters might crash with a `SystemStackError` on MRI 1.9 due to its | ||
limited fiber stack size. If your application is affected by this, the | ||
only known solution is to switch to JRuby, Rubinius or MRI 2.0. | ||
|
||
[webrick]: http://rubydoc.info/stdlib/webrick | ||
[reel]: https://github.com/celluloid/reel | ||
[httpkit]: https://github.com/lgierth/httpkit | ||
[rack]: https://github.com/rack/rack | ||
[shotgun]: https://github.com/rtomayko/shotgun | ||
[shotgun_example]: https://gist.github.com/4389220 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Application/Configurator | ||
|
||
A call to `Webmachine::Application#configure` returns a `Webmachine::Application` instance, | ||
so you could chain other method calls if you like. If you don't want to create your own separate | ||
application object `Webmachine.application` will return a global one. | ||
|
||
```ruby | ||
require 'webmachine' | ||
require 'my_resource' | ||
|
||
Webmachine.application.configure do |config| | ||
config.ip = '127.0.0.1' | ||
config.port = 3000 | ||
config.adapter = :WEBrick | ||
end | ||
|
||
# Start a web server to serve requests via localhost | ||
Webmachine.application.run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# GET | ||
* Override `resource_exists?`, `content_types_provided`, `allowed_methods`, and implement the method to render the resource. | ||
|
||
```ruby | ||
class OrderResource < Webmachine::Resource | ||
def allowed_methods | ||
["GET"] | ||
end | ||
|
||
def content_types_provided | ||
[["application/json", :to_json]] | ||
end | ||
|
||
def resource_exists? | ||
@order = Order.find(id) | ||
end | ||
|
||
def to_json | ||
@order.to_json | ||
end | ||
|
||
private | ||
|
||
def id | ||
request.path_info[:id] | ||
end | ||
end | ||
|
||
``` | ||
|
||
# POST to create a new resource in a collection | ||
* Override `post_is_create?` to return true | ||
* Override `create_path` to return the relative path to the new resource. Note that this will be called _before_ the resource is actually created, which means that you need to know the ID before the object has been inserted into the database. This might seem a hassle, but it stops you from exposing your database column IDs to the world, which is a naughty and lazy habit we've all picked up from Rails. | ||
* The response Content-Type and status will be set for you. | ||
|
||
```ruby | ||
class OrdersResource < Webmachine::Resource | ||
|
||
def allowed_methods | ||
["POST"] | ||
end | ||
|
||
def content_types_accepted | ||
[["application/json", :from_json]] | ||
end | ||
|
||
def post_is_create? | ||
true | ||
end | ||
|
||
def create_path | ||
@id = Order.next_id | ||
"/orders/#@id" | ||
end | ||
|
||
private | ||
|
||
def from_json | ||
order = Order.new(params).save(@id) | ||
response.body = order.to_json | ||
end | ||
|
||
def params | ||
JSON.parse(request.body.to_s) | ||
end | ||
end | ||
``` | ||
|
||
# POST to perform a task | ||
* Override `allowed_methods` and `process_post`. Put all the code to be executed in `process_post`. | ||
* `process_post` must return true, or the HTTP response code | ||
* Response headers like Content-Type will need to be set manually. | ||
|
||
```ruby | ||
class DispatchOrderResource < Webmachine::Resource | ||
|
||
def allowed_methods | ||
["POST"] | ||
end | ||
|
||
def resource_exists? | ||
@order = Order.find(id) | ||
end | ||
|
||
def process_post | ||
@order.dispatch | ||
response.headers['Content-Type'] = 'text/plain' | ||
response.body = "Successfully dispatched order #{id}" | ||
true | ||
end | ||
|
||
private | ||
|
||
def id | ||
request.path_info[:id] | ||
end | ||
end | ||
|
||
``` | ||
|
||
# PUT | ||
* Override `resource_exists?`, `content_types_accepted`, `allowed_methods`, and implement the method to create/replace the resource. | ||
|
||
```ruby | ||
class OrderResource < Webmachine::Resource | ||
|
||
def allowed_methods | ||
["PUT"] | ||
end | ||
|
||
def content_types_accepted | ||
[["application/json", :from_json]] | ||
end | ||
|
||
def resource_exists? | ||
@order = Order.find(id) | ||
end | ||
|
||
def from_json | ||
# Remember PUT should replace the entire resource, not merge the attributes! That's what PATCH is for. | ||
# It's also why you should not expose your database IDs as your API IDs. | ||
@order.destroy if @order | ||
new_order = Order.new(params) | ||
new_order.save(id) | ||
response.body = new_order.to_json | ||
end | ||
|
||
private | ||
|
||
def params | ||
JSON.parse(request.body.to_s) | ||
end | ||
|
||
def id | ||
request.path_info[:id] | ||
end | ||
end | ||
``` | ||
|
||
# PATCH | ||
* Webmachine does not currently support PATCH requests. See https://github.com/seancribbs/webmachine-ruby/issues/109 for more information and https://github.com/bethesque/pact_broker/blob/2918814e70bbda14df68598a6a41502a5eac4308/lib/pact_broker/api/resources/pacticipant.rb for a dirty hack to make it work if you need to. | ||
|
||
# DELETE | ||
* Override `resource_exists?` and `delete_resource` | ||
* `delete_resource` must return true | ||
* See callbacks.rb for documentation on asynchronous deletes. | ||
|
||
```ruby | ||
class OrderResource < Webmachine::Resource | ||
|
||
def allowed_methods | ||
["DELETE"] | ||
end | ||
|
||
def resource_exists? | ||
@order = Order.find(id) | ||
end | ||
|
||
def delete_resource | ||
Order.find(id).destroy | ||
true | ||
end | ||
|
||
private | ||
|
||
def id | ||
request.path_info[:id] | ||
end | ||
|
||
end | ||
``` |
Oops, something went wrong.