Skip to content

Instantly share code, notes, and snippets.

View otobrglez's full-sized avatar
🏗️
Building.

Oto Brglez otobrglez

🏗️
Building.
View GitHub Profile
@otobrglez
otobrglez / economics.rb
Created December 23, 2010 11:23
Economics challenge for (RPCFN: Economics 101 (#13)) - Oto Brglez
require 'nokogiri'
class World
attr_reader :continents
def initialize(world_xml="cia-1996.xml")
@continents = []
if File.exists?(world_xml)
@noko = Nokogiri::XML(File.open world_xml )
@otobrglez
otobrglez / as3.rake
Created June 29, 2011 13:40
Rake task for uploading Rails 3.1 compiled assets to Amazon S3 storage
# 1) Put 's3' gem in Gemfile.
# 2) Create as3.yml configuration for S3
# 3) Create initializer for as3.yml
# 4) Make "assets" folder inside your bucket
# 5) After running task run "RAILS_ENV=production rake assets:precompile"
# 6) Invoke task by running "rake as3:upload"
namespace :as3 do
desc "Uploads compiled assets (public/assets) to Amazone AS3"
task :upload do
@otobrglez
otobrglez / Banner.rb
Created July 5, 2011 11:57
Random banners for specific position with random order
# Show banners for fixed position with some visibility and date range limits.
# Also return banners in random order.
# Use like so:
# Banner.for_position('a').each do { |b| b... }
# Tested with postgresql, sqllite and rails 3.1...
def self.for_position(position = 'a', for_date=DateTime.now.to_date)
where("position = ?", position)
@otobrglez
otobrglez / importer.rb
Created July 7, 2011 22:59
Simple import of articles from legacy DB.
class Importer
attr_accessor :db
def initialize(db="#{Rails.root}/db/base-5-7-2011.sqlite3")
self.db = SQLite3::Database.new(db)
end
def import_articles
articles = @db.execute("
SELECT
@otobrglez
otobrglez / Article.rb
Created July 12, 2011 20:51
Finding related articles using Jaccard index and tags
# This method finds related articles using Jaccard index (optimized for PostgreSQL).
# More info: http://en.wikipedia.org/wiki/Jaccard_index
class Article < ActiveRecord::Base
def related(limit=10)
Article.find_by_sql(%Q{
SELECT
a.*,
( SELECT array_agg(t.name) FROM taggings tg, tags t
@otobrglez
otobrglez / car.rb
Created August 23, 2011 12:26
Experimenting with observer pattern (publish/subscribe pattern) in Ruby
# Simple Car class. Nothing special here...
class Car
attr_accessor :brand
attr_accessor :model
attr_accessor :year
def initialize(brand, model, year=2011)
@brand, @model, @year = brand, model, year
end
@otobrglez
otobrglez / dropbox.rake
Created August 25, 2011 10:01
Rake task for moving Heroku PostgreSQL backups to Dropbox (Rails)
# By Oto Brglez - @otobrglez
# Rake task. Put in your (lib/tasks) folder of your Rails application
# Execute with "rake dropbox:backup"
# Configuration must be inside config/dropbox.yml file
namespace :dropbox do
desc "Backup production database to dropbox"
task :backup do
@otobrglez
otobrglez / article.rb
Created October 3, 2011 23:25
rails named scopes with lambda. a.k.a. problems with date/time in scopes!
Note that scopes defined with scope will be evaluated when they are defined, rather than when they are used. For example, the following would be INCORRECT!!!:
scope :published,
where(:published => 1)
.where(:hidden => 0)
.where("publish_date <= ?", Time.now)
The example above would be "frozen" to the Time.now value when the model class was defined, and so the resultant SQL query would always be the same. The correct way to do this would be via a lambda, which will re-evaluate the scope each time it is called:
scope :published, -> {
@otobrglez
otobrglez / times.rb
Created November 16, 2011 12:31
Just playing with blocks...
# Playing with blocks :)
class Integer
def times(&block)
(0...self).each do |i|
block.call i
end
end
end
@otobrglez
otobrglez / gpx_joiner.rb
Created November 26, 2011 19:03
Simple script that I use for mergin GPX tracks.
#!/usr/bin/env ruby
require "nokogiri"
require "pathname"
require "date"
gpx_root_folder = "/"+["Users", ENV["USER"], "mainnav-tracklogs"].join("/")
if ARGV[0].nil? or ARGV[1].nil?
puts "Missing dates!"