Created
January 30, 2013 10:52
-
-
Save Dimillian/4672410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MSJSonParser.m | |
// MySeeen | |
// | |
// Created by Thomas Ricouard on 11/02/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import "MSJSonParser.h" | |
@implementation MSJSonParser | |
@synthesize delegate, method; | |
-(id)initWithParameters:(NSArray *)parameters ressource:(NSString *)ressource cacheFileName:(NSString *)filename | |
{ | |
self = [super init]; | |
if (self) { | |
NSFileManager *manager = [NSFileManager defaultManager]; | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); | |
NSString *cacheLibraryPath = [NSString stringWithFormat:@"%@/userCache",[paths objectAtIndex:0]]; | |
if (![manager fileExistsAtPath:cacheLibraryPath]) { | |
[manager createDirectoryAtPath:cacheLibraryPath withIntermediateDirectories:NO attributes:nil error:nil]; | |
} | |
filename = [filename stringByReplacingOccurrencesOfString:@"/" withString:@"2f"]; | |
_filename = [cacheLibraryPath stringByAppendingPathComponent:filename]; | |
_parameters = parameters; | |
_ressource = ressource; | |
} | |
return self; | |
} | |
-(void)cancel | |
{ | |
_restRequest.delegate = nil; | |
[_restRequest cancelRequest]; | |
} | |
-(void)parseFromServer{ | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; | |
if (self.method) { | |
_restRequest = [[MSRESTRequest alloc]initWithMethod:self.method ressource:_ressource parameters:_parameters shouldEscapeParameters:YES]; | |
} | |
else { | |
_restRequest = [[MSRESTRequest alloc]initWithMethod:@"GET" ressource:_ressource parameters:_parameters shouldEscapeParameters:YES]; | |
} | |
_restRequest.delegate = self; | |
[_restRequest executeRequest]; | |
[delegate jsonParserDidStartParsing:self fromFile:NO]; | |
} | |
#pragma mark - MSRestRequest delegate | |
-(void)requestDidStart | |
{ | |
} | |
-(void)requestDidFinishWithSuccess:(NSDictionary *)response | |
{ | |
[delegate jsonParserDidFinishParsing:response fromFile:NO]; | |
} | |
-(void)requestDidFinishWithJSON:(NSJSONSerialization *)json | |
{ | |
[self writeJsonToFile:json]; | |
} | |
-(void)requestDidFailWithError:(MSJSonError *)error | |
{ | |
[delegate jsonParserDidFinishParsingWithError:error]; | |
} | |
-(void)reQuestDidFailBecauseNotLogged | |
{ | |
[_restRequest executeRequest]; | |
} | |
-(void)requestDidFailBecauseNoActiveConnection | |
{ | |
[delegate jsonParserDidFailBecauseNoConnection]; | |
} | |
#pragma mark - METHOD | |
-(BOOL)isExistingFile | |
{ | |
NSFileManager *fileM = [NSFileManager defaultManager]; | |
return [fileM fileExistsAtPath:_filename]; | |
} | |
-(void)lazyParsingFromFileThenServer | |
{ | |
if ([self isExistingFile]){ | |
[self parseJsonFromFile]; | |
} | |
[self parseFromServer]; | |
} | |
-(void)parseJsonFromFile | |
{ | |
if (![self isExistingFile]){ | |
[self parseFromServer]; | |
} | |
[delegate jsonParserDidStartParsing:self fromFile:YES]; | |
NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:_filename]; | |
[input open]; | |
NSJSONSerialization *read = [NSJSONSerialization JSONObjectWithStream:input options:NSJSONReadingAllowFragments error:nil]; | |
NSDictionary *dic = (NSDictionary *)read; | |
[delegate jsonParserDidFinishParsing:dic fromFile:YES]; | |
[input close]; | |
} | |
-(void)writeJsonToFile:(NSJSONSerialization *)json | |
{ | |
if (json) { | |
NSOutputStream *opt = [NSOutputStream outputStreamToFileAtPath:_filename append:NO]; | |
[opt open]; | |
[NSJSONSerialization writeJSONObject:json toStream:opt options:NSJSONReadingAllowFragments error:nil]; | |
[opt close]; | |
} | |
} | |
-(void)loginViewControllerSuccess:(RPLoginViewController *)loginView | |
{ | |
[self lazyParsingFromFileThenServer]; | |
} | |
-(void)loginViewControllerCancel:(RPLoginViewController *)loginView | |
{ | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment