Skip to content

Commit

Permalink
Merge pull request #582 from transitland/onestop-id-exceptions
Browse files Browse the repository at this point in the history
Onestop ID exceptions, name fallbacks
  • Loading branch information
doublestranded committed May 6, 2016
2 parents d1d70e3 + 43c19ca commit 7f899c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/models/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,16 @@ def self.from_gtfs(entity, attrs={})
# GTFS Constructor
point = Stop::GEOFACTORY.point(*entity.coordinates)
geohash = GeohashHelpers.encode(point)
name = [entity.stop_name, entity.id, "unknown"]
.select(&:present?)
.first
onestop_id = OnestopId.handler_by_model(self).new(
geohash: geohash,
name: name
)
# Use stop_id as a fallback for an invalid onestop ID name component
onestop_id = OnestopId.handler_by_model(self).new(geohash: geohash, name: entity.stop_name)
if onestop_id.valid? == false
old_onestop_id = onestop_id.to_s
onestop_id = OnestopId.handler_by_model(self).new(geohash: geohash, name: entity.id)
logger.info "Stop.from_gtfs: Invalid onestop_id: #{old_onestop_id}, trying #{onestop_id.to_s}"
end
onestop_id.validate! # raise OnestopIdException
stop = Stop.new(
name: name,
name: entity.stop_name,
onestop_id: onestop_id.to_s,
geometry: point.to_s
)
Expand Down
8 changes: 8 additions & 0 deletions app/services/onestop_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module OnestopId
NAME_FILTER = /[^[:alnum:]\~\>\<]/
IDENTIFIER_TEMPLATE = Addressable::Template.new("gtfs://{feed_onestop_id}/{entity_prefix}/{entity_id}")

class OnestopIdException < StandardError
end

class OnestopIdBase

PREFIX = nil
Expand Down Expand Up @@ -50,6 +53,11 @@ def validate
return (errors.size == 0), errors
end

def validate!
valid, errors = self.validate
raise OnestopIdException.new(errors.join(', ')) unless valid
end

def valid?
return validate[0]
end
Expand Down

0 comments on commit 7f899c7

Please sign in to comment.