Skip to content

Instantly share code, notes, and snippets.

@postmodern
postmodern / enforce_ssl.rb
Created October 12, 2009 02:32
A Rack middleware app that enforces certain paths be requested over HTTPS.
require 'rack/utils'
module Rack
#
# EnforceSSL is a Rack middleware app that enforces that users visit
# specific paths via HTTPS. If a sensitive path is requested over
# plain-text HTTP, a 307 Redirect will be issued leading to the HTTPS
# version of the Requested URI.
#
# MIT License - Hal Brodigan (postmodern.mod3 at gmail.com)
@avdi
avdi / countdown_prompt.rb
Created July 17, 2009 00:03
A command-line prompt with a timeout and countdown.
# :PUBLISHER: markdown, shell, { command: 'rdiscount' }
# :BRACKET_CODE: '[ruby]', '[/ruby]'
# :TEXT:
#
# Have you ever started a long operation and walked away from the computer, and
# come back half an hour later only to find that the process is hung up waiting
# for some user input? It's a sub-optimal user experience, and in many cases it
# can be avoided by having the program choose a default if the user doesn't
# respond within a certain amount of time. One example of this UI technique in
# the wild is powering off your computer - most modern operating systems will
@bboe
bboe / tether.sh
Created October 20, 2011 20:18
OSX Lion tether to Android script using Azilink and Tunnelblick
#!/bin/bash
#
# azilink for OS X Lion
# based on http://pastie.org/405289 but works with Tunnelblick only
# (no need to install a separate copy of OpenVPN2 from macports
# or building from source by hand, thankfully)
# Requires:
# - azilink running on android phone (http://code.google.com/p/azilink/)
# (run the app and check the box to start the service).
# - adb on system path (comes with the Android SDK;
@mscottford
mscottford / heroku.rake
Created April 4, 2012 12:18
Rake task to restart one heroku web instance every 10 minutes
# Add `gem 'heroku-api', :git => 'https://github.com/heroku/heroku.rb.git'` to your Gemfile
# Set the APP_NAME environment variable to the name of the Heroku app you wish to control.
# Set the HEROKU_API_KEY environment variable to the API key found on the Heroku 'My Account' page.
# You can now create a scheduler instance that runs `bundle exec rake heroku:web:restart` to automate
# restarting workers. The task will select a different web instance every 10 minutes, based on the number of instances that are running.
namespace :heroku
namespace :web do
task :restart do
def transform_hash(original, options={}, &block)
options[:safe_descent] ||= {}
new_hash = {}
options[:safe_descent][original.object_id] = new_hash
original.inject(new_hash) { |result, (key,value)|
if (options[:deep] && Hash === value)
value = options[:safe_descent].fetch( value.object_id ) {
transform_hash(value, options, &block)
}
end
@timothee-alby
timothee-alby / programming_task_short.md
Created October 20, 2015 13:59
Programming Challenge - Overleaf - 2015-10-20

Programming challenge

A natural language time parser

You are a developer on a task manager app. Your users can create tasks and those tasks, in particular, have a time and date.

To provide a enhanced experience to your users upon task creation, you want the task date to be a free text field. A user would input strings such as in 2 days, 3 hours ago, tomorrow, on december 5th at noon, and you would save that information as a date object in the system.

Challenge

@loopj
loopj / bugsnag-thread.rb
Last active May 25, 2017 09:01
Send notifications to Bugsnag asynchronously using a thread
Bugsnag::Notification.class_eval do
class << self
def deliver_exception_payload_with_thread(*args)
Thread.new do
Bugsnag::Notification.deliver_exception_payload_without_thread(*args)
end
end
alias_method :deliver_exception_payload_without_thread, :deliver_exception_payload
alias_method :deliver_exception_payload, :deliver_exception_payload_with_thread
@timcharper
timcharper / console
Created July 9, 2010 06:08
script/console that loads your .irbrc before activating bundler
#!/usr/bin/env ruby
require "irb"
load "#{ENV['HOME']}/.irbrc"
def IRB.run_config; end
ARGV.unshift "console"
load File.dirname(__FILE__) + "/rails"
" cs'' => will switch surrounding simple/double quotes
" cs'[x] => will delegate to vim-surround
nmap <expr>cs' CSurroundQuotes()
fu! CSurroundQuotes()
let qch = s:findQuote()
let char = s:getChar('Question', 'Exchange quotes with?')
if char == "'"
let char = (qch ==# "\"" ? "'" : "\"") | end
@linjian
linjian / deferred_garbage_collection_all_in_one.rb
Created July 19, 2012 08:09
Deferred Garbage Collection All in One
# http://ariejan.net/2011/09/24/rspec-speed-up-by-tweaking-ruby-garbage-collection
#
# Usage:
# DEFER_GC=10 rspec spec/
# DEFER_GC=10 cucumber features/
#
# put it to spec/support/deferred_garbage_collection_all_in_one.rb
# or feature/support/hooks.rb
class DeferredGarbageCollection
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || -1).to_f