Skip to content

Instantly share code, notes, and snippets.

View cesarfigueroa's full-sized avatar

Cesar Figueroa cesarfigueroa

View GitHub Profile
@cesarfigueroa
cesarfigueroa / ordinals.rb
Last active September 7, 2017 03:29
Ordinal Indicators in Ruby
class Fixnum
def with_ordinals
if (11..13).include?(self.abs % 100)
'th'
else
case self.abs % 10
when 1 then 'st'
when 2 then 'nd'
when 3 then 'rd'
else 'th'
@cesarfigueroa
cesarfigueroa / encoder.sh
Last active October 5, 2015 14:27
Base64 Encoder with Data URIs
#!/usr/bin/env sh
read -p 'File? ' file
echo 'data:'$(file --mime-type -b $file)';base64,'$(base64 -i $file) | pbcopy
@cesarfigueroa
cesarfigueroa / gist:3610091
Created September 3, 2012 15:32
PGP Public Key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.12 (Darwin)
mQENBFBEE/gBCACy6e3EeDEmPG97fQpzdzbU8LBGxPWaq3UVymbrQeYudlP10kvm
EfR++wFT+9F5RR0pxZiGA5RxuDWQcEDfl9W/yZhg2on/phfLSXSDAt8mmWiWR6IG
caUneLrEGe5FPiVcnAirV/3/WdkL3QFiqVdz307MvB4qPi8FfK3Ziez0mzLs939R
wZduZfHVnU4+Ll1C7TF9LsyCS0o+DN5BpUF915QXTcaBk0IQhqr0Z5DAw7VOuzyP
ljLguaJDgFviqIvljNvQc/Ju0ReGQS7uDU7LEa5sZbn/G/t8lpGr81deIfNvSWuM
9YgJACQb3foNj/zCOwpln+bsRalHqp4SfuanABEBAAG0QENlc2FyIEZpZ3Vlcm9h
IChVc2VyIEludGVyZmFjZSBEZXNpZ25lcikgPGNlc2FyLmZpZ3Vlcm9hQG1lLmNv
@cesarfigueroa
cesarfigueroa / gist:4259020
Last active October 13, 2015 21:28
Prevent DataMapper timestamps from being manually set
property :created_at, DateTime, :writer => :private
property :updated_at, DateTime, :writer => :private
@cesarfigueroa
cesarfigueroa / resize.js
Last active October 14, 2015 01:57
Resize window to iPhone viewport
document.addEventListener('DOMContentLoaded', function () {
window.resizeBy(-window.innerWidth + 320, -window.innerHeight + 480);
});
@cesarfigueroa
cesarfigueroa / length.rb
Last active December 14, 2015 23:39
Grape length validator
class Length < Grape::Validations::SingleOptionValidator
def validate_param!(attr_name, params)
case @option
when Integer
unless params[attr_name].to_s.length == @option
throw :error, :status => 400,
:message => "#{attr_name} must be #{@option} characters long"
end
when Range
unless @option.include?(params[attr_name].to_s.length)
@cesarfigueroa
cesarfigueroa / titleize.rb
Last active December 16, 2015 06:39
Basic titleize function. Useful for copy-pasting from poorly formatted documents.
class String
def titleize
self.downcase.split.map(&:capitalize).join(' ')
end
end
module Subdomain
class NoSubdomain
def self.matches?(request)
request.subdomain.blank? && request.subdomain != 'www'
end
end
class API
def self.matches?(request)
request.subdomain == 'api'
@cesarfigueroa
cesarfigueroa / hash.rb
Last active December 17, 2015 22:19
Hash extensions for including and excluding keys.
class Hash
def only(*keys)
self.select { |key, _| keys.include?(key) }
end
def except(*keys)
self.reject { |key, _| keys.include?(key) }
end
end
@cesarfigueroa
cesarfigueroa / gist:5688172
Last active December 17, 2015 23:19
Ruby email regular expression
EMAIL_ADDRESS = /\A[A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,4}\z/