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
# File 'lib/gtfs_reader/source_updater.rb', line 16

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

Instance Method Details

#before_callbacksObject

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_columnsObject

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_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]



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

#closeObject



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

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

#download_sourceObject

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.message
  raise e
ensure
  zip.try :close
end

#process_filesObject



75
76
77
78
79
80
81
82
83
# File 'lib/gtfs_reader/source_updater.rb', line 75

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