Skip to content

Instantly share code, notes, and snippets.

@astery
astery / my_app.ex
Created March 9, 2024 19:15 — forked from alanpeabody/my_app.ex
Websockets in Elixir with Cowboy and Plug
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
Plug.Adapters.Cowboy.child_spec(:http, MyApp.Router, [], [
dispatch: dispatch
])
@astery
astery / access.ex
Last active June 11, 2020 15:35
Indifferent access in elixir
defmodule Access do
def indifferent_key(key) do
fn
:get, data, next ->
get_atom_or_string_key!(data, key)
|> case do
nil -> next.(nil)
key -> next.(Map.get(data, key))
end
@astery
astery / create_user_sync_handler.ex
Last active September 25, 2017 12:28
Projection waiting example
# EventBus is just event_handler which retranslates event_store events and allow to send new events which not stored in es
# dispatch - is a wrapper above commanded dispatch which allows to handle both types of commands: commanded and non commanded commands.
defmodule VOK.User.CommandHandlers.CreateUserSyncHandler do
import VOK.User.WaitHelpers
alias VOK.Events.{
UserCreated,
RoleGranted,
RoleGrantFailed,
@astery
astery / mix_test_summary.sh
Created September 15, 2017 12:24
Bash script which add summary after running `mix tests` in umbrella root
#!/usr/bin/env bash
set +m
shopt -s lastpipe
app_name="unknown"
declare -A app_tests
declare -A app_fails
declare -A app_warnings
declare -A app_errors
@astery
astery / mix.exs
Created November 4, 2016 12:04
Mix get.deps issue
defmodule MixTest.Mixfile do
use Mix.Project
def project do
[app: :mix_test,
version: "0.1.0",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
def health(game_persona):
return game_persona[1]
def set_health(game_persona, health):
game_persona[1] = health
def attack(game_persona):
return game_persona[2]
def set_attack(game_persona, attack):
@astery
astery / call.sh
Last active July 1, 2016 18:17
Rails test example
rake test:integration
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums = nums1 + nums2
nums = sorted(nums)
total = len(nums)
@astery
astery / generate_code.sh
Created June 23, 2016 16:55
Generate ticket code
#!/bin/bash
curl --request POST \
--url https://qjv4fthf6hkqjrmds-mock.stoplight-proxy.io/pbx/ticket_codes/generate \
--header 'content-type: application/json' \
--data '{"mobile_number":"+79201001010", "source":"pbx-89201001010"}'
@astery
astery / bind_card.sh
Last active June 23, 2016 16:54
Bind card
#!/bin/bash
curl --request POST \
--url https://qjv4fthf6hkqjrmds-mock.stoplight-proxy.io/pbx/passengers/bind_card \
--header 'content-type: application/json' \
--data '{"mobile_number":"+79201001010", "nfc":"556655", "source":"pbx-89202002222"}'