Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onestop id name truncation #576

Merged
merged 5 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write comment to mention we're reserving 14 characters (2x6 + 2 separators) for RSP components.

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