Skip to content

Instantly share code, notes, and snippets.

View hayesdavis's full-sized avatar

Hayes Davis hayesdavis

View GitHub Profile
class Retriable
class << self
def config(&block)
self.new(&block)
end
end
def initialize(&block)
max_attempts 5
require 'grafl'
# Get the root node (assumes you have an access token)
root = Grafl.root(:access_token=>'foo')
# Get the current user
user = root/:me
puts user.name
# Get the user's news feed
@hayesdavis
hayesdavis / blizzard.rb
Created October 19, 2010 17:09
Fun with snowflake
#First tweet on 21 Mar 2006 at 20:50:14.000 GMT (in ms)
TWEPOCH = 1288834974657
#High 42 bytes are timestamp, low 22 are worker, datacenter and sequence bits
SHIFT = 22
# Give it a snowflake id, it tells you what time it was created
# Will fail for very high ids because Ruby Time can only represent up to
# Jan 18, 2038 at 19:14:07 UTC (max signed int in seconds since unix epoch)
def what_time?(id)
##
# Below is a template for implementing the "OAuth Dance" with Twitter using the Ruby OAuth gem in a Rails app.
# Ruby OAuth gem is required by grackle and is found here: http://github.com/oauth/oauth-ruby
##
# Step 1: User clicks "Sign in with Twitter" button
# Step 2: User is routed to your controller action that looks like the method below
def start_oauth
client = Grackle::Client.new(:auth=>{
:type=>:oauth,
:consumer_key=>'SOMECONSUMERKEYFROMTWITTER', # Ids your app. "Consumer Key" on Twitter app page
:consumer_secret=>'SOMECONSUMERTOKENFROMTWITTER', # Your app's secret. "Consumer Secret" on Twitter app page
:token=>'ACCESSTOKENACQUIREDONUSERSBEHALF', # Ids a user. Most docs call this "access token".
:token_secret=>'SUPERSECRETACCESSTOKENSECRET' # Secret for a user. Often called "access token secret".
})
# Setup grackle to talk to flamingo
client = Grackle::Client.new
client.api_hosts[:flamingo] = "http://localhost:4711"
client.api = :flamingo
# Commence talking
# See what's being filtered
client.streams.filter?
client = Grackle::Client.new(:auth=>{:type=>:basic,:username=>SOME_USER,:password=>SOME_PASS},:ssl=>true)
# The ! means POST
client.direct_messages.new! :screen_name=>'hayesdavis', :text=>'testing'
Operation failed with the following exception: Connection reset by peer - send(2)
/usr/lib/ruby/gems/1.8/gems/mongo-0.18/lib/../lib/mongo/connection.rb:503:in `send_message_on_socket'
/usr/lib/ruby/gems/1.8/gems/mongo-0.18/lib/../lib/mongo/connection.rb:209:in `send_message'
/usr/lib/ruby/gems/1.8/gems/mongo-0.18/lib/../lib/mongo/collection.rb:257:in `update'
/usr/lib/ruby/gems/1.8/gems/mongo-0.18/lib/../lib/mongo/collection.rb:179:in `save'
client = Grackle::Client.new(:api=>:v1)
term = 'foo'
page = with_retry{client.search?(:q=>term,:rpp=>100)}
has_results = !page.results.empty?
while(has_results)
# page.results has tweets in it
unless page.next_page.nil?
page = with_retry{client.search?(:q=>term, :rpp=>100, :page=>page.page+1, :max_id=>page.max_id)}
has_results = !page.results.empty?
require 'rubygems'
require 'grackle'
require 'redis'
# Shows the list of mutual followers between two Twitter users using Redis
# sets and set intersections.
#
# Usage:
# ruby mutual_followers screen_name1 screen_name2 [reset]
#