From c127a39ec1010953eb7ee94214d1c2a3a1cce466 Mon Sep 17 00:00:00 2001 From: Adam Conrad Date: Mon, 16 Mar 2020 17:19:44 -0400 Subject: [PATCH] Upgrade Jason & Remove Poison --- README.md | 2 +- lib/slack/bot.ex | 2 +- lib/slack/rtm.ex | 6 +++--- lib/slack/sends.ex | 12 ++++++------ lib/slack/web/default_client.ex | 2 +- lib/slack/web/web.ex | 2 +- mix.exs | 4 ++-- test/integration/bot_test.exs | 2 +- test/slack/sends_test.exs | 23 +++++++++++++---------- test/slack/web/documentation_test.exs | 2 +- test/support/fake_slack/websocket.ex | 2 +- 11 files changed, 31 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 32c6b9d..3f7fc93 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ def application do end def deps do - [{:slack, "~> 0.22.0"}] + [{:slack, "~> 0.23.0"}] end ``` diff --git a/lib/slack/bot.ex b/lib/slack/bot.ex index 86073f2..dcc4bd8 100644 --- a/lib/slack/bot.ex +++ b/lib/slack/bot.ex @@ -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 diff --git a/lib/slack/rtm.ex b/lib/slack/rtm.ex index fdfef21..139c84e 100644 --- a/lib/slack/rtm.ex +++ b/lib/slack/rtm.ex @@ -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} @@ -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 diff --git a/lib/slack/sends.ex b/lib/slack/sends.ex index e932aab..d20a729 100644 --- a/lib/slack/sends.ex +++ b/lib/slack/sends.ex @@ -39,7 +39,7 @@ defmodule Slack.Sends do text: text, channel: channel } - |> Poison.encode!() + |> Jason.encode!() |> send_raw(slack) end @@ -60,7 +60,7 @@ defmodule Slack.Sends do channel: channel, thread_ts: thread } - |> Poison.encode!() + |> Jason.encode!() |> send_raw(slack) end @@ -72,7 +72,7 @@ defmodule Slack.Sends do type: "typing", channel: channel } - |> Poison.encode!() + |> Jason.encode!() |> send_raw(slack) end @@ -84,7 +84,7 @@ defmodule Slack.Sends do type: "ping" } |> Map.merge(Map.new(data)) - |> Poison.encode!() + |> Jason.encode!() |> send_raw(slack) end @@ -96,7 +96,7 @@ defmodule Slack.Sends do type: "presence_sub", ids: ids } - |> Poison.encode!() + |> Jason.encode!() |> send_raw(slack) end @@ -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 diff --git a/lib/slack/web/default_client.ex b/lib/slack/web/default_client.ex index fac8b6f..57484c2 100644 --- a/lib/slack/web/default_client.ex +++ b/lib/slack/web/default_client.ex @@ -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 diff --git a/lib/slack/web/web.ex b/lib/slack/web/web.ex index 7f926a7..91aedb6 100644 --- a/lib/slack/web/web.ex +++ b/lib/slack/web/web.ex @@ -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) diff --git a/mix.exs b/mix.exs index bda8cf7..d659391 100644 --- a/mix.exs +++ b/mix.exs @@ -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", @@ -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}, diff --git a/test/integration/bot_test.exs b/test/integration/bot_test.exs index a17a3a6..6d27e3e 100644 --- a/test/integration/bot_test.exs +++ b/test/integration/bot_test.exs @@ -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 diff --git a/test/slack/sends_test.exs b/test/slack/sends_test.exs index 3fe469c..3f319aa 100644 --- a/test/slack/sends_test.exs +++ b/test/slack/sends_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/slack/web/documentation_test.exs b/test/slack/web/documentation_test.exs index 2c181d3..2d57093 100644 --- a/test/slack/web/documentation_test.exs +++ b/test/slack/web/documentation_test.exs @@ -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") diff --git a/test/support/fake_slack/websocket.ex b/test/support/fake_slack/websocket.ex index cb547aa..f3bf9d5 100644 --- a/test/support/fake_slack/websocket.ex +++ b/test/support/fake_slack/websocket.ex @@ -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