Skip to content

Instantly share code, notes, and snippets.

View DiegoSalazar's full-sized avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / api_curl_test.sh
Created October 29, 2014 14:40
API request signature and test
#!/bin/bash
SECRET_KEY=$TEST_SENDER_KEY
ACCESS_ID=$TEST_SENDER_TOKEN
API_BASE=$TEST_ENDPOINT_BASE
query="{\"document\":{\"recipient_id\":\"$TEST_RECIPIENT_TOKEN\",\"data\":{\"id\":\"1\",\"first_name\":\"Bill\"}}}"
content_md5=$(echo -n $query | openssl md5 -binary | base64)
content_type='application/json'
@DiegoSalazar
DiegoSalazar / flatten_hash_recurse.rb
Created January 6, 2015 17:11
Flatten an arbitrarily nested hash into a flat hash with dotted key names
a = { a: 1, b: { c: 2, d: { e: 3 }}}
# define a recursive proc:
flatten_keys = -> (h, prefix = "") do
@flattened_keys ||= {}
h.each do |key, value|
# Here we check if there's "sub documents" by asking if the value is a Hash
# we also pass in the name of the current prefix and key and append a . to it
if value.is_a? Hash
flatten_keys.call value, "#{prefix}#{key}."
@DiegoSalazar
DiegoSalazar / find_hotspots.rb
Created February 6, 2015 03:00
Print out hotspots from some kind of data file
#!/usr/bin/env ruby
# from the command line run:
# ruby ./find_hotspots.rb FILE_NAME THRESHOLD
class Array
def to_ranges
compact.sort.uniq.inject([]) do |r,x|
r.empty? || r.last.last.succ != x ? r << (x..x) : r[0..-2] << (r.last.first..x)
end
@DiegoSalazar
DiegoSalazar / hide_when_no_scrollbars.js
Created February 11, 2015 17:22
Tiny jQuery plugin to hide/show an element when a target element gets scrollbars
$.fn.hideWhenNoScrollBars = function() {
return this.each(function() {
var el = $(this),
target = $(el.data('target'));
function hideWhenNoScrollbars(target, el) {
if (target.scrollHeight > target.clientHeight) { // has vertical scrollbars
el.show();
} else {
el.hide();
0 5 10 15 20
|-----|-----|-----|-----|
s-e---s--e--s-e---s----es
Intervals of 5 minutes
job start = s
job end = e
s should always correspond to a |
@DiegoSalazar
DiegoSalazar / temp_converter.rb
Created March 13, 2015 18:24
Temperature conversion, naive solution
def convertTemp(temp, fs, ts)
@conversions ||= {
'C' => {
'C' => ->(t) { t },
'F' => ->(t) { t * 9 / 5 + 32 },
'K' => ->(t) { t + 273.15 },
'R' => ->(t) { (t + 273.15) * 9 / 5 },
'De' => ->(t) { (100 - t) * 3 / 2 },
'N' => ->(t) { t * 33 / 100 },
'Re' => ->(t) { t * 4 / 5},
@DiegoSalazar
DiegoSalazar / multipart-form-data.txt
Last active August 29, 2015 14:18
multipart form data
User-Agent: curl/7.21.2 (x86_64-apple-darwin)
Host: localhost:8080
Accept: */*
Content-Length: 1143
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------83ff53821b7c
------------------------------83ff53821b7c
Content-Disposition: form-data; name="img"; filename="a.png"
Content-Type: application/octet-stream
@DiegoSalazar
DiegoSalazar / convert_params_to_form_data.rb
Created April 8, 2015 18:11
Converts a nested hash into a flat hash with bracketed keys as needed by http multipart form data
# Flatten a hash's keys down and format them like http form data
# e.g. { user: { id: 1, posts: [{ name: 'test' }] }, debug: true }
# becomes
# { 'user[id]' => 1, 'user[posts][0][name]' => 'test', 'debug' => true }
def convert_params_to_form_data(params, form_data = {}, prefix = '')
bracketeer = ->(p, k) { "#{p}#{p.empty? ? '' : '['}#{k}#{p.empty? ? '' : ']'}" }
params.each_with_object form_data do |(key, value), form_data|
case value when Hash
convert_params_to_form_data value, form_data, bracketeer.call(prefix, key)
window.lastScroll = 0;
$(function() {
var lastSnap, isAnimating,
snaps = $(".snap-to"),
$window = $(window);
var heights = snaps.map(function() {
return $(this).offset().top;
});
ROUTES = {
"GET /" => ["RootController", :show],
"GET /home" => ["HomeController", :index],
"GET /posts/:slug/comments/:id/edit" => ["CommentsController", :edit],
"POST /posts/:slug/comments" => ["CommentsController", :create],
"PUT /posts/:slug" => ["PostsController", :update]
}
router = Router.new ROUTES