Skip to content

Commit

Permalink
♻️ Build URLs using URLComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-araman committed May 6, 2021
1 parent 88ce4f6 commit 585699d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
35 changes: 14 additions & 21 deletions Sources/MasKit/Controllers/StoreSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,28 @@ extension StoreSearch {
/// - Parameter appName: MAS app identifier.
/// - Returns: URL for the search service or nil if appName can't be encoded.
func searchURL(for appName: String) -> URL? {
guard let urlString = searchURLString(forApp: appName) else { return nil }
return URL(string: urlString)
}

/// Builds the search URL for an app.
///
/// - Parameter appName: Name of app to find.
/// - Returns: String URL for the search service or nil if appName can't be encoded.
func searchURLString(forApp appName: String) -> String? {
if let urlEncodedAppName = appName.urlEncodedString {
return "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(urlEncodedAppName)"
guard var components = URLComponents(string: "https://itunes.apple.com/search") else {
return nil
}
return nil

components.queryItems = [
URLQueryItem(name: "media", value: "software"),
URLQueryItem(name: "entity", value: "macSoftware"),
URLQueryItem(name: "term", value: appName),
]
return components.url
}

/// Builds the lookup URL for an app.
///
/// - Parameter appId: MAS app identifier.
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
func lookupURL(forApp appId: Int) -> URL? {
guard let urlString = lookupURLString(forApp: appId) else { return nil }
return URL(string: urlString)
}
guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else {
return nil
}

/// Builds the lookup URL for an app.
///
/// - Parameter appId: MAS app identifier.
/// - Returns: String URL for the lookup service.
func lookupURLString(forApp appId: Int) -> String? {
"https://itunes.apple.com/lookup?id=\(appId)"
components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]
return components.url
}
}
16 changes: 0 additions & 16 deletions Sources/MasKit/Extensions/String+PercentEncoding.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Tests/MasKitTests/Controllers/StoreSearchSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class StoreSearchSpec: QuickSpec {
describe("url string") {
it("contains the app name") {
let appName = "myapp"
let urlString = storeSearch.searchURLString(forApp: appName)
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)"
}
it("contains the encoded app name") {
let appName = "My App"
let appNameEncoded = "My%20App"
let urlString = storeSearch.searchURLString(forApp: appName)
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString)
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)"
}
// Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil
xit("is nil when app name cannot be url encoded") {
let appName = "`~!@#$%^&*()_+ 💩"
let urlString = storeSearch.searchURLString(forApp: appName)
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString).to(beNil())
}
}
Expand Down

0 comments on commit 585699d

Please sign in to comment.