Skip to content

Instantly share code, notes, and snippets.

View newtonapple's full-sized avatar

David Dai newtonapple

  • Tobiko Data
  • San Francisco
View GitHub Profile
@newtonapple
newtonapple / rspec_rails_have_tag_example.rb
Created November 11, 2009 22:26
RSpec Rails have_tag() example
response.should have_tag("form#my_form") do
have_tag('input#hidden_input_id[type=hidden][value=?]', 'secret')
end
# matches the following HTML:
# <form id="my_form" method="POST" action="create">
# <input id="hidden_input_id" value="secret" type="hidden" />
# </form>
require 'rubygems'
require 'rbench'
require 'json'
TIMES = 1000
json = '{"foo1":"bar1","foo2":"bar2","foo3":"bar3"}'
ruby = "{'foo1'=>'bar1', 'foo2'=>'bar2', 'foo3'=>'bar3'}"
@newtonapple
newtonapple / URL String to Params
Created November 2, 2009 20:14
Rails like params hash from URL encoded strings.
ActionController::AbstractRequest.parse_query_parameters('foo[foo][0]=99') # => {"foo"=>{"foo"=>{"0"=>"99"}}}
ActionController::AbstractRequest.parse_query_parameters('foo[foo][]=99') # => {"foo"=>{"foo"=>["99"]}}
@newtonapple
newtonapple / rturk_regsiter_hit_type.rb
Created October 31, 2009 01:10
An example of RegisterHITType
# Initial implementation can be found here: http://github.com/newtonapple/rturk
# API Doc: http://docs.amazonwebservices.com/AWSMechTurk/latest/AWSMechanicalTurkRequester/
RTurk.setup(AWSAccessKeyId, AWSAccessKey, :sandbox => true)
hit_type = RTurk.RegisterHITType(:title => "Python") do |hit_type|
hit_type.assignments = 500
hit_type.description = "Python Programming!"
hit_type.question("http://localhost:3000/mturk")
hit_type.keywords = 'python, programming'
@newtonapple
newtonapple / rturk.rb
Created October 30, 2009 23:21
RTurk Example
RTurk.setup(AWSAccessKeyId, AWSAccessKey, :sandbox => true)
hit = RTurk::Hit.create(:title => "New Upload 3 Documents") do |hit|
hit.assignments = 500
hit.description = "no copytrighted materials!"
hit.question("http://localhost:3000/mturk")
hit.reward = 0.10
hit.qualifications.add :approval_rate, { :gt => 85 }
end
@newtonapple
newtonapple / bare_bone_mysql_test.rake
Created October 28, 2009 21:21
Rakefile: The bare minimal you'll need to clone a test db from your env.
require 'rubygems'
require 'rake'
require 'activerecord'
CURRENT_ENV = ENV['RAILS_ENV'] || 'development'
ROOT_PATH = File.dirname(__FILE__)
DB_CONFIG = YAML.load_file(File.join(ROOT_PATH, 'config', 'database.yml'))
namespace :db do
namespace :structure do
@newtonapple
newtonapple / sha1_vs_md5.rb
Created October 25, 2009 10:07
Benchmark: SHA1 vs MD5: SHA1 is about 1.4 x slower
require 'rubygems'
require 'rbench'
require 'digest/md5'
require 'digest/sha1'
MD5 = Digest::MD5
SHA1 = Digest::SHA1
TIMES = 100
short_string = 'this is a short string'
@newtonapple
newtonapple / loggers_bench.rb
Created September 9, 2009 19:34
Benchmark: Syslog VS Logger VS BufferedLogger
require 'rbench'
require 'activesupport'
require 'syslog'
require 'logger'
buffered = ActiveSupport::BufferedLogger.new('buffered.log')
logger = Logger.new('logger.log')
syslog = Syslog.open('rb_syslog')
TIMES = 5
@newtonapple
newtonapple / Snow Leopard for Ruby Developers
Created September 4, 2009 22:07
Snow Leopard Upgrade Guide for Rails Developers
Snow Leopard for Ruby Developers
## Official Rails Guide to upgrading Snow Leopard
http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard
## MacPorts:
reference page: http://www.macports.org/install.php
download macports: http://distfiles.macports.org/MacPorts/MacPorts-1.8.0-10.6-SnowLeopard.dmg
## Afte installing macports do this:
@newtonapple
newtonapple / rails_template.rb
Created August 27, 2009 22:50
A Rails Template: JQuery, Rspec, WillPaginate, BoilerPlate CSS, SQL Schema, NO Time Stamped Migration
## ENV
environment do
<<-ENV_CODE
config.active_record.timestamped_migrations = false
config.active_record.schema_format = :sql
# ENV['RAILS_ASSET_ID'] = '' # turn off assets timestamp
ENV_CODE
end