Created
March 1, 2015 18:59
-
-
Save nuclearace/f90b0feac83de1f591cc to your computer and use it in GitHub Desktop.
Number 36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Converter { | |
static let queryString = "http://www.freecurrencyconverterapi.com/api/v3/convert?q=" | |
static let requestQueue = NSOperationQueue() | |
static func convertFrom(from:String, to:String, amount:Double, callback:(Double) -> Void) { | |
let query = "\(from)_\(to)" | |
let q = queryString + "\(query)&compact=y" | |
let req = NSURLRequest(URL: NSURL(string: q)!) | |
NSURLConnection.sendAsynchronousRequest(req, queue: requestQueue) {res, data, err in | |
if err != nil { | |
println(err) | |
return | |
} | |
var err:NSError? | |
if let json = NSJSONSerialization.JSONObjectWithData(data, | |
options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary { | |
let value = (json[query] as! NSDictionary)["val"] as! Double | |
callback(amount * value) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var from = "USD" | |
var to = "" | |
var amount = 0.00 | |
for var i = 0; i < Process.arguments.count; i++ { | |
if Process.arguments[i] == "-to" { | |
to = Process.arguments[i + 1].uppercaseString | |
} else if Process.arguments[i] == "-from" { | |
from = Process.arguments[i + 1].uppercaseString | |
} else { | |
let a = (Process.arguments[i] as NSString).doubleValue | |
if a != 0 { | |
amount = a | |
} | |
} | |
} | |
if to == "" { | |
println("-to cannot be omitted") | |
} | |
Converter.convertFrom(from, to: to, amount: amount) {value in | |
println(value) | |
exit(0) | |
} | |
CFRunLoopRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment