Add Twitter API user representer.
authorRoger Braun <roger@rogerbraun.net>
Mon, 20 Mar 2017 20:30:18 +0000 (21:30 +0100)
committerRoger Braun <roger@rogerbraun.net>
Mon, 20 Mar 2017 20:30:18 +0000 (21:30 +0100)
lib/pleroma/web/twitter_api/representers/base_representer.ex [new file with mode: 0644]
lib/pleroma/web/twitter_api/representers/user_representer.ex [new file with mode: 0644]
test/web/twitter_api/representers/user_representer_test.exs [new file with mode: 0644]

diff --git a/lib/pleroma/web/twitter_api/representers/base_representer.ex b/lib/pleroma/web/twitter_api/representers/base_representer.ex
new file mode 100644 (file)
index 0000000..a4ef245
--- /dev/null
@@ -0,0 +1,28 @@
+defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do
+  defmacro __using__(_opts) do
+    quote do
+      def to_json(object) do to_json(object, %{}) end
+      def to_json(object, options) do
+        object
+        |> to_map(options)
+        |> Poison.encode!
+      end
+
+      def enum_to_list(enum, options) do
+        mapping = fn (el) -> to_map(el, options) end
+        Enum.map(enum, mapping)
+      end
+
+      def to_map(object) do
+        to_map(object, %{})
+      end
+
+      def enum_to_json(enum) do enum_to_json(enum, %{}) end
+      def enum_to_json(enum, options) do
+        enum
+        |> enum_to_list(options)
+        |> Poison.encode!
+      end
+    end
+  end
+end
diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex
new file mode 100644 (file)
index 0000000..079e250
--- /dev/null
@@ -0,0 +1,26 @@
+defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
+  use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
+
+  def to_map(user, options) do
+    image = "https://placehold.it/48x48"
+    map = %{
+      "id" => user.id,
+      "name" => user.name,
+      "screen_name" => user.nickname,
+      "description" => user.bio,
+      "following" => false,
+      # Fake fields
+      "favourites_count" => 0,
+      "statuses_count" => 0,
+      "friends_count" => 0,
+      "followers_count" => 0,
+      "profile_image_url" => image,
+      "profile_image_url_https" => image,
+      "profile_image_url_profile_size" => image,
+      "profile_image_url_original" => image,
+      "rights" => %{}
+    }
+
+    map
+  end
+end
diff --git a/test/web/twitter_api/representers/user_representer_test.exs b/test/web/twitter_api/representers/user_representer_test.exs
new file mode 100644 (file)
index 0000000..f08e392
--- /dev/null
@@ -0,0 +1,41 @@
+defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.User
+  alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+
+  setup do
+    user = %User{
+      email: "test@example.org",
+      name: "Test Name",
+      nickname: "testname",
+      password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
+      bio: "A tester."
+    }
+    user = Repo.insert!(user)
+    [user: user]
+  end
+
+  test "A user", %{user: user} do
+    image = "https://placehold.it/48x48"
+    represented = %{
+      "id" => user.id,
+      "name" => user.name,
+      "screen_name" => user.nickname,
+      "description" => user.bio,
+      # Fake fields
+      "favourites_count" => 0,
+      "statuses_count" => 0,
+      "friends_count" => 0,
+      "followers_count" => 0,
+      "profile_image_url" => image,
+      "profile_image_url_https" => image,
+      "profile_image_url_profile_size" => image,
+      "profile_image_url_original" => image,
+      "following" => false,
+      "rights" => %{}
+    }
+
+    assert represented == UserRepresenter.to_map(user)
+  end
+end