Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Wheeler wheeyls

View GitHub Profile
View auth.rb
require 'googleauth'
require 'googleauth/stores/file_token_store'
module SEO
class Auth
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
USER_ID_KEY = 'GOOGLE_SEARCH_CONSOLE_USER_ID'
attr_reader :token_file, :secret, :id
@wheeyls
wheeyls / versioned_filename.rb
Last active Apr 14, 2017
Setting up Cache-Control for CarrierWave uploads - making sure that file names are updated to keep the cache fresh.
View versioned_filename.rb
# This is for versioning files that we upload to S3.
#
# We want a unique time stamp in the name, so that we can cache files on browsers without
# worrying about cache-busting.
#
# The tricky part is deciding when to increment the version, this is accomplished
# by binding to the +before_cache+ hook.
#
# All you need to do to use this, is to call "has_versioned_filename" from an uploader,
# and add a string column "+mount_name+_version_token" to the model.
@wheeyls
wheeyls / intersection.rb
Last active Dec 23, 2015
Using ruby to grab an intersection of items in a list of categories.
View intersection.rb
class ManyToMany
def intersection(relation, cat_ids)
join = cat_ids.map do |cat_id|
outer_join_clause(cat_id)
end.join(' ')
where = cat_ids.map do |cat_id|
" cat#{cat_id}.id is not null "
end.join(' and ')
@wheeyls
wheeyls / basic-log.js
Last active Dec 19, 2015
Queue Example for my blog
View basic-log.js
function logEvent(eventData) {
$.ajax({
method: 'post',
url: '/analytics-endpoint',
data: eventData
});
}
@wheeyls
wheeyls / application_controller.rb
Last active Dec 13, 2017
A cached key-value store backend for Rails I18n. Designed to speed up translations when using a database like Redis or Mongo.
View application_controller.rb
class ApplicationController < ActionController::Base
before_filter :ensure_fresh_i18n
private
def ensure_fresh_i18n
I18n.backend.ensure_freshness! I18n.locale
end
end
View visibility_and_inheritance.rb
# inheritence question:
# When mixing in a module, how does super work?
# Does it behave the same for protected methods?
# And private methods?
class Parent
def log(name)
puts name
puts self.class
end
View redis_subscribe.rb
require 'stringio'
class RedisSubscribe < EM::Connection
def self.connect(host, port, pass = '')
Rails.logger.debug host
Rails.logger.debug port
client = EM.connect host, port, self
client.auth pass if pass.present?
@wheeyls
wheeyls / example.js
Last active Dec 14, 2015
Export a library for commonjs, nodejs, and browser.
View example.js
function myLib() {
return {
code: function () {}
, goes: function () {}
, here: function () {}
}
}
exporter('myLib', myLib);
View classy.js
(function () {
var initializing = false
, superPattern = /xyz/.test(function () { xyz; }) ? /\b_super\b/ : /.*/;
Object.subClass = function (properties) {
var _super = this.prototype;
initializing = true;
var proto = new this();
initializing = false;
View classy.js
(function () {
var initializing = false
, superPattern = /xyz/.test(function () { xyz; }) ? /\b_super\b/ : /.*/;
Object.subClass = function (properties) {
var _super = this.prototype;
initializing = true;
var proto = new this();
initializing = false;