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 4 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
19 changes: 17 additions & 2 deletions app/services/onestop_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ class StopOnestopId < OnestopIdBase
class RouteOnestopId < OnestopIdBase
PREFIX = :r
MODEL = Route

def to_s
geohash = @geohash[0...self.class::GEOHASH_MAX_LENGTH]
[
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 +128,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 +155,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