Skip to content

Commit 6b90230

Browse files
author
Joey Lorich
committed
Merge in service oriented delegates and routes
2 parents f14cc35 + f5f108f commit 6b90230

26 files changed

Lines changed: 722 additions & 203 deletions

EasyReader.xcodeproj/project.pbxproj

Lines changed: 108 additions & 0 deletions
Large diffs are not rendered by default.

EasyReader/Application/Api/CSFakedDataRequestor.m

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,53 @@
77
//
88

99
#import "CSFakedDataRequestor.h"
10+
#import "AKRouter.h"
1011

1112
@implementation CSFakedDataRequestor
1213

1314
- (void) requestRoute:(NSString *) routeName
14-
withParams:(NSDictionary *) params
15-
success:(void(^)(AFHTTPRequestOperation *operation, id responseObject)) successBlock
16-
failure:(void(^)(AFHTTPRequestOperation *operation, id responseObject)) failureBlock;
15+
withParams:(NSDictionary *) params
16+
success:(void(^)(AFHTTPRequestOperation *operation, id responseObject)) successBlock
17+
failure:(void(^)(AFHTTPRequestOperation *operation, id responseObject)) failureBlock;
1718
{
18-
NSDictionary *route = [self routes][routeName];
19-
NSString *method = route[@"method"];
20-
NSString *path = route[@"path"];
21-
22-
NSDictionary *data;
23-
24-
if([method isEqualToString:@"GET"]) {
25-
if ([path rangeOfString:@"feed_items"].location != NSNotFound) {
26-
data = [self feedItemsResponse];
27-
} else if ([path rangeOfString:@"feeds"].location != NSNotFound) {
28-
//for now, the feed defaults and search return the same values
29-
data = [self feedsResponse];
19+
20+
NSString *path = [[AKRouter shared] pathFor:routeName params:params];
21+
AKRequestMethod method = [[AKRouter shared] methodFor:routeName];
22+
23+
NSDictionary *data;
24+
25+
if(method == kAKRequestMethodGET) {
26+
if ([path rangeOfString:@"feed_items"].location != NSNotFound) {
27+
data = [self feedItemsResponse];
28+
} else if ([path rangeOfString:@"feeds"].location != NSNotFound) {
29+
//for now, the feed defaults and search return the same values
30+
data = [self feedsResponse];
31+
} else {
32+
//this path is not handled by the response faker
33+
data = @{};
34+
}
3035
} else {
31-
//this path is not handled by the response faker
32-
data = @{};
36+
//post/other requests not supported right now
37+
data = @{};
3338
}
34-
} else {
35-
//post/other requests not supported right now
36-
data = @{};
37-
}
38-
39-
if (successBlock) successBlock(nil, data);
39+
40+
if (successBlock) successBlock(nil, data);
4041
}
4142

4243
//these feeds are used for both the search and the defaults endpoints
4344
//their ids should match the feed_id fields on the feedItemsResponse
4445
- (NSDictionary *) feedsResponse
4546
{
46-
NSArray *items = [NSArray arrayWithObjects:@{@"id": @1,
47+
NSArray *items = [NSArray arrayWithObjects:@{@"id": @1,
4748
@"name": @"Cloudspace Feed",
4849
@"url": @"http://www.engadget.com/rss.xml",
4950
@"icon": @"http://s3.amazonaws.com/rss.cloudspace.com/feed/1/icon.png", //note: this image does not work on 3-11-2014
5051
@"feed_items": @[]},
51-
@{@"id": @2,
52-
@"name": @"EasyReader Feed",
53-
@"url": @"http://www.engadget.com/rss.xml",
54-
@"icon": @"http://s3.amazonaws.com/rss.cloudspace.com/feed/2/icon.png", //note: this image does not work on 3-11-2014
55-
@"feed_items": @[]}, nil];
52+
@{@"id": @2,
53+
@"name": @"EasyReader Feed",
54+
@"url": @"http://www.engadget.com/rss.xml",
55+
@"icon": @"http://s3.amazonaws.com/rss.cloudspace.com/feed/2/icon.png", //note: this image does not work on 3-11-2014
56+
@"feed_items": @[]}, nil];
5657
return @{@"feeds": items};
5758
}
5859

@@ -148,17 +149,17 @@ - (NSDictionary *) feedItemsResponse
148149
@"created_at": @"2014-03-05T22:32:41+00:00",
149150
@"updated_at": @"2014-03-05T22:32:41+00:00",
150151
@"published_at": @"2014-03-04T19:52:21+00:00"}, nil];
151-
152-
NSRange subarrayRange;
153-
if ( !self.requestCounter ) self.requestCounter = 1;
154-
if ( self.requestCounter == 1 ){
155-
subarrayRange = NSMakeRange(0,4);
156-
} else {
157-
subarrayRange = NSMakeRange(self.requestCounter*2,2);
158-
}
159-
self.requestCounter++;
160-
161-
return @{@"feed_items": [items subarrayWithRange:subarrayRange]};
152+
153+
NSRange subarrayRange;
154+
if ( !self.requestCounter ) self.requestCounter = 1;
155+
if ( self.requestCounter == 1 ){
156+
subarrayRange = NSMakeRange(0,4);
157+
} else {
158+
subarrayRange = NSMakeRange(self.requestCounter*2,2);
159+
}
160+
self.requestCounter++;
161+
162+
return @{@"feed_items": [items subarrayWithRange:subarrayRange]};
162163
}
163164

164165

EasyReader/Application/Api/CSResponsiveApiRequestor.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@
1717
success:(void(^)())successBlock
1818
failure:(void(^)())failureBlock;
1919

20-
@property NSDictionary *routes;
21-
22-
- (NSString *) buildUrlByPath:(NSString *) path;
2320

2421
@end

EasyReader/Application/Api/CSResponsiveApiRequestor.m

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#import "Feed.h"
1414
#import "User.h"
1515

16+
#import "AKRouter.h"
17+
1618
@implementation CSResponsiveApiRequestor
1719

1820
static dispatch_once_t pred;
@@ -23,87 +25,66 @@ @implementation CSResponsiveApiRequestor
2325
+ (CSResponsiveApiRequestor *) sharedRequestor
2426
{
2527
dispatch_once(&pred, ^{
26-
if (sharedInstance != nil) {
27-
return;
28-
}
29-
#ifdef DEBUG
30-
sharedInstance = [[CSFakedDataRequestor alloc] init];
31-
#else
32-
sharedInstance = [[CSResponsiveApiRequestor alloc] init];
33-
#endif
28+
if (sharedInstance != nil) {
29+
return;
30+
}
31+
32+
#ifdef DEBUG
33+
sharedInstance = [[CSFakedDataRequestor alloc] init];
34+
#else
35+
sharedInstance = [[CSResponsiveApiRequestor alloc] init];
36+
#endif
3437
});
3538
return sharedInstance;
3639
}
3740

38-
- (id)init
39-
{
40-
self = [super init];
41-
42-
self.routes = @{@"feedDefaults": @{@"path": @"/feeds/defaults",
43-
@"method": @"GET"},
44-
@"feedSearch": @{@"path": @"/feeds/search",
45-
@"method": @"GET"},
46-
@"feedItems": @{@"path": @"/feed_items",
47-
@"method": @"GET"},
48-
@"feedCreate": @{@"path": @"/feeds"}
49-
};
50-
return self;
51-
}
5241

5342
- (void) requestRoute:(NSString *)routeName
5443
withParams:(NSDictionary *)params
5544
success:(void(^)())successBlock
5645
failure:(void(^)())failureBlock
5746
{
58-
NSDictionary *route = [self routes][routeName];
59-
NSString *method = route[@"method"];
60-
NSString *fullUrl = [self buildUrlByPath:route[@"path"]];
61-
62-
NSDictionary *data;
63-
if( [method isEqualToString:@"GET"] ) {
64-
data = [self getRequest:fullUrl withParams:params];
65-
} else if([method isEqualToString:@"POST"]) {
66-
data = [self postRequest:fullUrl withParams:params];
67-
} else {
68-
data = @{};
69-
}
70-
71-
if (successBlock) successBlock(nil, data);
47+
48+
AKRoute *route = [[AKRouter shared] routeForName:routeName];
49+
NSString *fullUrl = [[[AKRouter shared] urlFor:routeName params:params] absoluteString];
50+
51+
NSDictionary *data;
52+
53+
switch (route.requestMethod)
54+
{
55+
case kAKRequestMethodGET:
56+
data = [self getRequest:fullUrl withParams:params];
57+
break;
58+
case kAKRequestMethodPOST:
59+
data = [self postRequest:fullUrl withParams:params];
60+
break;
61+
default:
62+
data = @{};
63+
}
64+
65+
if (successBlock) successBlock(nil, data);
7266
}
7367

7468
- (NSDictionary *) getRequest:(NSString *) path withParams:(NSDictionary *) params
7569
{
76-
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
77-
[manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
78-
NSLog(@"JSON: %@", responseObject);
79-
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
80-
NSLog(@"Error: %@", error);
81-
}];
82-
return @{};
70+
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
71+
[manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
72+
NSLog(@"JSON: %@", responseObject);
73+
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
74+
NSLog(@"Error: %@", error);
75+
}];
76+
return @{};
8377
}
8478

8579
- (NSDictionary *) postRequest:(NSString *) path withParams:(NSDictionary *) params
8680
{
87-
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
88-
[manager POST:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
89-
NSLog(@"JSON: %@", responseObject);
90-
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
91-
NSLog(@"Error: %@", error);
92-
}];
93-
return @{};
94-
}
95-
96-
//this should be converted into a constant whose value is determined by the environment
97-
- (NSString *) baseUrl
98-
{
99-
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"api_url"];
100-
}
101-
102-
//adds the base url onto the given path
103-
- (NSString *) buildUrlByPath:(NSString *) path
104-
{
105-
NSArray *urlParts = [NSArray arrayWithObjects: [self baseUrl], path, nil];
106-
return [urlParts componentsJoinedByString:@""];
81+
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
82+
[manager POST:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
83+
NSLog(@"JSON: %@", responseObject);
84+
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
85+
NSLog(@"Error: %@", error);
86+
}];
87+
return @{};
10788
}
10889

10990
@end

EasyReader/Application/AppDelegates/CSAppDelegate.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
#import "CSFeedItemUpdater.h"
1110
#import "CSMenuLeftViewController.h"
1211
#import "MFSideMenu.h"
12+
#import "SOApplicationDelegate.h"
1313

14-
@class Feed;
1514

16-
@interface CSAppDelegate : UIResponder <UIApplicationDelegate>
15+
/**
16+
* The main delegate for the application
17+
*/
18+
@interface CSAppDelegate : SOApplicationDelegate <UIApplicationDelegate>
19+
1720

1821
#pragma mark - Properties
19-
@property (strong, nonatomic) UIWindow *window;
20-
@property (nonatomic, retain) Feed *activeFeed;
2122

23+
/// The side menu container controller
2224
@property (strong, nonatomic) MFSideMenuContainerViewController *container;
2325

26+
2427
@end

0 commit comments

Comments
 (0)