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

Use padding: false option over gsub #142

Merged
merged 1 commit into from
May 13, 2022
Merged
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
11 changes: 2 additions & 9 deletions lib/global_id/global_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ def app=(app)

private
def parse_encoded_gid(gid, options)
new(Base64.urlsafe_decode64(repad_gid(gid)), options) rescue nil
end

# We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
def repad_gid(gid)
padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
gid + ('=' * padding_chars)
new(Base64.urlsafe_decode64(gid), options) rescue nil
end
end

Expand Down Expand Up @@ -70,7 +64,6 @@ def hash
end

def to_param
# remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
Base64.urlsafe_encode64(to_s, padding: false)
end
end