Search code examples
objective-ccocoapodsrubymotion

Rubymotion and Objective C getter blocks


I'm pretty new to RubyMotion and I've just hit a wall.

I'm trying to do some LinkedIn oauth work and I need to convert the following to RubyMotion

client = LIALinkedInHttpClient.clientForApplication(application, presentingViewController:nil)

client getAuthorizationCode:^(NSString * code) {
    [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {
        NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
        [self.client getPath:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation * operation, NSDictionary *result) {
            NSLog(@"current user %@", result);
        } failure:^(AFHTTPRequestOperation * operation, NSError *error) {
            NSLog(@"failed to fetch current user %@", error);
        }];
    } failure:^(NSError *error) {
        NSLog(@"Quering accessToken failed %@", error);
    }];
} cancel:^{
    NSLog(@"Authorization was cancelled by user");
} failure:^(NSError *error) {
    NSLog(@"Authorization failed %@", error);
}];

Could anyone possibly point me in the write direction?


Solution

  • The less-typing way using Ruby 2.0's stabby lambdas looks like this:

    client.getAuthorizationCode -> (code) {
      NSLog "Success"
    }, cancel: ->
      NSLog "Auth was cancelled"
    }, failure: -> (error) {
      NSLog "Auth failed"
    }