Skip to content

Commit

Permalink
Merge pull request #119 from edgurgel/fix-channels-handler
Browse files Browse the repository at this point in the history
Fix ChannelsHandler JSON generation
  • Loading branch information
edgurgel authored Feb 21, 2018
2 parents 59ec270 + 7cfd884 commit 26c17a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
15 changes: 7 additions & 8 deletions lib/poxa/channels_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,20 @@ defmodule Poxa.ChannelsHandler do
defp show(channel, attributes, req, state) do
occupied = Channel.occupied?(channel)
attribute_list = mount_attribute_list(attributes, channel)
{Poison.encode!(Enum.into([occupied: occupied] ++ attribute_list, %{})), req, state}
{Poison.encode!(Map.merge(%{occupied: occupied}, attribute_list)), req, state}
end

defp mount_attribute_list(attributes, channel) do
attribute_list =
attributes_data =
if Enum.member?(attributes, "subscription_count") do
[subscription_count: Channel.subscription_count(channel)]
%{subscription_count: Channel.subscription_count(channel)}
else
[]
%{}
end
attribute_list ++
if Enum.member?(attributes, "user_count") do
[user_count: PresenceChannel.user_count(channel)]
Map.put(attributes_data, :user_count, PresenceChannel.user_count(channel))
else
[]
attributes_data
end
end

Expand All @@ -105,7 +104,7 @@ defmodule Poxa.ChannelsHandler do
end

defp channels(filter, attributes) do
for channel <- Channel.all, filter_channel(channel, filter) do
for channel <- Channel.all, filter_channel(channel, filter), into: %{} do
{channel, mount_attribute_list(attributes, channel)}
end
end
Expand Down
20 changes: 10 additions & 10 deletions test/channels_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Poxa.ChannelsHandlerTest do
setup do
new Poison
new :cowboy_req
new Channel, [:passthrough]
on_exit fn -> unload() end
:ok
end
Expand Down Expand Up @@ -106,7 +107,7 @@ defmodule Poxa.ChannelsHandlerTest do
expect(Channel, :occupied?, 1, true)
expect(Channel, :subscription_count, 1, 5)
expected = %{:occupied => true, :subscription_count => 5}
expect(Poison, :encode!, [{[expected], :encoded_json}])
expect(Poison, :encode!, [expected], :encoded_json)

assert get_json(:req, {:one, :channel, ["subscription_count"]}) == {:encoded_json, :req, nil}

Expand All @@ -118,7 +119,7 @@ defmodule Poxa.ChannelsHandlerTest do
expect(Channel, :occupied?, 1, true)
expect(PresenceChannel, :user_count, 1, 3)
expected = %{:occupied => true, :user_count => 3}
expect(Poison, :encode!, [{[expected], :encoded_json}])
expect(Poison, :encode!, [expected], :encoded_json)

assert get_json(:req, {:one, :channel, ["user_count"]}) == {:encoded_json, :req, nil}

Expand All @@ -131,8 +132,8 @@ defmodule Poxa.ChannelsHandlerTest do
expect(Channel, :occupied?, 1, true)
expect(Channel, :subscription_count, 1, 5)
expect(PresenceChannel, :user_count, 1, 3)
expected = %{:occupied => true, :subscription_count => 5, :user_count => 3}
expect(Poison, :encode!, [{[expected], :encoded_json}])
expected = %{occupied: true, subscription_count: 5, user_count: 3}
expect(Poison, :encode!, [expected], :encoded_json)

assert get_json(:req, {:one, :channel, ["subscription_count", "user_count"]}) == {:encoded_json, :req, nil}

Expand All @@ -144,10 +145,9 @@ defmodule Poxa.ChannelsHandlerTest do
test "get_json on every single channel" do
expect(:cowboy_req, :qs_val, 3, {nil, :req})
expect(Channel, :all, 0, ["presence-channel", "private-channel"])
expect(Channel, :presence?, 1, true)
expect(PresenceChannel, :user_count, 1, 3)
expected = %{:channels => [{"presence-channel", user_count: 3}]}
expect(Poison, :encode!, [{[expected], :encoded_json}])
expect(PresenceChannel, :user_count, ["presence-channel"], 3)
expected = %{channels: %{"presence-channel" => %{user_count: 3}}}
expect(Poison, :encode!, [expected], :encoded_json)

assert get_json(:req, {:all, nil, ["user_count"]}) == {:encoded_json, :req, nil}

Expand All @@ -159,8 +159,8 @@ defmodule Poxa.ChannelsHandlerTest do
test "get_json on every single channel with filter" do
expect(Channel, :all, 0, ["presence-channel", "poxa-channel"])
expect(Channel, :matches?, 2, seq([false, true]))
expected = %{:channels => [{"poxa-channel", []}]}
expect(Poison, :encode!, [{[expected], :encoded_json}])
expected = %{channels: %{"poxa-channel" => %{}}}
expect(Poison, :encode!, [expected], :encoded_json)

assert get_json(:req, {:all, 'poxa-', []}) == {:encoded_json, :req, nil}

Expand Down

0 comments on commit 26c17a8

Please sign in to comment.