Skip to content

Instantly share code, notes, and snippets.

@jamesdaniels
jamesdaniels / recursive_symbolize_keys.rb
Created December 17, 2009 07:50
I use this on about all of my projects, a really great way to cleanup hashes. Especially useful for hashes loaded from yaml.
class Hash
def symbolize_keys
inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
options
end
end
def recursive_symbolize_keys
inject({}){|result, (key, value)|
new_key = case key
class Maze
Cardinals = Proc.new{|(x,y)| [[x-1,y],[x,y+1],[x+1,y],[x,y-1]].select{|c| c.min >= 0}}
MazeSeperator, MazeStart, MazeWall, MazeEnd, MazeOOB = "\n", 'A', '#', 'B', nil
Infinity = 1.0/0
X, Y = 0, 1
def initialize(maze)
raise ArgumentError, 'No end point' unless maze.include? MazeEnd
raise ArgumentError, 'No start point' unless maze.include? MazeStart
# flushed out
has_a_braintree_customer do
attribute_map :firstname => :first_name, :lastname => :last_name
end
has_a_subscription do
plans {:mild => {:price => 0}, :medium => {:price => 500}, :spicy => {:price => 1000}}
default_plan :mild # default: plan with lowest price
billing_frequency 30.days, :grace_period => 5.days # default: 30.days, :grace_period => 5.days
def binary_search(list, match, index = 0)
if list.empty?
-1
else
mid = list.length/2
case list[mid] <=> match
when 0
index + mid
when 1
binary_search(list[0...mid], match, index)

GitHub OAuth Busy Developer's Guide

This is a quick guide to OAuth2 support in GitHub for developers. This is still experimental and could change at any moment. This Gist will serve as a living document until it becomes finalized at Develop.GitHub.com.

OAuth2 is a protocol that lets external apps request authorization to private details in your GitHub account without getting your password. All developers need to register their application before getting started.

Web Application Flow

  • Redirect to this link to request GitHub access:
class User < ActiveRecord::Base
has_many :simple_oauths, :dependent => :destroy
accepts_nested_attributes_for :simple_oauths
validates_presence_of :username, :unless => :using_sso?
def using_sso?
!simple_oauths.empty?
require 'uri'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
Given(/^a service implementation "([^\"]*)" exists with attributes "([^\"]*)" and first display variable named "([^\"]*)" and second display variable named "([^\"]*)"$/) do |class_name, attributes_string, first_display_var, second_display_var|
begin
klass = Object.const_get(class_name)
rescue NameError
klass = Object.const_set(class_name, Class.new(ServiceImplementation))
aux_variables = attributes_string.split(',').map { |x| x.strip.to_s }
/* This is a FluidApp userscript for Google Inbox for Mac.
Includes options for total badge count and unread badge count.
1) You need the paid version of FluidApp (http://fluidapp.com/)
2) Point Fluid to https://inbox.google.com (I recommend the icon from here: https://medium.com/@chrismessina/create-an-icon-for-google-inbox-in-your-dock-ed269312e3bc)
3) Set the user-agent to Chrome
4) Go to window -> userscript and add click the plus sign under the first table (on the left). Name the item 'Inbox'.
5) In the second table (on the right) click the plus and type in "*inbox.google.com*".
6) Paste the contents of this document into the script area below that.
7) Configure badge count (for total or unread only) in the code below.
require 'zip'
require 'zip/zipfilesystem'
require 'openssl'
require 'nokogiri'
class Ipa
attr_accessor :ipa_path, :package_name
def initialize(ipa_path)
@jamesdaniels
jamesdaniels / haha.rb
Created March 3, 2011 06:31
Oldest ruby file (of my own creation) I could locate on my hard-drive, dated 2/7/04 - man I was bad
#************************************** this will flip all the bits passed to it * * * * * * *
def flip(binary)
# replace 1 with 2, then 0 with 1, and then 2 with 0
return ((binary.tr("1","2")).tr("0","1")).tr("2","0")\
end
#************************************** adding implementation into the Integer class * * * * *
class Integer
def in_unsigned_binary(bits) #******* returns the unsigned binary form of the integer * * *