Class: RailFeeds::NetworkRail::Schedule::Header::CIF
- Inherits:
-
Object
- Object
- RailFeeds::NetworkRail::Schedule::Header::CIF
- 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
-
#current_file_reference ⇒ String
Unique reference for the current file.
- #end_date ⇒ Date
-
#extracted_at ⇒ Time
When the BTD extract happened.
- #file_identity ⇒ Object
-
#previous_file_reference ⇒ String?
(the one to apply the update to).
- #start_date ⇒ Date
-
#update_indicator ⇒ String
‘F’ for a full extract, ‘U’ for an update extract.
-
#version ⇒ String
The version of the software that generated the CIF file.
Class Method Summary collapse
-
.from_cif(line) ⇒ Object
rubocop:disable Metrics/AbcSize Initialize a new header from a CIF file line.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#full? ⇒ Boolean
Test if this is a header for a full file.
- #hash ⇒ Object
-
#initialize(**attributes) ⇒ CIF
constructor
A new instance of CIF.
-
#to_cif ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#to_s ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#update? ⇒ Boolean
Test if this is a header for an update file.
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_reference ⇒ String
Returns 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_date ⇒ 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_at ⇒ Time
Returns 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_identity ⇒ Object
26 27 28 |
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 26 def file_identity @file_identity end |
#previous_file_reference ⇒ String?
(the one to apply the update to).
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_date ⇒ 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_indicator ⇒ String
Returns ‘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 |
#version ⇒ String
Returns 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
60 61 62 |
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 60 def full? update_indicator.eql?('F') end |
#hash ⇒ Object
68 69 70 |
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 68 def hash current_file_reference&.dup end |
#to_cif ⇒ Object
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_s ⇒ Object
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
55 56 57 |
# File 'lib/rail_feeds/network_rail/schedule/header/cif.rb', line 55 def update? update_indicator.eql?('U') end |