Class: GTFS::Source
- Inherits:
-
Object
- Object
- GTFS::Source
- Defined in:
- lib/gtfs/source.rb
Direct Known Subclasses
Constant Summary collapse
- ENTITIES =
[GTFS::Agency, GTFS::Stop, GTFS::Route, GTFS::Trip, GTFS::StopTime, GTFS::Calendar, GTFS::CalendarDate, GTFS::Shape, GTFS::FareAttribute, GTFS::FareRule, GTFS::Frequency, GTFS::Transfer, GTFS::FeedInfo, GTFS::Attribution, GTFS::Pathway, GTFS::Translation, GTFS::Level]
- REQUIRED_SOURCE_FILES =
ENTITIES.select(&:required_file?).map(&:filename)
- OPTIONAL_SOURCE_FILES =
ENTITIES.reject(&:required_file?).map(&:filename)
- SOURCE_FILES =
ENTITIES.map(&:filename)
- DEFAULT_OPTIONS =
{strict: true, encoding: "utf-8"}
Instance Attribute Summary collapse
-
#archive ⇒ Object
Returns the value of attribute archive.
-
#options ⇒ Object
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #entries ⇒ Object
- #extract_to_cache(source_path) ⇒ Object
- #files ⇒ Object
-
#initialize(source, opts = {}) ⇒ Source
constructor
A new instance of Source.
- #load_archive(source) ⇒ Object
- #parse_file(filename, options) ⇒ Object
- #raise_if_missing_source(filename) ⇒ Object
Constructor Details
#initialize(source, opts = {}) ⇒ Source
Returns a new instance of Source.
20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#archive ⇒ Object
Returns the value of attribute archive.
18 19 20 |
# File 'lib/gtfs/source.rb', line 18 def archive @archive end |
#options ⇒ Object
Returns the value of attribute options.
18 19 20 |
# File 'lib/gtfs/source.rb', line 18 def @options end |
#source ⇒ Object
Returns the value of attribute source.
18 19 20 |
# File 'lib/gtfs/source.rb', line 18 def source @source end |
Class Method Details
.build(data_root, opts = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/gtfs/source.rb', line 48 def self.build(data_root, opts={}) if File.exist?(data_root) src = LocalSource.new(data_root, opts) else src = URLSource.new(data_root, opts) end end |
.finalize(directory) ⇒ Object
32 33 34 |
# File 'lib/gtfs/source.rb', line 32 def self.finalize(directory) proc {FileUtils.rm_rf(directory)} end |
Instance Method Details
#entries ⇒ Object
56 57 58 |
# File 'lib/gtfs/source.rb', line 56 def entries Dir.entries(@tmp_dir) end |
#extract_to_cache(source_path) ⇒ Object
36 37 38 39 40 41 42 |
# 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 ⇒ Object
77 78 79 |
# File 'lib/gtfs/source.rb', line 77 def files @files ||= {} end |
#load_archive(source) ⇒ Object
44 45 46 |
# File 'lib/gtfs/source.rb', line 44 def load_archive(source) raise 'Cannot directly instantiate base GTFS::Source' end |
#parse_file(filename, options) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/gtfs/source.rb', line 81 def parse_file(filename, ) raise_if_missing_source filename open File.join(@tmp_dir, '/', filename), "r:#{[:encoding]}" do |f| files[filename] ||= yield f end end |
#raise_if_missing_source(filename) ⇒ Object
60 61 62 63 |
# 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 |