Skip to content

Commit

Permalink
Trimming URLs just in case, hopefully this alleviates issues we've se…
Browse files Browse the repository at this point in the history
…en in the demo
  • Loading branch information
TheSoundDefense committed Mar 16, 2017
1 parent 074af0a commit f4baae1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Sources/ServiceCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ public struct ServiceCredentials {

// Initializer.
public init(url: String, name: String, password: String) {
self.url = ServiceCredentials.removeTrailingSlash(from: url)
self.url = ServiceCredentials.trimURL(url)
self.name = name
self.password = password
}

// Remove "/alerts/v1" or "/messages/v1" from the end of the URL.
private static func trimURL(_ url: String) -> String {
let trimmedURL = ServiceCredentials.removeTrailingSlash(from: url)

let alertIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-10)
let messageIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-12)

if trimmedURL.substring(from: alertIndex) == "/alerts/v1" {
return trimmedURL.substring(to: alertIndex)
}

if trimmedURL.substring(from: messageIndex) == "/messages/v1" {
return trimmedURL.substring(to: messageIndex)
}

return url
}

// Trim off a trailing slash from the URL.
private static func removeTrailingSlash(from url: String) -> String {
var urlCopy = url
Expand Down

0 comments on commit f4baae1

Please sign in to comment.