class GTFS::Source
Constants
- DEFAULT_OPTIONS
- ENTITIES
- OPTIONAL_SOURCE_FILES
- REQUIRED_SOURCE_FILES
- SOURCE_FILES
Attributes
archive[RW]
options[RW]
source[RW]
Public Class Methods
build(data_root, opts={})
click to toggle source
# File lib/gtfs/source.rb, line 48 def self.build(data_root, opts={}) if File.exists?(data_root) src = LocalSource.new(data_root, opts) else src = URLSource.new(data_root, opts) end end
finalize(directory)
click to toggle source
# File lib/gtfs/source.rb, line 32 def self.finalize(directory) proc {FileUtils.rm_rf(directory)} end
new(source, opts={})
click to toggle source
# File lib/gtfs/source.rb, line 20 def initialize(source, opts={}) raise 'Source cannot be nil' if source.nil? @tmp_dir = Dir.mktmpdir ObjectSpace.define_finalizer(self, self.class.finalize(@tmp_dir)) @source = source load_archive(@source) @options = DEFAULT_OPTIONS.merge(opts) end
Public Instance Methods
entries()
click to toggle source
# File lib/gtfs/source.rb, line 56 def entries Dir.entries(@tmp_dir) end
extract_to_cache(source_path)
click to toggle source
# File lib/gtfs/source.rb, line 36 def extract_to_cache(source_path) Zip::File.open(source_path) do |zip| zip.entries.each do |entry| zip.extract(entry.name, File.join(@tmp_dir, '/', entry.name)) end end end
files()
click to toggle source
# File lib/gtfs/source.rb, line 77 def files @files ||= {} end
load_archive(source)
click to toggle source
# File lib/gtfs/source.rb, line 44 def load_archive(source) raise 'Cannot directly instantiate base GTFS::Source' end
parse_file(filename) { |f| ... }
click to toggle source
# File lib/gtfs/source.rb, line 81 def parse_file(filename) raise_if_missing_source filename open File.join(@tmp_dir, '/', filename), 'r:bom|utf-8' do |f| files[filename] ||= yield f end end
raise_if_missing_source(filename)
click to toggle source
# File lib/gtfs/source.rb, line 60 def raise_if_missing_source(filename) file_missing = !entries.include?(filename) raise InvalidSourceException.new("Missing required source file: #{filename}") if file_missing end