Skip to content

Instantly share code, notes, and snippets.

@petewarden
petewarden / extrapermomni.rb
Created November 28, 2011 01:55
Asking for extra permissions dynamically with OmniAuth in Sinatra
# This is called before the external site, so we can add dynamic Facebook permissions
# for example. See http://www.mikepackdev.com/blog_posts/2-dynamically-requesting-facebook-permissions-with-omniauth
get '/auth/:name/setup' do
if session[:extra_permissions]
request.env['omniauth.strategy'].options[:scope] = STANDARD_FACEBOOK_PERMISSIONS+','+session[:extra_permissions]
session.delete(:extra_permissions)
end
# This looks odd, but is the expected way to indicate success to Omniauth!
halt 404, 'Setup complete'
end
@petewarden
petewarden / webpage2png.rb
Created November 28, 2011 02:44
An example of using PhantomJS from Ruby to do server-side rendering of a web page into a PNG
require 'rubygems' if RUBY_VERSION < '1.9'
require 'tempfile'
require File.join(ENV['JETPAC_PATH'], 'library/logger')
require File.join(ENV['JETPAC_PATH'], 'library/email')
def image_command(command)
log "IMG: Running '#{command}'"
result = system(command)
if !result
@petewarden
petewarden / multipart.rb
Created November 28, 2011 03:14
A module to emulate a multipart form post in Ruby
# Takes a hash of string and file parameters and returns a string of text
# formatted to be sent as a multipart form post.
#
# Author:: Cody Brimhall <mailto:cbrimhall@ucdavis.edu>
# Created:: 22 Feb 2008
# Licensed under the WFTPL - http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data
# http://sam.zoy.org/wtfpl/
require 'rubygems'
require 'mime/types'
@petewarden
petewarden / photoupload.rb
Created November 28, 2011 03:20
A demonstration of uploading images to Facebook using the Graph API from ruby
require 'rubygems' if RUBY_VERSION < '1.9'
require 'net/https'
require 'uri'
require 'multipart'
FACEBOOK_GRAPH_SERVER='https://graph.facebook.com'
def post_https(url, data, header)
@petewarden
petewarden / upgradecassandra.sh
Created December 5, 2011 22:56
How to upgrade a 0.8.1 Datastax Cassandra AMI to 0.8.7
# Grab the 0.8.7 binaries, you may need to use a more current mirror
curl -O "http://mirrors.ibiblio.org/apache//cassandra/0.8.7/apache-cassandra-0.8.7-bin.tar.gz"
tar -xzf apache-cassandra-0.8.7-bin.tar.gz
# Shut down Cassandra before changing anything, may take a minute or two
sudo service cassandra stop
# Make backups of the directories we'll be altering
sudo cp -R /usr/bin /usr/bin_original
sudo cp -R /usr/share/cassandra /usr/share/cassandra_original
@petewarden
petewarden / smoketest.rb
Created December 8, 2011 21:58
A simple smoketest script for sanity checking websites from the command line
#!/usr/bin/ruby
#
# A module for a quick sanity test on whether a web page is working (for small values of working)
# See http://petewarden.typepad.com for more info
#
#
require 'rubygems' if RUBY_VERSION < '1.9'
require 'net/http'
require 'net/https'
@petewarden
petewarden / tumblrproxy.rb
Created October 25, 2012 03:28
An example of proxying a Tumblr blog as a subdirectory in Ruby and Sinatra
get '/blog*' do
path = params[:splat][0]
source_url = 'http://yourblog.tumblr.com' + path.gsub(/ /, '+')
source_content_type = ''
source_body = open(source_url) do |f|
source_content_type = f.content_type # "text/html"
f.read
end
if source_content_type == 'text/html'
output_base = request.base_url + '/blog'
// Call like wrapNamespaceInTracing(jetpac, 'jetpac');
g_callStack = [];
window.onerror = function(inputMessage, url, line) {
if (typeof g_callStack !== 'undefined') {
inputMessage += ' - stack=[' + g_callStack.join(' -> ') + ']';
g_callStack = [];
}
var message = '**** Javascript error **** ' + inputMessage + ' at ' + url + ':' + line;
console.log(message);
// Runs a small set of hand-calculated data through the implementation.
void TestWithSmallData() {
const int m = 4;
const int n = 2;
const int k = 3;
// Matrix A (LHS) is:
// | 7 | 10 | 13 | 16 |
// | 8 | 11 | 14 | 17 |
// | 9 | 12 | 15 | 18 |
const uint8_t a_data[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
void ReferenceEightBitIntGemm(bool transpose_a, bool transpose_b,
bool transpose_c, int m, int n, int k,
const uint8_t* a, int32_t a_offset, int lda,
const uint8_t* b, int32_t b_offset, int ldb,
uint8_t* c, int32_t c_offset, int32_t c_mult_int,
int32_t c_shift, int ldc) {
assert((c_shift >= 0) && (c_shift <= 32));
assert(a != nullptr);
assert(b != nullptr);