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 / iframe-loader.md
Last active April 11, 2023 06:37
How to add an iframe loader without JavaScript stuffs!
@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 / annotator-example-plugin.js
Last active December 18, 2020 17:21
Example AnnotatorJS Plugin showing events
Annotator.Plugin.Example = function (element, options) {
var myPlugin = {};
myPlugin.pluginInit = function () {
// This annotator instance
this.annotator
// LOADING
.subscribe("annotationsLoaded", function (annotations) {
console.log("annotationsLoaded called when the annotations have been loaded.");
console.log(annotations);
@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 / application_controller.rb
Last active March 4, 2024 09:07 — forked from speed-of-light/application_controller.rb
How to handle exceptions like 401, 501, 404 in Rails
# 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!
@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
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"iam:ListInstanceProfiles"
],
"Sid": "Stmt1381157441000",
"Resource": [
"arn:aws:iam::123456789012:instance-profile/"
@itskingori
itskingori / annotateit-ruby-jwt.md
Last active December 24, 2017 08:01
Example to show how to integrate a site with AnnotatorJS with AnnotateIt.org Store. Includes the Javascript part as well as some server side token generators : one in Ruby and the other in Python.

Replace the Auth options hash value having token: <TOKEN_FOR_TESTING_THAT_LASTS_FOR_A_DAY> with an actual token. This token can be generated by the server side scripts and copied in. It supercedes the tokenUrl: if set.

Just to be 100% sure, feel free to verify the validity of the JSON Web Token generated using jwt.io, JWT debugger. Oh, and the TTL is set to 86400 which is a day. Enough time to generate a token and play around with it.

Ps: If there are any amendments that I can make to improve clarity or fix issues, feel free to leave a comment below. I'll get a notification and act on it as soon as I can.

Ps 2: I was experiencing an issue with CORS and so I submitted the issue (and this code) to the annotator-dev mailing list. Check the July 2014 archives for an email with the subject ... '[annotator-dev] Integrating Site With AnnotateIt Store'.

@itskingori
itskingori / plot.R
Created June 17, 2014 23:15
Create divergent stacked bar graph using R from 5-point Likert data
# Set the working directory, not entirely necessary though
setwd("/PATH/TO/WORKING/DIRECTORY/something")
# import likert package
require(likert)
# Output to EPS
postscript("result-plot.eps", onefile=FALSE, horizontal=FALSE, height=2.5)
dataset <- read.table("plot-data.csv", header=TRUE, sep=",")
@itskingori
itskingori / contact_us_email.html.erb
Last active January 19, 2019 01:48
Getting started quickly with Amazon Email Sending Service (SES) and Ruby on Rails. Test in Rails 4
<p>Hello!</p>
<div>
<%= @message %>
</div>
<p>-- <%= @fullname %> (<%= @email %>)</p>