summaryrefslogtreecommitdiff
path: root/Juick/Model
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/Model')
-rw-r--r--Juick/Model/Message.h29
-rw-r--r--Juick/Model/Message.m52
-rw-r--r--Juick/Model/User.h27
-rw-r--r--Juick/Model/User.m49
4 files changed, 157 insertions, 0 deletions
diff --git a/Juick/Model/Message.h b/Juick/Model/Message.h
new file mode 100644
index 0000000..b100e7d
--- /dev/null
+++ b/Juick/Model/Message.h
@@ -0,0 +1,29 @@
+//
+// Message.h
+// Juick
+//
+// Created by Vitaly Takmazov on 29.10.13.
+// Copyright (c) 2013 com.juick. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "User.h"
+
+@interface Message : NSObject
+
+@property NSNumber *MID;
+@property NSNumber *RID;
+@property User *user;
+@property NSString *text;
+@property NSArray<NSString *> *tags;
+@property NSString *timestamp;
+@property NSString *attach;
+@property NSString *repliesBy;
+@property NSNumber *repliesCount;
+
++(void) pullNextFromPath:(NSString *)path params:(NSDictionary *)params callback:(void(^)(NSArray *))callback;
++(NSString *) messagesUrl;
++(NSString *) threadUrl;
++(NSString *) feedUrl;
+
+@end
diff --git a/Juick/Model/Message.m b/Juick/Model/Message.m
new file mode 100644
index 0000000..de08e98
--- /dev/null
+++ b/Juick/Model/Message.m
@@ -0,0 +1,52 @@
+//
+// Message.m
+// Juick
+//
+// Created by Vitaly Takmazov on 29.10.13.
+// Copyright (c) 2013 com.juick. All rights reserved.
+//
+
+#import "Message.h"
+#import "PDKeychainBindings.h"
+
+@implementation Message
+
++ (NSDictionary *)modelCustomPropertyMapper {
+ return @{@"text" : @"body",
+ @"MID" : @"mid",
+ @"RID" : @"rid",
+ @"attach" : @"photo.small",
+ @"repliesCount": @"replies",
+ @"repliesBy": @"repliesby"};
+}
+
+
++(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *))callback {
+ AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
+ if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) {
+ [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]];
+ }
+ [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
+ NSMutableArray *messages = [NSMutableArray new];
+ [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+ [messages addObject:[Message yy_modelWithJSON:obj]];
+ }];
+ callback(messages);
+ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ NSLog(@"REST Error: %@", error);
+ }];
+}
+
++(NSString *) messagesUrl {
+ return @"https://api.juick.com/messages";
+}
+
++(NSString *) threadUrl {
+ return @"https://api.juick.com/thread";
+}
+
++(NSString *) feedUrl {
+ return @"https://api.juick.com/home";
+}
+
+@end
diff --git a/Juick/Model/User.h b/Juick/Model/User.h
new file mode 100644
index 0000000..84300f3
--- /dev/null
+++ b/Juick/Model/User.h
@@ -0,0 +1,27 @@
+//
+// User.h
+// Juick
+//
+// Created by Vitaly on 01.02.14.
+// Copyright (c) 2014 com.juick. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "PDKeychainBindings.h"
+#import "SIAlertView.h"
+#import "APIClient.h"
+#import "YYModel.h"
+
+@interface User : NSObject
+@property (nonatomic, strong) NSString *uname;
+@property (nonatomic, strong) NSString *uid;
+
++ (void) get:(NSString *)name callback:(void(^)(User *))callback;
+
++ (BOOL) isAuthenticated;
+
++ (void) checkIsValid:(void(^)(BOOL))callback;
+
++ (void) throwUnableToLogin;
+
+@end
diff --git a/Juick/Model/User.m b/Juick/Model/User.m
new file mode 100644
index 0000000..f4b1e00
--- /dev/null
+++ b/Juick/Model/User.m
@@ -0,0 +1,49 @@
+//
+// User.m
+// Juick
+//
+// Created by Vitaly on 01.02.14.
+// Copyright (c) 2014 com.juick. All rights reserved.
+//
+
+#import "User.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 {
+ AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
+ [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]];
+ [manager POST:@"/post" parameters:nil progress:nil success:nil failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
+ if (statusCode == 400) {
+ callback(YES);
+ } else {
+ [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:nil password:nil];
+ callback(NO);
+ }
+ }];}
+
++(void) get:(NSString *) name callback:(void(^)(User *))callback {
+ AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
+ NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil];
+ [manager GET:@"/users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
+ callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]);
+ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
+ callback(nil);
+ }];
+}
+
+@end