Skip to content

Instantly share code, notes, and snippets.

View nddeluca's full-sized avatar

Nick DeLuca nddeluca

View GitHub Profile
@PatrickJS
PatrickJS / api.service.ts
Last active September 18, 2017 18:07
Universal Cache
// Something like this. I haven't tested this file
@Injectable()
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
Ruby 2.1.0 in Production: known bugs and patches
Last week, we upgraded the github.com 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
timruffles / hash_converter.rb
Created May 24, 2012 09:47
rails hash formatter - hash/json to/from camel case and underscores
module HashConverter
class << self
def to_underscore hash
convert hash, :underscore
end
def to_camel_case hash
convert hash, :camelize, :lower
end
def convert obj, *method
case obj
@klovadis
klovadis / gist:2549131
Created April 29, 2012 10:03
How to use optional arguments in node.js
// 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++) {
args.push(arguments[i]);
}
// first argument is the error object
@klovadis
klovadis / gist:2549029
Created April 29, 2012 09:35
Short circuit function chains if an error occurs
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
klovadis / gist:2548942
Created April 29, 2012 09:22
Typical node.js callback pattern
// 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
cee-dub / postgres_timestamp_defaults.rb
Created January 29, 2012 20:53
Convenient methods to let PostgresQL manage created/updated_at
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)
add_updated_at_trigger(table_name)
end
def add_default_now(table_name, column_name)
@shilov
shilov / redis_json_marshal_eval_benchmarks.rb
Created January 27, 2012 23:01
Ruby Redis benchmarks for JSON vs Marshal vs String
# http://forrst.com/posts/JSON_vs_Marshal_vs_eval_Which_is_the_fastest_fo-6Qy
require 'benchmark'
require 'json'
require 'redis'
# -----------------
puts "Initialize variables.."
@attilagyorffy
attilagyorffy / css_colour_validator.rb
Created October 29, 2011 12:27
CSS colour validation in Rails 3
# 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)
end
end
@hotchpotch
hotchpotch / unicorn_killer.rb
Created October 3, 2011 08:11
Unicorn process killer utility
# # your config.ru
# require 'unicorn_killer'
# use UnicornKiller::MaxRequests, 1000
# use UnicornKiller::Oom, 400 * 1024
module UnicornKiller
module Kill
def quit
sec = (Time.now - @process_start).to_i
warn "#{self.class} send SIGQUIT (pid: #{Process.pid})\talive: #{sec} sec"