Skip to content

Instantly share code, notes, and snippets.

View cantremember's full-sized avatar
✏️
Under Construction

Dan Foley cantremember

✏️
Under Construction
View GitHub Profile
@cantremember
cantremember / delayedCallback.js
Created March 23, 2012 04:10
delayedCallback(function(){ ... }, delay); // invoke the callback 'delay' ms after it is last called
// returns a function that will invoke the callback 'delay' ms after it is last called
// eg. invoke the callback 500ms after the last time a key is pressed
// based on http://stackoverflow.com/questions/2219924/idiomatic-jquery-delayed-event-only-after-a-short-pause-in-typing-e-g-timew
// but fixes the following:
// (a) returns a unique timer scope on every call
// (b) propagates context and arguments from the last call to the returned closure
// and adds .cancel() and .fire() for additional callback control
// then of course, you could use underscore's _.debounce
// it doesn't have the .cancel and .fire, but you may not care :)
function delayedCallback(callback, delay) {
@cantremember
cantremember / example-gist.java
Created March 23, 2012 05:14
trivial InetAddress.getByName() in Java
try {
if (getLog().isTraceEnabled())
getLog().trace("lookup : " + hostname);
// oh, lookup!
InetAddress ipAddress = InetAddress.getByName(hostname);
// NOTE: this is where it went bad on the AWS image
// *sigh*
@cantremember
cantremember / facebook-tunnel-nginx.conf
Created March 23, 2012 05:18
Facebook Tunneling with nginx example conf
#
# included into vhosts/localhost.conf (*.by-ip)
# answers to :80
#
# registered with FaceBook
location /forgiveness {
# no-trailing-/ case (the rewritten URI has a zero length)
if ($request_filename = '') {
rewrite (.*) $1/;
@cantremember
cantremember / facebook_tunnel_action.rb
Created March 23, 2012 05:21
Facebook Tunnel action for Rails
# allows for Facebook pub / dev tunneling
# have to set the Cookie within the scope of the app
# can't use Firefox cookie editor to hack it
def tunnel
# how convenient!
value = params[:id]
# this is how we get pub / dev tunneling
# see support/nginx
if value == 'nil'
@cantremember
cantremember / coming_up_to_spec.rb
Created March 23, 2012 05:24
coming up to speed with RSpec
specify "knowledge of nil" do
''.should_not be_nil
end
specify "numeric calculations" do
(355.0 / 113).should be_close(Math::PI, 0.1)
end
specify "changes made by a closure" do
array = []
@cantremember
cantremember / silly_mocking_spec.rb
Created March 23, 2012 05:25
silly mocking in RSpec
def expect_raise(type=Spec::Mocks::MockExpectationError, &block)
abort 'block must be provided' unless block_given?
block.should raise_error(type)
end
specify "the basics" do
@mock.should_receive :hello
@mock.should_not_receive :goodbye
@mock.hello
@cantremember
cantremember / twitter4r_mock_spec.rb
Created March 23, 2012 05:28
mocking Twitter4R in RSpec
before(:each) do
Twitter::Client.send :public, :create_http_get_request
@client = Twitter::Client.new
@client_clone = Twitter::Client.new
end
it "produces a querystring with all the crazy stuff Twitter imagined" do
the_params = nil
@cantremember
cantremember / net_http_response_mock.rb
Created March 23, 2012 05:29
silly mocking of Net::HTTPResponse
class Net::HTTPResponse
def self.mock(ops={})
# defaults
ops = {
:http_version => '1.1',
:code => '200',
:message => 'OK',
:body => nil,
}.merge(ops || {})
@cantremember
cantremember / twitter4r_search_patch.rb
Created March 23, 2012 05:31
a little patch to Twitter4R for Twitter Search API
@@config_mutex = Mutex.new
def configuration_mutex
@@config_mutex
end
alias :raw_http_connect :http_connect
def http_connect(*args, &block)
ops = (Hash === args.last) ? args.pop : {}
result = nil
@cantremember
cantremember / net_ssh_channel_observer.rb
Created March 23, 2012 05:40
Net::SSH::Channel observer
def observer_struct
@observer_struct ||= Struct.new(:channel, :stdout, :stderr, :exit_status, :exit_signal)
end
def observe_channel(ch, ops={})
observer = observer_struct.new(ch, '', '', 0, nil)
is_puts = !!ops[:puts]
# stdout
ch.on_data do |ch, data|