akkoma/lib/pleroma/web/twitter_api/representers/base_representer.ex

42 lines
1,015 B
Elixir
Raw Normal View History

2017-03-20 20:30:18 +00:00
defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do
defmacro __using__(_opts) do
quote do
2017-06-05 13:47:37 +00:00
alias Calendar.Strftime
2017-03-20 20:30:18 +00:00
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
2017-06-05 13:47:37 +00:00
def format_asctime(date) do
Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
end
def date_to_asctime(date) do
with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
format_asctime(date)
else _e ->
""
end
end
2017-03-20 20:30:18 +00:00
end
end
end