Skip to content

Instantly share code, notes, and snippets.

@tompave
tompave / percent_encoding_guard.rb
Last active August 29, 2015 14:20
Rack middleware guards
# Issue on github:
# https://github.com/rack/rack/issues/337
#
# Possible patch:
# https://gist.github.com/psychocandy/3130349
# but I don't like the idea of monkeypatching the standard library
#
# Rack middleware:
# http://stackoverflow.com/questions/16269897/rails-argumenterror-invalid-encoding
#
@tompave
tompave / risotto.md
Created April 16, 2015 17:06
risotto!

Risotto for 4 people

Ingredients

  • rice (type: Carnaroli or Arborio)

    • 80-100 g per person, total: ~400 g
    • a bit more if you're hungry
  • one onion

@tompave
tompave / rebooter.rb
Last active December 16, 2015 00:09
A simple implementation of a Ruby program that: works with a continuous event loop, is managed through UNIX signals, stops and reboots in response to file system changes (but that can be easily customized).
#! /usr/bin/env ruby
=begin
How to launch
$ chmod 700 rebooter.rb
$ ./rebooter.rb
It will create two files (will delete them on SIGTERM and SIGINT)
./rebooter.pid
./rebooter.control
@tompave
tompave / closures.rb
Created December 16, 2015 00:52
closures in Ruby
i = 1000
f1 = Proc.new { |x| x * x }
f2 = -> (func, int) { puts "the result is: '#{func.call(int)}', i is: '#{i}'" }
def run(range, operation, printer)
range.each do |i|
printer.call(operation, i)
end
end
@tompave
tompave / simple_redirect_server.rb
Last active December 21, 2015 07:59
A simple ruby HTTP server that listens on 127.0.0.1:80 and redirects all requests to a specific URL.
#! /usr/bin/env ruby
require 'webrick'
require 'uri'
TARGET_URL = URI.parse "http://tommaso.pavese.me/back_to_work/"
redirect_callback = Proc.new do |request, response|
response.set_redirect WEBrick::HTTPStatus::TemporaryRedirect, TARGET_URL
#define ENDPOINT_URL @"http://www.myawesomeserver.com/data/images"
// elsewhere: we will need this to send the request asynchronously
self.httpQueue = [[NSOperationQueue alloc] init];
/**
* upload method
*/
- (void)uploadImage:(UIImage*)image withImageName:(NSString*)imageName andParams:(NSDictionary*)paramsDict
@tompave
tompave / mark_red_rouge.rb
Last active January 4, 2016 15:29
simple markdown script. It uses Redcarpet for markdown and Rouge for code syntax highlighting.
#! /usr/bin/env ruby
# Author: Tommaso Pavese
# tommaso@pavese.me
# www.wonderingmachine.com
#
# Simple script for markdown using:
#
# Redcarpet: https://github.com/vmg/redcarpet
# Rouge: https://github.com/jayferd/rouge
require 'digest/md5'
require 'base32'
require 'rqrcode'
require 'rotp'
def print_as_qr(string)
qrcode = RQRCode::QRCode.new(string)
filename = File.expand_path "~/Desktop/qr_#{rand(10_000)}.png"
qrcode.as_png.save(filename, :fast_rgb)
end
@tompave
tompave / middleman_build.sh
Last active May 30, 2016 18:02
Build a Middleman site and commit it to an orphan master branch, see the related blog post for the setup: http://tommaso.pavese.me/2016/05/22/how-to-deploy-a-middleman-website-to-github-pages/
#!/bin/bash
middleman build &&
echo "--- middleman build complete"
git checkout master &&
echo "--- cleaning old build"
(ls -1 | grep -v 'build' | xargs rm -rf ) &&
@tompave
tompave / timing.exs
Last active August 20, 2016 15:44
Fun with Elixir macros and Streams
defmodule Timing do
def now do
:os.system_time(:milli_seconds)
end
# broken! the block is executed immediately
#
def ftime([do: block]) do
t0 = now
{ :ok, block, now - t0 }