Skip to content

Instantly share code, notes, and snippets.

@bjonord
Created September 16, 2012 11:09
Show Gist options
  • Save bjonord/3732012 to your computer and use it in GitHub Desktop.
Save bjonord/3732012 to your computer and use it in GitHub Desktop.
This is a "snippet" of a MKNetworkKit method in Objective-C and RubyMotion
// How to use..!
[MyNetworkClass loginWithUsername:self.username.text
password:self.password.text
responseBlock:^(NSDictionary* jsonDict, NSError* error) {
if([[jsonDict objectForKey:@"result"] boolValue]){
// Logged in!
}
}
];
// IMPLEMENTATION
typedef void(^JSONBlock)(NSDictionary* jsonDict, NSError* error);
MKNetworkEngine* _sharedEngine;
+ (MKNetworkOperation *)loginWithUsername:(NSString *)username password:(NSString *)password responseBlock:(JSONBlock)block {
// Dictionary with objectsForKeys
NSDictionary* params = @{@"username" : username, @"password" : password};
MKNetworkOperation* op = [_sharedEngine operationWithPath:@"login.php" params:params httpMethod:@"POST"];
// Post login credentials in background
[op onCompletion:^(MKNetworkOperation *completedOperation) {
// Convert to JSON-data in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:completedOperation.responseData options:0 error:&error];
// Call block on main thread again
dispatch_async(dispatch_get_main_queue(), ^{
if(error)
block(nil, error);
else
block(json, nil);
});
});
} onError:^(NSError *error) {
block(nil, error);
}];
[_sharedEngine enqueueOperation:op forceReload:YES];
return op;
}
# How to use..!
MyNetworkClass.loginWithUsername(self.username.text, password: self.password.text, responseBlock:
lambda |jsonDict, error| do
unless jsonDict["result"].nil?
# logged in
end
end
)
# IMPLEMENTATION
def loginWithUsername(username, password: password, responseBlock: block)
params = {"username" => username, "password" => password}
op = _sharedEngine.operationWithPath("login.php", params: params, httpMethod: "POST")
op.onCompletion(
lambda |completedOperation| do
Dispatch::Queue.concurrent("com.company.application.tasks").async do
error = Pointer.new(:object)
json = NSJSONSerialization.JSONObjectWithData(completedOperation.responseData, options: 0, error: error)
Dispatch::Queue.main.async do
block(json, nil) unless error
end
end
end,
onError:
lambda |error| do
block(nil, error)
end
)
_sharedEngine.enqueueOperation(op, forceReload: true)
op
end
@bjonord
Copy link
Author

bjonord commented Sep 16, 2012

While the snippets are not that different size wise there are some minor differences. I have not compiled and tested the code, just tried to translate it as best I can.

Most people will argue that the reason to pick Ruby(RubyMotion) over Obj-C is because the amount of code you will have to write to accomplish something, I find this to be the incorrect argument to successfully understand the major difference. What I have found to be the better way of approaching this subject is the style of coding you are comfortable with and maybe even familiar with. This however was not the case in my situation, since I wasn't familiar with either one of the two. All in all, after spending the past months learning Ruby and Obj-C(Cocoa Touch in particular), I find that it feels more natural to express yourself in code using Ruby than Objective-C.

Feel free to correct my Gist if you find it to contain errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment