Class: RailFeeds::NetworkRail::Schedule::Header::CIF

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_feeds/network_rail/schedule/header/cif.rb

Overview

A class to hole the information from the header row of a cif file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ CIF

Returns a new instance of CIF.



30
31
32
33
34
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 30

def initialize(**attributes)
  attributes.each do |attribute, value|
    send "#{attribute}=", value
  end
end

Instance Attribute Details

#current_file_referenceString

Returns Unique reference for the current file.

Returns:

  • (String)

    Unique reference for the current file.



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#end_dateDate

Returns:

  • (Date)


26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#extracted_atTime

Returns When the BTD extract happened.

Returns:

  • (Time)

    When the BTD extract happened.



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#file_identityObject



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

def file_identity
  @file_identity
end

#previous_file_referenceString?

(the one to apply the update to).

Returns:

  • (String, nil)

    Unique reference for the previous file



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#start_dateDate

Returns:

  • (Date)


26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#update_indicatorString

Returns ‘F’ for a full extract, ‘U’ for an update extract.

Returns:

  • (String)

    ‘F’ for a full extract, ‘U’ for an update extract.



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

#versionString

Returns The version of the software that generated the CIF file.

Returns:

  • (String)

    The version of the software that generated the CIF file.



26
27
28
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26

attr_accessor :file_identity, :extracted_at,
:current_file_reference, :previous_file_reference,
:update_indicator, :version, :start_date, :end_date

Class Method Details

.from_cif(line) ⇒ Object

rubocop:disable Metrics/AbcSize Initialize a new header from a CIF file line



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 38

def self.from_cif(line)
  fail ArgumentError, "Invalid line:\n#{line}" unless line[0..1].eql?('HD')

  new(
    file_identity: line[2..21].strip,
    extracted_at: Time.strptime(line[22..31] + 'UTC', '%d%m%y%H%M%Z'),
    current_file_reference: line[32..38].strip,
    previous_file_reference: line[39..45].strip,
    update_indicator: line[46].strip,
    version: line[47].strip,
    start_date: Date.strptime(line[48..53], '%d%m%y'),
    end_date: Date.strptime(line[54..59], '%d%m%y')
  )
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 64

def ==(other)
  hash == other&.hash
end

#full?Boolean

Test if this is a header for a full file

Returns:

  • (Boolean)


60
61
62
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 60

def full?
  update_indicator.eql?('F')
end

#hashObject



68
69
70
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 68

def hash
  current_file_reference&.dup
end

#to_cifObject

rubocop:disable Metrics/AbcSize



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 73

def to_cif
  format('%-80.80s', [
    'HD',
    format('%-20.20s', file_identity),
    # rubocop:disable Style/FormatStringToken
    format('%-10.10s', extracted_at&.strftime('%d%m%y%H%M')),
    # rubocop:enable Style/FormatStringToken
    format('%-7.7s', current_file_reference),
    format('%-7.7s', previous_file_reference),
    format('%-1.1s', update_indicator),
    format('%-1.1s', version),
    # rubocop:disable Style/FormatStringToken
    format('%-6.6s', start_date&.strftime('%d%m%y')),
    format('%-6.6s', end_date&.strftime('%d%m%y'))
    # rubocop:enable Style/FormatStringToken
  ].join) + "\n"
end

#to_sObject

rubocop:enable Metrics/AbcSize



92
93
94
95
96
97
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 92

def to_s
  "File #{file_identity.inspect} (version #{version}) " \
    "at #{extracted_at.strftime('%Y-%m-%d %H:%M')}. " \
    "#{full? ? 'A full' : 'An update'} extract " \
    "for #{start_date} to #{end_date}."
end

#update?Boolean

Test if this is a header for an update file

Returns:

  • (Boolean)


55
56
57
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 55

def update?
  update_indicator.eql?('U')
end