Last active
December 27, 2015 17:39
-
-
Save spiegela/7363485 to your computer and use it in GitHub Desktop.
More experimental RESTful router implementation
This file contains hidden or 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
| defmodule RESTRouter.FruitCollectionHandler do | |
| provides "application/json" | |
| provides "text/html" | |
| provides "text/plain" | |
| accepts "application/json" | |
| defp fetch | |
| Fruit.to_sequence.reverse | |
| end | |
| def write fruit_tuple | |
| fruit = Fruit.last | |
| id = if fruit do | |
| fruit.id + 1 | |
| else | |
| 1 | |
| end | |
| fruit = Fruit[id: id, content: conn.params[:content]] | |
| fruit.write | |
| fruit | |
| end | |
| end |
This file contains hidden or 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
| defmodule RESTRouter.FruitObjectHandler do | |
| defp fetch | |
| Fruit.read conn.params[:fruit_id] | |
| end | |
| defp write fruit_id, fruit_tuple | |
| # Some code to modify an existing fruit | |
| end | |
| def delete | |
| # Some code to delete an existing fruit | |
| end | |
| provides "application/json" # Assumes default provide_json | |
| provides "text/html" # Assumes default provide_html | |
| provides "text/plain" # Assumes default provide_plain | |
| accepts "application/json" # Assumes default accept_json | |
| def service_available | |
| # Some code... | |
| end | |
| def last_modified | |
| # Some code... | |
| end | |
| # Other REST callbacks are supported as well. | |
| end |
This file contains hidden or 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
| defmodule RESTRouter.FruitObjectHandler do | |
| # [REQUIRED] | |
| defp fetch | |
| Fruit.read conn.params[:fruit_id] | |
| end | |
| # [OPTIONAL] required if any _accept_ callback is defined | |
| defp write fruit_id, fruit_tuple | |
| # Some code to modify an existing fruit | |
| end | |
| # [OPTIONAL] required if delete method is accepted | |
| def delete | |
| # Some code to delete an existing fruit | |
| end | |
| provides "application/json" # Assumes default provide_json | |
| provides "text/html" # Assumes default provide_html | |
| provides "text/plain" # Assumes default provide_plain | |
| accepts "application/json" # Assumes default accept_json | |
| # [OPTIONAL] default is method below. | |
| def provide_json | |
| fetch |> Jiffy.encode |> conn.resp_body | |
| end | |
| # [OPTIONAL] default is method below. | |
| def provide_html | |
| conn.assign :collection, fetch | |
| render conn, "fruit_object.html" | |
| end | |
| # [OPTIONAL] default is method below. | |
| def provide_plain | |
| conn.assign :collection, fetch | |
| render conn, "fruit_object.txt" | |
| end | |
| # [OPTIONAL] default is method below. | |
| def accept_json | |
| Jiffy.decode conn.sent_body |> write | |
| end | |
| # [OPTIONAL] default values below | |
| allowed_methods ["GET", "PUT", "DELETE", "OPTIONS"] | |
| # [OPTIONAL] default value: true | |
| def service_available | |
| # Some code... | |
| end | |
| # [OPTIONAL] default value: nil | |
| def last_modified | |
| # Some code... | |
| end | |
| # Other REST callbacks are supported as well. | |
| end |
This file contains hidden or 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
| defmodule RESTRouter do | |
| use Dynamo | |
| use Dynamo.Router | |
| prepare do | |
| conn.fetch [:params, :cookies] | |
| end | |
| config :server, port: 3030 | |
| # New resource macro for simple restful handlers | |
| resource :fruit do | |
| # Automatically creates routes | |
| # /fruit <- for collections | |
| # /fruit/:fruit_id <- for objects | |
| # | |
| # Assumes default handler names: | |
| # RESTRouter.FruitCollectionHandler & RESTRouter.FruitObjectHandler | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment