Last active
August 29, 2015 14:01
-
-
Save cburnette/32fd9a1bba86dcfcdc2f to your computer and use it in GitHub Desktop.
Read event stream in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'ruby-box' | |
| require 'lru_redux' | |
| BOX_CLIENT_ID = 'YOUR_CLIENT_ID' | |
| BOX_CLIENT_SECRET = 'YOUR_CLIENT_SECRET' | |
| BOX_DEV_TOKEN = 'YOUR_DEV_TOKEN' | |
| def box_client | |
| box_session = RubyBox::Session.new({ | |
| client_id: BOX_CLIENT_ID, | |
| client_secret: BOX_CLIENT_SECRET, | |
| access_token: BOX_DEV_TOKEN | |
| }) | |
| RubyBox::Client.new(box_session) | |
| end | |
| client = box_client | |
| cache = LruRedux::Cache.new(1000) #used to de-dupe event stream | |
| position = 0 #this should be recorded so you don't always start at the beginning of the stream | |
| loop do | |
| eresp = client.event_response(position) | |
| position = eresp.next_stream_position #update the stream position so next loop picks up new events | |
| eresp.events.each do |ev| | |
| event_id = ev.event_id | |
| if (cache.fetch(event_id)==nil) | |
| cache[event_id] = true | |
| puts "id=#{event_id} :: type=#{ev.event_type} :: user=#{ev.created_by.name}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment