Skip to content

Instantly share code, notes, and snippets.

View areichman's full-sized avatar

Aaron Reichman areichman

View GitHub Profile
@areichman
areichman / app.bash
Created March 6, 2012 14:26
Run the Rails console or rake tasks from a deployed JRuby war file
#!/bin/bash
# Print the usage statement and exit if no options are provided. Otherwise, set
# the required variables.
#
if [ $# -lt 1 ]
then
echo "Usage:"
echo "app console Start the console application"
echo "app test Run the test suite"
@areichman
areichman / smugmug_oauth.rb
Created March 16, 2012 03:53
Some Ruby snippets I wrote to learn how to use OAuth and the SmugMug API
#!/usr/bin/ruby
# based on http://blog.andydenmark.com/2009/03/how-to-build-oauth-consumer.html
require 'rubygems'
require 'digest'
require 'base64'
require 'cgi'
require 'openssl'
require 'restclient'
require 'json'
@areichman
areichman / options_for_select.js
Created August 28, 2012 18:45
Rails-like options_for_select helper for Handlebars
// Create a set of HTML option tags for each of the supplied inputs. If the inputs are arrays
// themselves, it is assume they are of the form [displayName, value].
//
// e.g.
// directions = ['North', 'South']
// {{options_for_select directions}}
//
// results in:
// <option>North</option>
// <option>South</option>
@areichman
areichman / play.css
Created June 7, 2013 14:43
CSS play button
.play {
position: relative;
z-index: 1;
width: 100%;
height: 100%;
}
.play:before { /* triangle */
content: "";
display: block;
@areichman
areichman / rename.sh
Created December 24, 2013 15:35
Remove underscores from filenames
for name in *\ *; do mv -v "$name" "${name// /}"; done
@areichman
areichman / dl-metadata.less
Created May 21, 2014 19:29
Custom styles for Bootstrap's .dl-horizontal class to display key-value pairs
.dl-metadata {
dl {
border-top: 1px solid #e5e5e5;
width: 100%;
overflow: hidden;
dt {
width: 100%;
padding: 12px 20px 0 0;
color: #888;
@areichman
areichman / multiparty_reformat_fields.js
Last active July 5, 2023 22:05
Make the nested fields parsed by multiparty look like req.body from body-parser
// Make the nested fields parsed by multiparty look like req.body from body-parser
// e.g. 'metadata[foo]': ['1'] => {metadata: {foo: 1}}
// 'metadata[foo]': ['bar'] => {metadata: {foo: 'bar'}}
// 'metadata[foo][]': ['bar', 'bat'] => {metadata: {foo: ['bar', 'bat']}}
var qs = require('qs');
function reformatFields(fields) {
// convert numbers to real numbers instead of strings
function toNumber(i) {
@areichman
areichman / reformat_tags.js
Created August 20, 2014 17:24
Format an input tag list (either comma-delimited or an array) as an output array with blanks and whitespace removed
function reformatTags(tags) {
var _tags = (tags || '').toString().trim().replace(/\s*,\s*/g, ',').replace(/^,|,$/g, '').split(',');
if (_tags[0] === '' && _tags.length === 1) _tags = []; // hack for empty form submissions
return _tags;
};
@areichman
areichman / request_mock.js
Created February 12, 2015 21:36
Create a mock response for the request module using a Jasmine spy
var request = require('request')
spyOn(request, 'get').andCallFake(function(params, callback) {
var res = {
statusCode: 200
}
var body = {
// JSON attributes
}
callback(null, res, body)
@areichman
areichman / check_box.js
Created March 25, 2015 20:23
Check box helper for Handlebars, based on the Rails check_box_helper
// Checkbox helper, based on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
//
// Usage:
// {{check_box object name="test" type="checkbox" value=true hidden="false"}}
//
// Note that the value in the hash can be specified with/without quotes so a proper type comparison
// can be made.
//
Handlebars.registerHelper('check_box', function(context, options) {
var checked = context !== undefined && context == options.hash.value ? 'checked' : '',