Packages
ex_ipfs_pubsub
0.0.1
Elixir IPFS pubsub experiment module elixir.
Retired package: Renamed - renamed to myspace_pubsub because ipfs doesn't support pubsub natively anymore
Current section
Files
Jump to
Current section
Files
lib/ex_ipfs_pubsub/message.ex
defmodule ExIpfsPubsub.Message do
@moduledoc false
require Logger
@enforce_keys [:from, :data, :seqno, :topic_ids]
defstruct from: nil, data: nil, seqno: nil, topic_ids: nil
# This is the raw format used in IPFS. Not really so interesting.
# But users can request it.
@type t :: %__MODULE__{
from: binary,
data: binary,
seqno: binary,
topic_ids: list
}
# Sample message:
# {"from":"12D3KooWS9Wzyr6CprW7mZUdushaHvSFf2XGvPhtoBonUYabFECo","data":"uSGVpCg","seqno":"uF0CyYs8EKiE","topicIDs":["uYmFobmVy"]
@spec new({:error, any}) :: {:error, any}
def new({:error, data}), do: {:error, data}
@spec new(map) :: t()
def new(opts) when is_map(opts) do
Logger.debug("Creating message form(#{inspect(opts)})")
%__MODULE__{
from: opts["from"],
data: opts["data"],
seqno: opts["seqno"],
topic_ids: opts["TopicIDs"]
}
end
@spec new({:ok, map}) :: t()
def new({:ok, opts}) when is_map(opts) do
Logger.debug("Pubsub.Message.new/map(#{inspect(opts)})")
new(opts)
end
@spec new(list) :: list(t())
def new(response) when is_list(response) do
Logger.debug("Pubsub.Message.new/list(#{inspect(response)})")
Enum.map(response, &new/1)
end
@spec new(binary) :: binary()
def new(response) when is_binary(response) do
Logger.debug("Pubsub.Message.new/binary(#{inspect(response)})")
new(Jason.decode!(response))
end
end