Module: RailFeeds::NetworkRail::SMART

Defined in:
lib/rail_feeds/network_rail/smart.rb

Overview

rubocop:disable Metrics/ModuleLength A module for getting information out of the SMART data.

Defined Under Namespace

Classes: Berth, Step

Class Method Summary collapse

Class Method Details

.build_berths(steps) ⇒ Hash{String=>Hash{String=>RailFeeds::NetworkRail::SMART::Step}} Nested hashes which take a String for the td_area, then a String for

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength Generate an berth data from step data. You’ll get an array of berths which list the steps into and out of them, which in turn have references to other (via the to_berth attribute) to other berths. the berth.id (from either step.from_berth or step.to_berth) to get a specific berth.

Parameters:

  • Array<RailFeeds::NetworkRail::SMART::Step] (Array<RailFeeds::NetworkRail::SMART::Step] steps The steps to build the berth information from.)

    steps The steps to build the berth information from.

Returns:

  • (Hash{String=>Hash{String=>RailFeeds::NetworkRail::SMART::Step}} Nested hashes which take a String for the td_area, then a String for)

    HashString=>Hash{String=>RailFeeds::NetworkRail::SMART::Step} Nested hashes which take a String for the td_area, then a String for



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rail_feeds/network_rail/smart.rb', line 80

def self.build_berths(steps)
  berths = Hash.new do |hash, key|
    hash[key] = Hash.new do |hash2, key2|
      hash2[key2] = Berth.new nil, Set.new, Set.new, Set.new, Set.new
    end
  end

  steps.each do |step|
    next if step.event_direction.nil?

    # from_berth -> step -> to_berth   --->  up
    from_berth = berths.dig(step.td_area, step.from_berth)
    to_berth = berths.dig(step.td_area, step.to_berth)

    from_berth.id ||= step.from_berth
    to_berth.id ||= step.to_berth

    from_berth.send("#{step.to_direction}_steps").add step
    to_berth.send("#{step.from_direction}_steps").add step
    from_berth.send("#{step.to_direction}_berths").add step.to_berth
  end

  # Convert sets to arrays
  berths.each do |_area, hash|
    hash.each do |_id, value|
      value.up_steps = value.up_steps.to_a
      value.down_steps = value.down_steps.to_a
      value.up_berths = value.up_berths.to_a
      value.down_berths = value.down_berths.to_a
    end
  end
end

.download(file, credentials = Credentials) ⇒ Object

Download the current SMART data.

Parameters:



33
34
35
36
# File 'lib/rail_feeds/network_rail/smart.rb', line 33

def self.download(file, credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.download 'ntrod/SupportingFileAuthenticate?type=SMART', file
end

.fetch(credentials = Credentials) ⇒ IO

Fetch the current SMART data.

Parameters:

Returns:

  • (IO)


41
42
43
44
# File 'lib/rail_feeds/network_rail/smart.rb', line 41

def self.fetch(credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.fetch 'ntrod/SupportingFileAuthenticate?type=SMART'
end

.fetch_data(credentials = Credentials) ⇒ Array<RailFeeds::NetworkRail::SMART::Step>

Load SMART data from the internet.

Parameters:

Returns:



61
62
63
64
65
66
# File 'lib/rail_feeds/network_rail/smart.rb', line 61

def self.fetch_data(credentials = Credentials)
  client = HTTPClient.new(credentials: credentials)
  client.fetch_unzipped('ntrod/SupportingFileAuthenticate?type=SMART') do |file|
    break parse_json file.read
  end
end

.load_file(file) ⇒ Array<RailFeeds::NetworkRail::SMART::Step>

Load SMART data from either a .json or .json.gz file.

Parameters:

  • file (String)

    The path of the file to open.

Returns:



49
50
51
52
53
54
55
# File 'lib/rail_feeds/network_rail/smart.rb', line 49

def self.load_file(file)
  Zlib::GzipReader.open(file) do |gz|
    parse_json gz.read
  end
rescue Zlib::GzipFile::Error
  parse_json File.read(file)
end