Skip to content

Instantly share code, notes, and snippets.

@gmanley
gmanley / settings.json
Created March 24, 2014 18:21
settings
{
"detect_indentation": false,
"ensure_newline_at_eof_on_save": true,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true
}
require 'net/telnet'
headings = %w(id expires bytes cache_key)
rows = []
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
matches = localhost.cmd("String" => "stats items", "Match" => /^END/).scan(/STAT items:(\d+):number (\d+)/)
slabs = matches.inject([]) { |items, item| items << Hash[*['id','items'].zip(item).flatten]; items }
@gmanley
gmanley / uuid.rb
Last active December 26, 2015 18:09
UUID helper module.
require 'securerandom'
module UUID
UUID_PATTERN = /(\w{8})-?(\w{4})-?(\w{4})-?(\w{4})-?(\w{12})/
VALID_UUID_PATTERN = /\A#{UUID_PATTERN}\Z/
def self.included(base)
base.send(:extend, ClassMethods)
end
require 'benchmark'
begin
require 'active_support/values/time_zone'
rescue LoadError
puts 'Please do `gem install active_support`'
exit(1)
end
def benchmark_hash(hash, key, inverted_hash)
Benchmark.bmbm do |x|
#!/usr/bin/ruby
# -*- encoding: binary -*-
$stdout.sync = $stderr.sync = true
# this is used to show or watch the number of active and queued
# connections on any listener socket from the command line
require 'aggregate'
require 'csv'
require 'raindrops'
require 'optparse'
validates :full_url,
presence: true,
format: { with: URI.regexp(%w(http https)),
message: "The 'url' parameter is invalid!" }
deals = ['uuid1', 'uuid2', 'uuid3'].map do |uuid|
deal_info = double()
deal_info.stub_chain(:[], :uuid) { uuid }
deal_info
end
@gmanley
gmanley / turbo_dev.rb
Last active December 19, 2015 19:18
Middleware that causes static asset requests to bypass rails altogether. This can make requests a lot faster. From 4.5s to 1.5s in some apps. Only relevant in development environments as production already does this.
# Extracted from http://git.io/HeGfdA
# Full credit to https://github.com/SamSaffron
#
# Middleware that causes static asset requests to bypass rails altogether. This
# can make requests a lot faster. From 4.5s to 1.5s in some apps. Only relevant
# in development environments as production already does this.
#
# Put this file in lib/middleware and add the following code to your
# development.rb environment file:
#
def initialize(model = nil, mounted_as = nil)
if model
self.class.class_eval do
model.image_types.each do |type|
version type.name.to_sym do
process :resize_to_fill => [type.crop_x, type.crop_y]
# process :store_geometry
end
end
end
# app/validators/uuid_validator.rb
class UUIDValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
UUIDTools::UUID.parse_hexdigest(value)
rescue ArgumentError
UUIDTools::UUID.parse(value)
rescue ArgumentError
record.errors[attribute] << (options[:message] || "is not a valid UUID")
end