Ruby: Difference between revisions

From Open Rail Data Wiki
Updated Ruby example code
Line 1: Line 1:
Install the [https://rubygems.org/gems/stomp Stomp] gem with <code>gem install stomp</code>.


The following code will connect to the datafeeds service, subscribe to the the RTPPM feed and print all messages to STDOUT.


== Simple Ruby script ==
To subscribe to multiple topics in a single connection, copy the <code>client.subscribe</code> section of code, change the topic name and the '-RTPPM' suffix
Here is a very simple Ruby script to get you subscribing to messages, they simply get output to the console.
You'll obviously need Ruby installed (I'm using 1.9.3 via RVM) and the stomp ruby gem installed <code>gem install stomp</code>


You will of course, also need to have signed up to the Network Rail datafeed programme and have your login credentials.
<pre>
Then set up environment variables with DATAFEEDS_USER as your username and DATAFEEDS_PASSWORD as your password.
require "stomp"
 
class NrPollerPoller
 
  # Initialize the poller
 
  def initialize
 
    @hostname = 'datafeeds.networkrail.co.uk'
    @username = 'nobody@example.com'
    @password = 'topsecretpassword'
 
    puts "Stomp consumer for Network Rail Open Data Distribution Service"
 
  end
 
 
  # Connect to the service and process messages
 
  def run
 
    client_headers = { "accept-version" => "1.1", "heart-beat" => "5000,10000", "client-id" => Socket.gethostname, "host" => @hostname }
    client_hash = { :hosts => [ { :login => @username, :passcode => @password, :host => @hostname, :port => 61618 } ], :connect_headers => client_headers }
 
    client = Stomp::Client.new(client_hash)
 
    # Check we have connected successfully
 
    raise "Connection failed" unless client.open?
    raise "Unexpected protocol level #{client.protocol}" unless client.protocol == Stomp::SPL_11
    raise "Connect error: #{client.connection_frame().body}" if client.connection_frame().command == Stomp::CMD_ERROR
 
    puts "Connected to #{client.connection_frame().headers['server']} server with STOMP #{client.connection_frame().headers['version']}"
 
 
    # Subscribe to the RTPPM topic and process messages
 
    client.subscribe("/topic/RTPPM_ALL", { 'id' => client.uuid(), 'ack' => 'client', 'activemq.subscriptionName' => Socket.gethostname + '-RTPPM' }) do |msg|
 
      puts msg.body
 
    end
 
    client.join
 
 
    # We will probably never end up here
 
    client.close
    puts "Client close complete"


<code>
require 'stomp'
begin
  # Credentials set here as environment variables
  @user = ENV["DATAFEEDS_USER"];
  @password = ENV["DATAFEEDS_PASSWORD"]
  @host = "datafeeds.networkrail.co.uk"
  @port = 61618
  # Example destination add yours here
  @destination = "/topic/TD_ALL_SIG_AREA"
  puts "Connecting to datafeeds as #{@user} using stomp protocol stomp://#{@host}:#{@port}\n"
  @connection = Stomp::Connection.open @user, @password, @host, @port, true
  @connection.subscribe @destination
  while true
    @msg = @connection.receive
    puts @msg
   end
   end
  @connection.disconnect
 
rescue
end
end
 
</code>
e = NrPollerPoller.new
e.run
</pre>

Revision as of 12:19, 18 December 2012

Install the Stomp gem with gem install stomp.

The following code will connect to the datafeeds service, subscribe to the the RTPPM feed and print all messages to STDOUT.

To subscribe to multiple topics in a single connection, copy the client.subscribe section of code, change the topic name and the '-RTPPM' suffix

require "stomp"

class NrPollerPoller

  # Initialize the poller

  def initialize

    @hostname = 'datafeeds.networkrail.co.uk'
    @username = 'nobody@example.com'
    @password = 'topsecretpassword'

    puts "Stomp consumer for Network Rail Open Data Distribution Service"

  end


  # Connect to the service and process messages

  def run

    client_headers = { "accept-version" => "1.1", "heart-beat" => "5000,10000", "client-id" => Socket.gethostname, "host" => @hostname }
    client_hash = { :hosts => [ { :login => @username, :passcode => @password, :host => @hostname, :port => 61618 } ], :connect_headers => client_headers }

    client = Stomp::Client.new(client_hash)

    # Check we have connected successfully

    raise "Connection failed" unless client.open?
    raise "Unexpected protocol level #{client.protocol}" unless client.protocol == Stomp::SPL_11
    raise "Connect error: #{client.connection_frame().body}" if client.connection_frame().command == Stomp::CMD_ERROR

    puts "Connected to #{client.connection_frame().headers['server']} server with STOMP #{client.connection_frame().headers['version']}"


    # Subscribe to the RTPPM topic and process messages

    client.subscribe("/topic/RTPPM_ALL", { 'id' => client.uuid(), 'ack' => 'client', 'activemq.subscriptionName' => Socket.gethostname + '-RTPPM' }) do |msg|

      puts msg.body

    end

    client.join


    # We will probably never end up here

    client.close
    puts "Client close complete"

  end

end

e = NrPollerPoller.new
e.run