Skip to content

Commit

Permalink
Use padding:false over gsub
Browse files Browse the repository at this point in the history
  • Loading branch information
daemonsy committed May 12, 2022
1 parent 3ddb0f8 commit 503de9e
Showing 1 changed file with 2 additions and 9 deletions.
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

0 comments on commit 503de9e

Please sign in to comment.