Setup:
$ mongo
> use pubsub
> db.createCollection('messages', { capped: true, size: 100000 })
> db.messages.insert({})
require 'rubygems' | |
require 'eventmachine' | |
require 'feedzirra' | |
require 'restclient' | |
feeds_url = [ "http://stackoverflow.com/feeds/tag/ruby-on-rails", | |
"http://stackoverflow.com/feeds/tag/ruby", | |
"http://stackoverflow.com/feeds/tag/git", | |
"http://stackoverflow.com/feeds/tag/mongodb", | |
"http://stackoverflow.com/feeds/tag/nginx", |
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 ) |
query = BSON::OrderedHash.new.tap do |query| | |
query["geoNear"]="locations" | |
query["near"] = lon_lat | |
end | |
lon,lat =lon_lat | |
query = Location.collection.db.command(query)['results'].sort_by do |r| | |
r['distance'] = Haversine.distance(lat,lon,r['obj']['lon_lat'][1],r['obj']['lon_lat'][0]) | |
end |
# Gemfile | |
… | |
gem 'uuidtools' | |
… |
scope :ordered_by_article_count, | |
select("users.*, c.count as article_count"). | |
joins("LEFT JOIN (SELECT user_id, COUNT(user_id) as count | |
FROM articles WHERE state = 'active' GROUP BY user_id) as c ON c.user_id = users.id"). | |
where("c.count IS NOT NULL"). | |
order("c.count DESC") |
# 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 |
# 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 | |
# from http://pushandpop.blogspot.com.au/2011/05/handling-gzip-responses-in-ruby-nethttp.html | |
# i wanted syntax highlighting | |
require 'net/http' | |
debug = Proc.new{|msg| STDERR.puts "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] #{msg}" } | |
page = nil | |
http = Net::HTTP.new( "www.google.com", 80 ) | |
req = Net::HTTP::Get.new( "/search?num=20&hl=en&noj=1&q=test&btnG=Search", { "Accept-Encoding" => "gzip", "User-Agent" => "gzip" } ) |