Skip to content

Instantly share code, notes, and snippets.

View damonvjanis's full-sized avatar

Damon Janis damonvjanis

  • GridPoint
  • St. George, UT
View GitHub Profile
@damonvjanis
damonvjanis / states.ex
Created October 17, 2018 19:08
An Elixir util module to translate US two letter state codes to state names, and state names to two letter codes.
defmodule States do
@moduledoc """
A util that allows you to search for the uppercase abbreviation
of a state by name (case insensitive) or the capitalized name
of a state by abbreviation (case insensitive).
"""
@states_to_abbr %{
"Alabama" => "AL",
"Alaska" => "AK",
@damonvjanis
damonvjanis / APR.ex
Created October 17, 2018 19:12
Elixir function to calculate APR (Annual Percentage Rate) on a simple installment loan
defmodule APR do
@moduledoc """
This module has a single function, `calc_apr`, for calculating an APR on a simple installment loan.
This code was a conversion of a Groovy implementation of the algorithim in this answer on Stack Overflow: https://stackoverflow.com/a/3190428/7223229
"""
@doc """
Takes three arguments: the number of payments (in months), monthly payments, and the amount being financed not including interest.
@damonvjanis
damonvjanis / datasets.exs
Last active December 5, 2018 21:47
Generates some big data sets to test matching
random = fn -> Enum.random(97..122) end
string = fn -> to_string([random.(), random.(), random.(), random.()]) end
leads =
for _ <- 1..10_000 do
%{"first" => string.(), "last" => string.(), "email" => string.() <> "@example.com"}
end
contacts =
for _ <- 1..10_000 do
@damonvjanis
damonvjanis / lists.ex
Last active December 5, 2018 21:36
Uses lists to do a matching comparison on big datasets
for lead <- leads do
matching_contacts = Enum.filter(contacts, fn c -> c["email"] == lead["email"] end)
# Return a tuple with the lead and any contacts that match
{lead, matching_contacts}
end
@damonvjanis
damonvjanis / maps.ex
Last active December 5, 2018 21:35
Uses maps to do a matching comparison on big datasets
contacts = Enum.group_by(contacts, fn c -> c["email"] end)
for lead <- leads do
matching_contacts = contacts[lead["email"]]
if matching_contacts do
# Return a tuple with the lead and any contacts that match
{lead, matching_contacts}
end
end
@damonvjanis
damonvjanis / prod.secret.exs
Created August 21, 2020 21:49
prod.secret.exs
config :kaffe,
producer: [
endpoints: ["<YOUR_BOOSTRAP_SERVER>": 9092],
topics: ["sms.inbound.v1"],
ssl: true,
sasl: %{
mechanism: :plain,
login: "<YOUR_API_KEY>",
password: "<YOUR_API_SECRET>"
}
@damonvjanis
damonvjanis / router.ex
Created August 21, 2020 21:55
router.ex
scope "/api", MyAppWeb do
pipe_through :api
post "/inbound", InboundController, :create
end
@damonvjanis
damonvjanis / inbound_controller.ex
Created August 22, 2020 17:34
inbound_controller.ex
defmodule MyAppWeb.InboundController do
use MyAppWeb, :controller
def create(conn, params) do
# POST the message to Kafka
text(conn, "")
end
end
@damonvjanis
damonvjanis / mix.exs
Created August 22, 2020 17:37
mix.exs
defp deps do
[
{:kaffe, "~> CURRENT_VERSION_NUMBER"}
]
end
defmodule MyAppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :my_app
use SiteEncrypt.Phoenix
@impl Phoenix.Endpoint
def init(_key, config) do
{:ok, SiteEncrypt.Phoenix.configure_https(config)}
end
@impl SiteEncrypt