Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cookin' with Cool.io

Cookin' with Cool.io

Bryce "BonzoESC" Kerley

February 21, 2010
Tweet

More Decks by Bryce "BonzoESC" Kerley

Other Decks in Programming

Transcript

  1. Event-driven Programming •Be reactive instead of proactive •Don’t manage sockets

    and connections •Framework for network services Monday, February 21, 2011
  2. Synchronous s = TCPSocket.open('localhost', '80') while line = s.gets puts

    line.chop end s.close Monday, February 21, 2011
  3. Cool.io Clients and servers are Watcher objects Watcher objects are

    attached to a Loop Monday, February 21, 2011
  4. Cool.io Clients and servers are Watcher objects Watcher objects are

    attached to a Loop The Loop checks for events Monday, February 21, 2011
  5. 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
  6. 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
  7. > 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. > 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
  14. 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
  15. 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
  16. 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