Skip to content

Instantly share code, notes, and snippets.

@pgeraghty
pgeraghty / gfw.js
Last active August 29, 2015 14:16
W2V example script - Global Forest Watch
// W2V script intended for http://www.globalforestwatch.org/map/3/15.00/27.00/ALL/grayscale/loss,forestgain?begin=2001-01-01&end=2013-01-01&threshold=30
w2v.info("Injecting polyfill");
// Inject W2V polyfill so that this script will run in normal browsers without errors
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)){ return; }
js = d.createElement(s); js.id = id;
js.onload = function(){
@pgeraghty
pgeraghty / format_compact_duration.rb
Created February 24, 2015 01:42
format compact duration as days, hours, minutes, seconds e.g. 3d23h10s
# seconds = duration in seconds as integer i.e. time.to_i
[
seconds / (60*60*24), # days
seconds % (60*60*24) / (60*60), # hours
seconds % (60*60) / 60, # minutes
seconds % 60 # seconds
].zip(%w(d h m s)).map { |u| u.join unless u.first.zero? }.compact.tap { |a| a << '<1s' if a.none? }.join
# Uses Object#tap rather than Rails Object enhancements like Object#presence
@pgeraghty
pgeraghty / gist:bd3cd6ac42c5cf7d9e06
Created February 2, 2015 01:57
Test HTML embed
<a href="#">Test</a>
@pgeraghty
pgeraghty / deploy.rb
Created December 7, 2014 14:11
Mina - serial deployment to multiple hosts
set :domains, %w[host1 host2 host3]
desc "Deploy to all servers"
task :deploy_all do
isolate do
domains.each do |domain|
set :domain, domain
invoke :deploy
run!
end
@pgeraghty
pgeraghty / gist:9600721
Last active August 29, 2015 13:57
Migrate Carrierwave local images to S3
Image.where.not(photo: nil).each do |image|
image_path = URI.parse(image.photo_url).path
image.update_attribute("remote_photo_url", "http://localhost:3000#{image_path}")
end
@pgeraghty
pgeraghty / gist:9469912
Created March 10, 2014 17:35
Benchmark require load time and memory usage
require 'benchmark'
def require(file)
puts `pmap #{Process.pid} | grep 'total'`
puts Benchmark.measure('') { super }.format("%t require #{file}")
end
@pgeraghty
pgeraghty / rails_admin.rb
Last active December 7, 2023 15:22
Add Delayed Job to Rails Admin (make it visible as a model)
RailsAdmin.config do |config|
config.included_models = RailsAdmin::Config.models_pool << 'Delayed::Job'
config.model Delayed::Job do
label 'Task'
navigation_label 'Background Processing'
end
end
@pgeraghty
pgeraghty / gist:5431830
Created April 22, 2013 01:13
ox 2.0.0 failure
~ $ ruby -rox -ropen-uri -e 'Ox.sax_parse(Ox::Sax.new, open("http://go.alphashare.com/external/external.php?__method=external_xmlfeed&__feed=kyr&__company_serial=376"))'
*** glibc detected *** ruby: double free or corruption (!prev): 0x00000000018f0530 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f15e8cc4b96]
/home/paul/.rvm/gems/ruby-2.0.0-p0/gems/ox-2.0.0/ext/ox/ox.so(ox_sax_drive_cleanup+0x21)[0x7f15e73ebaa1]
/home/paul/.rvm/gems/ruby-2.0.0-p0/gems/ox-2.0.0/ext/ox/ox.so(ox_sax_parse+0x640)[0x7f15e73ee1c0]
/home/paul/.rvm/gems/ruby-2.0.0-p0/gems/ox-2.0.0/ext/ox/ox.so(+0x12c5d)[0x7f15e73f0c5d]
/home/paul/.rvm/rubies/ruby-2.0.0-p0/lib/libruby.so.2.0(+0x1996ff)[0x7f15e919e6ff]
/home/paul/.rvm/rubies/ruby-2.0.0-p0/lib/libruby.so.2.0(+0x1a6e4d)[0x7f15e91abe4d]
@pgeraghty
pgeraghty / gist:4948780
Created February 13, 2013 22:03
ActiveRecord::Base.sample hack for choosing a random model instance - useful for tests and seeding
module ActiveRecord
class Base
# Use like User.sample or User.pluck(:id).sample
def self.sample
order('RAND()').first rescue nil
end
end
end
@pgeraghty
pgeraghty / gist:4754245
Last active December 12, 2015 09:49
Finding emails and URLs in MySQL columns
# contains email address
where("column_name REGEXP '[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]@[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}'").count
# contains url
where("column_name REGEXP ?", "(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}").count