Skip to content

Instantly share code, notes, and snippets.

View GBH's full-sized avatar
🦀
🦀🦀🦀🦀🦀🦀🦀🦀🦀

Oleg GBH

🦀
🦀🦀🦀🦀🦀🦀🦀🦀🦀
View GitHub Profile
@GBH
GBH / gist:9218575
Created February 25, 2014 21:47
Hash#deep_slice
class Hash
def deep_slice(*allowed_keys)
sliced = { }
allowed_keys.each do |allowed_key|
if allowed_key.is_a?(Hash)
key = allowed_key.keys.first
sliced[key] = self[key].deep_slice(*[allowed_key.values].flatten) if self[key]
else
sliced[allowed_key] = self[allowed_key] if self[allowed_key]
@GBH
GBH / gist:a0c6e7ec555881242f61
Created November 17, 2014 14:20
Ruby ancestor chain fun
module Foo
def hello
puts 'foo'
end
end
module Bar
def hello
puts 'bar'
end
vagrant@jun-group:~/player$ rails s torquebox
=> Booting TorqueBox
=> Rails 3.2.18 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
NameError: missing class or uppercase package name (`com.jungroup.player.geocoding.ZipcodeTranslator')
get_proxy_or_package_under_package at org/jruby/javasupport/JavaUtilities.java:54
method_missing at file:/home/vagrant/.rvm/rubies/jruby-1.7.8/lib/jruby.jar!/jruby/java/java_package_module_template.rb:14
(root) at /home/vagrant/player/lib/geo_code.rb:7
@GBH
GBH / gist:d601b212e1e473c62e90
Created April 1, 2015 18:35
Testing for translation consistency
require_relative '../test_helper'
class I18nIntegrationTest < ActionDispatch::IntegrationTest
def collect_combined_keys(hash, ns = nil)
hash.collect do |k, v|
keys = [ ]
keys << collect_combined_keys(v, "#{ns}.#{k}") if v.is_a?(Hash)
keys << "#{ns}.#{k}"
end.flatten
@GBH
GBH / each_with_alpha_index.rb
Last active August 29, 2015 14:20
each_with_alpha_index
module Enumerable
# Iterate through enumerable with alpha index instead of integers
def each_with_alpha_index(index = 'a')
each do |element|
yield element, index
index.next!
end
end
end
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pp'
doc = Nokogiri::HTML(open('http://www.ibao.org/html/find_a_broker/mapfind.asp?tcode=13'))
out = []
@GBH
GBH / gist:2570212
Created May 1, 2012 18:17
Freshbooks OAUTH Song and Dance
require 'oauth'
require 'oauth/signature/plaintext'
c = OAuth::Consumer.new(
'account_name', 'oath_secret', {
:http_method => :post,
:scheme => :query_string,
:signature_method => 'PLAINTEXT',
:oauth_callback => 'oob',
:site => 'https://testingtesting0123.freshbooks.com',
@GBH
GBH / gist:3451944
Last active October 9, 2015 06:17
Textmate snippet `controller`. Scope `source.ruby`
class ${1:Model}sController < ApplicationController
before_action :build_${1/./\l$0/}, only: [:new, :create]
before_action :load_${1/./\l$0/}, only: [:show, :edit, :update, :destroy]
def index
@${1/./\l$0/}s = ${1:Model}.page(params[:page])
end
def show
@GBH
GBH / gist:3452023
Last active October 9, 2015 06:17
Textmate snippet for `controllertest`. Scope `source.ruby`
require_relative '../test_helper'
class ${1:Model}sControllerTest < ActionController::TestCase
def setup
@${1/./\l$0/} = ${1/./\l$0/}s(:default)
end
def test_get_index
get :index
resources :objects do
put :publish, :on => :member
end
before_filter :load_object
def update
@object.update_attributes!(params[:object])