Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
stephanschubert / jquery.reduce.js
Last active December 21, 2015 12:29
The missing counterpart of jQuery.map()
/* jQuery core team does not want to add 'reduce' -
* Details: http://dev.jquery.com/ticket/1886
*/
(function($) {
$.reduce = function(array, callback, initialValue) {
if (Array.prototype.reduce) {
return Array.prototype.reduce.call(array, callback, initialValue);
}
@stephanschubert
stephanschubert / active_data.js.coffee
Last active January 29, 2016 09:17
Simple two-way data binding
do ($ = jQuery, exports = window) ->
class ActiveDataBinder
constructor: (uid) ->
# Use a jQuery object as simple PubSub
pubSub = $ {}
# We expect a 'data' attribute specifying the binding
@stephanschubert
stephanschubert / install_mysql2_gem_on_osx.sh
Last active May 25, 2017 08:09
How to install mysql2 gem on OSX
# Assumes you're using homebrew.
# Adjust the version to your convenience.
env ARCHFLAGS="-Os -g -fno-strict-aliasing -arch x86_64" gem install mysql2 -v '0.3.19' -- \
--with-mysql-include=`brew --prefix mysql`/include \
--with-mysql-config=`brew --prefix mysql`/bin/mysql_config
@stephanschubert
stephanschubert / profile.rb
Created September 24, 2013 08:34
Easy profiling w/ ruby-prof
def profile(prefix = "profile")
result = RubyProf.profile { yield }
dir = File.join(Rails.root, "tmp", "performance", params[:controller].parameterize
FileUtils.mkdir_p(dir)
file = File.join(dir, "callgrind.%s.%s.%s" % [prefix.parameterize, params[:action].parameterize, Time.now.to_s.parameterize] )
open(file, "w") { |f| RubyProf::CallTreePrinter.new(result).print(f, :min_percent => 1) }
end
@stephanschubert
stephanschubert / using.rb
Last active December 24, 2015 23:58
Find all classes which include a module.
def using(klass)
Module.constants.find_all do |name|
klass > Object.const_get(name) if name != name.upcase
end
end
@stephanschubert
stephanschubert / multi_fetch.rb
Created November 25, 2013 12:58
Hash#multi_fetch - Fetch multiple keys from a Hash and support default values when a key is missing.
# Usage:
#
# h = { a: 1, b: 2 }
#
# h.multi_fetch(:a) # => [1]
# h.multi_fetch(:a, :b) # => [1, 2]
# h.multi_fetch(:a, :c) # => [1, nil]
# h.multi_fetch(a: 1, c: 3) # => [1, 3]
class Hash
@stephanschubert
stephanschubert / jquery-plugin-template.js
Created November 25, 2013 13:16
Template for authoring a state-of-the-art jQuery plugin.
/*!
* jQuery lightweight plugin boilerplate
* Original author: @ajpiano
* Further changes, comments: @addyosmani
* Licensed under the MIT license
*/
// the semi-colon before the function invocation is a safety
// net against concatenated scripts and/or other plugins
// that are not closed properly.
@stephanschubert
stephanschubert / share_counts.md
Last active October 10, 2017 19:58 — forked from jonathanmoore/gist:2640302
How to retrieve the share counts for an URL from various social media platforms.

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@stephanschubert
stephanschubert / mysql_ramdisk_osx.sh
Created January 21, 2014 13:36
Move MySQL db into 1GB ramdisk on OSX
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://2048000`
/usr/local/Cellar/mysql/5.6.15/scripts/mysql_install_db \
--basedir=/usr/local/Cellar/mysql/5.6.15 \
--datadir=/Volumes/ramdisk
/usr/local/Cellar/mysql/5.6.15/bin/mysqld \
--basedir=/usr/local/Cellar/mysql/5.6.15 \
--datadir=/Volumes/ramdisk \
--plugin-dir=/usr/local/Cellar/mysql/5.6.15/lib/plugin \
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end