Skip to content

Commit

Permalink
Create a separate API client for the APIs that can only be used with …
Browse files Browse the repository at this point in the history
…vandyvans.com
  • Loading branch information
sethfri committed Jan 30, 2014
1 parent 35bc2aa commit 735fae6
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 125 deletions.
12 changes: 12 additions & 0 deletions Vandy Vans/Vandy Vans.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
2DC2059616E45591008B3F31 /* VVNotificationCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DC2059516E45591008B3F31 /* VVNotificationCell.m */; };
2DCA1D6A181E32F8003A85E5 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2DCA1D69181E32F8003A85E5 /* Images.xcassets */; };
2DDDB2F8165DBAA5007077E9 /* VVArrivalTimeTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DDDB2F7165DBAA5007077E9 /* VVArrivalTimeTableViewController.m */; };
2DFFD398189A4356006B095E /* VVVandyVansClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DFFD397189A4356006B095E /* VVVandyVansClient.m */; };
2DFFD39E189A4671006B095E /* VVVandyVansResponseSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DFFD39D189A4671006B095E /* VVVandyVansResponseSerializer.m */; };
809D88CC0AAF4B31A477FF7E /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BA171022C5534BA0B4C4AA79 /* libPods.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -119,6 +121,10 @@
2DCA1D69181E32F8003A85E5 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
2DDDB2F6165DBAA5007077E9 /* VVArrivalTimeTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VVArrivalTimeTableViewController.h; sourceTree = "<group>"; };
2DDDB2F7165DBAA5007077E9 /* VVArrivalTimeTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VVArrivalTimeTableViewController.m; sourceTree = "<group>"; };
2DFFD396189A4356006B095E /* VVVandyVansClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VVVandyVansClient.h; sourceTree = "<group>"; };
2DFFD397189A4356006B095E /* VVVandyVansClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VVVandyVansClient.m; sourceTree = "<group>"; };
2DFFD39C189A4671006B095E /* VVVandyVansResponseSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VVVandyVansResponseSerializer.h; sourceTree = "<group>"; };
2DFFD39D189A4671006B095E /* VVVandyVansResponseSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VVVandyVansResponseSerializer.m; sourceTree = "<group>"; };
ADCAEF1C8A2C492EB2B3961B /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = SOURCE_ROOT; };
BA171022C5534BA0B4C4AA79 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -290,6 +296,10 @@
2D95BF661855E8D600AA585C /* VVSyncromaticsClient.m */,
2D95BF681855ED3700AA585C /* VVSyncromaticsResponseSerializer.h */,
2D95BF691855ED3700AA585C /* VVSyncromaticsResponseSerializer.m */,
2DFFD396189A4356006B095E /* VVVandyVansClient.h */,
2DFFD397189A4356006B095E /* VVVandyVansClient.m */,
2DFFD39C189A4671006B095E /* VVVandyVansResponseSerializer.h */,
2DFFD39D189A4671006B095E /* VVVandyVansResponseSerializer.m */,
);
name = Networking;
sourceTree = "<group>";
Expand Down Expand Up @@ -404,9 +414,11 @@
2D34C56D162718C500F170CA /* main.m in Sources */,
2D34C571162718C500F170CA /* VVAppDelegate.m in Sources */,
2D972BFA187935360019FA1F /* VVVanAnnotation.m in Sources */,
2DFFD398189A4356006B095E /* VVVandyVansClient.m in Sources */,
2D34C59316271A9100F170CA /* VVStopTableViewController.m in Sources */,
2D34C59616271B8600F170CA /* VVMapViewController.m in Sources */,
2DDDB2F8165DBAA5007077E9 /* VVArrivalTimeTableViewController.m in Sources */,
2DFFD39E189A4671006B095E /* VVVandyVansResponseSerializer.m in Sources */,
2D5BE9FF1660A5A000CEFFA3 /* VVArrivalTime.m in Sources */,
2DA198AC16AAB8F7008F5CB5 /* VVReportTableViewController.m in Sources */,
2DA198B016AAC0CC008F5CB5 /* VVAboutTableViewController.m in Sources */,
Expand Down
101 changes: 51 additions & 50 deletions Vandy Vans/Vandy Vans/VVRoute.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "VVRoute.h"
#import "VVVandyVansClient.h"
#import "VVSyncromaticsClient.h"

NSString * const kAnnotationsDateKey = @"annotationsDate";
Expand Down Expand Up @@ -71,31 +72,31 @@ + (void)annotationsForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArr
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if (![fileManager fileExistsAtPath:annotationsPath] || ([[userDefaults objectForKey:kAnnotationsDateKey] timeIntervalSinceNow] <= kStaleTimeInterval)) {
[[VVSyncromaticsClient sharedClient] fetchStopsForRoute:route
withCompletionBlock:^(NSArray *stops, NSError *error) {
if (stops) {
NSData *stopsData = [NSKeyedArchiver archivedDataWithRootObject:stops];
[fileManager createFileAtPath:annotationsPath
contents:stopsData
attributes:nil];
NSDate *now = [NSDate date];
[userDefaults setObject:now
forKey:[self annotationsKeyForRouteColor:route.routeColor]];
if (![userDefaults objectForKey:kAnnotationsDateKey]) {
[userDefaults setObject:now
forKey:kAnnotationsDateKey];
}
[userDefaults synchronize];
completionBlock(stops);
} else {
NSLog(@"ERROR: %@", error);
}
}];
[[VVVandyVansClient sharedClient] fetchStopsForRoute:route
withCompletionBlock:^(NSArray *stops, NSError *error) {
if (stops) {
NSData *stopsData = [NSKeyedArchiver archivedDataWithRootObject:stops];
[fileManager createFileAtPath:annotationsPath
contents:stopsData
attributes:nil];

NSDate *now = [NSDate date];

[userDefaults setObject:now
forKey:[self annotationsKeyForRouteColor:route.routeColor]];

if (![userDefaults objectForKey:kAnnotationsDateKey]) {
[userDefaults setObject:now
forKey:kAnnotationsDateKey];
}

[userDefaults synchronize];

completionBlock(stops);
} else {
NSLog(@"ERROR: %@", error);
}
}];
} else {
NSArray *annotations = [NSKeyedUnarchiver unarchiveObjectWithFile:annotationsPath];
completionBlock(annotations);
Expand All @@ -110,31 +111,31 @@ + (void)polylineForRoute:(VVRoute *)route withCompletionBlock:(void (^)(MKPolyli
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if (![fileManager fileExistsAtPath:polylinePath] || ([[userDefaults objectForKey:kPolylineDateKey] timeIntervalSinceNow] <= kStaleTimeInterval)) {
[[VVSyncromaticsClient sharedClient] fetchPolylineForRoute:route
withCompletionBlock:^(MKPolyline *polyline, NSError *error) {
if (polyline) {
NSData *polylineData = [NSKeyedArchiver archivedDataWithRootObject:polyline];
[fileManager createFileAtPath:polylinePath
contents:polylineData
attributes:nil];
NSDate *now = [NSDate date];
[userDefaults setObject:[NSDate date]
forKey:[self polylineKeyForRouteColor:route.routeColor]];
if (![userDefaults objectForKey:kPolylineDateKey]) {
[userDefaults setObject:now
forKey:kPolylineDateKey];
}
[userDefaults synchronize];
completionBlock(polyline);
} else {
NSLog(@"ERROR: %@", error);
}
}];
[[VVVandyVansClient sharedClient] fetchPolylineForRoute:route
withCompletionBlock:^(MKPolyline *polyline, NSError *error) {
if (polyline) {
NSData *polylineData = [NSKeyedArchiver archivedDataWithRootObject:polyline];
[fileManager createFileAtPath:polylinePath
contents:polylineData
attributes:nil];

NSDate *now = [NSDate date];

[userDefaults setObject:[NSDate date]
forKey:[self polylineKeyForRouteColor:route.routeColor]];

if (![userDefaults objectForKey:kPolylineDateKey]) {
[userDefaults setObject:now
forKey:kPolylineDateKey];
}

[userDefaults synchronize];

completionBlock(polyline);
} else {
NSLog(@"ERROR: %@", error);
}
}];
} else {
MKPolyline *polyline = [NSKeyedUnarchiver unarchiveObjectWithFile:polylinePath];
completionBlock(polyline);
Expand Down
5 changes: 0 additions & 5 deletions Vandy Vans/Vandy Vans/VVSyncromaticsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import "AFHTTPSessionManager.h"

@class VVRoute;
@class MKPolyline;
@class VVStop;

@interface VVSyncromaticsClient : AFHTTPSessionManager
Expand All @@ -19,10 +18,6 @@
/** This method gives the API key for accessing Syncromatics's Vandy Vans service. */
+ (NSString *)apiKey;

- (NSURLSessionDataTask *)fetchStopsForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArray *stops, NSError *error))completionBlock;

- (NSURLSessionDataTask *)fetchPolylineForRoute:(VVRoute *)route withCompletionBlock:(void (^)(MKPolyline *polyline, NSError *error))completionBlock;

- (NSURLSessionDataTask *)fetchVansForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArray *vans, NSError *error))completionBlock;

- (void)fetchArrivalTimesForStop:(VVStop *)stop withCompletionBlock:(void (^)(NSArray *arrivalTimes))completionBlock;
Expand Down
38 changes: 0 additions & 38 deletions Vandy Vans/Vandy Vans/VVSyncromaticsClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#import "VVRoute.h"
#import "VVStop.h"

@import MapKit;

static NSString * const kVVSyncromaticsBaseURLString = @"http://api.syncromatics.com/";

@implementation VVSyncromaticsClient
Expand Down Expand Up @@ -59,42 +57,6 @@ + (NSString *)apiKey {

#pragma mark - Instance Methods

- (NSURLSessionDataTask *)fetchStopsForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArray *stops, NSError *error))completionBlock {
NSString *path = [[[[@"Route" stringByAppendingPathComponent:route.routeID] stringByAppendingPathComponent:@"Direction"] stringByAppendingPathComponent:@"0"] stringByAppendingPathComponent:@"Stops"];

NSURLSessionDataTask *task = [self GET:path
parameters:@{@"api_key": [self.class apiKey]}
success:^(NSURLSessionDataTask *task, id responseObject) {
[self respondSuccessfullyWithTask:task
responseObject:responseObject
andCompletionBlock:completionBlock];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self respondUnsuccessfullyWithTask:task
error:error
andCompletionBlock:completionBlock];
}];

return task;
}

- (NSURLSessionDataTask *)fetchPolylineForRoute:(VVRoute *)route withCompletionBlock:(void (^)(MKPolyline *polyline, NSError *error))completionBlock {
NSString *path = [[@"Route" stringByAppendingPathComponent:route.routeID] stringByAppendingPathComponent:@"Waypoints"];

NSURLSessionDataTask *task = [self GET:path
parameters:@{@"api_key": [self.class apiKey]}
success:^(NSURLSessionDataTask *task, id responseObject) {
[self respondSuccessfullyWithTask:task
responseObject:responseObject
andCompletionBlock:completionBlock];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self respondUnsuccessfullyWithTask:task
error:error
andCompletionBlock:completionBlock];
}];

return task;
}

- (NSURLSessionDataTask *)fetchVansForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArray *vans, NSError *error))completionBlock {
NSString *path = [[@"Route" stringByAppendingPathComponent:route.routeID] stringByAppendingPathComponent:@"Vehicles"];

Expand Down
33 changes: 1 addition & 32 deletions Vandy Vans/Vandy Vans/VVSyncromaticsResponseSerializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#import "VVStop.h"
#import "VVRoute.h"

@import MapKit;

@implementation VVSyncromaticsResponseSerializer

- (id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error {
Expand All @@ -23,36 +21,7 @@ - (id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data er

NSString *lastPathComponent = response.URL.lastPathComponent;

if ([lastPathComponent isEqualToString:@"Waypoints"]) {
NSArray *coordinateResponseArray = responseObject;
NSUInteger coordinateResponseArrayCount = [coordinateResponseArray count];

CLLocationCoordinate2D coordinates[coordinateResponseArrayCount];

for (NSUInteger i = 0; i < coordinateResponseArrayCount; ++i) {
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([coordinateResponseArray[i][@"Latitude"] doubleValue],
[coordinateResponseArray[i][@"Longitude"] doubleValue]);
coordinates[i] = coordinate;
}

responseObject = [MKPolyline polylineWithCoordinates:coordinates
count:coordinateResponseArrayCount];
} else if ([lastPathComponent isEqualToString:@"Stops"]) {
NSArray *stopResponseArray = responseObject;

NSMutableArray *stops = [NSMutableArray arrayWithCapacity:[stopResponseArray count]];

for (NSDictionary *stop in stopResponseArray) {
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
pointAnnotation.title = stop[@"Name"];
pointAnnotation.coordinate = CLLocationCoordinate2DMake([stop[@"Latitude"] doubleValue],
[stop[@"Longitude"] doubleValue]);

[stops addObject:pointAnnotation];
}

responseObject = [stops copy];
} else if ([lastPathComponent isEqualToString:@"Vehicles"]) {
if ([lastPathComponent isEqualToString:@"Vehicles"]) {
NSArray *vehicleResponseArray = responseObject;

NSMutableArray *vans = [NSMutableArray arrayWithCapacity:[vehicleResponseArray count]];
Expand Down
25 changes: 25 additions & 0 deletions Vandy Vans/Vandy Vans/VVVandyVansClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// VVVandyVansClient.h
// Vandy Vans
//
// Created by Seth Friedman on 1/30/14.
// Copyright (c) 2014 VandyMobile. All rights reserved.
//

#import "AFHTTPSessionManager.h"

@class VVRoute;
@class MKPolyline;

@interface VVVandyVansClient : AFHTTPSessionManager

+ (instancetype)sharedClient;

/** This method gives the API key for accessing Syncromatics's Vandy Vans service. */
+ (NSString *)apiKey;

- (NSURLSessionDataTask *)fetchStopsForRoute:(VVRoute *)route withCompletionBlock:(void (^)(NSArray *stops, NSError *error))completionBlock;

- (NSURLSessionDataTask *)fetchPolylineForRoute:(VVRoute *)route withCompletionBlock:(void (^)(MKPolyline *polyline, NSError *error))completionBlock;

@end
Loading

0 comments on commit 735fae6

Please sign in to comment.