From 45443b2ec485c2937a489d0a356175764c8ff398 Mon Sep 17 00:00:00 2001 From: Lily Rose Date: Tue, 17 Jun 2025 23:53:38 +1000 Subject: [PATCH] Initial commit --- .github/workflows/test.yml | 23 ++++++++++++++ .gitignore | 5 +++ LICENSE.md | 9 ++++++ README.md | 24 ++++++++++++++ gleam.toml | 18 +++++++++++ manifest.toml | 29 +++++++++++++++++ src/spacetraders_websocket.gleam | 43 ++++++++++++++++++++++++++ test/spacetraders_websocket_test.gleam | 13 ++++++++ 8 files changed, 164 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 gleam.toml create mode 100644 manifest.toml create mode 100644 src/spacetraders_websocket.gleam create mode 100644 test/spacetraders_websocket_test.gleam diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7c92c48 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: erlef/setup-beam@v1 + with: + otp-version: "27.1.2" + gleam-version: "1.11.1" + rebar3-version: "3" + # elixir-version: "1" + - run: gleam deps download + - run: gleam test + - run: gleam format --check src test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dac4107 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.beam +*.ez +/build +erl_crash.dump +.env \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..1a347a8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2025 Lily Rose + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d81c6e --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# spacetraders_websocket + +[![Package Version](https://img.shields.io/hexpm/v/spacetraders_websocket)](https://hex.pm/packages/spacetraders_websocket) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/spacetraders_websocket/) + +```sh +gleam add spacetraders_websocket@1 +``` +```gleam +import spacetraders_websocket + +pub fn main() -> Nil { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at . + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +``` diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..5b61792 --- /dev/null +++ b/gleam.toml @@ -0,0 +1,18 @@ +name = "spacetraders_websocket" +version = "1.0.0" +description = "A Gleam SDK for the spacetraders.io game websocket endpoint" +licences = ["MIT"] +repository = { type = "forgejo", host = "7cs.dev", user = "lily", repo = "gleam-spacetraders-websocket" } +# links = [{ title = "Website", href = "" }] + +[dependencies] +gleam_stdlib = ">= 0.44.0 and < 2.0.0" +gleam_http = ">= 4.0.0 and < 5.0.0" +stratus = ">= 0.9.7 and < 1.0.0" +gleam_otp = ">= 0.16.1 and < 1.0.0" +gleam_erlang = ">= 0.34.0 and < 1.0.0" +gleam_json = ">= 3.0.1 and < 4.0.0" +birl = ">= 1.8.0 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..237f75f --- /dev/null +++ b/manifest.toml @@ -0,0 +1,29 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "birl", version = "1.8.0", build_tools = ["gleam"], requirements = ["gleam_regexp", "gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "2AC7BA26F998E3DFADDB657148BD5DDFE966958AD4D6D6957DD0D22E5B56C400" }, + { name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" }, + { name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" }, + { name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" }, + { name = "gleam_json", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "5BA154440B22D9800955B1AB854282FA37B97F30F409D76B0824D0A60C934188" }, + { name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" }, + { name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" }, + { name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" }, + { name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" }, + { name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" }, + { name = "gramps", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "59194B3980110B403EE6B75330DB82CDE05FC8138491C2EAEACBC7AAEF30B2E8" }, + { name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" }, + { name = "ranger", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_yielder"], otp_app = "ranger", source = "hex", outer_checksum = "C8988E8F8CDBD3E7F4D8F2E663EF76490390899C2B2885A6432E942495B3E854" }, + { name = "stratus", version = "0.9.7", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gramps", "logging"], otp_app = "stratus", source = "hex", outer_checksum = "4B3344B24B0808AF5828EFE3BEB824D3298E0ECAC8069D85D14A6A33BAD85CBD" }, +] + +[requirements] +birl = { version = ">= 1.8.0 and < 2.0.0" } +gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" } +gleam_http = { version = ">= 4.0.0 and < 5.0.0" } +gleam_json = { version = ">= 3.0.1 and < 4.0.0" } +gleam_otp = { version = ">= 0.16.1 and < 1.0.0" } +gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +stratus = { version = ">= 0.9.7 and < 1.0.0" } diff --git a/src/spacetraders_websocket.gleam b/src/spacetraders_websocket.gleam new file mode 100644 index 0000000..e59a526 --- /dev/null +++ b/src/spacetraders_websocket.gleam @@ -0,0 +1,43 @@ +import gleam/erlang/process.{type Subject} +import gleam/http +import gleam/http/request +import gleam/option +import gleam/otp/actor +import stratus + +pub fn main() -> Nil { + Nil +} + +pub fn connect( + token: String, + initial_state: state, + on_message: fn(state, stratus.Message(user)) -> + Result(state, process.ExitReason), + on_close: fn(state) -> Nil, +) -> Result(Subject(stratus.InternalMessage(user)), actor.StartError) { + stratus.websocket( + request: request.Request( + method: http.Get, + headers: [#("Authorization", "Bearer " <> token)], + body: "", + scheme: http.Https, + host: "api.spacetraders.io", + port: option.None, + path: "/v2/my/socket.io", + query: option.None, + ), + init: fn() { #(initial_state, option.None) }, + loop: fn(msg, state, conn) { + case on_message(state, msg) { + Ok(state) -> actor.continue(state) + Error(reason) -> { + let _ = stratus.close(conn) + actor.Stop(reason) + } + } + }, + ) + |> stratus.on_close(on_close) + |> stratus.initialize +} diff --git a/test/spacetraders_websocket_test.gleam b/test/spacetraders_websocket_test.gleam new file mode 100644 index 0000000..fba3c88 --- /dev/null +++ b/test/spacetraders_websocket_test.gleam @@ -0,0 +1,13 @@ +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + let name = "Joe" + let greeting = "Hello, " <> name <> "!" + + assert greeting == "Hello, Joe!" +}