Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / nginx.conf
Last active August 6, 2024 13:21
commented nginx.conf for Ruby on Rails
# A commented nginx configuration file for Ruby on Rails
#
# Author: Tommaso Pavese
# [email protected]
# http://tommaso.pavese.me
#
# License: http://www.wtfpl.net/
#
#
# Tested with:
@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
# [email protected]
# www.wonderingmachine.com
#
# Simple script for markdown using:
#
# Redcarpet: https://github.com/vmg/redcarpet
# Rouge: https://github.com/jayferd/rouge
@tompave
tompave / gist:942dec39d2112cced0d8
Created May 19, 2014 15:36
client IP in Rails 3.2
# in a controller
# request.remote_ip -> http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-remote_ip
# default IP lookup -> http://api.rubyonrails.org/classes/ActionDispatch/RemoteIp/GetIp.html#method-i-calculate_ip
#
# request.remote_ip looks at different sources and makes a "best guess" to find the correct IP. This is usually easy
# with a plain app-server setup, but with reverse-proxy configurations (Mongrels behind Apache, Unicorns behind nginx) the
# requests are issued by the reverse-proxy (often localhost).
#
# Since we have a cluster of Unicorns behind a nginx reverse-proxy, the actual client IP is (almost) surely found in the
@tompave
tompave / User.rb
Created May 24, 2014 15:44
An example of auto expiring user tokens
require 'digest/md5'
class User
attr_accessor :name, :token_valid_for
attr_reader :pwd_hash
def initialize(name, pwd)
self.name = name
self.password = pwd
@tompave
tompave / async_rake.rb
Last active July 5, 2023 17:13
how to run rake tasks asynchronously from controllers by spawning child processes
class ApplicationController < ActionController::Base
private
# Runs a rake task asyncronously in a child process
#
# for example, in any controller:
# async_rake("async:import_fixtures")
# async_rake("db:maintenance:cleanup", table: "things", ids: [12, 114, 539])
#
@tompave
tompave / foobar_config.rb
Last active August 29, 2015 14:01
Some configuration techniques in Ruby
module Foobar
module Config
class << self
attr_writer :foo, :bar, :baz
def foo(value = nil)
if value
self.foo = value
else
@foo
@tompave
tompave / timer.rb
Created June 25, 2014 23:37
Ruby simple timer
def timer(seconds, &block)
Thread.new {
t_0 = Time.now
sleep seconds
elapsed = Time.now - t_0
puts "timer triggered after #{elapsed} seconds (error: #{elapsed - seconds})."
yield
}
end