Skip to content

Instantly share code, notes, and snippets.

@kowsik
kowsik / gist:789339
Created January 21, 2011 06:58
Monkey patch for RestClient
class RestClient::Request
Empty = {}.freeze
def default_headers
Empty
end
end
module RestAPI
Headers = { :content_type => :json }.freeze
def default_headers
@kowsik
kowsik / gist:789343
Created January 21, 2011 07:03
include_docs
map: function(doc) {
if (something_or_the_other) {
emit(doc.attribute, doc);
}
}
@kowsik
kowsik / gist:789354
Created January 21, 2011 07:15
optimistic update_doc
def update_doc doc
loop do
yield doc
begin
save_doc doc
break
rescue RestClient::RequestFailed => e
if e.http_code == 409
doc = get doc['_id']
next
@kowsik
kowsik / gist:790015
Created January 21, 2011 17:15
_bulk_delete
# Assuming the docs have created_at, delete everything older than
# 5 minutes. We assume an emit like this:
#
# emit(Date.parse(doc.created_at), doc._rev)
#
# Notice that the value in emitted kv pair is the doc's current _rev
old = (Time.now - 300).utc.to_i * 1000
opts = { :startkey => old, :descending => true }
@kowsik
kowsik / gist:790038
Created January 21, 2011 17:27
Built-in reduce
map: function(doc) {
if (doc.type === 'queue') {
emit(null, 1);
}
},
reduce: '_sum'
@kowsik
kowsik / gist:790044
Created January 21, 2011 17:32
total rows
{"total_rows":0,"offset":0,"rows":[]}
@kowsik
kowsik / gist:792862
Created January 24, 2011 05:03
pong
loop do
76.times do |i|
print " " * i
print "o"
print "\r"
$stdout.flush
sleep 0.05
end
76.times do |i|
@kowsik
kowsik / blitz.rb
Created April 20, 2011 23:14
Integrate Blitz into your test frameworks
require 'rubygems'
require 'blitz'
require 'pp'
# Run a sprint
require 'blitz'
sprint = Blitz::Curl.parse('-r california www.example.com')
result = sprint.execute
pp :duration => result.duration
@kowsik
kowsik / blitz.tropo.rb
Created April 22, 2011 17:23
Using blitz.io and Tropo for easy Application Monitoring
require 'rubygems'
require 'restclient'
require 'blitz'
require 'pp'
TROPO_USER = ENV['TROPO_USER']
TROPO_PASS = ENV['TROPO_PASS']
TROPO_APIK = ENV['TROPO_APIK']
TROPO_CALL = ENV['TROPO_CALL']
@kowsik
kowsik / blitz.scale.tropo.rb
Created April 22, 2011 17:35
Using blitz.io and Tropo for scheduled load tests and alerting
# Try running a Rush (load test) from california
opts = {
:region => 'california',
:url => 'http://www.mudynamics.com',
:pattern => [{ :start => 1, :end => 10000, :duration => 120 }]
}
result = Blitz::Curl::Rush.execute(opts)
last = result.timeline.last
if last.errors or last.timeouts
# Do something about this!