0.01
No release in over 3 years
Low commit activity in last 3 years
Reads and parses zip files conforming to Google's GTFS spec. Such files can take up quite a bit of memory when inflated, so this gem prefers to read them as a stream of rows. GTFS Spec: https://developers.google.com/transit/gtfs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1
~> 2.3
~> 0.13
~> 0.9

Runtime

>= 5.0, < 7.0
~> 0.8
~> 2.3
 Project Readme

GTFS Reader

Build Status

gem 'gtfs_reader'

GTFS Reader is a gem designed to help process the contents of a "GTFS Feed":

The General Transit Feed Specification (GTFS) defines a common format for public transportation schedules and associated geographic information. GTFS "feeds" allow public transit agencies to publish their transit data and developers to write applications that consume that data in an interoperable way.

Essentially, a GTFS feed is a ZIP file containing CSV-formatted .txt files following the specification.

Usage

Simple Example

require 'gtfs_reader'

GtfsReader.config do
  # verbose true # TODO: uncomment for verbose output
  return_hashes true

  sources do
    sample do
      url 'http://localhost/sample-feed.zip' # you can also use a filepath here
      before { |etag| puts "Processing source with tag #{etag}..." }
      handlers do
        agency { |row| puts "Read Agency: #{row[:agency_name]}" }
        routes { |row| puts "Read Route: #{row[:route_long_name]}" }
      end
    end
  end
end

GtfsReader.update(:sample) # or GtfsReader.update_all!

Assuming that http://localhost/sample-feed.zip returns the Example Feed, this script will print the following:

Processing source with tag 4d9d3040c284f0581cd5620d5c131109...
Read Agency: Demo Transit Authority
Read Route: Airport - Bullfrog
Read Route: Bullfrog - Furnace Creek Resort
Read Route: Stagecoach - Airport Shuttle
Read Route: City
Read Route: Airport - Amargosa Valley

Custom Feed Format

By default, this gem parses files in the format specified by the GTFS Feed Spec. You can see this FeedDefinition in config/defaults/gtfs_feed_definition.rb. However, in many cases these feeds are created by people who aren't technically-proficient and may not exactly conform to the spec. In the event that you want to parse a file with a different format, you can do so in the GtfsReader.config block:

GtfsReader.config do
  sources do
    sample do
      feed_definition do
        file(:drivers, required: true) do # for my_file.txt
          col(:licence_number, required: true, unique: true)

          # If the employment column contains "1", the symbol :fulltime will be
          # returned, otherwise :temporary will be returned

          col :employment, &output_map({ female: '1', male: '2' }, :unspecified)

          # This will allow you to create a custom parser. Within the given
          # block you can reference other columns in the current row by name.
          col :name do |name|
            case employment
            when :fulltime  then "Mr. #{name}"
            when :temporary then "#{name} the newbie"
            else            name
            end
          end
        end
      end

      # ...

    end
  end
end