Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Refactored server to use AFNetworking internally
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan committed Mar 9, 2016
1 parent 5d75dbb commit a78b000
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@

#import "Kiwi.h"

#import <AFNetworking/AFNetworking.h>
#import <AFNetworking/AFNetworkActivityIndicatorManager.h>
#import <HNKGooglePlacesAutocomplete/HNKGooglePlacesServer.h>
#import <HNKServerFacade/HNKServer.h>

SPEC_BEGIN(HNKGooglePlacesServerSpec)

describe(@"HNKGooglePlacesServer", ^{

describe(@"Method: initialize",
^{
__block id mockServer;
__block id mockHTTPSessionManager;

beforeEach(^{

mockServer = [HNKServer nullMock];
mockHTTPSessionManager = [AFHTTPSessionManager nullMock];

[HNKServer stub:@selector(alloc) andReturn:mockServer];
[AFHTTPSessionManager stub:@selector(alloc) andReturn:mockHTTPSessionManager];

});

it(@"Should setup HNKServer",
it(@"Should setup AFHTTPSessionManager",
^{
[[mockServer should] receive:@selector(initWithBaseURL:) withArguments:@"https://maps.googleapis.com/maps/api/place/"];
[[mockHTTPSessionManager should] receive:@selector(initWithBaseURL:) withArguments:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/"]];

[HNKGooglePlacesServer initialize];
});
Expand Down
43 changes: 24 additions & 19 deletions Pod/Classes/HNKGooglePlacesServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,50 @@
// THE SOFTWARE.
//

#import <AFNetworking/AFNetworking.h>
#import <AFNetworking/AFNetworkActivityIndicatorManager.h>
#import "HNKGooglePlacesServer.h"
#import "HNKServer.h"

static NSString *const kHNKGooglePlacesServerBaseURL = @"https://maps.googleapis.com/maps/api/place/";

@implementation HNKGooglePlacesServer

#pragma mark - Overrides

static HNKServer *server = nil;
static AFHTTPSessionManager *httpSessionManager = nil;

+ (void)initialize
{
if (self == [HNKGooglePlacesServer class]) {

server = [[HNKServer alloc] initWithBaseURL:kHNKGooglePlacesServerBaseURL];
httpSessionManager = [[AFHTTPSessionManager alloc]
initWithBaseURL:[NSURL URLWithString:kHNKGooglePlacesServerBaseURL]];

httpSessionManager.responseSerializer.acceptableContentTypes =
[NSSet setWithObject:@"application/json"];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
}
}

#pragma mark - Requests

+ (void)GET:(NSString *)path parameters:(NSDictionary *)parameters completion:(void (^)(id, NSError *))completion
{
[server GET:path
parameters:parameters
completion:^(id responseObject, NSError *error) {

if (completion) {

if (error) {

completion(nil, error);
return;
}

completion(responseObject, nil);
}

}];
NSString *urlString = [kHNKGooglePlacesServerBaseURL stringByAppendingPathComponent:path];

[httpSessionManager GET:urlString
parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject) {
if (completion) {
completion(responseObject, nil);
}
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
if (completion) {
completion(nil, error);
}
}];
}

@end

0 comments on commit a78b000

Please sign in to comment.