Domanda

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?

È stato utile?

Soluzione

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"
}

Altri suggerimenti

Here's an idea of how you'd use Objective-C blocks in RubyMotion:

client.getAuthorizationCode(lambda { |code|

}, cancel: lambda { 

}, failure: lambda { |error|

})

I believe you can use the lambda shorthand -> or Proc if you prefer. See the RubyMotion docs for more info. They demonstrate using do and end to begin and finish the block, but for this purpose, I prefer braces.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top