Permalink
Browse files

Merge pull request #144 from Tonkpils/options-support

Add support for OPTIONS method
  • Loading branch information...
2 parents f535475 + fcd49c7 commit d205495e947312232f8eb3ddb2a1c3976de1d558 @devinus committed Dec 9, 2013
Showing with 23 additions and 3 deletions.
  1. +13 −2 lib/dynamo/http/case.ex
  2. +9 −1 lib/dynamo/router/base.ex
  3. +1 −0 test/dynamo/router/base_test.exs
View
@@ -46,7 +46,7 @@ defmodule Dynamo.HTTP.Case do
end
The example above will automatically work, since
- `get`/`post`/`put`/`patch`/`delete` recycles the connection before
+ `get`/`post`/`put`/`patch`/`delete`/`options` recycles the connection before
each request.
When recycled, all response information previously set in
@@ -74,7 +74,7 @@ defmodule Dynamo.HTTP.Case do
If the connection was already recycled, it won't be recycled once again.
- Finally, notice that all `get`/`post`/`put`/`patch`/`delete` macros
+ Finally, notice that all `get`/`post`/`put`/`patch`/`delete`/`options` macros
are simply a proxy to `process/4`. So in case you want to dispatch
to different dynamos at the same time, `process/4` may be useful.
"""
@@ -151,6 +151,17 @@ defmodule Dynamo.HTTP.Case do
do_method :DELETE, arg1, arg2
end
+ @doc """
+ Does a OPTIONS request to the given path:
+
+ options("/foo")
+ options(conn, "/foo")
+
+ """
+ defmacro options(arg1, arg2 // nil) do
+ do_method :OPTIONS, arg1, arg2
+ end
+
defp do_method(method, arg1, nil) do
quote do
unquote(__MODULE__).process @endpoint, unquote(method), unquote(arg1)
View
@@ -34,7 +34,7 @@ defmodule Dynamo.Router.Base do
In the example above, a request will only match if it is
a `GET` request and the route "/hello". The supported
- verbs are `get`, `post`, `put`, `patch` and `delete`.
+ verbs are `get`, `post`, `put`, `patch`, `delete` and `options`.
A route can also specify parameters which will then be
available in the function body:
@@ -320,6 +320,14 @@ defmodule Dynamo.Router.Base do
compile(:generate_match, path, Keyword.merge(contents, via: :delete))
end
+ @doc """
+ Dispatches to the path only if it is options request.
+ See `match/3` for more examples.
+ """
+ defmacro options(path, contents) do
+ compile(:generate_match, path, Keyword.merge(contents, via: :options))
+ end
+
## Match Helpers
# Entry point for both forward and match that is actually
@@ -134,6 +134,7 @@ defmodule Dynamo.Router.BaseTest do
assert put("/8/foo").assigns[:value] == 8
assert patch("/8/foo").assigns[:value] == 8
assert delete("/8/foo").assigns[:value] == 8
+ assert options("/8/foo").assigns[:value] == 8
end
test "forwarding to another endpoint" do

0 comments on commit d205495

Please sign in to comment.