Skip to content

Instantly share code, notes, and snippets.

@a-leung
a-leung / websocketClient.js
Created June 4, 2015 16:18
Websocket Rails client javascript class
(function(websocketClient, $, undefined) {
var PING_INTERVAL = 42000,
RECONNECT_INTERVAL = 10000,
WEBSOCKET_URL = window.location.host + '/websocket';
var config, reconnectInterval, keepAliveInterval, dispatcher;
websocketClient.init = function(websocketConfig) {
config = websocketConfig;
setupDispatcher();
};
@a-leung
a-leung / random-v2.rb
Created October 24, 2012 15:15
random number guesser
GUESS_RANGE = 100
guessNumber = rand(GUESS_RANGE)
puts "Guess the number! (Hint: between 0 and #{GUESS_RANGE})"
guessCount = 0
while (userGuess = gets.to_i) != guessNumber # keep getting more numbers
# give out hints to the user
puts 'Too high' if userGuess > guessNumber
@a-leung
a-leung / random.rb
Created October 25, 2012 17:32
random number guesser - first take
number = rand(10)
puts 'Guess the number!'
input = 'input number'
while input != number
input = gets.to_i # keep getting more numbers
# give out hints to the user
if input > number
puts 'Too high'
end
param[:attribute] = true
# model passes validation as model.attribute = true
has_many model_B
before_save set_model_b_attributes
def set_model_b_attributes
self.bs.each do |b|
b.value = self.value
end
end
@a-leung
a-leung / model.rb
Last active January 28, 2016 20:36
rspec and ActiveRecord::Base.transaction testing
def set_new_name_on_items?
success = ActiveRecord::Base.transaction do
model.items.each do |i|
i.name = 'new name'
i.save or raise ActiveRecord::Rollback
end
!!success
end
@a-leung
a-leung / output from pg_restore
Last active February 4, 2016 22:05
postgres pg_restore debug
pg_restore: executing SEQUENCE SET key_configurations_id_seq
pg_restore: processing data for table "kiosk_scans"
out of memory
@a-leung
a-leung / refund.rb
Created February 8, 2016 16:50
rspec mocking issue
def refund
self.items
ActiveRecord::Base.transaction do
self.items.each { |i| i.update!(refund: true) }
end
end
def refund_v2
refundable_items = self.items.where(refund: false)
@a-leung
a-leung / refund_stripe.rb
Last active March 8, 2016 21:04
Stripe Stripe-Mock refunding
gem 'stripe', '1.31.0'
require 'stripe'
Stripe.api_key = <insert API key here>
token = Stripe::Token.create(
:card => {
:number => "4242424242424242",
:exp_month => 3,
:exp_year => 2017,
:cvc => "314"
@a-leung
a-leung / .tmux.conf
Last active March 17, 2016 19:23
tmux.conf
###################
# Prefix key setup
###################
set -g prefix C-a # remap prefix to Control + a --> i'm old that way.
# bind 'C-a C-a' to type 'C-a'
bind a send-prefix # send 'C-a' into terminal app by typing: 'C-a a'
bind C-a last-window # bounce between windows just by typing 'C-a C-a'
unbind C-b # get rid of C-b as prefix
########################