Skip to content

Commit

Permalink
Merge pull request #576 from transitland/onestop_id_name_truncation
Browse files Browse the repository at this point in the history
Onestop id name truncation
  • Loading branch information
doublestranded committed May 4, 2016
2 parents 4d23672 + d24f867 commit 8d5a156
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/services/onestop_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ class StopOnestopId < OnestopIdBase
class RouteOnestopId < OnestopIdBase
PREFIX = :r
MODEL = Route

def to_s
geohash = @geohash[0...self.class::GEOHASH_MAX_LENGTH]
# Both Route and their RouteStopPatterns will share a name component whose max length
# is dependent on the fixed length components of the RouteStopPattern onestop id (which includes the Route onestop id)
# a variable length route geohash, and the total limitation of 64 chars for all onestop ids.
# Route onestop ids will have 1 prefix, 2 dashes, and a variable-length route geohash up to 10 characters long.
# RouteStopPattern onestop ids will have a route onestop id plus 2 dashes and 2 geohashes (stop pattern and geometry)
# of 6 chars long each. The final max value of the name length is computed in RouteStopPatternOnestopId.max_name_length
# and will be between 64 - (1 + 2 + 10 + 2 + 2*6) = 37 and 46 chars long.
[
self.class::PREFIX,
geohash,
@name[0...RouteStopPatternOnestopId.max_name_length(geohash.length)],
].join(COMPONENT_SEPARATOR)[0...self.class::MAX_LENGTH]
end
end

class RouteStopPatternOnestopId < OnestopIdBase
Expand Down Expand Up @@ -119,10 +135,11 @@ def initialize(string: nil, route_onestop_id: nil, stop_pattern: nil, geometry_c
end

def to_s
geohash = @geohash[0...self.class::GEOHASH_MAX_LENGTH]
[
self.class::PREFIX,
@geohash[0...self.class::GEOHASH_MAX_LENGTH],
@name,
geohash,
@name[0...self.class.max_name_length(geohash.length)],
@stop_hash,
@geometry_hash
].join(COMPONENT_SEPARATOR)[0...self.class::MAX_LENGTH]
Expand All @@ -145,6 +162,11 @@ def self.route_onestop_id(onestop_id)
onestop_id.split(COMPONENT_SEPARATOR)[0..2].join(COMPONENT_SEPARATOR)
end

def self.max_name_length(geohash_length)
num_fixed_chars = NUM_COMPONENTS + 2*(HASH_LENGTH)
MAX_LENGTH - (num_fixed_chars + geohash_length)
end

private

def validate_hash(value)
Expand Down
21 changes: 21 additions & 0 deletions spec/services/onestop_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class TestOnestopId < OnestopId::OnestopIdBase
end
end

context 'RouteOnestopId' do
it 'truncates beyond maximum length' do
expect(
OnestopId::RouteOnestopId.new(
geohash: '9q9',
name: 'pneumonoultramicroscopicsilicovolcanoconiosis'
).to_s
).to eq('r-9q9-pneumonoultramicroscopicsilicovolcanoconiosi')
end
end

context 'RouteStopPatternOnestopId' do
context 'invalid arguments' do
it 'fails gracefully when given an invalid stop pattern' do
Expand Down Expand Up @@ -116,6 +127,16 @@ class TestOnestopId < OnestopId::OnestopIdBase
).to eq('fca1a5')
end

it 'truncates beyond maximum length' do
expect(
OnestopId::RouteStopPatternOnestopId.new(
route_onestop_id: 'r-9q9-pneumonoultramicroscopicsilicovolcanoconiosis',
stop_pattern: ['s-9q9-stop~1', 's-9q9-stop~2'],
geometry_coords: [[-122.0, 40.0], [-121.0, 41.0]]
).to_s
).to eq('r-9q9-pneumonoultramicroscopicsilicovolcanoconiosi-fca1a5-48fed0')
end

context '#validate' do
it 'requires valid route onestop id' do
expect(
Expand Down

0 comments on commit 8d5a156

Please sign in to comment.