Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created November 19, 2016 06:52
Show Gist options
  • Save railsstudent/024bc245ff3b6b55850a9f8c27f9c405 to your computer and use it in GitHub Desktop.
Save railsstudent/024bc245ff3b6b55850a9f8c27f9c405 to your computer and use it in GitHub Desktop.
class UriBuilder
constructor: (@url) ->
@params = {}
idx = @url.indexOf('?')
if idx >= 0
lstParams = @url.substring(idx + 1)
@url = @url.substring(0, idx)
paramPairs = lstParams.split('&')
for paramPair in paramPairs
param = paramPair.split('=')
@params[param[0]] = param[1]
build: () ->
strParams = ''
for k, v of @params
strParams += '&' if strParams isnt ''
strParams += k + '=' + encodeURIComponent(v)
@url + ('?' + strParams if strParams isnt '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment