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 / 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 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!"
@otobrglez
otobrglez / Makefile
Created October 5, 2015 20:47
Playing with static and const in C
run: test
./test
test:
clang -ansi test.c -o test
clean:
rm -rf test
@otobrglez
otobrglez / asset_file.css.erb
Created June 19, 2012 11:51
Serving fonts from Amazon S3 with Rails "Proxy" to avoid XSS problems. (Dirty way!)
@font-face {
font-family: 'HPBold';
src: url("<%= ('http://myproductionhostorsomething.com/font/HPSimplifiedW04-Bold.eot') %>");
font-weight: normal;
font-style: normal;
}
@otobrglez
otobrglez / peerindex.rb
Created June 20, 2012 10:00
Ruby wrapper for PeerIndex V2 REST API
# By Oto Brglez - <oto.brglez@opalab.com>
require 'httparty'
class PeerIndex
include HTTParty
format :json
base_uri "http://api.peerindex.net/v2"
def get(handler)
guard 'livereload', :apply_css_live => false, :apply_js_live => false do
watch(%r{app/.+\.(erb|haml|js|css|scss)})
watch(%r{app/helpers/.+\.rb})
watch(%r{(public/|app/assets).+\.(css|js|erb|html)})
watch(%r{(app/assets/.+\.css)\.s[ac]ss}) { |m| m[1] }
watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
watch(%r{(app/assets/.+\.js)\.erb}) { |m| m[1] }
watch(%r{config/locales/.+\.yml})
end