Skip to content

Instantly share code, notes, and snippets.

@SafeAF
SafeAF / authentication_with_bcrypt_in_rails_4.md
Last active September 4, 2015 01:13 — forked from thebucknerlife/authentication_with_bcrypt_in_rails_4.md
Simple Authentication in Rail 4 Using Bcrypt

#Simple Authentication with Bcrypt

This tutorial is for adding authentication to a vanilla Ruby on Rails app using Bcrypt and has_secure_password.

The steps below are based on Ryan Bates's approach from Railscast #250 Authentication from Scratch (revised).

You can see the final source code here: repo. I began with a stock rails app using rails new gif_vault

##Steps

@SafeAF
SafeAF / us-ips.rb
Last active September 7, 2015 08:28
iptables rule generator to allow US-based IP addresses
require 'csv'
require 'ipaddr'
last = nil
CSV.foreach("us.csv") do |row|
next if row.empty?
from = IPAddr.new(row[0])
to = IPAddr.new(row[1])
mask = from.to_i ^ to.to_i
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby

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

@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
@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 / 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 / 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 / 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 / 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