Guest User

Untitled

a guest
Nov 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. require 'rubygems'
  2. require 'stomp'
  3. require 'json'
  4. require 'mongo'
  5.  
  6. begin
  7. # Credentials set here as environment variables
  8. @user = ENV["DATAFEEDS_USER"];
  9. @password = ENV["DATAFEEDS_PASSWORD"]
  10. @host = "datafeeds.networkrail.co.uk"
  11. @port = 61618
  12.  
  13. # Example destination add yours here
  14. @destination = "/topic/TRAIN_MVT_ALL_TOC"
  15.  
  16. puts "Connecting to datafeeds as #{@user} using stomp protocol stomp://#{@host}:#{@port}\n"
  17. @connection = Stomp::Connection.open @user, @password, @host, @port, true
  18. @connection.subscribe @destination
  19.  
  20. while true
  21. @msg = @connection.receive
  22.  
  23. # Use JSON library to parse the messge body
  24. message_body = JSON.parse(@msg.body)
  25.  
  26. # Assume mongo database is called rail (or it will create one if it doesn't exist
  27. db = Mongo::Connection.new.db("rail")
  28.  
  29. # Create/use collection called td
  30. coll = db.collection("td")
  31.  
  32. message_body.each do |td|
  33.  
  34. # Sanity check debug, output each td message as nice looking JSON
  35. # puts JSON.pretty_generate(td)
  36.  
  37. # insert into collection and return with the id
  38. id = coll.insert(td)
  39.  
  40. end
  41. end
  42.  
  43. @connection.disconnect
  44. rescue
  45. end
Add Comment
Please, Sign In to add comment