Skip to content

Instantly share code, notes, and snippets.

@leon
leon / ui-router-resolve.js
Created September 13, 2013 13:47
ui-router resolve
$stateProvider.state('main', {
abstract: true,
url: '/:account',
resolve: {
auth: function ($q, $state, $stateParams, $location, AccountRepo, Security) {
return AccountRepo.auth().then(function (user) {
// If not logged in redirect to login with path preserved
if (user === false) {
$state.go('account.login', {redirect: $location.url()});
return $q.reject('not logged in');
# Multiple inheritance with Modules as an alternative to injected composition
# from Sandi Metz's talk [Nothing is Something](http://confreaks.tv/videos/bathruby2015-nothing-is-something)
# Like Sandi's 'direct' DI method this has behavior outside of the base class
# that gets composed together. However in this gist I compose modules in class
# definitions instead of injecting collaborators.
# Tradeoffs between this and Sandi's version are that in this case the API consumer doesn't
# have to know how to make a RandomEchoHouse (no `house = House.new(formatter: Whatever.new)`),
# but also the API consumer can't make anything not already accounted for either.
@gionn
gionn / fog_cloudfiles_timeout.md
Last active March 20, 2017 12:52
Fog CloudFiles (Rackspace) reporting timeout errors

If you are getting something like the following, on a box without ipv6 connectivity:

/opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/excon-0.44.4/lib/excon/socket.rb:291:in `raise_timeout_error': read timeout reached (Excon::Errors::Timeout)
	from /opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/excon-0.44.4/lib/excon/socket.rb:49:in `rescue in readline'
	[...]
	from /opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/fog-core-1.29.0/lib/fog/core/connection.rb:81:in `request'
	from /opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/fog-1.28.0/lib/fog/rackspace/service.rb:42:in `request'
	from /opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/fog-1.28.0/lib/fog/rackspace/storage.rb:444:in `request'
	from /opt/rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/fog-1.28.0/lib/fog/rackspace/requests/storage/put_object.rb:35:in `put_object'
@1st8
1st8 / gist:3128135
Created July 17, 2012 08:47
God process monitoring + Upstart + rbenv
description "God Process Monitoring"
author "Christoph Geschwind <christoph@mixxt.net>"
start on runlevel [2345]
stop on runlevel [!2345]
respawn # respawn the service if it dies
respawn limit 5 10 # stop respawning if it fails 5 times in 10 seconds
pre-start script
@weppos
weppos / capistrano_database_yml.rb
Created July 27, 2008 10:04
Provides a couple of tasks for creating the database.yml configuration file dynamically when deploy:setup is run.
#
# = Capistrano database.yml task
#
# Provides a couple of tasks for creating the database.yml
# configuration file dynamically when deploy:setup is run.
#
# Category:: Capistrano
# Package:: Database
# Author:: Simone Carletti <weppos@weppos.net>
# Copyright:: 2007-2010 The Authors
require "uri"
(URI::REGEXP.constants - ["PATTERN"]).each do |rc|
puts "#{rc}: #{URI::REGEXP.const_get(rc)}"
end
URI::REGEXP::PATTERN.constants.each do |pc|
puts "#{pc}: #{URI::REGEXP::PATTERN.const_get(pc)}"
end
ENV["WATCHR"] = "1"
system 'clear'
def growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Test Results"
image = message.include?('0 failures, 0 errors') ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
system %(#{growlnotify} #{options} &)
end
@mattetti
mattetti / rails_json_session.rb
Last active September 23, 2020 07:04
This is a monkey patch to change Rails 4's default session/signed cookie serializer from Marshal to JSON for security and compatibility reasons. Note that this is a hack, a pretty terrible one and you should only use it if you know what you're doing. Also, I only wrote this patch for my own personal use, so don't be surprised if it doesn't work …
# Hack to change the Rails cookie serializer from Marshal to JSON and therefore allow the session
# to be shared between different languages but also avoid that someone knowing the
# cookie secret key could execute arbitrary code on the server by unmarshalling
# modified Ruby code added to the session/permanent cookie.
#
# Note that all users will beed to login again since both the remember me cookie and the session cookies
# won't be valid. Note also that the remember me cookie is tested multiple times per request even when it fails.
# for performance reasons you might want to delete it if these extra cycles are too costly for you.
#
# Rails 4 (not tested on Rails 3).
@nogweii
nogweii / sysctl.changed.conf
Created February 22, 2012 20:30
A massive collection of various sysctl files, designed for drop-in CCDC fixing
fs.file-max = 65535
fs.inode-max = 32768
fs.suid_dumpable = 0
kernel.core_uses_pid = 1
kernel.exec-shield = 1
kernel.maps_protect = 1
kernel.msgmax = 65536
kernel.msgmnb = 65536
kernel.panic = 30
kernel.panic_on_oops = 30
@albertstill
albertstill / enigma_machine.rb
Last active February 25, 2021 18:46
Understand how the Enigma machine works with 30 lines of Ruby
Plugboard = Hash[*('A'..'Z').to_a.sample(20)]
Plugboard.merge!(Plugboard.invert)
Plugboard.default_proc = proc { |_, key| key }
def build_a_rotor
Hash[('A'..'Z').zip(('A'..'Z').to_a.shuffle)]
end
ROTOR_1, ROTOR_2, ROTOR_3 = build_a_rotor, build_a_rotor, build_a_rotor