summaryrefslogtreecommitdiff
path: root/Juick/User.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/User.m')
-rw-r--r--Juick/User.m67
1 files changed, 67 insertions, 0 deletions
diff --git a/Juick/User.m b/Juick/User.m
new file mode 100644
index 0000000..6f1678a
--- /dev/null
+++ b/Juick/User.m
@@ -0,0 +1,67 @@
+//
+// 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 {
+ 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) {
+ NSLog(@"REST Error: %@", error);
+ }];
+ [operation start];
+}
+
+@end