Browse files

Renaming Websocket -> Raw Websocket. Adding cowboy_dispatch macro

  • Loading branch information...
1 parent ff7cce3 commit 65e29b381face040f2ae3e52a14ba25fcb69c4d9 @jeregrine committed Feb 25, 2014
View
13 lib/phoenix/adapters/cowboy.ex
@@ -6,4 +6,17 @@ defmodule Phoenix.Adapters.Cowboy do
Dict.put(options, :dispatch, [{:_, dispatch}])
end
+
+ defmacro __using__(_options) do
+ quote do
+ Module.register_attribute __MODULE__, :dispatch_options, accumulate: true,
+ persist: false
+ import unquote(__MODULE__)
+ end
+ end
+ defmacro cowboy_dispatch(path, handler, options \\ []) do
+ quote do
+ @dispatch_options {unquote(path), unquote(handler), unquote(options)}
+ end
+ end
end
View
3 lib/phoenix/examples/controller.ex
@@ -58,7 +58,8 @@ defmodule Phoenix.Examples.Controllers.Files do
end
defmodule Phoenix.Examples.Controllers.Echo do
- use Phoenix.Controller.Websocket
+ use Phoenix.Websocket.RawHandler
+
def stream(data, socket, _state) do
reply socket, data
end
View
2 lib/phoenix/examples/router.ex
@@ -12,6 +12,6 @@ defmodule Router do
resources "comments", Comments
end
- websocket "/echo", Eco
+ raw_websocket "/echo", Eco
end
end
View
3 lib/phoenix/router/router.ex
@@ -7,7 +7,8 @@ defmodule Phoenix.Router do
defmacro __using__(plug_adapter_options \\ []) do
quote do
use Phoenix.Router.Mapper
- use Phoenix.Router.WebsocketMapper
+ use Phoenix.Adapters.Cowboy
+ import Phoenix.Router.RawWebsocketMapper
import unquote(__MODULE__)
@before_compile unquote(__MODULE__)
View
14 lib/phoenix/router/websocket_mapper.ex
@@ -1,15 +1,7 @@
-defmodule Phoenix.Router.WebsocketMapper do
- defmacro __using__(_options) do
+defmodule Phoenix.Router.RawWebsocketMapper do
+ defmacro raw_websocket(path, handler, options \\ []) do
quote do
- Module.register_attribute __MODULE__, :dispatch_options, accumulate: true,
- persist: false
- import unquote(__MODULE__)
- end
- end
-
- defmacro websocket(path, handler, options \\ []) do
- quote do
- @dispatch_options {unquote(path), unquote(handler), unquote(options) }
+ cowboy_dispatch unquote(path), unquote(handler), unquote(options)
end
end
end
View
4 lib/phoenix/controller/websocket.ex → lib/phoenix/websocket/raw_websocket.ex
@@ -1,4 +1,4 @@
-defmodule Phoenix.Controller.Websocket do
+defmodule Phoenix.Websocket.RawHandler do
@moduledoc """
This module is a convenience for setting up a basic cowboy websocket.
@@ -44,7 +44,7 @@ defmodule Phoenix.Controller.Websocket do
defrecord Socket, conn: nil, pid: nil
- import Phoenix.Controller.Websocket
+ import unquote(__MODULE__)
@doc false
def init({unquote(transport), :http}, req, opts) do
View
8 test/phoenix/websocket_test.exs → .../phoenix/websocket/raw_websocket_test.exs
@@ -1,21 +1,21 @@
defmodule Phoenix.Controller.WebsocketTest do
use ExUnit.Case, async: true
- alias Phoenix.Controller.Websocket
+ import Phoenix.Websocket.RawHandler
defrecord Socket, conn: nil, pid: nil
test "verify correct return from terminate" do
- Websocket.terminate(Socket.new(pid: self()))
+ terminate(Socket.new(pid: self()))
assert_received :shutdown
end
test "verify correct return from hibernate" do
- Websocket.hibernate(Socket.new(pid: self))
+ hibernate(Socket.new(pid: self))
assert_received :hibernate
end
test "verify basic reply" do
- Websocket.reply(Socket.new(pid: self), {:text, "hello"})
+ reply(Socket.new(pid: self), {:text, "hello"})
assert_received {:send, {:text, "hello"}, []}
end
end

0 comments on commit 65e29b3

Please sign in to comment.