// // User.m // Juick // // Created by Vitaly on 01.02.14. // Copyright (c) 2014 com.juick. All rights reserved. // #import "User.h" #import "PDKeychainBindings.h" #import "SIAlertView.h" @implementation User +(BOOL) isAuthenticated { return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil; } +(void) throwUnableToLogin { SIAlertView *alert = [[SIAlertView alloc] initWithTitle:@"Error" andMessage:@"Unable to login, check username/password, or network connectivity"]; [alert addButtonWithTitle:@"OK" type:SIAlertViewButtonTypeCancel handler:^(SIAlertView *alertView) { // <#code#> }]; [alert show]; } +(void) checkIsValid:(void (^)(BOOL))callback { RKObjectManager *manager = [RKObjectManager sharedManager]; [manager.HTTPClient setAuthorizationHeaderWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodPOST path:@"/post" parameters:nil]; RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { // this will never happens } failure:^(RKObjectRequestOperation *operation, NSError *error) { if (operation.HTTPRequestOperation.response.statusCode == 400) { callback(YES); } else { [manager.HTTPClient setAuthorizationHeaderWithUsername:nil password:nil]; callback(NO); } }]; [operation start]; } +(void) get:(NSString *) name callback:(void(^)(User *))callback { RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; [mapping addAttributeMappingsFromDictionary:@ { @"uname": @"uname", @"uid" : @"uid", }]; NSString *path = @"/users"; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodGET path:path parameters:params]; RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { callback([[mappingResult array] firstObject]); } failure:^(RKObjectRequestOperation *operation, NSError *error) { callback(nil); }]; [operation start]; } @end