Skip to content

Instantly share code, notes, and snippets.

@kerryb
kerryb / parser.ex
Created Oct 12, 2022
Horrible code to convert some nested text data to json
View parser.ex
text
|> String.split("\n")
|> Enum.reduce({["{"], " "}, fn line, {output, current_indent} ->
case Regex.run(~r/^( +)(.*): ?(.*)/, line, capture: :all_but_first) do
[indent, key, ""] ->
close_braces =
if indent < current_indent do
Enum.map_join(
(String.length(current_indent) - 4)..String.length(indent)//-4,
"",
@kerryb
kerryb / space.exs
Created Oct 7, 2022
Who’s in space?
View space.exs
#!/usr/bin/env elixir
Mix.install([
{:httpoison, "~> 1.8"},
{:jason, "~> 1.3"}
])
defmodule Space do
def run do
fetch_data()
View Repo.ex
defmodule MyApp.Repo do
require Logger
alias Ecto.Association.NotLoaded
alias Ecto.{Changeset, Schema}
alias MyApp.Auth.User
...
@doc """
Wrapper round `Ecto.Repo.insert/2`, which logs the fields of the inserted
@kerryb
kerryb / README.md
Last active May 19, 2019
Basic given/when/then macros for ExUnit
View README.md

README

A spike looking at adding basic given-when-then steps to ExUnit tests.

Features

  • define tests as sequences of calls to given_, when_ and then_ (unfortunately when is a reserved word)
  • match steps by calling defwhen etc with a string matching the one used in the step
  • interpolate values into step descriptions using {braces}
  • placeholder variable names are available as methods on the magic args variable
@kerryb
kerryb / child.ex
Last active Jul 2, 2018
Supervised task, with timeout
View child.ex
defmodule Child do
def task(parent) do
Process.sleep(:timer.seconds(2))
send(parent, :finished)
end
end
@kerryb
kerryb / ytd.bash
Last active Feb 23, 2017
Year-to-date running stats
View ytd.bash
ytd() {
ytd=$(find ~/Dropbox/Apps/tapiriik -name $(date +%Y)-*_Running*.tcx -print0 | xargs -0 -n1 awk '/DistanceMeters/ {a=$0} END {gsub(/\s*<[^>]*>/, "", a); print a}' | paste -sd+ - | sed 's/\(.*\)/0.000621371 * (\1)/' | bc | sed 's/\(\..\).*/\1/')
proj=$(echo "$ytd * 365 / $(date +%j)" | bc)
echo "YTD: $ytd. Projected: $proj"
}
View roman_numerals.rb
class RomanNumerals
NUMBERS = {
1000 => "M",
900 => "CM",
500 => "D",
400 => "CD",
100 => "C",
90 => "XC",
50 => "L",
40 => "XL",
@kerryb
kerryb / blockchain.txt
Created Jan 12, 2016
Blockchain verification
View blockchain.txt
Verifying that +kerryb is my blockchain ID. No, me neither. https://onename.com/kerryb
View keybase.md

Keybase proof

I hereby claim:

  • I am kerryb on github.
  • I am kerryb (https://keybase.io/kerryb) on keybase.
  • I have a public key whose fingerprint is 8DA4 4B51 5A90 D3AE 07B9 2FD2 5E07 C65F E855 9D1D

To claim this, I am signing this object:

@kerryb
kerryb / application.rb
Created Jun 24, 2014
Simple rails example of form interacting with non-activerecord backend code
View application.rb
# config/application.rb
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"