summaryrefslogtreecommitdiff
path: root/Juick/User.m
blob: e1bef3a3d7456fd3847aa195b6180b648af5abce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  User.m
//  Juick
//
//  Created by Vitaly on 01.02.14.
//  Copyright (c) 2014 com.juick. All rights reserved.
//

#import "User.h"
#import "PDKeychainBindings.h"

@implementation User

+(BOOL) isAuthenticated {
    return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil;
}

+(void) throwUnableToLogin {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to login, check username/password, or network connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [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