Skip to content

Instantly share code, notes, and snippets.

View sgonyea's full-sized avatar

Scott Gonyea sgonyea

  • Google
  • Mountain View, CA
View GitHub Profile
@sgonyea
sgonyea / pgp.rb
Created March 2, 2012 01:34
BouncyCastle PGP Encryption in JRuby, _WITHOUT_ writing the data to disk before encryption
require 'java'
require 'java/bcpg-jdk16-146.jar'
module MyProject
def self.keys_dir
@keys_dir ||= Bundler.root.join("config/pgp/")
end
module PGP
java_import 'java.io.ByteArrayInputStream'
module OurService
def run_thrift_server
handler = TcUser::Thrift::UserServiceHandler.new
processor = TcUser::Thrift::UserService::Processor.new(handler)
transport = ::Thrift::ServerSocket.new(port)
t_factory = ::Thrift::BufferedTransportFactory.new
@server = ::Thrift::MutexableThreadPoolServer.new(processor, transport, t_factory, nil, {:num => 20, :mutex => mutex})
info "Service '#{name}' started with Process ID #{$$}"
@sgonyea
sgonyea / ohm-expiring.rb
Created January 20, 2012 22:23
Mixin to add support for expiring keys within Ohm
module Ohm
module Expiring
def self.included(base)
base.instance_exec {
include InstanceMethods
extend ClassMethods
}
end
/usr/local/share/workspace/honk$ irb
1.9.3p0 :001 > require 'digest/sha1'
LoadError: dlopen(/Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/x86_64-darwin11.1.0/digest/sha1.bundle, 9): Symbol not found: _rb_Digest_SHA1_Finish
Referenced from: /Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/x86_64-darwin11.1.0/digest/sha1.bundle
Expected in: flat namespace
in /Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/x86_64-darwin11.1.0/digest/sha1.bundle - /Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/x86_64-darwin11.1.0/digest/sha1.bundle
from /Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/s/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):1
from /Users/s/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
sgonyea@s:~/workspace/myproj$ gem install curb --version 0.7.15
Successfully installed cucumber-0.3.100
1 gem installed
sgonyea@s:~$ gem install encryptor
Successfully installed dynamic_form-1.1.4
1 gem installed
sgonyea@s:~/workspace/myproj$ rvm use jruby@global
@sgonyea
sgonyea / nginx_log_regex.rb
Created November 30, 2011 19:07
A documented regex, Ruby. You can do this. It's great.
def log_regex
@log_regex ||= begin
ip_regex = '(?:\d+\.){3}\d+'
log_regex = /^
(#{ip_regex})\s-\s-\s # IP
\[(.*?)\]\s # Date
"(.*?)"\s # Request
(\d+)\s # Response Code
(\d+)\s # Bytes Sent by Server
([\d\.]+)\s # Request Time
@sgonyea
sgonyea / exportCookies-monkey_patch_edition.rb
Created November 22, 2011 05:57
MacRuby script to export Safari 5.1 cookies to a human-readable format
#!/usr/bin/env macruby
require 'csv'
framework 'Foundation'
CSV_Headers = %w[domain path expiresDate name value].to_csv
class NSHTTPCookie
def to_csv
[domain, path, expiresDate, name, value].to_csv
end
a = (1..10).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = (11..20).to_a
# => [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[a,b].transpose.flatten
# => [1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20]
describe "MTPS" do
subject { Thrift::MutexableThreadPoolServer.new nil, nil }
def Thread.block=(block); @block = block; end
def Thread.block(&block)
self.block = block if block_given?
return(@block)
end
let(:server) { subject }
@sgonyea
sgonyea / gist:1088561
Created July 18, 2011 04:45
What the hell, rake?
Last login: Sun Jul 17 21:03:05 on ttys003
scott@mbp:~$ ds
Using /Users/scott/.rvm/gems/ruby-1.9.2-p290
scott@mbp:~/Sites/my_project$ gem uninstall rake
You have requested to uninstall the gem:
rake-0.9.1
chunky_png-1.2.0 depends on [rake (>= 0)]
factory_girl_rails-1.1.beta1 depends on [rake (>= 0)]