This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Include the internal API somewhere | |
header('content-type:text/plain'); | |
$user = 'andrew'; | |
$response = $app->api->setBusinessOptions($user, array( | |
'b2b' => true, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function format_number(number) { | |
return (new String(number)).split('').reverse().join('').replace(/([0-9]{3})/g, '$1,').replace(/,$/, '').split('').reverse().join(''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo defaults write com.apple.loginwindow LoginHook /Applications/MAMP/bin/start.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scientific | |
# The scientific method | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
OlderNewer