Skip to content

Instantly share code, notes, and snippets.

@bcardiff
bcardiff / logger2.cr
Last active April 11, 2018 23:12
Logger POC with binding context
abstract class Logger2
enum Severity
DEBUG
INFO
end
abstract def log(severity : Severity, message, context : String)
macro bind(*, context = nil, forward = nil)
struct LoggerBinding
@bcardiff
bcardiff / logger2.cr
Created April 11, 2018 14:38
Logger POC
module Logger2
enum Severity
DEBUG
INFO
end
abstract def log(severity, message, context)
macro using(context, forward = nil)
getter logger : Logger2?
@bcardiff
bcardiff / README.md
Created November 10, 2017 03:23
crystal-env preview

crystal-env

This shard provides environment detection. The selected environment is configured using CRYSTAL_ENV environment variable.

Installation

Add this to your application's shard.yml:

dependencies:
struct NamedTuple
macro map_keys(tuple, properties)
{% begin %}
%tuple = {{tuple}}
NamedTuple.new(
{% for key, value in properties %}
{{value.id}}: %tuple[{{key.symbolize}}],
{% end %}
)
{% end %}
@bcardiff
bcardiff / script.js
Created May 21, 2017 16:55
Send eventbrite.com notifications to Slack via webscript.io
local action = json.parse(request.body).config.action
http.request {
url = 'https://hooks.slack.com/services/...',
method = 'POST',
data = json.stringify({text = 'Action: '..action})
}
return 200
@bcardiff
bcardiff / list-deps.cr
Last active March 9, 2024 17:57
List binary dependencies to build a minimal docker image from scratch
unless ARGV.size > 0
puts " Missing executable file argument"
puts " Usage (in a Dockerfile)"
puts " RUN crystal run ./path/to/list-deps.cr -- ./bin/executable"
exit 1
end
executable = File.expand_path(ARGV[0])
unless File.exists?(executable)
require "json"
struct Slice(T)
def to_json(json : JSON::Builder)
json.array do
each do |e|
e.to_json(json)
end
end
end
@bcardiff
bcardiff / pool_retry_reconnect.cr
Created December 16, 2016 15:11
Test the reconnect/retry logic in crystal-pg
require "pg"
# start a posgress
# $ docker run --name mypg -e POSTGRES_DB=crystal -e POSTGRES_USER=postgress -e POSTGRES_PASSWORD= -p 5432:5432 postgres:9.5
# <ctrl+c> to stop server
# restart it
# $ docker start -a mypg
# <ctrl+c> to stop server
@bcardiff
bcardiff / README.md
Last active October 6, 2016 19:37
Extract csv from a pdf with checkboxes
$ brew install imagemagick
$ brew install tesseract
$ ls -l1
Basic Infrastructure.pdf
extract.rb

$ ruby extract.rb 2> /dev/null
extracting page 1
.....................
@bcardiff
bcardiff / README.md
Created April 8, 2016 08:59
sample-data-confirm

This sample show how to attach globally to events over dynamically created elements. And how can data-* attributes be used to customize behaviour.