Skip to content

Instantly share code, notes, and snippets.

View itskingori's full-sized avatar
🚢
Shipping

King'ori Maina itskingori

🚢
Shipping
View GitHub Profile
@itskingori
itskingori / mime_type_extenstions.txt
Created February 18, 2014 05:11
Extensions and the respective mime-types
# Text
text/cache-manifest manifest appcache;
text/css css;
text/html html htm shtml;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
text/x-vcard vcf;
@itskingori
itskingori / loop_test.rb
Created March 25, 2014 12:10
Outputs each number from 1 to 100, however … when the number is a multiple of 3 print WAKKA instead of that number. When the number is a multiple of 5, print WAAAA instead of that number. For numbers which are multiples of 3 AND 5, print WAKKAWAAAA.
#!/usr/bin/env ruby
(1..100).each { |n|
puts n unless( n%3 == 0 || n%5 == 0)
puts 'WAKKA' if (n%3 == 0 && n%5 != 0)
puts 'WAAAA' if (n%5 == 0 && n%3 != 0)
puts 'WAKKAWAAAA' if (n%3 == 0 && n%5 == 0)
}
# As used with CanCan and Devise
class ApplicationController < ActionController::Base
protect_from_forgery
include ErrorResponseActions
rescue_from CanCan::AccessDenied, :with => :authorization_error
rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found
before_filter :authenticate!
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"iam:ListInstanceProfiles"
],
"Sid": "Stmt1381157441000",
"Resource": [
"arn:aws:iam::123456789012:instance-profile/"
@itskingori
itskingori / doing_complicated_things.rb
Created October 1, 2014 06:34
If you have a long running or complicated task that you want to distribute across multiple processes, you can break it into small segments and use DelayedJob to distribute the workload. You can send a lot of email quickly using the methodology below (and a lot of DJ workers).
User.find_in_batches do |user_batch|
do_something_complicated(user_batch)
end
def do_something_complicated(users)
users.each do
user.something_complicated
end
end
@itskingori
itskingori / disable_db_tasks_on_production.rake
Created December 17, 2014 06:54
Disable dangerous rake tasks in production. It's a matter of adding a prerequisite to those dangerous tasks, that checks if they are being run in production, and exit accordingly. Also added is a flag to override this safeguard, together with some code to backup the DB.
# Could be in lib/tasks/disable_db_tasks_on_production.rake
# See original http://www.developingandstuff.com/2014/06/disable-dangerous-rake-tasks-in.html
DISABLED_TASKS = [
'db:drop',
'db:migrate:reset',
'db:schema:load',
'db:seed',
# ...
]
@itskingori
itskingori / possessive.rb
Last active August 29, 2015 14:22
Adds possessive-ness to String in a Rails app
# /lib/yourapp/possessive.rb
module Yourapp
module Possessive
# Returns a possessive form of a string
def possessive
return self if self.empty?
self + ('s' == self[-1, 1] ? "'" : "'" + 's')
end
end
@itskingori
itskingori / exercise01.php
Last active December 31, 2015 22:08
Showing text in a browser
<!DOCTYPE html>
<html>
<head>
<title>Exercise 01: Show text in a browser</title>
</head>
<body>
<?php
// Create a PHP page, with the standard HTML <head>, <title> and <body> tags.
// This is not strictly necessary but is good practice and should the first
@itskingori
itskingori / exercise04.php
Created December 20, 2013 06:53
Creating Arrays
<!DOCTYPE html>
<html>
<head>
<title>Exercise 04: Creating Arrays</title>
</head>
<body>
<?php
// Create array that holds all the provinces of Kenya and output them to the
// browser from top to bottom
@itskingori
itskingori / exercise05.php
Last active December 31, 2015 22:09
Manipulating Arrays
<!DOCTYPE html>
<html>
<head>
<title>Exercise 05: Manipulating Arrays</title>
</head>
<body>
<?php
// Create your array of 30 high temperatures then find the average high
// temp, the five warmest high temps and the five coolest high temps. Print