Skip to content

Instantly share code, notes, and snippets.

View richhollis's full-sized avatar

Richard Hollis richhollis

View GitHub Profile
@richhollis
richhollis / add_user_name_to_users.rb
Last active May 9, 2020 07:26
Rails 4.2.5.1 and Devise 3.5.6 using attr_encrypted with email attribute encrypted and username clear text
class AddUserNameForAuthenticationToUsers < ActiveRecord::Migration
def up
add_column :users, :username, :string, null: false, default: ""
add_index :users, :username, unique: true
add_column :users, :encrypted_email, :string
remove_column :users, :email, :string
add_index :users, :encrypted_email
end
def down
remove_column :users, :username
tables = [] # or ActiveRecord::Base.connection.tables
collation = "utf8_unicode_ci"
char_set = "utf8"
db = "your_database_name"
# write out
puts "USE #{db};"
puts "ALTER DATABASE #{db} CHARACTER SET #{char_set} COLLATE #{collation};"
tables.each do |t|
puts "ALTER TABLE #{t} CHARACTER SET #{char_set} COLLATE #{collation};" # changes for new records
puts "ALTER TABLE #{t} CONVERT TO CHARACTER SET #{char_set} COLLATE #{collation};" # migrates old records
@richhollis
richhollis / cors_middleware.rb
Last active August 16, 2017 20:33
CorsMiddleware which can be useful for testing SwaggerUI resources in development
class CorsMiddleware
def initialize(app)
@app = app
end
def call(env)
default_headers = {
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, PUT, PATCH, OPTIONS',
'Access-Control-Allow-Headers' => 'Content-Type, api_key, Authorization, origin'
@richhollis
richhollis / response.html
Created January 10, 2017 12:30
RubyMoney/google_currency captcha html response
<!DOCTYPE html>
<html>
<head>
<script>
(function(){(function(){function e(a){this.t={};this.tick=function(a,c,b){var d=void 0!=b?b:(new Date).getTime();this.t[a]=[d,c];if(void 0==b)try{window.console.timeStamp("CSI/"+a)}catch(h){}};this.tick("start",null,a)}var a,d;window.performance&&(d=(a=window.performance.timing)&&a.responseStart);var f=0<d?new e(d):new e;window.jstiming={Timer:e,load:f};if(a){var c=a.navigationStart;0<c&&d>=c&&(window.jstiming.srt=d-c)}if(a){var b=window.jstiming.load;0<c&&d>=c&&(b.tick("_wtsrt",void 0,c),b.tick("wtsrt_","_wtsrt",
d),b.tick("tbsd_","wtsrt_"))}try{a=null,window.chrome&&window.chrome.csi&&(a=Math.floor(window.chrome.csi().pageT),b&&0<c&&(b.tick("_tbnd",void 0,window.chrome.csi().startE),b.tick("tbnd_","_tbnd",c))),null==a&&window.gtbExternal&&(a=window.gtbExternal.pageT()),null==a&&window.external&&(a=window.external.pageT,b&&0<c&&(b.tick("_tbnd",void 0,window.external.startE),b.tick("tbnd_","_tbnd",c))),a&&(window.jstiming.pt=a)}catch(g){}})();}).call(this);
</script
@richhollis
richhollis / dropbox-monit
Last active May 11, 2016 14:33
Shell script for monit to launch dropbox daemon with ionice idle priority and cpu limit 20%
check process dropbox with pidfile /home/username/.dropbox/dropbox.pid
start program = "/home/username/.dropbox-dist/dropboxd-nice"
as uid username and gid username
stop program = "bin/bash -c '/bin/kill -9 `cat /home/username/.dropbox/dropbox.pid`'"
as uid username and gid username
@richhollis
richhollis / letsencrypt.md
Created February 1, 2016 12:41 — forked from xrstf/letsencrypt.md
Let's Encrypt on Ubuntu 14.04, nginx with webroot auth

Let's Encrypt on Ubuntu 14.04, nginx with webroot auth

This document details how I setup LE on my server. Firstly, install the client as described on http://letsencrypt.readthedocs.org/en/latest/using.html and make sure you can execute it. I put it in /root/letsencrypt.

As it is not possible to change the ports used for the standalone authenticator and I already have a nginx running on port 80/443, I opted to use the webroot method for each of my domains (note that LE does not issue wildcard certificates by design, so you probably want to get a cert for www.example.com and example.com).

Configuration

For this, I placed config files into etc/letsencrypt/configs, named after <domain>.conf. The files are simple:

@richhollis
richhollis / application.rb
Created January 8, 2016 18:00 — forked from averyvery/application.rb
Inline CSS or JS in Rails
config.assets.precompile += [
# precompile any CSS or JS file that doesn't start with _
/(^inline[^_\/]|\/[^_])[^\/]*.(js|css)$/,
...
@richhollis
richhollis / gist:7614183
Created November 23, 2013 12:39
Clojure Week 3 Homework - Poker hand
(defn deck []
(for [suit [:clubs :hearts :spades :diamonds]
pip (range 2 15)]
{:suit suit
:pip pip}))
(defn hand-frequencies [hand]
(frequencies (map :pip (hand))))
(defn n-of-a-kind?
@richhollis
richhollis / gist:7563637
Last active December 28, 2015 21:19
Clojure GitHub API Homework
(ns crash-course-github
(:require [clj-http.client :as http]
[cheshire.core :refer [parse-string]]
[clojure.pprint :refer [pprint]]))
(use 'crash-course-github)
(defn query-github
"Run an arbitrary query against Github API."
[query]
@richhollis
richhollis / gist:7548877
Last active December 28, 2015 19:18
Crash course in Clojure - Week 1 Homework
(require 'crash-course-clojure.spotify)
(use 'crash-course-clojure.spotify)
(def tracks
(love :tracks)
)
(def tracks-sorted
(sort (fn [x y] (> (count(x :name)) (count(y :name)))) tracks)
)