Skip to content

Instantly share code, notes, and snippets.

@robertomiranda
robertomiranda / tweet.rb
Created July 29, 2011 16:13 — forked from jfgomez86/tweet.rb
Finding latest tweets by unique users
##
# Find latest *n* tweets, but don't repeat tweets by users.
# Example:
#
# If we have the following table:
#
# id | user_id | created_at
# 1 | 1 | 3 days ago
# 2 | 2 | 3 days ago
# 3 | 1 | 2 days ago
@robertomiranda
robertomiranda / gist:1168322
Created August 24, 2011 15:34
Find tournament's players by categories
class Tournament < ActiveRecord::Base
#SELECT "players".*
#FROM "players"
#WHERE "players"."id" IN (
#SELECT "players"."id"
#FROM "players"
#INNER JOIN "pairs"
#ON ("pairs"."player_id" = "players"."id" OR "pairs"."partner_id" = "players"."id") AND "pairs"."tournament_id" = 4)
def player
players = Player.arel_table
@robertomiranda
robertomiranda / README.rdoc
Created August 26, 2011 16:33 — forked from woodie/README.rdoc
Rails 3 on App Engine

Rails 3.0.pre on App Engine

You can Rails 3 on App Engine, but it won’t be especially useful until bundler 10. You should try these instead:

Install the Development Environment

The gems for the development environment include a pre-release appengine-tools gem that provides a pre-release version of jruby-rack.

@robertomiranda
robertomiranda / gist:1326448
Created October 30, 2011 21:10
Finding recently next matches's players by category
#"SELECT "matches".* FROM "matches" WHERE "matches"."id"
#IN (
# SELECT MIN("matches"."id") AS min_id FROM "pairs" INNER JOIN "matches"
# ON ("pairs"."player_id" = 14 OR "pairs"."partner_id" = 14) AND ("pairs"."id" = #"matches"."first_pair_id" OR "pairs"."id" = "matches"."second_pair_id") AND "matches"."winner_pair" IS NULL AND #"matches"."wo_pair" IS NULL AND ("matches"."start_date" IS NULL OR "matches"."start_date" >= '2011-10-30 #21:09:48.398210') GROUP BY "matches"."category_id")"
class Player < ActiveRecord::Base
def planning_matches
matches = Match.arel_table
pairs = Pair.arel_table
condition = (pairs[:player_id].eq(self.id).or(pairs[:partner_id].eq(self.id)))
.and(pairs[:id].eq(matches[:first_pair_id]).or(pairs[:id].eq(matches[:second_pair_id]))
@robertomiranda
robertomiranda / heroku_autoscaler.rb
Created November 2, 2011 15:15 — forked from jfgomez86/heroku_autoscaler.rb
Quick Dirty Heroku AutoScaling script that may save you money. Works with Delayed Job.
# Inspired by http://verboselogging.com/2010/07/30/auto-scale-your-resque-workers-on-heroku
module HerokuAutoscale
module Scaler
class << self
@@heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASS'])
def workers
@@heroku.info(ENV['HEROKU_APP'])[:workers].to_i
end
@robertomiranda
robertomiranda / gist:1353365
Created November 9, 2011 22:36 — forked from gerson/gist:1353362
Tracking facebook friends with me app permission
def suggested_rivals_facebook
friend_user_ids = []
if authentications.size > 0 && authentications.map(&:provider).include?("facebook")
auth = authentications.where(:provider => 'facebook').map.first
friends_application = FbGraph::Query.new("SELECT uid FROM user WHERE is_app_user=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = #{auth.uid})").fetch("#{auth.access_token}")
friends_application.collect! {|par| par["uid"].to_s}
friend_user_ids = User.where("authentications.provider" => "facebook").any_in("authentications.uid" => friends_application).not_in("_id" => rivals.map(&:competitor)).map
end
friend_user_ids
end
@robertomiranda
robertomiranda / gist:1423831
Created December 2, 2011 16:24
Finding activities Member's followers
#"SELECT "activities".* FROM "activities" WHERE "activities"."member_id" IN (SELECT "members"."id"
#FROM "members"
#INNER JOIN "member_followers" ON "member_followers"."following_member_id" = "members"."id"
#WHERE "member_followers"."follower_id" = 1491)"
def time_line
members = Member.arel_table
member_followers = MemberFollower.arel_table
condition=(member_followers[:follower_id].eq(self.id))
Activity.where(:member_id =>Member.select(members[:id]).joins(:member_followers).on(member_followers[:following_member_id].eq(members[:id])).where(condition))
@robertomiranda
robertomiranda / gist:1483121
Created December 15, 2011 22:05
Facebook Post
require 'net/http'
require 'json'
require "ostruct"
module Facebook
class Post< OpenStruct
@@url = "http://graph.facebook.com"
class << self
def find(id)
@robertomiranda
robertomiranda / factories.rb
Created January 6, 2012 20:32 — forked from anonymous/factories.rb
Simulate Paperclip Attachments With FactoryGirl
Factory.define :item do |f|
include ActionDispatch::TestProcess
f.name "Macbook Pro 15"
f.price_in_dollars 1500
f.photo Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'rails.png'), 'image/png')
end
@robertomiranda
robertomiranda / gist:1725551
Created February 2, 2012 20:24
Scrapping with nokogiri
require 'net/http'
require "nokogiri"
require 'open-uri'
module Scrapper
def get_images_from(url)
images = []
doc = Nokogiri::HTML(open(url))
doc.xpath("/html/body//img[@src[ not(contains(.,'\.gif'))
and contains(.,'://')