Skip to content

Instantly share code, notes, and snippets.

@jamesdaniels
jamesdaniels / get-exif-data.rb
Created February 2, 2009 21:17
Gets the EXIF meta data for a file
`exiftool /Users/james/Desktop/james.psd`.split("\n").collect{|a|
[
(array = a.split(':')).first.strip,
(array.shift && array).join(':').strip
]
}
@jamesdaniels
jamesdaniels / verified_facebook_authenticity.rb
Created February 3, 2009 01:34
Verify communications from facebook
def verifed_facebook_autheticity(signature, params)
Digest::MD5.hexdigest((params.keys.sort.collect {|key| "#{key}=#{params[key]}"} << APP_CONFIG[:facebook_api_secret]).to_s).strip == signature
end
@jamesdaniels
jamesdaniels / XCode on git
Created February 24, 2009 00:51
. gitattributes for xcode projects
# .gitattributes
*.pbxproj -crlf -diff -merge
# .gitignore
# xcode noise
build/*
*.pbxuser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
</head>
<body>
@jamesdaniels
jamesdaniels / facebook_session.rb
Created June 11, 2009 17:21
facebook authentication
def facebook_postback
if params[:session] && params[:session] != 'loggedout' && eval("params[:session] = #{params[:session].gsub(/\\"/, '"').gsub(/:/, ' => ')}")
access_denied unless verifed_facebook_autheticity(params[:session][:sig], {'expires' => params[:session][:expires], 'session_key' => params[:session][:session_key], 'ss' => params[:session][:secret], 'user' => params[:session][:uid]})
session = Facebooker::Session.create(APP_CONFIG[:facebook_api_key], APP_CONFIG[:facebook_api_secret])
facebook_user = Facebooker::User.new(params[:session][:uid], session)
begin
if logged_in?
link_facebook_account(facebook_user)
else
create_facebook_user(facebook_user)
class Circuit
def initialize(resistors)
@resistors = resistors
@final_node = 0
@resistors.each do |(source, destination, resistance)|
@final_node = [@final_node, source, destination].max
end
class Polynomial
def initialize(coefficients)
@coefficients = coefficients.reverse # reverse so the index equates to the exponent of each coefficient
raise ArgumentError, 'Need at least 2 coefficients' if coefficients.size < 2
end
def to_s
# Build an array of sprintfs, LIFO to get most significant order
@coefficients.inject([]) {|builder, coefficient| ["%+ix^%i" % [coefficient, builder.size]] + builder}.to_s.
gsub(/(\+0x\^\d+|x\^0|\^1\d{0})/, ''). # Clean out 0x^y, x^0, ^1
gsub(/\d{0}1x/, 'x'). # Replace 1x with just x
@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