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

🌍 Localize queries #443

Merged
merged 1 commit into from
Dec 20, 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
23 changes: 23 additions & 0 deletions Sources/MasKit/Controllers/StoreSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ extension StoreSearch {
URLQueryItem(name: "entity", value: "macSoftware"),
URLQueryItem(name: "term", value: appName),
]

if let country = country {
components.queryItems!.append(country)
}

return components.url
}

Expand All @@ -44,6 +49,24 @@ extension StoreSearch {
}

components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]

if let country = country {
components.queryItems!.append(country)
}

return components.url
}

private var country: URLQueryItem? {
// CommerceKit and StoreFoundation don't seem to expose the region of the Apple ID signed
// into the App Store. Instead, we'll make an educated guess that it matches the currently
// selected locale in macOS. This obviously isn't always going to match, but it's probably
// better than passing no "country" at all to the iTunes Search API.
// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
guard let region = Locale.autoupdatingCurrent.regionCode else {
return nil
}

return URLQueryItem(name: "country", value: region)
}
}
9 changes: 6 additions & 3 deletions Tests/MasKitTests/Controllers/StoreSearchSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2019 mas-cli. All rights reserved.
//

import Foundation
import Nimble
import PromiseKit
import Quick
Expand All @@ -26,19 +27,21 @@ struct StoreSearchForTesting: StoreSearch {
public class StoreSearchSpec: QuickSpec {
override public func spec() {
let storeSearch = StoreSearchForTesting()
let region = Locale.autoupdatingCurrent.regionCode!

describe("url string") {
it("contains the app name") {
let appName = "myapp"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)"
expect(urlString) == "https://itunes.apple.com/search?"
+ "media=software&entity=macSoftware&term=\(appName)&country=\(region)"
}
it("contains the encoded app name") {
let appName = "My App"
let appNameEncoded = "My%20App"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString)
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)"
expect(urlString) == "https://itunes.apple.com/search?"
+ "media=software&entity=macSoftware&term=\(appNameEncoded)&country=\(region)"
}
// Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil
xit("is nil when app name cannot be url encoded") {
Expand Down