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

♻️ Build URLs using URLComponents #360

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
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
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