Skip to content

Instantly share code, notes, and snippets.

View earlonrails's full-sized avatar

Kevin Krauss earlonrails

  • Disney Streaming
  • San Francisco
View GitHub Profile
@earlonrails
earlonrails / check_me.js
Created June 17, 2011 21:35
check the inner html of an object
function check_me(obj){
alert(obj.innerHTML);
}
@earlonrails
earlonrails / rename.rb
Created June 26, 2011 21:40
Ruby version of rename function in linux for use with mac osx. Rename files ex. rename .txt .bak *.txt
#!/usr/bin/ruby
opts = []
args = []
ARGV.each do |a|
if a =~ /^(-[a-zA-Z]+)/
opts << a
else
args << a
end
@earlonrails
earlonrails / format_phone_nums.js
Created July 28, 2011 17:40
Format Phone numbers to US format with javascript
function strToAni(str) {
var regX = /(^[2-9]\d{0,2})\-?(\d{0,3})\-?(\d{0,4})$/;
var matches = regX.exec(str);
fstr = matches[1];
if (matches[2]){
fstr += "-" + matches[2];
}
if (matches[3]){
fstr += "-" + matches[3];
}
@earlonrails
earlonrails / img_controller.rb
Created July 29, 2011 02:51
send a jpeg file over https using ruby. Also including rails controller receiving method.
def receive_images_contr_method
image_path = "tmp/my_img#{params[:id_my_img]}.jpg"
content_length = request.content_length
my_read = request.body.read(content_length)
my_base64 = Base64.decode64(my_read)
image = open(image_path, "w")
image.write(my_base64)
image.close
end
@earlonrails
earlonrails / console_usage.rb
Created September 9, 2011 21:27
Dynamic Fields using mongoid. Use Mongoid to store a random size hash.
hsh = {}; (0..rand(5)).each { |idx| hsh["val#{idx}"] = [rand(5),rand(5),(idx % 2 == 0) ? hsh.clone: rand(5)] * (rand(2)+1) }; hsh
my_taco = TacoHash.new
my_taco.write_attributes(hsh)
my_taco.save
my_new_taco = TacoHash.last
#<TacoHash _id: 4e6a5a031a2c840d83000002, val0: [2, 0, {}, 2, 0, {}], val1: [2, 1, 2], val2: [1, 4, {"val0"=>[2, 0, {}, 2, 0, {}], "val1"=>[2, 1, 2]}], val3: [1, 1, 1, 1, 1, 1], val4: [4, 2, {"val0"=>[2, 0, {}, 2, 0, {}], "val1"=>[2, 1, 2], "val2"=>[1, 4, {"val0"=>[2, 0, {}, 2, 0, {}], "val1"=>[2, 1, 2]}], "val3"=>[1, 1, 1, 1, 1, 1]}, 4, 2, {"val0"=>[2, 0, {}, 2, 0, {}], "val1"=>[2, 1, 2], "val2"=>[1, 4, {"val0"=>[2, 0, {}, 2, 0, {}], "val1"=>[2, 1, 2]}], "val3"=>[1, 1, 1, 1, 1, 1]}]>
my_new_taco.val0
@earlonrails
earlonrails / check_http.rb
Created September 16, 2011 20:10
Check http or https connections with ruby using Net::HTTP or Net::HTTPS
require 'uri'
require 'net/http'
require 'net/https'
opts = ARGV.collect{|a| a if a =~ /^(-[a-zA-Z]+)/}.compact
url = ARGV - opts
use_https = lambda{|http| http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE}
requester = lambda{|http, uri| http.request(Net::HTTP::Get.new(uri.request_uri))}
if url.size > 1
puts "Multiple Urls not Supported"
@earlonrails
earlonrails / config_initializers_mongoid.rb
Created October 5, 2011 17:13
Use a replicaSet with Ruby Ree, Rails 2.3.4, Mongoid 1.9.2 and Mongo 1.0.9. Mongoid.database= Errors needs admin, so connect to admin then swap db after 2 seconds.
require 'mongoid'
# MongoDB ENV vars
File.open(File.join(RAILS_ROOT, 'config/mongodb.yml'), 'r') do |f|
@settings = YAML.load(f)[RAILS_ENV]
end
port = @settings["port"].nil? ? Mongo::Connection::DEFAULT_PORT : @settings["port"]
host_arr = @settings["host"]
connect = Mongo::Connection.multi([[host_arr[0], 27017], [host_arr[1], 27017], [host_arr[2], 27017]], :read_secondary => true)
db = connect.db(@settings["database"])
@earlonrails
earlonrails / mp3_visualizer.rb
Created October 7, 2011 23:52
Use icanhasaudio gem to decode mp3 and find the points of the sound wave to plot on a graph
#!/usr/local/bin/ruby
require 'stringio'
require 'rubygems'
require 'icanhasaudio'
reader = ::Audio::MPEG::Decoder.new
str_io = StringIO.new
input = File.open(ARGV[0], "rb")
plot_points = []
buf = reader.send(:skip_id3_header, input)
@earlonrails
earlonrails / fizzbuzz.rb
Created October 21, 2011 01:37
FIZZ BUZZ! This is to make sure that no one (myself mainly) has to figure out the fizz buzz problem anymore!
(1..100).each do |x|
case
when 0 == ((x%3)+(x%5))
puts "fizz buzz"
when 0 == (x%3)
puts "fizz"
when 0 == (x%5)
puts "buzz"
else
puts x
@earlonrails
earlonrails / swap_a_n_b.rb
Created October 21, 2011 21:01
swap a and b without global variables and only 2 local vars
def swap(a,b)
a = a + b
b = a - b
a = a - b
return {:a => a, :b => b}
end