Skip to content

Instantly share code, notes, and snippets.

View h3h's full-sized avatar

Bradford Fults h3h

View GitHub Profile
@h3h
h3h / object_attrlist.rb
Last active August 29, 2015 13:57
Accessing multiple attributes/methods of an object with a concise syntax.
module Attrlist
def /(attrlist)
attrlist.map { |a| send(a) }
end
end
class Time
include Attrlist
end
@h3h
h3h / routes.rb
Last active August 29, 2015 14:00
Getting the first segments of currently defined static routes
Rails.application.routes.set.
map { |r| r.optimized_path[1..-1].split('/').first }.
uniq.
compact.
reject { |s| s.include?('*') || s.include?(':') }
@h3h
h3h / README.md
Last active August 29, 2015 14:04
Adding to/from GeoJSON Conversions to Sequel Queries

SQL for INSERT:

INSERT INTO geographies (type, name, geometry)
VALUES (
  'Location',
  'Vintage Heart Coffee',
  ST_GeomFromGeoJSON('{"type": "Point", "coordinates": [30.2642, -97.7277]}')
);
-- INSERT 0 1
@h3h
h3h / yep.rb
Created August 10, 2014 20:23
Random Plausible HTML Tags
def random_plausible_html_tags(num=5)
num.times.map { [*('a'..'z')].sample(rand(6) + 1).join }
end
@h3h
h3h / circular_enumerator.rb
Created August 15, 2014 20:59
CircularEnumerator
# Utility converter to make a circular enumerator, wrapping around to the
# beginning of a list after it has exhausted all entries.
#
# Example:
#
# >> e = CircularEnumerator([1,2,3,4,5])
# => #<Enumerator: [1, 2, 3, 4, 5]:each>
#
# >> 10.times.map { e.next }
# => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
@h3h
h3h / time_utilities.rb
Last active August 29, 2015 14:05
Parse ISO-8601 time strings and respect the embedded time zone.
require 'active_support/core_ext/time'
require 'active_support/values/time_zone'
module TimeUtilities
# Parses an ISO-8601 time string with a time zone and keeps the time zone in
# the Time object returned, unlike Ruby Core.
#
# Examples:
#
@h3h
h3h / background_validation.js.coffee
Created October 20, 2014 14:53
Automatic Per-field Background Validation for Forms
# Automatic Per-field Background Validation for Forms
#
# Usage:
#
# 1. Add a 'bg-validatible' class to your <form>
# 2. Add data-bg-validation-url-template="/validation/{field_id}" to your <form>
# 3. Add data-bg-validation-success-message="Looks good!" to your <form> or each validatible element
# 4. Add data-bg-validation-output="relative_selector_to_field" to the <form> or each validatable element
# 5. Add data-bg-validation-field="some_field_id" to each validatable field
# 6. (Optionally) Add a data-bg-validation-include="other_id,other2_id" attribute to the validatable field to send
#!/bin/bash -e
number_of_cores=`sysctl -n hw.ncpu`
brew update && \
brew upgrade ruby-build libxml2 libxslt libiconv
brew link --force libiconv && \
bundle config --global build.nokogiri "--with-xml2-include=/usr/local/include --with-xml2-lib=/usr/local/lib --with-xslt-dir=/usr/local/opt/libxslt --with-iconv-include=/usr/local/include --with-iconv-lib=/usr/local/lib" && \
bundle config --global jobs `expr $number_of_cores - 1` && \
rbenv install 2.1.5 && \
@h3h
h3h / explanation.md
Last active August 29, 2015 14:11
HR 4681 Section 309

TL;DR

Congress (House & Senate) just passed (amidst the release of the torture report and the Gruber hearing) an amendment to the annual intelligence agencies’ budget bill (HR 4681, Section 309) that makes it legal to:

  • Collect all electronic communications from everyone in the world
  • Store all of that data for at least 5 years
  • Store the data indefinitely if it’s encrypted or evidence of a crime (presumably until a supercomputer can break the encryption, assuming that hadn’t happened within 5 years)
  • Disseminate the data (presumably at the inteliigence agencies’ own discretion, e.g. to your local police or the DEA)
class Time
def within?(time_period)
self >= Time.now && self <= Time.now + time_period
end
end
# >> (Time.now + 55.hours).within? 2.days
# false
# >> (Time.now + 45.hours).within? 2.days
# true