Skip to content

Instantly share code, notes, and snippets.

@dsager
dsager / keybase.md
Created June 13, 2014 06:40
Keybase proof

Keybase proof

I hereby claim:

  • I am dsager on github.
  • I am dsager (https://keybase.io/dsager) on keybase.
  • I have a public key whose fingerprint is F92D 43DC 3A9E 32EC CF94 BB12 1CCF DB6B B557 139A

To claim this, I am signing this object:

@dsager
dsager / mobile_detection.php
Last active August 29, 2015 14:05
Some PHP functions to detect if a UA is mobile/tablet
<?php
/**
* Inspired by the neat Browser Gem for Ruby: https://github.com/fnando/browser
*
* Copyright (c) 2014 Daniel Sager
* License: https://gist.github.com/dsager/0edcbf806b5b86e78455#file-2014
*/
/**
* @param null|string $ua
@dsager
dsager / 2014
Created September 14, 2014 18:03
MIT License
The MIT License (MIT)
Copyright (c) 2014 Daniel Sager
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:
@dsager
dsager / get_gists.php
Created September 14, 2014 18:33
Small script to fetch a list of a user's Gists on GitHub
<?php
/**
* Small script to fetch a list of a user's Gists on GitHub
*
* Copyright (c) 2014 Daniel Sager
* License: MIT, https://gist.github.com/dsager/0edcbf806b5b86e78455#file-2014
*/
date_default_timezone_set('UTC');
$url = 'https://gist.github.com/dsager.atom';
@dsager
dsager / one_has_many.md
Last active August 29, 2015 14:06
One in a has_many - Access Special Association Objects in ActiveRecord

One in a has_many - Access Special Association Objects in ActiveRecord

ActiveRecord models that define a has_many association often need access to a specific entry of this list. Think of an user that has many email addresses but only one that is his primary address. Or a Blog post with many comments of which one is featured.

How a lot of people do it

A pattern that seems to be quite common is to [extend the association by implementing a method that gets you the specific record][ar-ass-extension]:

class User < ActiveRecord::Base
@dsager
dsager / parse_git_repo_def.sh
Last active August 29, 2015 14:21
small script to parse a string containing a git repository definition with multiple remotes.
#!/usr/bin/env bash
FIELD_SEP="|"
ROW="src/foo\ bar | git@github.com:foo/foobar.git foo | git@github.com:bar/foobar.git bar | git@github.com:baz/foobar.git baz |"
# cut out the directory first
DIR=${ROW%%${FIELD_SEP}*}
TRIMMED_DIR="$(echo -e "${DIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
ROW="${ROW#*${FIELD_SEP}}"
echo "> dir: $DIR"
@dsager
dsager / wb_themes.rb
Created October 25, 2016 12:24
world-bank theme classifier
#
# script to load project data from the world bank and generate a training set
# for a classifier
#
# Get a list of projects from the world bank and filter out projects w/o abstracts & themes
#
# curl -s 'http://search.worldbank.org/api/v2/projects?format=json&fl=project_name,project_abstract,theme_namecode&source=IBRD&rows=50000'
# | jq '[.projects[] | select(.project_abstract? and .theme_namecode?)] | map({"text": [.project_name, .project_abstract.cdata] | join(" - "), "themes": .theme_namecode | map(.code)})'
# > wb-projects.json
#
@dsager
dsager / look_at_images.rb
Created February 6, 2017 11:14
Google Cloud Vision: Devex Article Images 2016
# Script to get Google CLoud Vision annotations for a bunch of images
# Images in /images are expected to be also present in the specified GCS bucket
#
# setup:
# gem install google-cloud-vision
# export GOOGLE_CLOUD_PROJECT="XX-XX"
# export GOOGLE_CLOUD_KEYFILE="~/path/to/XX-XX.json"
#
require 'google/cloud/vision'
@dsager
dsager / idea-backup.sh
Last active April 25, 2017 13:30
Shell Script to backup IntelliJ IDEA project settings
#! /bin/bash
BAKDIR="${HOME}/Dropbox/config-backup/intellij/project-settings"
BAKFILE="${BAKDIR}/idea-folders.txt"
#create backup folder
mkdir -p "${BAKDIR}"
# find all `.idea` folders in home
if [ ! -e "${BAKFILE}" ]; then