Class: GtfsReader::SourceUpdater
- Inherits:
-
Object
- Object
- GtfsReader::SourceUpdater
- Defined in:
- lib/gtfs_reader/source_updater.rb
Overview
Downloads remote Feed files, checks that they are valid, and passes each file in the feed to the handlers in the given [Source].
Instance Method Summary collapse
-
#before_callbacks ⇒ Object
Call the “before” callback set on this source.
-
#check_columns ⇒ Object
Check that every file has its required columns.
-
#check_files ⇒ Object
Parse the filenames in the feed and check which required and optional files are present.
- #close ⇒ Object
-
#download_source ⇒ Object
Download the data from the remote server.
-
#initialize(name, source) ⇒ SourceUpdater
constructor
A new instance of SourceUpdater.
- #process_files ⇒ Object
Constructor Details
#initialize(name, source) ⇒ SourceUpdater
Returns a new instance of SourceUpdater.
16 17 18 19 |
# File 'lib/gtfs_reader/source_updater.rb', line 16 def initialize(name, source) @name, @source = name, source @temp_files = {} end |
Instance Method Details
#before_callbacks ⇒ Object
Call the “before” callback set on this source
22 23 24 25 26 |
# File 'lib/gtfs_reader/source_updater.rb', line 22 def before_callbacks if @source.before @source.before.call fetch_data_set_identifier end end |
#check_columns ⇒ Object
Check that every file has its required columns
67 68 69 70 71 72 73 |
# File 'lib/gtfs_reader/source_updater.rb', line 67 def check_columns @found_files.each do |file| @temp_files[file.filename].open do |data| FileReader.new data, file, validate: true end end end |
#check_files ⇒ Object
Parse the filenames in the feed and check which required and optional files are present.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gtfs_reader/source_updater.rb', line 54 def check_files @found_files = [] check_required_files check_optional_files # Add feed files of zip to the list of files to be processed @source.feed_definition.files.each do |req| if filenames.include? req.filename @found_files << req end end end |
#close ⇒ Object
46 47 48 |
# File 'lib/gtfs_reader/source_updater.rb', line 46 def close @temp_files.values.each &:close end |
#download_source ⇒ Object
Download the data from the remote server
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gtfs_reader/source_updater.rb', line 29 def download_source Log.debug { " Reading #{@source.url.green}" } zip = Tempfile.new 'gtfs' zip.binmode zip << open(@source.url).read zip.rewind extract_to_tempfiles zip Log.debug { "Finished reading #{@source.url.green}" } rescue Exception => e Log.error e. raise e ensure zip.try :close end |