Permalink
Browse files

more parsers more specs, huzzah

  • Loading branch information...
1 parent 2a8a024 commit af80785095c13a38f71028d945e932d67a9d1482 @nerdEd committed Feb 16, 2012
View
@@ -1,5 +1,3 @@
-require 'CSV'
-
module GTFS
class Agency
include GTFS::Model
View
@@ -1,8 +1,12 @@
+require 'CSV'
+
module GTFS
module Model
def self.included(base)
base.extend ClassMethods
+ const_set('PREFIX', '')
+
def valid?
!self.class::REQUIRED_ATTRS.any?{|f| self.send(f.to_sym).nil?}
end
View
@@ -0,0 +1,15 @@
+module GTFS
+ class Route
+ include GTFS::Model
+
+ required_attrs :id, :short_name, :long_name, :type
+ optional_attrs :agency_id, :desc, :url, :color, :text_color
+ attr_accessor *attrs
+
+ column_prefix :route_
+
+ def self.parse_routes(data)
+ return parse_models(data)
+ end
+ end
+end
View
@@ -1,45 +1,16 @@
-require 'CSV'
-
module GTFS
class Stop
- REQUIRED_ATTRS = %w{id name lat lon}
- OPTIONAL_ATTRS = %w{code desc zone_id url location_type parent_station timezone}
- ATTRS = REQUIRED_ATTRS + OPTIONAL_ATTRS
+ include GTFS::Model
+ required_attrs :id, :name, :lat, :lon
+ optional_attrs :code, :desc, :zone_id, :url, :location_type, :parent_station, :timezone
+ column_prefix :stop_
+ attr_accessor *attrs
LOCATION_TYPE_STOP = 0
LOCATION_TYPE_STATION = 1
- attr_accessor *ATTRS
-
- def initialize(attrs)
- attrs.each do |key, val|
- instance_variable_set("@#{key}", val)
- end
- end
-
- def valid?
- !REQUIRED_ATTRS.any?{|f| self.send(f.to_sym).nil?}
- end
-
- def self.strip_prefix(source)
- prefix = /stop_/
- return source.gsub(prefix, '') unless source.respond_to?(:each)
- source.map {|s| s.gsub(prefix, '')}
- end
-
def self.parse_stops(data)
- return [] if data.nil? || data.empty?
-
- stops = []
- CSV.parse(data, :headers => true) do |row|
- attr_hash = {}
- row.to_hash.each do |key, val|
- attr_hash[strip_prefix(key)] = val
- end
- stop = Stop.new(attr_hash)
- stops << stop if stop.valid?
- end
- stops
+ return parse_models(data)
end
end
end
View
@@ -0,0 +1,13 @@
+module GTFS
+ class StopTime
+ include GTFS::Model
+
+ required_attrs :trip_id, :arrival_time, :departure_time, :stop_id, :stop_sequence
+ optional_attrs :stop_headsign, :pickup_type, :drop_off_type, :shape_dist_traveled
+ attr_accessor *attrs
+
+ def self.parse_stop_times(data)
+ return parse_models(data)
+ end
+ end
+end
View
@@ -0,0 +1,15 @@
+module GTFS
+ class Trip
+ include GTFS::Model
+
+ required_attrs :route_id, :service_id, :id
+ optional_attrs :headsign, :short_name, :direction_id, :block_id, :shape_id
+ attr_accessor *attrs
+
+ column_prefix :trip_
+
+ def self.parse_trips(data)
+ return parse_models(data)
+ end
+ end
+end
@@ -9,34 +9,6 @@
subject {GTFS::Agency.parse_agencies(source_text)}
- context 'with a nil source' do
- let(:source_text) {nil}
- it {should == []}
- end
-
- context 'with an empty source' do
- let(:source_text) {''}
- it {should == []}
- end
-
- context 'with a source w/ only headers' do
- let(:source_text) {"agency_id,agency_name,agency_url,agency_timezone,agency_lang,agency_phone\n"}
- it {should == []}
- end
-
- context 'with a valid row of data' do
- let(:source_text) {header_line + valid_line}
- its(:size) {should == 1}
- end
-
- context 'with multiple valid rows of data' do
- let(:source_text) {header_line + valid_line + valid_line}
- its(:size) {should == 2}
- end
-
- context 'with 1 invalid and 1 valid row of data' do
- let(:source_text) {header_line + valid_line + invalid_line}
- its(:size) {should == 1}
- end
+ include_examples 'models'
end
end
@@ -0,0 +1,14 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe GTFS::Route do
+ describe 'Route.parse_routes' do
+ let(:header_line) {"route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color\n"}
+ let(:invalid_header_line) {",agency_id,,route_long_name,,route_type,,route_color,\n"}
+ let(:valid_line) {"4679,1,001,SINAI - FORT McHENRY,,3,,0000FF,FFFFFF\n"}
+ let(:invalid_line) {",1,,,,3,,,FFFFFF\n"}
+
+ subject {GTFS::Route.parse_routes(source_text)}
+
+ include_examples 'models'
+ end
+end
@@ -0,0 +1,15 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe GTFS::Stop do
+ describe 'Trip.parse_stops' do
+ let(:header_line) {"stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station\n"}
+ let(:invalid_header_line) {"stop_lon, zone_id, stop_url, location_type\n"}
+ let(:valid_line) {"3,C093,LANIER & SINAI HOSPITAL,39.351145,-76.663113,,,,\n"}
+ let(:invalid_line) {"3,,,,-76.663113,,,,\n"}
+
+ subject {GTFS::Stop.parse_stops(source_text)}
+
+ include_examples 'models'
+ end
+end
+
@@ -0,0 +1,15 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe GTFS::StopTime do
+ describe 'StopTime.parse_stop_times' do
+ let(:header_line) {"trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,shape_dist_traveled\n"}
+ let(:invalid_header_line) {",arrival_time,,stop_id,,stop_headsign,,drop_off_type,\n"}
+ let(:valid_line) {"982385,4:34:00,4:34:00,277,1,,0,0,\n"}
+ let(:invalid_line) {",,4:34:00,,1,,,0,\n"}
+
+ subject {GTFS::StopTime.parse_stop_times(source_text)}
+
+ include_examples 'models'
+ end
+end
+
@@ -0,0 +1,16 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe GTFS::Trip do
+ describe 'Trip.parse_trips' do
+ let(:header_line) {"route_id,service_id,trip_id,trip_headsign,direction_id,block_id,shape_id\n"}
+ let(:invalid_header_line) {",,,,direction_id,block_id,shape_id\n"}
+ let(:valid_line) {"4679,1,982394,1 FT McHENRY,0,189021,59135\n"}
+ let(:invalid_line) {",1,,1 FT McHENRY,,189021,\n"}
+
+ subject {GTFS::Trip.parse_trips(source_text)}
+
+ include_examples 'models'
+ end
+end
+
+
View
@@ -7,6 +7,8 @@
require 'ruby-debug'
require 'gtfs'
+require File.expand_path(File.dirname(__FILE__) + '/support/model_shared_examples')
+
RSpec.configure do |config|
# Configure you some RSpec
end
@@ -0,0 +1,31 @@
+shared_examples 'models' do |collection_class|
+ context 'with a nil source' do
+ let(:source_text) {nil}
+ it {should == []}
+ end
+
+ context 'with an empty source' do
+ let(:source_text) {''}
+ it {should == []}
+ end
+
+ context 'with a source w/ only headers' do
+ let(:source_text) {header_line}
+ it {should == []}
+ end
+
+ context 'with a valid row of data' do
+ let(:source_text) {header_line + valid_line}
+ its(:size) {should == 1}
+ end
+
+ context 'with multiple valid rows of data' do
+ let(:source_text) {header_line + valid_line + valid_line}
+ its(:size) {should == 2}
+ end
+
+ context 'with 1 invalid and 1 valid row of data' do
+ let(:source_text) {header_line + valid_line + invalid_line}
+ its(:size) {should == 1}
+ end
+end

0 comments on commit af80785

Please sign in to comment.