Skip to content

Instantly share code, notes, and snippets.

# 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
@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
@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 / rb_c_xor_file_contents_extension.rb
Last active September 1, 2015 17:13 — forked from nruth/rb_c_xor_file_contents_extension.rb
RubyInline C experimentation
# run with Piece.xor_files 'output_filepath', ['infilepath1','infilepath2']
require 'inline'
Class Piece
inline do |builder|
builder.include '<stdlib.h>'
builder.include '<stdio.h>'
builder.c_singleton <<-C_CODE
/*
rb_input_paths_array should be a ruby array of strings,
@SafeAF
SafeAF / config.json
Last active September 1, 2015 13:24 — forked from anonymous/config.json
Bootstrap Customizer Config
{
"vars": {
"@gray-base": "#000",
"@gray-darker": "lighten(@gray-base, 13.5%)",
"@gray-dark": "lighten(@gray-base, 20%)",
"@gray": "lighten(@gray-base, 33.5%)",
"@gray-light": "lighten(@gray-base, 46.7%)",
"@gray-lighter": "lighten(@gray-base, 93.5%)",
"@brand-primary": "darken(#428bca, 6.5%)",
"@brand-success": "#5cb85c",
@SafeAF
SafeAF / example.erb
Last active August 29, 2015 14:27 — forked from jjulian/example.erb
Running Ruby under Apache like PHP, using cgi - http://stackoverflow.com/a/1901772/231245
<% header "Content-Type" => "text/html" %>
<h1>Let's run some ruby code: <%= rand %></h1>
@SafeAF
SafeAF / key and value.rb
Created August 9, 2015 17:30
active support instrumentation
### start_processing.action_controller
| Key | Value |
| ------------- | --------------------------------------------------------- |
| `:controller` | The controller name |
| `:action` | The action |
| `:params` | Hash of request parameters without any filtered parameter |
| `:format` | html/js/json/xml etc |
| `:method` | HTTP request verb |
@SafeAF
SafeAF / redis-admin-tips.pro
Created July 30, 2015 20:42
redis pro stuff admin settings configs best practices
http://redis.io/topics/admin
Redis Administration
This page contains topics related to the administration of Redis instances. Every topic is self contained in form of a FAQ. New topics will be created in the future.
Redis setup hints
We suggest deploying Redis using the Linux operating system. Redis is also tested heavily on OS X, and tested from time to time on FreeBSD and OpenBSD systems. However Linux is where we do all the major stress testing, and where most production deployments are working.
Make sure to set the Linux kernel overcommit memory setting to 1. Add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect immediately.
Make sure to disable Linux kernel feature transparent huge pages, it will affect greatly both memory usage and latency in a negative way. This is accomplished with the following command: echo never > sys/kernel/mm/transparent_hugepage/enabled.
Make sure to setup some swap in your syst
@SafeAF
SafeAF / redis_json_marshal_eval_benchmarks.rb
Last active August 29, 2015 14:26 — forked from shilov/redis_json_marshal_eval_benchmarks.rb
Ruby Redis benchmarks for JSON vs Marshal vs String and code examples
Redis Marshaling
If you have time consuming method in your app that you call frequently, chances are that you’d like to cache it. Redis is great key-value store that can be used for this purpose. However, redis only store string values. In order to store other kinds of objects, you’ll have to use marshaling.
There is a built-in ruby class called Marshal that does the job well. The only thing you have to do is to call Marshal.dump method and pass it an object. The exact opposite is the Marshal.load method.
I’ve created helper method for easier integration with Redis. Here is how it looks:
module RedisHelper
@SafeAF
SafeAF / rvm-usage.txt
Last active August 29, 2015 14:26
rvm advanced usage examples
$ \curl -sSL https://get.rvm.io | bash -s stable
$ echo "source ~/.profile" >> ~/.bash_profile
$ echo "gem: —no-document" >> ~/.gemrc
$ rvm autolibs enable
$ rvm install ruby
$ gem install rails
$ rvm use 2.2 --default
# Update your RVM to the latest statble version.
rvm get latest
# Update your RVM to the most recent version. (most bug fixes)