Skip to content

Instantly share code, notes, and snippets.

View DiegoSalazar's full-sized avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@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;
});
@DiegoSalazar
DiegoSalazar / prompt_func
Created December 5, 2011 16:22
terminal bash prompt
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
@DiegoSalazar
DiegoSalazar / gist:1726679
Created February 3, 2012 00:17
pre-commit hook to alert if a certain string found in code
#!/bin/sh
RED="\033[0;31m"
WHITE="\033[1;37m"
if git diff-index -p -M --cached HEAD | grep '#debug' > /dev/null; then
#echo 'debug lines found in commit. Aborting' >&2
#exit 1
echo "\n\t${RED}You left a debug comment in the code!${WHITE}\n" >&2
fi
attachments: parent_id, asset_id
domain_names: organisation_id
event_memberships: user_id, event_id
events: editor_id
group_actions: user_id, group_id
groups: user_id
icons: parent_id
invitations: sender_id
legacy_actions: item_upon_id
news_items: author_id
named_scope :with_over_lapping_dates, lambda {|starting_date, ending_date| {:conditions => ["? BETWEEN `start_date` AND `end_date` OR ? BETWEEN `start_date` AND `end_date`", starting_date, ending_date]}}
@DiegoSalazar
DiegoSalazar / order_spoof_finder.rb
Created September 13, 2012 18:15
Order spoof finder
class DasSpoofenSpleiza
def initialize(product_id)
@product = Product.find product_id, :include => :variants
end
def filter
start = Time.now
puts "\nGetting all order_items for product #{@product.id}..."
order_items = OrderItem.all({
:joins => [:order],
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