Skip to content

Instantly share code, notes, and snippets.

View mjc-gh's full-sized avatar
🎯
Open Sourcing

Michael Coyne mjc-gh

🎯
Open Sourcing
View GitHub Profile
@mjc-gh
mjc-gh / gist:1140424
Created August 11, 2011 18:50
rserve Utility Script
#!/usr/bin/env ruby
require 'sinatra'
# simple script to serve static files from an arbitrary directory via Sinatra
# usage: rserve [/path/to/dir] [port]
# path will default to '.'
pub = File.expand_path(ARGV.shift || '.')
puts "Setting Public to #{pub}"
@mjc-gh
mjc-gh / gist:1212114
Created September 12, 2011 19:21
VCR and MiniTest
describe "Something" do
before do
# __name__ defined in MiniTest::Unit::TestCase
VCR.insert_cassette(__name__)
end
after do
VCR.eject_cassette
end
end
@mjc-gh
mjc-gh / nfl.rb
Created September 18, 2011 17:27
NFL Mobile Schedule Parser
require 'httparty'
require 'nokogiri'
require 'date'
require 'json'
abort 'usage: ruby nfl.rb [WEEK NUM]' if ARGV.size < 1
resp = HTTParty.get("http://m.nfl.com/scores/reg/#{ARGV.shift}/")
parser = Nokogiri::HTML(resp.body)
@mjc-gh
mjc-gh / try.js
Created September 22, 2011 22:34
Javascript Object#try
// attempts to call a function; returns undef if function is not defined
Object.prototype.try = function(name, args){
return this[name] ? this[name].apply(this, args) : undefined;
};
// more robust version
Object.prototype.try = function(name){
return this[name] ? this[name].apply(this, Array.prototype.slice.call(arguments, 1)) : undefined;
};
@mjc-gh
mjc-gh / settings.rb
Created December 20, 2011 04:43
Settings Class
class Settings
attr_reader :config
def initialize(fname)
@file_name = fname
config = YAML.load_file(fname) rescue false
@config = config || {}
end
@mjc-gh
mjc-gh / page_visibility.js
Created February 9, 2012 18:23
Page Visibility API Wrapper with focus\blur failback
(function($){
var prop = 'VisibilityState';
var evt = 'visibilitychange';
var vendors = ['webkit', 'ms'];
var vendor;
function set_state(state){
$(window).trigger('visibilitychange', state);
@mjc-gh
mjc-gh / application_controller_test.rb
Created March 15, 2012 17:50
test application controller method
class ApplicationControllerTest < ActionController::TestCase
def setup
# create and initialize anonymous controller
@controller = Class.new(ApplicationController) do
def index
# what goes here all depends on the situation and the method your trying to test
end
end::new
end
@mjc-gh
mjc-gh / location_hash.js
Created June 14, 2012 03:24
Location Hash Wrapper
var LocationHash = (function(ev){
var hash, changed = [];
function read_hash(){
var list = location.hash.substr(1).split('&');
hash = {};
for (var i = 0, pair; pair = list[i]; i++){
var split = pair.split('=');
@mjc-gh
mjc-gh / test_helper.rb
Last active June 13, 2020 16:14
Minitest methods
require 'minitest/autorun'
class Minitest::Test
def self.test(name, &block)
define_method "test_#{name.gsub(/\s+/, '_')}", &block
end
def self.setup(&block)
define_method :setup, &block
end
@mjc-gh
mjc-gh / event_source_factory.js
Created October 5, 2013 16:29
Simple EventSource factory object for angular.js
!function(factories){
factories.factory('$eventSource', ['$window', function($window){
var EventSource = $window.EventSource;
function parse(obj){
try { var json = JSON.parse(obj); }
catch(e) { json = {}; }
finally { return json; }
}