Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xli
xli / gist:77242
Created March 11, 2009 01:20 — forked from chad/gist:76951
# How to find out where a method comes from.
# Learned this from Dave Thomas while teaching Advanced Ruby Studio
# Makes the case for separating method definitions into
# modules, especially when enhancing built-in classes.
module Perpetrator
def crime
end
end
class Fixnum
//find out caller's class name (found from log5j)
public static void xxx() {
String name = new Exception().getStackTrace()[1].getClassName();
....
}
// or
String name = Thread.currentThread().getStackTrace()[3].getClassName()
import static org.jruby.javasupport.JavaEmbedUtils.invokeMethod;
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;
public class JRubyObject {
public static JRubyObject load(Ruby runtime, String dump) {
return new JRubyObject(((IRubyObject) marshalClass(runtime).send("load", dump)));
}
@xli
xli / autoload_threadsafe.rb
Created April 24, 2011 06:53
autoload threadsafe
# Load this file before load anything.
# JRuby-Rack will load rack before load rails, so should load this file when JRuby runtime initialized.
# Call AutoloadThreadsafe.eager_load_all! at the end of rails environment rb
module AutoloadThreadsafe
module Recorder
def self.included(base)
base.class_eval do
alias :unsafe_autoload :autoload
def autoload(m, filename)
@xli
xli / platform_config.rb
Created January 9, 2012 02:18
Load a config.yml file to setup global variables
require 'fileutils'
require 'yaml'
require 'erb'
module PlatformConfig
def load(config_yml)
unless File.exists?(config_yml)
FileUtils.cp("#{config_yml}.example", config_yml)
end
if platform_config = YAML.load(ERB.new(File.read(config_yml)).result)[RUBY_PLATFORM]
@xli
xli / gist:1592604
Created January 11, 2012 02:25
git repo info in termial for pair machine
PS1='\u@\h \W$(__git_ps1 "(%s)[$(git config --get user.name)]")\$ '
PS1='\h \W$(__git_ps1 "($(git config --get user.name)@%s)")\$ '
@xli
xli / gist:dc9b857be8814d598669
Created April 6, 2015 05:21
upload servercert for cloudfront
export PATH=$PATH:~/Downloads/IAMCli-1.5.0/bin
export AWS_IAM_HOME=~/Downloads/IAMCli-1.5.0
iam-servercertupload --aws-credential-file <credential.csv> -s <sname> -b <ssl.crt.txt> -k <ssl.key.txt> -c intermediate.crt.txt -p /cloudfront/<sname>/
# aws-ruby-sdk v1 API
def s3_multipart_upload_credentials(bucket, key, role_arn)
sts = AWS::STS.new
policy = AWS::STS::Policy.new
policy.allow(:actions => ["s3:PutObject",
"s3:GetObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts",
"s3:ListBucketMultipartUploads"],
:resources => "arn:aws:s3:::#{[bucket,key].join('/')}")
@xli
xli / s3-multipart-upload-example-step3.rb
Last active August 29, 2015 14:20
initialize data attributes for EvaporateJS, should output as a HTML tag attributes for JS usage
bucket = <s3 bucket name>
s3key = <s3 file key> # use a random uniq key
{
"data-aws-id" => ENV["S3_MULTIPART_UPLOAD_ACCESS_KEY_ID"],
"data-bucket" => bucket,
"data-key" => s3key,
"data-signer-url" => <your-data-signer-url>
}
@xli
xli / s3-multipart-upload-example-step4.js
Last active August 29, 2015 14:20
setup Evaporate with temp credentials from HTML tag data attributes
// need jQuery and EvaporateJS
var config = $("[data-signer-url]");
var _e_ = new Evaporate({
signerUrl: config.data("signer-url"),
bucket: config.data("bucket"),
aws_key: config.data("aws-id")
});
var configuration = {
name: config.data("key"),
// see EvaporateJS document for other configurations need setup here