diff --git a/.travis.yml b/.travis.yml
index baf63af..53e1292 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,7 @@
language: objective-c
+osx_image: xcode7.1
before_install:
- gem i cocoapods --no-ri --no-rdoc
- gem i xcpretty --no-ri --no-rdoc
script:
-- xcodebuild -workspace Polyglot.xcworkspace -scheme Polyglot -sdk iphonesimulator test | xcpretty -c
+- set -o pipefail && xcodebuild -workspace Polyglot.xcworkspace -scheme PolyglotSample -sdk iphonesimulator9.1 test | xcpretty -c
diff --git a/Podfile.lock b/Podfile.lock
index 01e9589..853bf12 100644
--- a/Podfile.lock
+++ b/Podfile.lock
@@ -1,5 +1,5 @@
PODS:
- - Nocilla (0.9.0)
+ - Nocilla (0.10.0)
- Polyglot (0.4.0)
DEPENDENCIES:
@@ -11,7 +11,7 @@ EXTERNAL SOURCES:
:path: .
SPEC CHECKSUMS:
- Nocilla: 4d713c56e7357fd0cac0363854d4a58021ad3f41
- Polyglot: dddf99978e893f05bd9f3461922de8958f41480b
+ Nocilla: ae0a2b05f3087b473624ac2b25903695df51246a
+ Polyglot: 29a83191af1d56f1af28f195ff2cd748814dab47
-COCOAPODS: 0.36.0
+COCOAPODS: 0.39.0
diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift
index a142e22..9ee78b1 100644
--- a/Polyglot/Polyglot.swift
+++ b/Polyglot/Polyglot.swift
@@ -88,22 +88,22 @@ public class Polyglot {
/**
- :param: clientId Microsoft Translator client ID.
- :param: clientSecret Microsoft Translator client secret.
+ - parameter clientId: Microsoft Translator client ID.
+ - parameter clientSecret: Microsoft Translator client secret.
*/
public init(clientId: String, clientSecret: String) {
- self.session = Session(clientId: clientId, clientSecret: clientSecret)
- self.toLanguage = Language.English
+ session = Session(clientId: clientId, clientSecret: clientSecret)
+ toLanguage = Language.English
}
/**
Translates a given piece of text.
- :param: text The text to translate.
- :param: callback The code to be executed once the translation has completed.
+ - parameter text: The text to translate.
+ - parameter callback: The code to be executed once the translation has completed.
*/
public func translate(text: String, callback: ((translation: String) -> (Void))) {
- self.session.getAccessToken { token in
+ session.getAccessToken { token in
self.fromLanguage = text.language
let toLanguageComponent = "&to=\(self.toLanguage.rawValue.urlEncoded!)"
let fromLanguageComponent = (self.fromLanguage != nil) ? "&from=\(self.fromLanguage!.rawValue.urlEncoded!)" : ""
@@ -114,14 +114,29 @@ public class Polyglot {
request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in
- var translation = ""
- if let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) {
- translation = xmlString.stringByReplacingOccurrencesOfString("", withString: "")
- translation = translation.stringByReplacingOccurrencesOfString("", withString: "")
+ let translation: String
+ guard
+ let data = data,
+ let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String
+ else {
+ translation = ""
+ return
+ }
+
+ translation = self.translationFromXML(xmlString)
+
+ defer {
+ dispatch_async(dispatch_get_main_queue()) {
+ callback(translation: translation)
+ }
}
- callback(translation: translation)
}
task.resume()
}
}
+
+ private func translationFromXML(XML: String) -> String {
+ let translation = XML.stringByReplacingOccurrencesOfString("", withString: "")
+ return translation.stringByReplacingOccurrencesOfString("", withString: "")
+ }
}
diff --git a/Polyglot/Session.swift b/Polyglot/Session.swift
index 159c5de..d54b45a 100644
--- a/Polyglot/Session.swift
+++ b/Polyglot/Session.swift
@@ -36,7 +36,7 @@ class Session {
}
func getAccessToken(callback: ((token: String) -> (Void))) {
- if ((self.accessToken == nil) || self.isExpired) {
+ if (accessToken == nil || isExpired) {
let url = NSURL(string: "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13")
let request = NSMutableURLRequest(URL: url!)
@@ -46,9 +46,14 @@ class Session {
request.HTTPBody = bodyString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in
- let resultsDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
-
- let expiresIn = resultsDict["expires_in"] as! NSString
+ guard
+ let data = data,
+ let resultsDict = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers),
+ let expiresIn = resultsDict["expires_in"] as? NSString
+ else {
+ callback(token: "")
+ return
+ }
self.expirationTime = NSDate(timeIntervalSinceNow: expiresIn.doubleValue)
let token = resultsDict["access_token"] as! String
@@ -59,11 +64,11 @@ class Session {
task.resume()
} else {
- callback(token: self.accessToken!)
+ callback(token: accessToken!)
}
}
private var isExpired: Bool {
- return self.expirationTime?.earlierDate(NSDate()) == self.expirationTime
+ return expirationTime?.earlierDate(NSDate()) == self.expirationTime
}
}
diff --git a/Polyglot/StringExtension.swift b/Polyglot/StringExtension.swift
index 24a277b..bfe1830 100644
--- a/Polyglot/StringExtension.swift
+++ b/Polyglot/StringExtension.swift
@@ -31,7 +31,7 @@ extension String {
}
public var language: Language? {
- if (count(self) > 0) { // Prevent Index Out of Bounds in NSLinguisticTagger
+ if characters.count > 0 { // Prevent Index Out of Bounds in NSLinguisticTagger
let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeLanguage], options: 0)
tagger.string = self
if let result = tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil) {
diff --git a/PolyglotSample.xcodeproj/project.pbxproj b/PolyglotSample.xcodeproj/project.pbxproj
index 90876a7..0516092 100644
--- a/PolyglotSample.xcodeproj/project.pbxproj
+++ b/PolyglotSample.xcodeproj/project.pbxproj
@@ -7,8 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
- 6BF86B27E801452077EAF608 /* Pods_PolyglotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 925AA115444C33786930F106 /* Pods_PolyglotTests.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
- 76E093B31C869D9803863B0A /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DB9A4E55DA20E420F9D1A394 /* Pods.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
+ 6BF86B27E801452077EAF608 /* Pods_PolyglotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 925AA115444C33786930F106 /* Pods_PolyglotTests.framework */; };
+ 76E093B31C869D9803863B0A /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DB9A4E55DA20E420F9D1A394 /* Pods.framework */; };
B60F875D1985F1C200ABB94A /* PolyglotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B60F875C1985F1C200ABB94A /* PolyglotTests.swift */; };
B6156E3F1999D93B002CE52F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B67FC7841985F20B0030ABF8 /* Main.storyboard */; };
B6682231199C68AE00DFA909 /* StringExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6682230199C68AE00DFA909 /* StringExtensionTests.swift */; };
@@ -177,6 +177,8 @@
B60F873C1985F1C200ABB94A /* Project object */ = {
isa = PBXProject;
attributes = {
+ LastSwiftMigration = 0710;
+ LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0600;
ORGANIZATIONNAME = "Ayaka Nonaka";
TargetAttributes = {
diff --git a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme
similarity index 95%
rename from PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme
rename to PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme
index b03f537..2f479dc 100644
--- a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme
+++ b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme
@@ -23,10 +23,10 @@
+ shouldUseLaunchSchemeArgsEnv = "YES">
@@ -48,15 +48,18 @@
ReferencedContainer = "container:PolyglotSample.xcodeproj">
+
+
@@ -72,10 +75,10 @@
diff --git a/PolyglotSample/ViewController.swift b/PolyglotSample/ViewController.swift
index db6ef55..3615f59 100644
--- a/PolyglotSample/ViewController.swift
+++ b/PolyglotSample/ViewController.swift
@@ -36,28 +36,23 @@ Start translating.
class ViewController: UIViewController {
- var translator: Polyglot?
+ let translator = Polyglot(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET")
@IBOutlet weak var inputTextField: UITextField!
@IBOutlet weak var translationLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.translator = Polyglot(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET")
- }
-
@IBAction func didTapTranslateButton(sender: AnyObject) {
+ guard let text = inputTextField.text else { return }
+
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
- self.translator?.translate(self.inputTextField.text) { translation in
- dispatch_async(dispatch_get_main_queue(), {
- if let language = self.translator?.fromLanguage?.rawValue {
- self.translationLabel.text = "Translated from \(language.capitalizedString): \(translation)"
- }
- else {
- self.translationLabel.text = translation
- }
- UIApplication.sharedApplication().networkActivityIndicatorVisible = false
- })
+ translator.translate(text) { translation in
+ if let language = self.translator.fromLanguage?.rawValue {
+ self.translationLabel.text = "Translated from \(language.capitalizedString): \(translation)"
+ }
+ else {
+ self.translationLabel.text = translation
+ }
+ UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
}