View halp.rb
module Halp
extend self
def format_host(str)
"#{(url = URI(str)).host}#{":#{url.port}" if url.port != 80}"
end
end
View states_by_fips.json
{
"01": {
"abbreviation": "AL",
"name": "Alabama"
},
"02": {
"abbreviation": "AK",
"name": "Alaska"
},
"03": {
View congressional-district-maps.md

Every ten years, after the decennial census and as mandated by the U.S. Constitution, states are subdivided into sets of congressional districts for representation in the U.S. House of Representatives. GovTrack provides maps of these congressional boundaries for your reference and makes the maps available for embedding on other sites. Beyond looking up the district for a specific address, maps introduce a geographic context that tells us about the demographics and rural/urban nature of a district, as well as whether that district's boundaries show signs of gerrymandering.

Recently, we redesigned GovTrack's map of congressional districts to be more responsive, more legible, and easier to reproduce. To create these maps, we used data from the U.S. Census Bureau, a few cartographic tricks and tools, and web map hosting from Mapbox. I'll explain the process here, and you can reference the exact workflow [on GitHub](https://github.com/aaron

View ec2hostname.rb
#!/usr/bin/ruby
# chkconfig: 35 99 01
# description: EC2 DNS registration
# processname: ec2hostname
require 'aws-sdk'
require 'net/http'
`touch /var/lock/subsys/ec2hostname`
View ec2hostname.rb
#!/usr/bin/ruby
# chkconfig: 35 99 01
# description: EC2 DNS loadbalancing
# processname: ec2hostname
require 'aws-sdk'
require 'net/http'
`touch /var/lock/subsys/ec2hostname`
View alias_task.rake
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end
View db_redo.rake
def interesting_tables
rval = ActiveRecord::Base.connection.tables.sort
rval.reject! do |tbl|
['schema_migrations','schema_info', 'sessions', 'public_exceptions'].include?(tbl)
end
rval
end
namespace :db do
namespace :backup do
View howto-manually-add-trust-cert-to-rubygems.md

SSL upgrades on rubygems.org and RubyInstaller versions

UPDATE 2014-12-21: RubyGems 1.8.30, 2.0.15 and 2.2.3 have been released. It requires manual installation, please see instructions below.


Hello,

If you reached this page, means you've hit this SSL error when trying to

View email_validator.rb
# blog post: http://blog.slashpoundbang.com/post/12694519460/email-validation-in-rails-3-or-active-model-without
# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
# Domain must be present and have two or more parts.
def validate_each(record, attribute, value)
address = Mail::Address.new value
record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
end
end
View benchmark_downcase_vs_enum_downcase.rb
# "[A]pplying downcase once is better than calling it (possibly) multiple times?"--sawa
# via http://stackoverflow.com/questions/38162768/38163148#comment63754959_38163167
# "How to make a string 1st letter caps, 2nd letter noncaps, 3rd letter caps, 4th letter noncaps…?"
require "benchmark"
n = 1_000_000
Benchmark.bmbm do |x|
x.report("String#downcase.chars.map.with_index {|c,i| i%2==0 ? c.upcase : c}.join") do
n.times {
"Stackoverflow is awesome!".downcase.chars.map.with_index {|c,i| i%2==0 ? c.upcase : c}.join