Share
Embed
Transcript
48 slides
-
Event-driven Programming •Be reactive instead of proactive •Don’t manage sockets
and connections Monday, February 21, 2011 -
Event-driven Programming •Be reactive instead of proactive •Don’t manage sockets
and connections •Framework for network services Monday, February 21, 2011 -
Synchronous s = TCPSocket.open('localhost', '80') while line = s.gets puts
line.chop end s.close Monday, February 21, 2011 -
Event-Driven cool.io.connect 'localhost', '80' do on_read do |data| puts data
end end cool.io.run Monday, February 21, 2011 -
Event-Driven Error handling done for you Easy to de-couple Abstracted
with a nice DSL Monday, February 21, 2011 -
Event-Driven Most web frameworks are event-driven get '/' do 'Hello
World' end Monday, February 21, 2011 -
Cool.io Formerly rev Uses libev Tony Arcieri made it Monday,
February 21, 2011 -
Cool.io Clients and servers are Watcher objects Watcher objects are
attached to a Loop Monday, February 21, 2011 -
Cool.io Clients and servers are Watcher objects Watcher objects are
attached to a Loop The Loop checks for events Monday, February 21, 2011 -
Cool.io Clients and servers are Watcher objects Watcher objects are
attached to a Loop The Loop checks for events Events turned into method calls on Watchers Monday, February 21, 2011 -
Let’s Stream Something The Twitter Streaming API allows high-throughput near-realtime
access to various subsets of public and protected Twitter data. http://dev.twitter.com/pages/streaming_api Monday, February 21, 2011 -
> curl http://stream.twitter.com/1/statuses/sample.json -u$TWITTER_CREDS {"id_str":"39478605398540289","user": {"id_str":"49713582","profile_background_tile":true,"followers_count": 115,"profile_link_color":"0084B4","follow_request_sent":null,"created_at":"Mon Jun 22 18:18:50
+0000 2009","profile_sidebar_border_color":"C0DEED","location":"","profile_use_background_image":tru e,"description":"one day your life will flash before your eyes, make sure its worth watching!! ","is_translator":false,"show_all_inline_media":false,"geo_enabled":false,"notifications":null ,"favourites_count":0,"profile_background_color":"C0DEED","listed_count": 1,"profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/ 201506934\/music.jpg","protected":false,"url":null,"profile_image_url":"http:\/\/a2.twimg.com \/profile_images\/1205125052\/ 168396_486467418449_733593449_6116244_8314553_n_normal.jpg","name":"Amy Jones","contributors_enabled":false,"lang":"en","statuses_count": 3936,"verified":false,"time_zone":null,"profile_text_color":"333333","id": 49713582,"following":null,"utc_offset":null,"friends_count": 95,"profile_sidebar_fill_color":"DDEEF6","screen_name":"amyyjonesss"},"in_reply_to_status_id": null,"truncated":false,"text":"eaten way too much sugar and am not tired at all now :|","favorited":false,"created_at":"Mon Feb 21 00:16:47 +0000 2011","source":"\u003Ca href=\"http:\/\/blackberry.com\/twitter\" rel=\"nofollow\"\u003ETwitter for BlackBerry\u00ae \u003C\/a \u003E","geo":null,"in_reply_to_screen_name":null,"in_reply_to_status_id_str":null,"entities": {"user_mentions":[],"hashtags":[],"urls": []},"contributors":null,"place":null,"retweeted":false,"in_reply_to_user_id_str":null,"retweet _count":0,"in_reply_to_user_id":null,"id":39478605398540289,"coordinates":null} Monday, February 21, 2011 -
Twitter Streaming API Random selection Statuses, deletions, location scrubs JSON-encoded
Monday, February 21, 2011 -
Twitter Streaming API Random selection Statuses, deletions, location scrubs JSON-encoded
Never ends Monday, February 21, 2011 -
Twitter Streaming API 1.Connect to stream.twitter.com 2.Ask for sample.json 3.Throw
data into JSON parser as we receive it Monday, February 21, 2011 -
Twitter Streaming API 1.Connect to stream.twitter.com 2.Ask for sample.json 3.Throw
data into JSON parser as we receive it 4.When we have a complete JSON hash, display it Monday, February 21, 2011 -
Start the Client loop = Cool.io::Loop.default client = TwitterClient.connect.attach(loop) client.request
loop.run Monday, February 21, 2011 -
Connect and Attach class TwitterClient < Cool.io::HttpClient def self.connect super
'stream.twitter.com', 80 end def request super 'GET', '/1/statuses/sample.json', :head=>{ 'Authorization'=>"Basic #{creds}", 'Accept'=>'*/*', 'Connection'=>'keep-alive'} end Monday, February 21, 2011 -
Response Handling def on_response_header(header) super @yajl = Yajl::Parser.new(:symbolize_keys=>true) @yajl.on_parse_complete =
method(:on_element) end def on_body_data(data) @yajl << data end Monday, February 21, 2011 -
Tweet Handling def on_element(element) return on_delete(element[:delete]) if element[:delete] return on_tweet(element)
rescue NoMethodError => e $stderr.puts "UNEXPECTED #{element.inspect}" end def on_tweet(tweet) $stdout.puts "#{tweet[:user][:screen_name]}: #{tweet[:text]}" end Monday, February 21, 2011 -
> bundle exec ./bin/cookin AmmmyClemmensen: STAY HOME FROM SCHOOL, REPLY
TO ANDY, WAKE UP LATER & SEE I HAVE A REPLY FROM HIM, AND THAT MY STACK APP PRIZE CAME. FUCK YEAH BEST DAY. :D ff6_celes_bot: @misorugy ͓Α͏ɺΈͭɻࠓ1 ͕ΜΓ·͠ΐ͏Ͷɻ Redacting tweet 39485416960831488 from 25491312 mapaquet: @moalla Mais oui,c'est vrai, ce sont les procureurs & avocats qu'on veut baillonner. :) Monday, February 21, 2011 -
WARNING The next few slides are about something that doesn’t
exist yet. Don’t try and use it. I will laugh at you. Monday, February 21, 2011 -
DSL for a Twitter Bot? Cookin.with('miamirb') do track '#miamirb' do
|t| # tweets with #miamirb t.reply 'Thanks for mentioning #miamirb!' end track 'miami ruby', 'miami rails' do |t| # tweets with miami and ruby or rails t.reply 'Have you asked anybody in the Miami Ruby Brigade?' end follow 'miamirb' do |t| # tweets with @miamirb t.reply "I'll pass that on!" dm 'BonzoESC', "#{t[:user][:screen_name]} said something #{t.permalink}" end end Monday, February 21, 2011 -
DSL for a Twitter Bot? Credential Storage (OAuth) Username/ID conversion
for follow Monday, February 21, 2011 -
DSL for a Twitter Bot? Cookin.with('miamirb') do track '#miamirb' do
|t| t.reply 'Thanks' end end 1. Check authentication store for ‘miamirb’ 2. If authentication failed, OAuthenticate 3. Create a Cookin::FilterClient instance 4. Add ‘#miamirb’ to the track list of the instance 5. Add processing rule for tweets matching ‘#miamirb’ to the instance 6. Connect the instance to the loop and start the loop Monday, February 21, 2011