Skip to content

Instantly share code, notes, and snippets.

@richcaudle
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richcaudle/441ef26018ac5428e6b3 to your computer and use it in GitHub Desktop.
Save richcaudle/441ef26018ac5428e6b3 to your computer and use it in GitHub Desktop.
# include the datasift library
require 'datasift'
# configuration options
config = {:username => 'YOUR_USERNAME', :api_key => 'YOUR_API_KEY', :enable_ssl => true}
# create a client
@datasift = DataSift::Client.new(config)
# Declare a filter in CSDL, looking for content mentioning music
csdl = 'interaction.content contains "music"'
# Compile the filter
filter = @datasift.compile csdl
# Handler: Message (i.e. a new Tweet) is received
on_message = lambda { |message, stream, hash| puts "Received interaction: #{message}" }
# Handler: Message deleted by user
on_delete = lambda { |stream, m| puts 'You must delete this to be compliant with T&Cs ==> ' + m }
# Handler: An error occurred
on_error = lambda { |stream, e| puts "An error has occurred: #{e.message}" }
# Handler: Connected to DataSift
on_connect = lambda do |stream|
puts 'Connected to DataSift'
stream.subscribe(filter[:data][:hash], on_message)
end
# Create stream object, and start streaming data
conn = DataSift::new_stream(config, on_delete, on_error, on_connect)
conn.stream.read_thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment