Skip to content

Instantly share code, notes, and snippets.

@bergusman
Created September 22, 2015 20:14
Show Gist options
  • Save bergusman/ee135a170386095d8888 to your computer and use it in GitHub Desktop.
Save bergusman/ee135a170386095d8888 to your computer and use it in GitHub Desktop.
Load Ruble Rates
- (void)loadRatesWithCompletion:(void (^)(NSNumber *usd, NSNumber *eur, NSError *error))completion {
NSURL *url = [NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.xchange+where+pair+=+%22USDRUB,EURRUB%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
if (completion) {
completion(nil, nil, error);
}
}
if (data) {
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (json) {
double usd = [json[@"query"][@"results"][@"rate"][0][@"Rate"] doubleValue];
double eur = [json[@"query"][@"results"][@"rate"][1][@"Rate"] doubleValue];
if (completion) {
completion(@(usd), @(eur), nil);
}
} else {
if (completion) {
completion(nil, nil, nil);
}
}
} else {
if (completion) {
completion(nil, nil, nil);
}
}
});
if (data) {
}
}] resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment