Skip to content

Instantly share code, notes, and snippets.

@cburnette
Last active August 29, 2015 14:01
Show Gist options
  • Save cburnette/32fd9a1bba86dcfcdc2f to your computer and use it in GitHub Desktop.
Save cburnette/32fd9a1bba86dcfcdc2f to your computer and use it in GitHub Desktop.
Read event stream in Ruby
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