Skip to content

Instantly share code, notes, and snippets.

View ashrewdmint's full-sized avatar

Andrew Smith ashrewdmint

View GitHub Profile
@ashrewdmint
ashrewdmint / gist:132138
Created June 18, 2009 19:18
Internal API test
<?php
// Include the internal API somewhere
header('content-type:text/plain');
$user = 'andrew';
$response = $app->api->setBusinessOptions($user, array(
'b2b' => true,
function format_number(number) {
return (new String(number)).split('').reverse().join('').replace(/([0-9]{3})/g, '$1,').replace(/,$/, '').split('').reverse().join('');
}
class Import < ActiveRecord::Base
belongs_to :user
validates_presence_of :user
validate :data_must_be_valid_file, :data_must_be_csv
def data_must_be_valid_file
limit = 100
errors.add(:data, 'must be a file') unless data.class == Tempfile
errors.add(:data, 'must not be less than ' + limit.to_s + ' Kb') unless data.size < limit * 1024
end
@ashrewdmint
ashrewdmint / validators.rb
Created July 22, 2009 03:46
validates_as_file validator
# Helpful stuff from: http://chrisblunt.com/blog/2009/04/18/rails-writing-dry-custom-validators/
ActiveRecord::Base.class_eval do
def self.validates_as_file(*attr_names)
options = {:max_size => 1 * 1024**2, :extensions => '.txt'}.merge(attr_names.extract_options!)
options[:extensions] = [options[:extensions]] unless options[:extensions].is_a?(Array)
validates_each(attr_names, options) do |record, attr_name, value|
unless value.class == Tempfile
record.errors.add(attr_name, 'must be a file') unless value.class == Tempfile
# There are some faster ones out there, but I thought this was cute:
class Array
def duplicates
self.reject {|item| self.select {|sibling| sibling == item}.length == 1}.uniq
end
end
$ sudo defaults write com.apple.loginwindow LoginHook /Applications/MAMP/bin/start.sh
def scientific
# The scientific method
end
def cdn_url(count, path)
max = 98
range = 8
number = count / 8
number = (number > max ? max : number).to_s
number = number.length == 1 ? '0' + number : number
return "http://cdn-static-#{number}.viddler.com/css/#{path}"
end
1. Installing the nokogiri gem.
Without libxml2 installed, nokogiri gives you an error asking you to run 'yum install libxml2-devel'
My slice is on Ubuntu, so I have to run 'aptitude install libxml2-dev'. Then nokogiri asks me to install libxslt, so I run 'aptitude install libxslt'.
Nokogiri installs and all is well with the world, until...
2. Then I get this when trying to run my app: http://github.com/tenderlove/mechanize/issues#issue/9
@ashrewdmint
ashrewdmint / gist:277528
Created January 14, 2010 21:33
Smart character limit functions for PHP
// Intelligently truncate a string to a certain number of characters.
// Each uppercase or wider-than-normal character reduces the final length.
function truncate($string, $length, $ellipsis = true) {
// Count all the uppercase and other wider-than-normal characters
$wide = strlen(preg_replace('/[^A-Z0-9_@#%$&]/', '', $string));
// Reduce the length accordingly
$length = round($length - $wide * 0.2);