Skip to content

Instantly share code, notes, and snippets.

View stereosupersonic's full-sized avatar

MICE Portal stereosupersonic

View GitHub Profile
@stereosupersonic
stereosupersonic / update_all_bundles.rb
Created March 10, 2011 09:14
update all my textmate bundles from github
#!/usr/bin/env ruby
Dir.chdir(File.expand_path("~/Library/Application\ Support/TextMate/Bundles/")) do
Dir.foreach(".") do |dir|
if File.directory?(dir)
Dir.chdir(dir) do
puts "Update Bundle #{File.basename Dir.getwd}"
puts %x{git pull}
end
end
@stereosupersonic
stereosupersonic / analytics.rb
Created October 31, 2011 13:56
analytics_stub
# analytics/app/support/report_invoker.rb
class ReportInvoker
def self.build_reports(dir)
new(dir).build_reports
end
attr_reader :dir
def initialize(dir)
@stereosupersonic
stereosupersonic / example_controller.rb
Created September 29, 2015 06:59
growing rails application contriller
class NotesController < ApplicationController
def index
load_notes
end
def show
load_note
end
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
def yellow(text); colorize(text, 33); end
@stereosupersonic
stereosupersonic / import_export_json.rb
Created March 21, 2012 08:26
Import export data with json
require 'json'
#export
users = User.all.map {|user| {:id => user.id, :name => user.name }}
File.open("/tmp/users.json","w") do |f|
f.write(JSON.pretty_generate(users))
end
#import
@stereosupersonic
stereosupersonic / internet_connection.rb
Created May 3, 2012 08:32
check internet connenction
#from here: http://stackoverflow.com/questions/2385186/check-if-internet-connection-exists-with-ruby
require 'open-uri'
def internet_connection?
begin
true if open("http://www.google.com/")
rescue
false
end
require 'rubygems'
require 'ohai'
ohai = Ohai::System.new
puts ohai.data.to_hash
require 'listen' #https://github.com/guard/listen
dir = "/Users/michaeldeimel/entwicklung/freeletics/core-user-rails"
listener = Listen.to(dir) do |modified, added, removed|
puts "modified absolute path: #{modified}"
puts "added absolute path: #{added}"
puts "removed absolute path: #{removed}"
end
listener.start # not blocking
sleep
message_verifier = Rails.application.message_verifier("some salt")
token = message_verifier.generate("my versecure thing")
message_verifier.verify(token) # => "my versecure thing"
# more params
token = message_verifier.generate([@user.id, email, "some message"])
user_id, email, message = message_verifier.verify(token) # => "my versecure thing"
@stereosupersonic
stereosupersonic / export.rb
Created July 15, 2013 08:40
Export to csv
File.open("out.csv", 'w') { |f| f << Ranking.where(:client_instance_id => 14).map {|r| "#{r.id};\"#{r.supplier.name}\";\"#{r.comment}\" "}.join("\n") }