Skip to content

Instantly share code, notes, and snippets.

@nuclearace
Created March 1, 2015 18:59
Show Gist options
  • Save nuclearace/f90b0feac83de1f591cc to your computer and use it in GitHub Desktop.
Save nuclearace/f90b0feac83de1f591cc to your computer and use it in GitHub Desktop.
Number 36
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)
}
}
}
}
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