Skip to content

Instantly share code, notes, and snippets.

View maxjustus's full-sized avatar

Max Justus Spransy maxjustus

  • People With Jetpacks
  • Los Angeles, CA
View GitHub Profile
function percentChanged(newVal, oldVal) {
if(oldVal == 0) {
return (newVal * 100);
} else {
return parseInt((((newVal - oldVal) / oldVal) * 100).toFixed());
}
}
@maxjustus
maxjustus / Parayield
Created November 11, 2011 15:59
Parallelize a certain section of code and yield the result to a block
require 'benchmark'
require 'net/http'
require 'uri'
thrs = []
url = URI.parse('http://yahoo.com')
class Parayield
attr_accessor :threads
@maxjustus
maxjustus / gist:1454583
Created December 10, 2011 04:33
Rando bukk.it
require 'net/http'
p = Net::HTTP.get(URI.parse('http://bukk.it'))
regexp = /href=\"(.*?\..*?)\"/
images = p.scan(regexp).flatten
image = images[rand(images.length)]
puts "http://bukk.it/" + image
add "add bitty account" link in networks index page
problem with twitter lists on twitter account (won't load)
followers button on twitter account (won't load?)
remove quick reply actions (pictures, link) until we have them working
loader on timeline network nav change (wtf does this mean?)
performance on loading a single twitter account (can't really be helped, api request bound..)
add ok/apply button on report date select
padding on left of users index
show on report clicks (load info in bottom) (wtf does this mean?)
all external links in system should load to new page using target="_blank" (fb post links don't do this atm)
@maxjustus
maxjustus / gist:1677060
Created January 25, 2012 16:23
Ruby Decor8rz
class A
def hi
'hi'
end
end
module B
def hi
super + 'hi'
end
@maxjustus
maxjustus / gist:1705643
Created January 30, 2012 17:47
group by partial date with postgres
select date_trunc('day', created_at), sum(value) from channel_stats group by date_trunc('day', created_at);
@maxjustus
maxjustus / gist:1706277
Created January 30, 2012 19:50
Elimidate temporary array/hash variables
def do_stuff(stuff)
{}.tap do |thing|
thing['guys'] = stuff
end
end
@maxjustus
maxjustus / delegates_matcher.rb
Created January 30, 2012 23:47
handy rspec it_delegates matcher
module RSpec::Example::DelegatesMatcher
def it_delegates(*args)
options = args.pop
if options.is_a?(Hash)
options.symbolize_keys!
delegated_instance = options.fetch(:to) { raise 'You must provide a :to option to it_delegates' }
else
raise 'You must provide an options hash to it_delegates'
end
@maxjustus
maxjustus / gist:1724372
Created February 2, 2012 16:25
Hash with symbol to string lookup
opts = {'meep' => 'feep', :derp => 'herp'}
opts.default_proc = ->(h,k) { h.fetch(k.to_s) { nil } }
opts[:meep]
=> 'feep'
opts['derp']
=> 'herp'
@maxjustus
maxjustus / gist:1935698
Created February 28, 2012 22:25
Using Postgres cursors to iterate through a table
class YrModel < ActiveRecord::Base
has_many :derps
def self.find_with_cursor(query, &blk)
cursor_name = "cursor_#{(rand * 1000000).ceil}"
self.transaction do
self.connection.execute("DECLARE #{cursor_name} CURSOR FOR #{query.to_sql}")
while !(records = self.find_by_sql("FETCH FORWARD 1000 FROM #{cursor_name}")).empty?
#FIXME I think this method is deprecated in 3.1 in favor of #preload
preload_associations(records, [:derps])
blk.call(records)