aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/twitter_api/representers/base_representer.ex
blob: 3d31e60795cee7a6aa67b45b49d7fe3ff36635c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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)
        |> Jason.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)
        |> Jason.encode!()
      end
    end
  end
end