Skip to content

Commit

Permalink
Merge pull request #219 from BlakeWilliams/acc-upgrade-jason
Browse files Browse the repository at this point in the history
[IMPORTANT] Swap Jason for Poison
  • Loading branch information
acconrad authored Mar 17, 2020
2 parents b032f28 + c127a39 commit bca99f7
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def application do
end

def deps do
[{:slack, "~> 0.22.0"}]
[{:slack, "~> 0.23.0"}]
end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/slack/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ defmodule Slack.Bot do
binstring
|> :binary.split(<<0>>)
|> List.first()
|> Poison.Parser.parse!(%{keys: :atoms})
|> Jason.decode!(keys: :atoms)
end

defp handle_exception(e) do
Expand Down
6 changes: 3 additions & 3 deletions lib/slack/rtm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Slack.Rtm do
end

defp handle_response({:ok, %HTTPoison.Response{body: body}}) do
case Poison.Parser.parse!(body, %{keys: :atoms}) do
case Jason.decode!(body, keys: :atoms) do
%{ok: true} = json ->
{:ok, json}

Expand All @@ -33,8 +33,8 @@ defmodule Slack.Rtm do
{:error, "Invalid RTM response"}
end
rescue
error in Poison.ParseError ->
%Poison.ParseError{pos: _, value: reason, rest: _} = error
error in Jason.DecodeError ->
%Jason.DecodeError{data: reason, position: _, token: _} = error
{:error, %Slack.JsonDecodeError{reason: reason, string: body}}
end

Expand Down
12 changes: 6 additions & 6 deletions lib/slack/sends.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Slack.Sends do
text: text,
channel: channel
}
|> Poison.encode!()
|> Jason.encode!()
|> send_raw(slack)
end

Expand All @@ -60,7 +60,7 @@ defmodule Slack.Sends do
channel: channel,
thread_ts: thread
}
|> Poison.encode!()
|> Jason.encode!()
|> send_raw(slack)
end

Expand All @@ -72,7 +72,7 @@ defmodule Slack.Sends do
type: "typing",
channel: channel
}
|> Poison.encode!()
|> Jason.encode!()
|> send_raw(slack)
end

Expand All @@ -84,7 +84,7 @@ defmodule Slack.Sends do
type: "ping"
}
|> Map.merge(Map.new(data))
|> Poison.encode!()
|> Jason.encode!()
|> send_raw(slack)
end

Expand All @@ -96,7 +96,7 @@ defmodule Slack.Sends do
type: "presence_sub",
ids: ids
}
|> Poison.encode!()
|> Jason.encode!()
|> send_raw(slack)
end

Expand Down Expand Up @@ -133,7 +133,7 @@ defmodule Slack.Sends do

case im_open do
{:ok, response} ->
case Poison.Parser.parse!(response.body, %{keys: :atoms}) do
case Jason.decode!(response.body, keys: :atoms) do
%{ok: true, channel: %{id: id}} -> on_success.(id)
e = %{error: _error_message} -> on_error.(e)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/web/default_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Slack.Web.DefaultClient do
url
|> HTTPoison.post!(body, [], opts())
|> Map.fetch!(:body)
|> Poison.Parser.parse!(%{})
|> Jason.decode!(%{})
end

defp opts do
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/web/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Slack.Web do
Enum.reduce(files, %{}, fn file, module_names ->
json =
File.read!("#{__DIR__}/docs/#{file}")
|> Poison.Parser.parse!(%{})
|> Jason.decode!(%{})

doc = Slack.Web.Documentation.new(json, file)

Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Slack.Mixfile do
def project do
[
app: :slack,
version: "0.22.0",
version: "0.23.0",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
name: "Slack",
Expand All @@ -29,7 +29,7 @@ defmodule Slack.Mixfile do
[
{:httpoison, "~> 1.2"},
{:websocket_client, "~> 1.2.4"},
{:poison, "~> 4.0"},
{:jason, "~> 1.1"},
{:ex_doc, "~> 0.19", only: :dev},
{:credo, "~> 0.5", only: [:dev, :test]},
{:plug, "~> 1.6", only: :test},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/bot_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ defmodule Slack.Integration.BotTest do
end

defp send_message_to_client(pid, message) do
send(pid, Poison.encode!(%{type: "message", text: message, channel: "C0123abc"}))
send(pid, Jason.encode!(%{type: "message", text: message, channel: "C0123abc"}))
end
end
23 changes: 13 additions & 10 deletions test/slack/sends_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Slack.SendsTest do

test "send_message sends message formatted to client" do
result = Sends.send_message("hello", "channel", %{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"channel"}/}
assert result == {nil, ~s/{"channel":"channel","text":"hello","type":"message"}/}
end

test "send_message understands #channel names" do
Expand All @@ -30,7 +30,7 @@ defmodule Slack.SendsTest do
}

result = Sends.send_message("hello", "#channel", slack)
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"C456"}/}
assert result == {nil, ~s/{"channel":"C456","text":"hello","type":"message"}/}
end

test "send_message understands @user names" do
Expand All @@ -42,7 +42,7 @@ defmodule Slack.SendsTest do
}

result = Sends.send_message("hello", "@user", slack)
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"D789"}/}
assert result == {nil, ~s/{"channel":"D789","text":"hello","type":"message"}/}
end

test "send_message understands user ids (Uxxx)" do
Expand All @@ -54,7 +54,7 @@ defmodule Slack.SendsTest do
}

result = Sends.send_message("hello", "U123", slack)
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"D789"}/}
assert result == {nil, ~s/{"channel":"D789","text":"hello","type":"message"}/}
end

test "send_message understands user ids (Wxxx)" do
Expand All @@ -66,7 +66,7 @@ defmodule Slack.SendsTest do
}

result = Sends.send_message("hello", "W123", slack)
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"D789"}/}
assert result == {nil, ~s/{"channel":"D789","text":"hello","type":"message"}/}
end

test "send_message with a thread attribute includes thread_ts in message to client" do
Expand All @@ -78,12 +78,15 @@ defmodule Slack.SendsTest do
}

result = Sends.send_message("hello", "D789", slack, "1555508888.000100")
assert result == {nil, ~s/{"type":"message","thread_ts":"1555508888.000100","text":"hello","channel":"D789"}/}

assert result ==
{nil,
~s/{"channel":"D789","text":"hello","thread_ts":"1555508888.000100","type":"message"}/}
end

test "indicate_typing sends typing notification to client" do
result = Sends.indicate_typing("channel", %{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"typing","channel":"channel"}/}
assert result == {nil, ~s/{"channel":"channel","type":"typing"}/}
end

test "send_ping sends ping to client" do
Expand All @@ -93,16 +96,16 @@ defmodule Slack.SendsTest do

test "send_ping with data sends ping + data to client" do
result = Sends.send_ping(%{foo: :bar}, %{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"ping","foo":"bar"}/}
assert result == {nil, ~s/{"foo":"bar","type":"ping"}/}
end

test "subscribe_presence sends presence subscription message to client" do
result = Sends.subscribe_presence(["a_user_id"], %{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"presence_sub","ids":["a_user_id"]}/}
assert result == {nil, ~s/{"ids":["a_user_id"],"type":"presence_sub"}/}
end

test "subscribe_presence without ids sends presence subscription message to client" do
result = Sends.subscribe_presence(%{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"presence_sub","ids":[]}/}
assert result == {nil, ~s/{"ids":[],"type":"presence_sub"}/}
end
end
2 changes: 1 addition & 1 deletion test/slack/web/documentation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Slack.Web.DocumentationTest do
file_content =
"#{__DIR__}/../../../lib/slack/web/docs/oauth.v2.access.json"
|> File.read!()
|> Poison.Parser.parse!(%{})
|> Jason.decode!(%{})

doc = Documentation.new(file_content, "oauth.v2.access.json")

Expand Down
2 changes: 1 addition & 1 deletion test/support/fake_slack/websocket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Slack.FakeSlack.Websocket do

def websocket_handle({:text, message}, req, state) do
pid = Application.get_env(:slack, :test_pid)
send(pid, {:bot_message, Poison.decode!(message)})
send(pid, {:bot_message, Jason.decode!(message)})

{:ok, req, state}
end
Expand Down

0 comments on commit bca99f7

Please sign in to comment.