Skip to content

Instantly share code, notes, and snippets.

# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
@czarneckid
czarneckid / gist:762065
Created January 1, 2011 22:45
Creating high score leaderboards with Redis
NOTE: You will need at least Redis 2.1.6 to use the ZREVRANGEBYSCORE method.
Add players to HIGHSCORES table:
fossil:~ dczarnecki$ redis-cli
redis> zadd HIGHSCORES 1 player_1
(integer) 1
redis> zadd HIGHSCORES 2 player_2
(integer) 1
redis> zadd HIGHSCORES 3 player_3
@zefer
zefer / nginx_cors_s3_upload_proxy_full
Created February 18, 2011 13:29
My nginx config to allow CORS (cross-site) uploads to Amazon S3, with added config e.g. timeouts & security
# DO NOT RESPOND TO REQUESTS OTHER THAN yourdomain.com
server {
listen 80 default;
server_name _;
return 444;
}
# FILE UPLOADS
server {
listen 80;
@zefer
zefer / copy-s3-bucket.rb
Created April 4, 2011 11:24
Copy contents of an S3 bucket to a another bucket using an EC2 instance and a simple Ruby script. Useful for transferring large amounts of data and will work across geographic regions.
require 'rubygems'
require 'right_aws'
aws_access_key_id = 'your-access-key'
aws_secret_access_key = 'your-secret-key'
target_bucket = 'your-source-bucket'
destination_bucket = 'your-destination-bucket'
s3 = RightAws::S3Interface.new(aws_access_key_id, aws_secret_access_key)
@dhh
dhh / gist:981520
Created May 19, 2011 19:27
bulk api
# Bulk API design
#
# resources :posts
class PostsController < ActiveController::Base
# GET /posts/1,4,50,90
# post_url([ @post, @post ])
def show_many
@posts = Post.find(params[:ids])
end
@guilleiguaran
guilleiguaran / config_with_redis.rb
Created July 18, 2011 06:25
Save configuration parameters in Redis database
# Usage:
#
# - With set/get:
# App.config.set("some_config_value", "123456")
# value = App.config.get("some_config_value")
#
# - With hash:
# App.config["some_config_value"] = "123456"
# value = App.config["some_config_value"]
#
@jfgomez86
jfgomez86 / tweet.rb
Created July 29, 2011 16:10
Finding latest tweets by unique users
##
# Find latest *n* tweets, but don't repeat tweets by users.
# Example:
#
# If we have the following table:
#
# id | user_id | created_at
# 1 | 1 | 3 days ago
# 2 | 2 | 3 days ago
# 3 | 1 | 2 days ago
@robertomiranda
robertomiranda / gist:1168322
Created August 24, 2011 15:34
Find tournament's players by categories
class Tournament < ActiveRecord::Base
#SELECT "players".*
#FROM "players"
#WHERE "players"."id" IN (
#SELECT "players"."id"
#FROM "players"
#INNER JOIN "pairs"
#ON ("pairs"."player_id" = "players"."id" OR "pairs"."partner_id" = "players"."id") AND "pairs"."tournament_id" = 4)
def player
players = Player.arel_table
@rkh
rkh / chat.rb
Created December 14, 2011 12:55
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@josevalim
josevalim / 0_README.markdown
Created January 9, 2012 13:07
Hello world of Sinatra-like libraries on Elixir, Ruby and Node.js