Class: GtfsReader::SourceUpdater

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(name, source) ⇒ SourceUpdater

Returns a new instance of SourceUpdater.

Parameters:

  • name (String)

    an arbitrary string describing this source

  • source (Source)


16
17
18
19
20
# File 'lib/gtfs_reader/source_updater.rb', line 16

def initialize(name, source)
  @name = name
  @source = source
  @temp_files = {}
end

Instance Method Details

#before_callbacksObject

Call the “before” callback set on this source



23
24
25
# File 'lib/gtfs_reader/source_updater.rb', line 23

def before_callbacks
  @source.before.call(fetch_data_set_identifier) if @source.before
end

#check_columnsObject

Check that every file has its required columns



64
65
66
67
68
69
70
# File 'lib/gtfs_reader/source_updater.rb', line 64

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_filesObject

Parse the filenames in the feed and check which required and optional files are present.

Raises:

  • (RequiredFilenamesMissing)

    if the feed is missing a file which is marked as “required” in the [FeedDefinition]



53
54
55
56
57
58
59
60
61
# File 'lib/gtfs_reader/source_updater.rb', line 53

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|
    @found_files << req if filenames.include?(req.filename)
  end
end

#closeObject



45
46
47
# File 'lib/gtfs_reader/source_updater.rb', line 45

def close
  @temp_files.values.each(&:close)
end

#download_sourceObject

Download the data from the remote server



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gtfs_reader/source_updater.rb', line 28

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 StandardException => e
  Log.error(e.message)
  raise e
ensure
  zip.try(:close)
end

#process_filesObject



72
73
74
75
76
77
78
79
80
# File 'lib/gtfs_reader/source_updater.rb', line 72

def process_files
  @found_files.each do |file|
    if @source.handlers.handler?(file.name)
      process_from_temp_file(file)
    else
      Log.warn { "Skipping #{file.filename.yellow} (no handler)" }
    end
  end
end