Skip to content

Instantly share code, notes, and snippets.

View nddeluca's full-sized avatar

Nick DeLuca nddeluca

View GitHub Profile
PatrickJS / api.service.ts
Last active September 18, 2017 18:07
Universal Cache
View api.service.ts
// Something like this. I haven't tested this file
export class ApiService {
constructor(public _http: Http, public _cache: Cache) {
// whatever domain/feature method name
getModel(url) {
// you want to return the cache if there is a response in it. This would cache the first response so if your API isn't idempotent you probably want to remove the item from the cache after you use it. LRU of 1
View gist:8393897
Ruby 2.1.0 in Production: known bugs and patches
Last week, we upgraded the rails app to ruby 2.1.0 in production.
While testing the new build for rollout, we ran into a number of bugs. Most of
these have been fixed on trunk already, but I've documented them below to help
anyone else who might be testing ruby 2.1 in production.
@naruse I think we should backport these patches to the ruby_2_1 branch and
release 2.1.1 sooner rather than later, as some of the bugs are quite critical.
I'm happy to offer any assistance I can to expedite this process.
timruffles / hash_converter.rb
Created May 24, 2012 09:47
rails hash formatter - hash/json to/from camel case and underscores
View hash_converter.rb
module HashConverter
class << self
def to_underscore hash
convert hash, :underscore
def to_camel_case hash
convert hash, :camelize, :lower
def convert obj, *method
case obj
klovadis / gist:2549131
Created April 29, 2012 10:03
How to use optional arguments in node.js
View gist:2549131
// example function where arguments 2 and 3 are optional
function example( err, optionalA, optionalB, callback ) {
// retrieve arguments as array
var args = [];
for (var i = 0; i < arguments.length; i++) {
// first argument is the error object
klovadis / gist:2549029
Created April 29, 2012 09:35
Short circuit function chains if an error occurs
View gist:2549029
var fs = require('fs');
// read a file
function read_the_file(filename, callback) {
// begin by reading a file
fs.readFile(filename, function (err, contents) {
// an error occurred, i.e. the file was not found.
// instead of throwing an error, skip the other
klovadis / gist:2548942
Created April 29, 2012 09:22
Typical node.js callback pattern
View gist:2548942
// include the filesystem module
var fs = require('fs');
// fs.readFile: read a file and all its contents,
// then call a callback function
fs.readFile('/some/file', function (err, contents) {
// if any error occurred, throw it
if (err) throw err;
cee-dub / postgres_timestamp_defaults.rb
Created January 29, 2012 20:53
Convenient methods to let PostgresQL manage created/updated_at
View postgres_timestamp_defaults.rb
require 'active_support/core_ext/string/filters'
module PostgresTimestampDefaults
def add_timestamp_defaults(table_name)
add_default_now(table_name, :created_at)
add_default_now(table_name, :updated_at)
def add_default_now(table_name, column_name)
shilov / redis_json_marshal_eval_benchmarks.rb
Created January 27, 2012 23:01
Ruby Redis benchmarks for JSON vs Marshal vs String
View redis_json_marshal_eval_benchmarks.rb
require 'benchmark'
require 'json'
require 'redis'
# -----------------
puts "Initialize variables.."
attilagyorffy / css_colour_validator.rb
Created October 29, 2011 12:27
CSS colour validation in Rails 3
View css_colour_validator.rb
# Put this file under Rails.root /lib
class CssColourValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return false unless value
record.errors[attribute] << (options[:message] || 'is not a valid CSS colour') unless ::HexadecimalColourValidator.matches?(value) or ::WebSafeColourValidator.matches?(value)
hotchpotch / unicorn_killer.rb
Created October 3, 2011 08:11
Unicorn process killer utility
View unicorn_killer.rb
# # your
# require 'unicorn_killer'
# use UnicornKiller::MaxRequests, 1000
# use UnicornKiller::Oom, 400 * 1024
module UnicornKiller
module Kill
def quit
sec = ( - @process_start).to_i
warn "#{self.class} send SIGQUIT (pid: #{})\talive: #{sec} sec"