Skip to content

Instantly share code, notes, and snippets.

@SafeAF
SafeAF / Gemfile
Created November 30, 2015 05:53 — forked from ahaedike/Gemfile
source "https://rubygems.org"
gem 'eventmachine'
gem 'rubysl-stringio'
gem 'sinatra'
gem 'yajl-ruby', require: 'yajl'
gem 'thin'
gem 'em-websocket', :git=>'https://github.com/igrigorik/em-websocket.git'
@SafeAF
SafeAF / mongoose-encrypted-schematype-field.md
Created November 26, 2015 05:12 — forked from kljensen/mongoose-encrypted-schematype-field.md
Encrypt a text field in Mongoose MongoDB ORM

Encrypting text fields in Mongoose is easy using Node's built-in crypto module. You might want to do this if you're using MongoDB as a service (see the recent MongoHQ security breach); or, if you're storing OAuth tokens that could, in the wrong hands, screw with somebody's account on a 3rd party service. (Of course, you should never encrypt passwords: those should be hashed.)

Imagine you have a Mongoose model like that shown below, which is modified only slighly from the example on the MongooseJS homepage.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var User = mongoose.model('User', {
 name: String,
@SafeAF
SafeAF / computeDistance.js
Created November 6, 2015 06:43 — forked from joseche/computeDistance.js
Compute the distance between two coordinates
function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);
var Radius = 6371; // radius of the Earth in km
var distance =
Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
@SafeAF
SafeAF / gist:7d1f4eecdc06ca591d86
Created October 31, 2015 09:51 — forked from npassaro/gist:307a23f40287482a5275
How to use activerecord standalone
require 'active_record'
require 'sqlite3'
require 'logger'
ActiveRecord::Base.logger = Logger.new('debug.log')
ActiveRecord::Base.configurations = YAML::load(IO.read('database.yml'))
ActiveRecord::Base.establish_connection('development')
class Schema < ActiveRecord::Migration
def change
@SafeAF
SafeAF / refactoring-1_my_thoughts_and_philosophy_on_refactoring.md
Last active October 24, 2015 10:53
refactoring code thoughts, methods, and philosophy

Refactoring

  1. You don't understand your code unless you can execute it in your head.. in the shower. Pro-points if you can decompose down to assembly and execute.
  2. You learn as much or more about your code through refactoring as when you wrote it the first time, especially applies to architecture.
  3. Refactoring is very often architecture centric/focused (code&data flow/paths, abstraction, composition, systems, coupling & interfaces) vs implementation ( flow control, conditions/constraints, data, operations etc). This makes architecture higher level and therefore requiring deeper thought and different thought processes.
  4. Anytime you are building systems/writing code for even mildly complex programs it is key to map it out on paper first. It seems trivial to the uniniated but it is a definite classifer regarding pro vs nube engineers.
  5. Don't be afraid of breaking things, after all you wrote tests right? YOU DID WRITE TESTS DIDNT YOU?!?! Ouch, that one stings..

SJK

@SafeAF
SafeAF / curses.rb
Created October 24, 2015 10:36
ruby curses example
initialize ten workers. Each worker is responsible for keeping track of the work she has completed, @percent, and then reporting that progress on her own line, @index, of the console.
#!/usr/bin/env ruby
require 'curses'
Curses.noecho
Curses.init_screen
class Worker
@SafeAF
SafeAF / mongoid.yml
Created October 24, 2015 01:17
database configurations adn other server configs
development:
# Configure available database clients. (required)
clients:
# Defines the default client. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: my_db
# Provides the hosts the default client can connect to. Must be an array
# of host:port pairs. (required)
@SafeAF
SafeAF / chef-ruby-install-recipe.rb
Last active October 23, 2015 08:55
chef recipes
#For example, to use the default recipe in a role named "base", use 'ruby' in the run list and set the node[:languages[:ruby][:default_version] attribute:
{
"name": "base",
"description": "Base role is applied to all systems",
"json_class": "Chef::Role",
"default_attributes": {
},
"override_attributes": {
@SafeAF
SafeAF / sysctl.conf
Created October 22, 2015 01:35 — forked from rschmitty/sysctl.conf
ubuntu sysctl.conf settings
# changes from http://www.cyberciti.biz/faq/linux-kernel-etcsysctl-conf-security-hardening/
# Protect ICMP attacks
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Turn on protection for bad icmp error messages
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Turn on syncookies for SYN flood attack protection
net.ipv4.tcp_syncookies = 1

Commands examples

If the namespace is not used then the commands will perform on top of the default database. bundle exec rake db:create bundle exec rake db:migrate

By using the namespace we are going to use all the configuration for our alternate DB. bundle exec rake store:db:create bundle exec rake store:db:migrate