Skip to content

Instantly share code, notes, and snippets.

View samleb's full-sized avatar
:octocat:
Freelance CTO

Samuel Lebeau samleb

:octocat:
Freelance CTO
View GitHub Profile
document.onmouseup = function(e) {
e = e || window.event;
var element = e.target || e.srcElement;
if (element.getAttributeNode('data-autoselect')) element.select();
};
{
"host": {
"ipv4": "123.45.67.89",
"hostname": "the.host.name", // or `null` if reverse DNS lookup failed
"addr": {
/* if IP is in a private range (e.g. 192.168.0.1) */
"city": "private",
"country": "private"
"geo": {
"latitude": null,
require 'fiber'
require 'em-http'
require 'em-synchrony/em-http'
require 'active_support/cache'
require 'active_support/core_ext/numeric/time'
require 'static_gmaps'
class GeoIp < Struct.new(:country, :city, :latitude, :longitude)
CACHE = ActiveSupport::Cache::MemoryStore.new(expires_in: 2.days)
HOSTIP_API_URL_FORMAT = 'http://api.hostip.info/get_html.php?ip=%s&position=true'.freeze
function makeObservable(object) {
var _listeners = {},
_slice = [].slice;
function addEventListener(eventName, handler) {
var handlers = _listeners[eventName];
if (!handlers) {
handlers = _listeners[eventName] = [];
}
handlers.push(handler);
@samleb
samleb / gist:752590
Created December 23, 2010 05:09
Experimenting on caching `stat` calls in Rack::File
require 'time'
require 'rack/utils'
require 'rack/mime'
module Rack
# Rack::File serves files below the +root+ directory given, according to the
# path info of the Rack request.
# e.g. when Rack::File.new("/etc") is used, you can access 'passwd' file
# as http://localhost:9292/passwd
#
@samleb
samleb / gist:813088
Created February 6, 2011 03:30
Use ActionView 3 outside of request/response cycle
class View < ActionView::Base
include Your::Application.routes.url_helpers
include ApplicationController._helpers
delegate :config, :to => ActionController::Base
end
# For some reason `render :partial => 'foo/bar'` doesn't work...
View.new.render :file => 'app/views/foo/_bar.html.haml'
$(function() {
$('#rooms-index').each(function() {
var GOOGLE_MAPS,
SECONDS_IN_A_DAY = 86400,
form = $('#room_search_form'),
mapElement = $('#map'),
results = $('#results'),
controls = form.find('input, select'),
mapController,
+ (void)displaySpinnerOverlayOver:(UIView *)view {
UIActivityIndicatorView *spinner;
if (spinnerOverlayView == nil) {
spinnerOverlayView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
spinnerOverlayView.opaque = NO;
spinnerOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
spinnerOverlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;;
spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
@samleb
samleb / ordered_set.rb
Created July 8, 2011 13:25
Ruby `OrderedSet`
require 'set'
if defined?(RUBY_VERSION) && RUBY_VERSION.to_f >= 1.9
class OrderedSet < Set
def ==(other)
return false unless other.is_a?(OrderedSet) && size == other.size
enum = other.to_enum
all? { |x| x == enum.next }
end