Skip to content

Instantly share code, notes, and snippets.

@camertron
camertron / torquebox_error
Created September 22, 2014 19:15
Torquebox Error
12:12:11,975 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.967-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=JITCompiler
12:12:11,975 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.975-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=Config
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=ParserStats
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=ClassCache
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=Runtime
12:12:13,237 ERROR [org.torquebox.core.runtime] (ServerService Thread Pool -- 47) Error during execution
@camertron
camertron / endpoint.rb
Last active August 29, 2015 14:08
Grape Middleware for Error Reporting
module Grape
class Endpoint
def build_middleware_with_error_tracking
builder = build_middleware_without_error_tracking
builder.use(::ErrorTrackingMiddleware)
# HAAAAACK!
builder.instance_variable_get(:'@use').tap do |use_arr|
middleware_proc = use_arr.delete_at(use_arr.size - 1)
use_arr.insert(0, middleware_proc)
@camertron
camertron / number_transliterator.rb
Created October 30, 2014 19:36
Number Transliteration
require 'twitter_cldr'
class NumberTransliterator
class << self
def to_english_str(str)
str.each_char.map do |char|
transliterate_char(char) || char
end.join
end
@camertron
camertron / ruby_learning_11_21_2014.md
Last active August 29, 2015 14:10
Lumos Labs Learning Team Bi-Weekly Ruby Snippet (11/21/2014)

One of the language features that really impressed me when I first started using Ruby was all the magic of Enumerable. No other language I know of lets you iterate so effectively over collections of objects. Let's say you wanted to sum together an array of integers in Java, for example:

int[] numbers = new int[5] { 5, 3, 8, 1, 2 };
int sum = 0;

for (int i = 0; i < numbers.length; i ++) {
  sum += numbers[i];
}
@camertron
camertron / convert_params.rb
Last active August 29, 2015 14:17
Converts params that appear in a Rails log to HTTP GET/POST params
require 'json'
require 'cgi'
def convert(data)
params = JSON.parse(data.gsub('=>', ':'))
[].tap do |out|
walk_hash(params, out, [])
end.join('&')
end
@camertron
camertron / file_scope.rb
Last active August 29, 2015 14:19
Provides a way to perform Ruby `require` statements at the file (really the block) level
require 'find'
def provide(*requires, &block)
modules = requires.map do |req|
Module.new do
file_fragment = "#{req}.rb"
file = Find.find(*($: + ['.'])) do |f|
break f if f =~ /#{file_fragment}\z/
end
@camertron
camertron / ruby_learning_5_13_2015.md
Last active August 29, 2015 14:21
Learning team bi-weekly code snippet for 5/13/2015

Three Useful Ruby Methods you Didn't Know Existed, and What to do With Them Now You've Wised Up

Yep, it's time for some cool Ruby trickery.

String#force_encoding

At their core, Ruby strings are just sequences of bytes. How you or your computer chooses interpret and display those bytes can mean the difference between legible text and complete garbage. Consider UTF-8 text for example. We now have great Unicode support in Ruby (as of 1.9) via the String#encode method and its friends, but again, strings are really just bytes.

Telling Ruby your string is encoded in UTF-8 tells it how to print your string on the screen or in your web app. For example, let's say you have a string that contains Japanese text (says 'arigato') "ありがと". Furthermore, let's pretend Ruby thinks the string is encoded in ASCII, which doesn't support Japanese characters. If you tried to print your string in the terminal, you might see something like this: "\xE3\x81\x82\xE3\x82\x8A\xE3\x81\x8C\xE3\x81\xA8". What you're seeing

@camertron
camertron / ruby-cldr-export.rb
Created May 24, 2012 23:48
Exporting base CLDR data using Sven Fuch's ruby-cldr gem
require 'rubygems'
require 'ruby-cldr'
require 'cldr'
require 'cldr/data'
require 'cldr/download'
require 'yaml'
# NOTE: nb = no (norwegian)
locales = ["he", "en", "fr", "it", "de", "es", "ja", "ko", "ru", "tr", "pt", "fil", "hi", "ms", "id", "nl", "da", "sv", "pl", "hu", "fi", "nb", "zh", "zh-Hant", "ar", "fa", "ur", "th", "uk", "ca", "el", "af", "cs", "eu"]
components = ["calendars", "currencies", "delimiters", "languages", "numbers", "Plurals", "territories", "timezones", "units"]
@camertron
camertron / custom_units_yml.rb
Created July 28, 2012 21:23
Creating custom units.yml from Twitter Translation Center
require 'rubygems'
require 'json'
require 'ya2yaml'
require 'twitter_cldr'
require 'fileutils'
ENDPOINT = "http://translate.twttr.com/api/2/twitter/phrase/{{phrase_id}}/translations.json"
merged = { :day => 19636, :hour => 19638, :second => 19639, :minute => 19634 }.inject({}) do |final_ret, (label, phrase_id)|
cur_data = JSON.parse(`curl #{ENDPOINT.gsub("{{phrase_id}}", phrase_id.to_s)} --silent`).inject({}) do |ret, (locale, trans)|
@camertron
camertron / composition_exclusions.rb
Created July 28, 2012 22:29
Dumping composition exclusions into yml
require 'open-uri'
require 'fileutils'
require 'yaml'
PROPS_URL = "http://www.unicode.org/Public/6.1.0/ucd/DerivedNormalizationProps.txt"
OUTPUT_FILE = "/tmp/cldr/composition_exclusions.yml"
EXPECTED_TOTAL_POINTS = 1120
data = open(PROPS_URL).read
start_pos = data.index("# Derived Property: Full_Composition_Exclusion")