class NetworkRail::Client

Constants

HOST_NAME
HOST_PORT

Attributes

client[RW]

Public Class Methods

new() click to toggle source
# File lib/network_rail/client.rb, line 15
def initialize
  raise Exception::NoLoginCredentials if NetworkRail.user_name.nil? or NetworkRail.password.nil?
  
  self.client = Stomp::Client.new(connection_parameters)
  
  raise Exception::ConnectionError unless client.open?
  
  if client.connection_frame().command == Stomp::CMD_ERROR
    raise Exception::AuthenticationError if client.connection_frame().body.match /SecurityException/
    raise Exception::ConnectionError
  end
end

Public Instance Methods

train_movements(args = {operator: :all}) { |factory| ... } click to toggle source
# File lib/network_rail/client.rb, line 28
def train_movements(args = {operator: :all}, &block)
  raise Exception::BlockRequired if !block
  
  operator_code = NetworkRail::Operators.business_codes[args[:operator]]
  
  client.subscribe("/topic/TRAIN_MVT_#{operator_code}_TOC") do |message|
    movements = JSON.parse message
    movements.each {|movement| yield NetworkRail::Message::TrainMovement.factory(movement) }
  end
end

Private Instance Methods

connection_headers() click to toggle source
# File lib/network_rail/client.rb, line 43
def connection_headers
  {
    "accept-version" => "1.1",
    "host"           => HOST_NAME
  }
end
connection_parameters() click to toggle source
# File lib/network_rail/client.rb, line 50
def connection_parameters
  {
    hosts: [
      {
        login:    NetworkRail.user_name,
        passcode: NetworkRail.password,
        host:     HOST_NAME,
        port:     HOST_PORT
      }
    ],
    connect_headers: connection_headers
  }
end