Skip to content

Commit 54e9720

Browse files
author
chatgris
committed
Initial commit
Signed-off-by: chatgris <chatgri@gmail.com>
0 parents  commit 54e9720

8 files changed

+124
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/_build
2+
/deps
3+
erl_crash.dump
4+
*.ez

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT LICENSE
2+
3+
Copyright (c) chatgris <chatgri@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# EventSourceEncoder
2+
3+
EventSourceEncoder is a Elixir library to encode data into EventSource
4+
compliant data.
5+
6+
## Examples
7+
8+
``` elixir
9+
iex> EventSourceEncoder.encode(1, "This is data")
10+
"id:1\\ndata: This is data\\n\\n"
11+
12+
iex> EventSourceEncoder.encode(1, "This is new data", :login)
13+
"id:1\\nevent:login\\ndata: This is new data\\n\\n"
14+
```

config/config.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies. The Mix.Config module provides functions
3+
# to aid in doing so.
4+
use Mix.Config
5+
6+
# Note this file is loaded before any dependency and is restricted
7+
# to this project. If another project depends on this project, this
8+
# file won't be loaded nor affect the parent project.
9+
10+
# Sample configuration:
11+
#
12+
# config :my_dep,
13+
# key: :value,
14+
# limit: 42
15+
16+
# It is also possible to import configuration files, relative to this
17+
# directory. For example, you can emulate configuration per environment
18+
# by uncommenting the line below and defining dev.exs, test.exs and such.
19+
# Configuration from the imported file will override the ones defined
20+
# here (which is why it is important to import them last).
21+
#
22+
# import_config "#{Mix.env}.exs"

lib/event_source_encoder.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule EventSourceEncoder do
2+
@moduledoc """
3+
This module serialize a given id, data, and event to a EventSource message.
4+
"""
5+
6+
@doc """
7+
Encode an id and data to an EventSource message.
8+
9+
* `id` - The event ID to set the EventSource object's last event ID value to.
10+
* `data` - The data field for the message.
11+
* `event` - The event's type.
12+
13+
## Examples
14+
15+
iex> EventSourceEncoder.encode(1, "This is data")
16+
"id:1\\ndata: This is data\\n\\n"
17+
18+
iex> EventSourceEncoder.encode(1, "This is new data", :login)
19+
"id:1\\nevent:login\\ndata: This is new data\\n\\n"
20+
"""
21+
def encode(id, data) do
22+
"id:#{id}\ndata: #{data}\n\n"
23+
end
24+
25+
def encode(id, data, event) do
26+
"id:#{id}\nevent:#{event}\ndata: #{data}\n\n"
27+
end
28+
end

mix.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule EventSourceEncoder.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[app: :event_source_encoder,
6+
version: "0.0.1",
7+
elixir: "~> 0.14.2",
8+
deps: deps]
9+
end
10+
11+
# Configuration for the OTP application
12+
#
13+
# Type `mix help compile.app` for more information
14+
def application do
15+
[applications: []]
16+
end
17+
18+
# Dependencies can be hex.pm packages:
19+
#
20+
# {:mydep, "~> 0.3.0"}
21+
#
22+
# Or git/path repositories:
23+
#
24+
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1"}
25+
#
26+
# Type `mix help deps` for more examples and options
27+
defp deps do
28+
[]
29+
end
30+
end

test/event_source_encoder_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule EventSourceEncoderTest do
2+
use ExUnit.Case
3+
doctest EventSourceEncoder
4+
end

test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)