mirror of
https://github.com/mas-cli/mas
synced 2025-02-16 12:38:30 +00:00
Address comments
- Url encode appName - Add attribution for synchronous NSURLSession code - Run script/sort - Add ResultKeys struct for json keys
This commit is contained in:
parent
e27b963d22
commit
587d84a671
4 changed files with 46 additions and 25 deletions
|
@ -187,11 +187,11 @@
|
|||
children = (
|
||||
ED0F238E1B87A54700AE40CD /* AppStore */,
|
||||
ED0F23801B87524700AE40CD /* Commands */,
|
||||
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */,
|
||||
ED0F238C1B8756E600AE40CD /* Error.swift */,
|
||||
ED031A7B1B5127C00097692E /* main.swift */,
|
||||
EDEAA12C1B51CF8000F2FC3F /* mas-cli-Bridging-Header.h */,
|
||||
EDB6CE8A1BAEB95100648B4D /* mas-cli-Info.plist */,
|
||||
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */,
|
||||
);
|
||||
path = "mas-cli";
|
||||
sourceTree = "<group>";
|
||||
|
@ -348,31 +348,31 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */,
|
||||
0EBF5CDD379D7462C3389536 /* Result.swift in Sources */,
|
||||
3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */,
|
||||
15E27926A580EABEB1B218EF /* Switch.swift in Sources */,
|
||||
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */,
|
||||
30EA893640B02CCF679F9C57 /* Option.swift in Sources */,
|
||||
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */,
|
||||
3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */,
|
||||
0C47E694564FCB59996690DD /* Command.swift in Sources */,
|
||||
345960DE661C85EB2609263C /* ArgumentType.swift in Sources */,
|
||||
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */,
|
||||
F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */,
|
||||
ED0F23871B87537200AE40CD /* Account.swift in Sources */,
|
||||
F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */,
|
||||
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */,
|
||||
345960DE661C85EB2609263C /* ArgumentType.swift in Sources */,
|
||||
0C47E694564FCB59996690DD /* Command.swift in Sources */,
|
||||
3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */,
|
||||
ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */,
|
||||
693A98991CBFFA760004D3B4 /* Search.swift in Sources */,
|
||||
ED0F238D1B8756E600AE40CD /* Error.swift in Sources */,
|
||||
3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */,
|
||||
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */,
|
||||
ED0F237F1B87522400AE40CD /* Install.swift in Sources */,
|
||||
ED0F23901B87A56F00AE40CD /* ISStoreAccount.swift in Sources */,
|
||||
ED0F23831B87533A00AE40CD /* List.swift in Sources */,
|
||||
ED031A7C1B5127C00097692E /* main.swift in Sources */,
|
||||
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */,
|
||||
30EA893640B02CCF679F9C57 /* Option.swift in Sources */,
|
||||
ED0F23851B87536A00AE40CD /* Outdated.swift in Sources */,
|
||||
ED0F23891B87543D00AE40CD /* PurchaseDownloadObserver.swift in Sources */,
|
||||
0EBF5CDD379D7462C3389536 /* Result.swift in Sources */,
|
||||
319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */,
|
||||
693A98991CBFFA760004D3B4 /* Search.swift in Sources */,
|
||||
EDC90B651C70045E0019E396 /* SignIn.swift in Sources */,
|
||||
EDE296531C700F4300554778 /* SignOut.swift in Sources */,
|
||||
EDA3BE521B8B84AF00C18D70 /* SSPurchase.swift in Sources */,
|
||||
15E27926A580EABEB1B218EF /* Switch.swift in Sources */,
|
||||
EDD3B3631C34709400B56B88 /* Upgrade.swift in Sources */,
|
||||
EDB6CE8C1BAEC3D400648B4D /* Version.swift in Sources */,
|
||||
);
|
||||
|
|
|
@ -6,28 +6,34 @@
|
|||
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
||||
//
|
||||
|
||||
struct ResultKeys {
|
||||
static let ResultCount = "resultCount"
|
||||
static let Results = "results"
|
||||
static let TrackName = "trackName"
|
||||
static let TrackId = "trackId"
|
||||
}
|
||||
|
||||
struct SearchCommand: CommandType {
|
||||
typealias Options = SearchOptions
|
||||
let verb = "search"
|
||||
let function = "Search for apps from the Mac App Store"
|
||||
|
||||
func run(options: Options) -> Result<(), MASError> {
|
||||
let searchRequest = NSURLRequest(URL: NSURL(string: searchURLString(options.appName))!)
|
||||
|
||||
guard let searchData = NSURLSession.requestSynchronousData(searchRequest),
|
||||
let searchJsonString = try? NSJSONSerialization.JSONObjectWithData(searchData, options: []) as! Dictionary<String, AnyObject> else {
|
||||
guard let searchURLString = searchURLString(options.appName),
|
||||
let searchJson = NSURLSession.requestSynchronousJSONWithURLString(searchURLString) as? [String: AnyObject] else {
|
||||
return .Failure(MASError(code:.SearchError))
|
||||
}
|
||||
|
||||
guard let resultCount = searchJsonString["resultCount"] as? Int where resultCount > 0,
|
||||
let results = searchJsonString["results"] as? Array<Dictionary<String, AnyObject>> else {
|
||||
print("No apps found")
|
||||
guard let resultCount = searchJson[ResultKeys.ResultCount] as? Int where resultCount > 0,
|
||||
let results = searchJson[ResultKeys.Results] as? [[String: AnyObject]] else {
|
||||
print("No results found")
|
||||
return .Failure(MASError(code:.NoSearchResultsFound))
|
||||
}
|
||||
|
||||
for result in results {
|
||||
if let appName = result["trackName"] as? String,
|
||||
appId = result["trackId"] as? Int {
|
||||
if let appName = result[ResultKeys.TrackName] as? String,
|
||||
appId = result[ResultKeys.TrackId] as? Int {
|
||||
print("\(String(appId)) \(appName)")
|
||||
}
|
||||
}
|
||||
|
@ -35,8 +41,11 @@ struct SearchCommand: CommandType {
|
|||
return .Success(())
|
||||
}
|
||||
|
||||
func searchURLString(appName: String) -> String {
|
||||
return "https://itunes.apple.com/search?entity=macSoftware&term=\(appName)&attribute=allTrackTerm"
|
||||
func searchURLString(appName: String) -> String? {
|
||||
if let urlEncodedAppName = appName.URLEncodedString() {
|
||||
return "https://itunes.apple.com/search?entity=macSoftware&term=\(urlEncodedAppName)&attribute=allTrackTerm"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
||||
//
|
||||
|
||||
// Synchronous NSURLSession code found at: http://ericasadun.com/2015/11/12/more-bad-things-synchronous-nsurlsessions/
|
||||
|
||||
import Foundation
|
||||
|
||||
/// NSURLSession synchronous behavior
|
||||
|
@ -49,3 +51,13 @@ public extension NSURLSession {
|
|||
return NSURLSession.requestSynchronousJSON(request)
|
||||
}
|
||||
}
|
||||
|
||||
public extension String {
|
||||
|
||||
/// Return an URL encoded string
|
||||
func URLEncodedString() -> String? {
|
||||
let customAllowedSet = NSCharacterSet.URLQueryAllowedCharacterSet()
|
||||
return self.stringByAddingPercentEncodingWithAllowedCharacters(customAllowedSet)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,10 +17,10 @@ public struct StderrOutputStream: OutputStreamType {
|
|||
let registry = CommandRegistry<MASError>()
|
||||
let helpCommand = HelpCommand(registry: registry)
|
||||
registry.register(AccountCommand())
|
||||
registry.register(SearchCommand())
|
||||
registry.register(InstallCommand())
|
||||
registry.register(ListCommand())
|
||||
registry.register(OutdatedCommand())
|
||||
registry.register(SearchCommand())
|
||||
registry.register(SignInCommand())
|
||||
registry.register(SignOutCommand())
|
||||
registry.register(UpgradeCommand())
|
||||
|
|
Loading…
Add table
Reference in a new issue