Skip to content

Instantly share code, notes, and snippets.

@Brayden
Last active December 18, 2015 10:39

Revisions

  1. Brayden revised this gist Jun 30, 2013. 2 changed files with 4 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions BBStandardAPI.h
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ typedef NS_ENUM(NSUInteger, kAPIEndpoint) {
    kEndpointSessionStatus, // /session/status GET / / /
    kEndpointAccountNew, // /account/new / POST / /
    kEndpointAccountOrders, // /account/orders GET / / /
    kEndpointAccountOrderHistory, // /account/orders/{id} GET / / /
    kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE
    kEndpointShoprunnerNew, // /shoprunner/new / POST / /
    kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE
    @@ -38,6 +39,7 @@ typedef NS_ENUM(NSUInteger, kAPIEndpoint) {
    kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / /
    kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE
    kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE
    kEndpointAccountCredits, // /account/credits GET / / /
    };

    @interface BBStandardAPI : NSObject
    2 changes: 2 additions & 0 deletions BBStandardAPI.m
    Original file line number Diff line number Diff line change
    @@ -77,6 +77,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    else if (endpoint == kEndpointSessionStatus) return @"/session/status";
    else if (endpoint == kEndpointAccountNew) return @"/account/new";
    else if (endpoint == kEndpointAccountOrders) return @"/account/orders";
    else if (endpoint == kEndpointAccountOrderHistory) return @"/account/order/%@";
    else if (endpoint == kEndpointAccountWishlist) return @"/account/wishlist";
    else if (endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new";
    else if (endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy";
    @@ -87,6 +88,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    else if (endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard";
    else if (endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing";
    else if (endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping";
    else if (endpoint == kEndpointAccountCredits) return @"/account/credits";

    return @"";
    }
  2. Brayden revised this gist Jun 28, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions BBStandardAPI.h
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ typedef NS_ENUM(NSUInteger, kAPIMethod) {
    DELETE
    };

    typedef enum {
    typedef NS_ENUM(NSUInteger, kAPIEndpoint) {
    kEndpointCustom, // {path} GET / POST / PUT / DELETE
    kEndpointCategories, // /categories GET / / /
    kEndpointSubcategory, // /categories/{id} GET / / /
    @@ -38,7 +38,7 @@ typedef enum {
    kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / /
    kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE
    kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE
    } kAPIEndpoint;
    };

    @interface BBStandardAPI : NSObject

  3. Brayden revised this gist Jun 28, 2013. 3 changed files with 185 additions and 177 deletions.
    52 changes: 52 additions & 0 deletions BBStandardAPI.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    @class MBProgressHUD;

    typedef NS_ENUM(NSUInteger, kAPIMethod) {
    GET,
    POST,
    PUT,
    DELETE
    };

    typedef enum {
    kEndpointCustom, // {path} GET / POST / PUT / DELETE
    kEndpointCategories, // /categories GET / / /
    kEndpointSubcategory, // /categories/{id} GET / / /
    kEndpointCategoryProducts, // /categories/{id}/products GET / / /
    kEndpointProduct, // /products/{id} GET / / /
    kEndpointProductSearch, // /products{?q} GET / / /
    kEndpointCart, // /checkout/cart GET / POST / PUT / DELETE
    kEndpointPromo, // /checkout/promo / POST / /
    kEndpointCheckoutPaypal, // /checkout/paypal / POST / /
    kEndpointCheckoutGoogle, // /checkout/google / POST / /
    kEndpointCheckoutCC, // /checkout/cc / POST / /
    kEndpointCheckoutConfirm, // /checkout/confirm GET / POST / /
    kEndpointCheckoutReceipt, // /checkout/receipt GET / / /
    kEndpointCheckoutShipping, // /checkout/shipping_methods GET / / PUT /
    kEndpointCheckoutDestinations, // /checkout/destinations GET / / PUT /
    kEndpointSessionNew, // /session/new / POST / /
    kEndpointSessionDestroy, // /session/destroy / / / DELETE
    kEndpointSessionStatus, // /session/status GET / / /
    kEndpointAccountNew, // /account/new / POST / /
    kEndpointAccountOrders, // /account/orders GET / / /
    kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE
    kEndpointShoprunnerNew, // /shoprunner/new / POST / /
    kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE
    kEndpointAccountLoyalty, // /account/loyalty/show GET / / /
    kEndpointAccountLoyaltyStatements, // /account/loyalty/statements GET / / /
    kEndpointAccountLoyaltyTransaction, // /account/loyalty/transactions GET / / /
    kEndpointGiftcards, // /giftcards GET / / /
    kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / /
    kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE
    kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE
    } kAPIEndpoint;

    @interface BBStandardAPI : NSObject

    @property (nonatomic, strong) NSOperationQueue *queue;
    @property (nonatomic, strong) NSString *baseHost;
    @property (nonatomic, strong) NSArray *categories;

    + (BBStandardAPI *)sharedManager;
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion;

    @end
    133 changes: 133 additions & 0 deletions BBStandardAPI.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    #import "BBStandardAPI.h"
    #import "MBProgressHUD.h"

    @implementation BBStandardAPI


    #pragma mark - Singleton -

    + (BBStandardAPI *)sharedManager {
    static BBStandardAPI *sharedManager;
    static dispatch_once_t token = 0;

    dispatch_once(&token, ^{
    sharedManager = [[BBStandardAPI alloc] init];
    });

    return sharedManager;
    }

    - (id)init {
    if (self = [super init]) {
    self.queue = [[NSOperationQueue alloc] init];
    self.queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
    self.queue.name = @"com.brandingbrand.api";
    }

    return self;
    }



    #pragma mark - Wrapper Methods -

    + (NSMutableURLRequest *)newRequestWithMethod:(kAPIMethod)method endpoint:(NSString *)endpoint andOptions:(NSDictionary *)options {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [[BBStandardAPI sharedManager] baseHost], endpoint]]];

    if (method == GET) [request setHTTPMethod:@"GET"];
    else if (method == POST) [request setHTTPMethod:@"POST"];
    else if (method == PUT) [request setHTTPMethod:@"PUT"];
    else if (method == DELETE) [request setHTTPMethod:@"DELETE"];

    if (options) {
    NSError *err;
    NSData *data = [NSJSONSerialization dataWithJSONObject:options options:NSJSONWritingPrettyPrinted error:&err];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:data];
    } else {
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    }

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setTimeoutInterval:20.0f];

    return request;
    }

    + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    if (endpoint == kEndpointCustom) return @"%@";
    else if (endpoint == kEndpointCategories) return @"/categories";
    else if (endpoint == kEndpointSubcategory) return @"/categories/%@";
    else if (endpoint == kEndpointCategoryProducts) return @"/categories/%@/products";
    else if (endpoint == kEndpointProduct) return @"/products/%@";
    else if (endpoint == kEndpointProductSearch) return @"/products?%@";
    else if (endpoint == kEndpointCart) return @"/checkout/cart";
    else if (endpoint == kEndpointPromo) return @"/checkout/promo";
    else if (endpoint == kEndpointCheckoutPaypal) return @"/checkout/paypal";
    else if (endpoint == kEndpointCheckoutGoogle) return @"/checkout/google";
    else if (endpoint == kEndpointCheckoutCC) return @"/checkout/cc";
    else if (endpoint == kEndpointCheckoutConfirm) return @"/checkout/confirm";
    else if (endpoint == kEndpointCheckoutReceipt) return @"/checkout/receipt";
    else if (endpoint == kEndpointCheckoutShipping) return @"/checkout/shipping_methods";
    else if (endpoint == kEndpointCheckoutDestinations) return @"/checkout/destinations";
    else if (endpoint == kEndpointSessionNew) return @"/session/new";
    else if (endpoint == kEndpointSessionDestroy) return @"/session/destroy";
    else if (endpoint == kEndpointSessionStatus) return @"/session/status";
    else if (endpoint == kEndpointAccountNew) return @"/account/new";
    else if (endpoint == kEndpointAccountOrders) return @"/account/orders";
    else if (endpoint == kEndpointAccountWishlist) return @"/account/wishlist";
    else if (endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new";
    else if (endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy";
    else if (endpoint == kEndpointAccountLoyalty) return @"/account/loyalty/show";
    else if (endpoint == kEndpointAccountLoyaltyStatements) return @"/account/loyalty/statements";
    else if (endpoint == kEndpointAccountLoyaltyTransaction) return @"/account/loyalty/transactions";
    else if (endpoint == kEndpointGiftcards) return @"/giftcards";
    else if (endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard";
    else if (endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing";
    else if (endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping";

    return @"";
    }

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion {
    NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]];
    NSArray *parts = [parameter componentsSeparatedByString:@"%@"];
    if (parts.count > 1) {
    parameter = [@"" mutableCopy];
    for (int index = 0; index < parts.count - 1; index++) {
    [parameter appendFormat:@"%@%@", parts[index], params[index]];
    }

    [parameter appendFormat:@"%@", parts[parts.count - 1]];
    }

    UIView *view = [[UIApplication sharedApplication] keyWindow];
    if (view) {
    [MBProgressHUD showHUDAddedTo:view animated:YES];
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [NSURLConnection sendAsynchronousRequest:[BBStandardAPI newRequestWithMethod:method endpoint:parameter andOptions:options] queue:[[BBStandardAPI sharedManager] queue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (data != nil) {
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
    dispatch_async(dispatch_get_main_queue(), ^{
    completion(result, nil, [(NSHTTPURLResponse*)response statusCode]);
    });
    } else {
    NSLog(@"-- BBStandardAPI -- Error: %@", error.description);
    }

    dispatch_async(dispatch_get_main_queue(), ^{
    if (view != nil) {
    [MBProgressHUD hideHUDForView:view animated:YES];
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });
    }];
    }

    @end
    177 changes: 0 additions & 177 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,177 +0,0 @@
    #import <Foundation/Foundation.h>

    typedef enum {
    GET,
    POST,
    PUT,
    DELETE
    } kAPIMethod;

    typedef enum {
    kEndpointCategories, // /categories GET / / /
    kEndpointSubcategory, // /categories/{id} GET / / /
    kEndpointCategoryProducts, // /categories/{id}/products GET / / /
    kEndpointProduct, // /products/{id} GET / / /
    kEndpointProductSearch, // /products{?q} GET / / /
    kEndpointCart, // /checkout/cart GET / POST / PUT / DELETE
    kEndpointPromo, // /checkout/promo / POST / /
    kEndpointCheckoutPaypal, // /checkout/paypal / POST / /
    kEndpointCheckoutGoogle, // /checkout/google / POST / /
    kEndpointCheckoutCC, // /checkout/cc / POST / /
    kEndpointCheckoutConfirm, // /checkout/confirm GET / POST / /
    kEndpointCheckoutReceipt, // /checkout/receipt GET / / /
    kEndpointCheckoutShipping, // /checkout/shipping_methods GET / / PUT /
    kEndpointCheckoutDestinations, // /checkout/destinations GET / / PUT /
    kEndpointSessionNew, // /session/new / POST / /
    kEndpointSessionDestroy, // /session/destroy / / / DELETE
    kEndpointSessionStatus, // /session/status GET / / /
    kEndpointAccountNew, // /account/new / POST / /
    kEndpointAccountOrders, // /account/orders GET / / /
    kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE
    kEndpointShoprunnerNew, // /shoprunner/new / POST / /
    kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE
    kEndpointAccountLoyalty, // /account/loyalty/show GET / / /
    kEndpointAccountLoyaltyStatements, // /account/loyalty/statements GET / / /
    kEndpointAccountLoyaltyTransaction, // /account/loyalty/transactions GET / / /
    kEndpointGiftcards, // /giftcards GET / / /
    kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / /
    kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE
    kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE
    } kAPIEndpoint;

    @interface BBStandardAPI : NSObject

    + (BBStandardAPI *)sharedManager;

    @property (nonatomic, strong) NSOperationQueue *queue;
    @property (nonatomic, strong) NSString *baseHost;
    @property (nonatomic, strong) NSArray *categories;

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion;

    @end









    #import "BBStandardAPI.h"

    @implementation BBStandardAPI


    #pragma mark - Singleton -

    + (BBStandardAPI *)sharedManager {
    static BBStandardAPI *sharedManager;
    static dispatch_once_t token = 0;

    dispatch_once(&token, ^{
    sharedManager = [[BBStandardAPI alloc] init];
    });

    return sharedManager;
    }

    - (id)init {
    if (self = [super init]) {
    self.queue = [[NSOperationQueue alloc] init];
    self.queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
    self.queue.name = @"com.brandingbrand.api";
    }

    return self;
    }



    #pragma mark - Wrapper Methods -

    + (NSMutableURLRequest *)newRequestWithMethod:(kAPIMethod)method endpoint:(NSString *)endpoint andOptions:(NSDictionary *)options {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [[BBStandardAPI sharedManager] baseHost], endpoint]]];

    if(method == GET) [request setHTTPMethod:@"GET"];
    else if(method == POST) [request setHTTPMethod:@"POST"];
    else if(method == PUT) [request setHTTPMethod:@"PUT"];
    else if(method == DELETE) [request setHTTPMethod:@"DELETE"];

    if(options) {
    NSError *err;
    NSData *data = [NSJSONSerialization dataWithJSONObject:options options:NSJSONWritingPrettyPrinted error:&err];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:data];
    } else {
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    }

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setTimeoutInterval:20.0f];

    return request;
    }

    + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    if(endpoint == kEndpointCategories) return @"/categories";
    else if(endpoint == kEndpointSubcategory) return @"/categories/%@";
    else if(endpoint == kEndpointCategoryProducts) return @"/categories/%@/products";
    else if(endpoint == kEndpointProduct) return @"/products/%@";
    else if(endpoint == kEndpointProductSearch) return @"/products?%@";
    else if(endpoint == kEndpointCart) return @"/checkout/cart";
    else if(endpoint == kEndpointPromo) return @"/checkout/promo";
    else if(endpoint == kEndpointCheckoutPaypal) return @"/checkout/paypal";
    else if(endpoint == kEndpointCheckoutGoogle) return @"/checkout/google";
    else if(endpoint == kEndpointCheckoutCC) return @"/checkout/cc";
    else if(endpoint == kEndpointCheckoutConfirm) return @"/checkout/confirm";
    else if(endpoint == kEndpointCheckoutReceipt) return @"/checkout/receipt";
    else if(endpoint == kEndpointCheckoutShipping) return @"/checkout/shipping_methods";
    else if(endpoint == kEndpointCheckoutDestinations) return @"/checkout/destinations";
    else if(endpoint == kEndpointSessionNew) return @"/session/new";
    else if(endpoint == kEndpointSessionDestroy) return @"/session/destroy";
    else if(endpoint == kEndpointSessionStatus) return @"/session/status";
    else if(endpoint == kEndpointAccountNew) return @"/account/new";
    else if(endpoint == kEndpointAccountOrders) return @"/account/orders";
    else if(endpoint == kEndpointAccountWishlist) return @"/account/wishlist";
    else if(endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new";
    else if(endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy";
    else if(endpoint == kEndpointAccountLoyalty) return @"/account/loyalty/show";
    else if(endpoint == kEndpointAccountLoyaltyStatements) return @"/account/loyalty/statements";
    else if(endpoint == kEndpointAccountLoyaltyTransaction) return @"/account/loyalty/transactions";
    else if(endpoint == kEndpointGiftcards) return @"/giftcards";
    else if(endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard";
    else if(endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing";
    else if(endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping";

    return @"";
    }

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion {
    NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]];
    NSArray *parts = [parameter componentsSeparatedByString:@"%@"];
    if(parts.count > 1) {
    parameter = [@"" mutableCopy];
    for(int index = 0; index < parts.count - 1; index++) {
    [parameter appendFormat:@"%@%@", parts[index], params[index]];
    }

    [parameter appendFormat:@"%@", parts[parts.count - 1]];
    }

    [NSURLConnection sendAsynchronousRequest:[BBStandardAPI newRequestWithMethod:method endpoint:parameter andOptions:options] queue:[[BBStandardAPI sharedManager] queue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if(data != nil) {
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
    dispatch_async(dispatch_get_main_queue(), ^{
    completion(result, nil, [(NSHTTPURLResponse*)response statusCode]);
    });
    } else
    NSLog(@"-- BBStandardAPI -- Error: %@", error.description);
    }];
    }

    @end
  4. Brayden revised this gist Jun 20, 2013. No changes.
  5. Brayden revised this gist Jun 20, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ + (BBStandardAPI *)sharedManager;
    @property (nonatomic, strong) NSString *baseHost;
    @property (nonatomic, strong) NSArray *categories;

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion;
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion;

    @end

    @@ -150,7 +150,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    return @"";
    }

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion {
    NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]];
    NSArray *parts = [parameter componentsSeparatedByString:@"%@"];
    if(parts.count > 1) {
    @@ -167,7 +167,7 @@ + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method op
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
    dispatch_async(dispatch_get_main_queue(), ^{
    completion(result, nil);
    completion(result, nil, [(NSHTTPURLResponse*)response statusCode]);
    });
    } else
    NSLog(@"-- BBStandardAPI -- Error: %@", error.description);
  6. Brayden revised this gist Jun 14, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ + (BBStandardAPI *)sharedManager;
    @property (nonatomic, strong) NSString *baseHost;
    @property (nonatomic, strong) NSArray *categories;

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion;
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion;

    @end

    @@ -150,7 +150,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    return @"";
    }

    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]];
    NSArray *parts = [parameter componentsSeparatedByString:@"%@"];
    if(parts.count > 1) {
  7. Brayden revised this gist Jun 14, 2013. 1 changed file with 137 additions and 47 deletions.
    184 changes: 137 additions & 47 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,53 @@
    @interface APIWrapper : NSObject

    #import <Foundation/Foundation.h>

    typedef enum {
    GET,
    POST,
    PUT,
    DELETE
    } kAPIMethod;

    typedef enum {
    kEndpointCategories, // /categories GET / / /
    kEndpointSubcategory, // /categories/{id} GET / / /
    kEndpointCategoryProducts, // /categories/{id}/products GET / / /
    kEndpointProduct, // /products/{id} GET / / /
    kEndpointProductSearch, // /products{?q} GET / / /
    kEndpointCart, // /checkout/cart GET / POST / PUT / DELETE
    kEndpointPromo, // /checkout/promo / POST / /
    kEndpointCheckoutPaypal, // /checkout/paypal / POST / /
    kEndpointCheckoutGoogle, // /checkout/google / POST / /
    kEndpointCheckoutCC, // /checkout/cc / POST / /
    kEndpointCheckoutConfirm, // /checkout/confirm GET / POST / /
    kEndpointCheckoutReceipt, // /checkout/receipt GET / / /
    kEndpointCheckoutShipping, // /checkout/shipping_methods GET / / PUT /
    kEndpointCheckoutDestinations, // /checkout/destinations GET / / PUT /
    kEndpointSessionNew, // /session/new / POST / /
    kEndpointSessionDestroy, // /session/destroy / / / DELETE
    kEndpointSessionStatus, // /session/status GET / / /
    kEndpointAccountNew, // /account/new / POST / /
    kEndpointAccountOrders, // /account/orders GET / / /
    kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE
    kEndpointShoprunnerNew, // /shoprunner/new / POST / /
    kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE
    kEndpointAccountLoyalty, // /account/loyalty/show GET / / /
    kEndpointAccountLoyaltyStatements, // /account/loyalty/statements GET / / /
    kEndpointAccountLoyaltyTransaction, // /account/loyalty/transactions GET / / /
    kEndpointGiftcards, // /giftcards GET / / /
    kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / /
    kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE
    kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE
    } kAPIEndpoint;

    @interface BBStandardAPI : NSObject

    + (BBStandardAPI *)sharedManager;

    @property (nonatomic, strong) NSOperationQueue *queue;
    @property (nonatomic, strong) NSString *baseHost;
    @property (nonatomic, strong) NSArray *categories;
    @property (nonatomic, assign) NSInteger loadingCount;

    + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion;
    + (APIWrapper *)sharedManager;
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion;

    @end

    @@ -16,72 +59,119 @@ + (APIWrapper *)sharedManager;



    #import "APIWrapper.h"

    @implementation APIWrapper

    + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
    [queue setName:@"com.brandingbrand.api"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", BASE_URL, endpoint]]];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [request setTimeoutInterval:15.0f];

    [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] + 1];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *resposne, NSData *data, NSError *error) {
    if(data != nil) {
    NSError *jsonError;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];

    dispatch_async(dispatch_get_main_queue(), ^{
    completion(dictionary, nil);
    });
    } else {
    NSLog(@"API Error: %@", error.description);
    }

    [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] - 1];
    }];
    }
    #import "BBStandardAPI.h"

    @implementation BBStandardAPI


    #pragma mark - Singleton -

    #pragma mark - Singleton Setup -

    + (APIWrapper *)sharedManager {
    static APIWrapper *sharedManager;
    + (BBStandardAPI *)sharedManager {
    static BBStandardAPI *sharedManager;
    static dispatch_once_t token = 0;

    dispatch_once(&token, ^{
    sharedManager = [[APIWrapper alloc] init];
    sharedManager = [[BBStandardAPI alloc] init];
    });

    return sharedManager;
    }

    - (id)init {
    if (self = [super init]) {
    self.categories = [[NSArray alloc] init];
    self.queue = [[NSOperationQueue alloc] init];
    self.queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
    self.queue.name = @"com.brandingbrand.api";
    }

    return self;
    }



    #pragma mark - Wrapper Methods -

    #pragma mark - Setter Methods -
    + (NSMutableURLRequest *)newRequestWithMethod:(kAPIMethod)method endpoint:(NSString *)endpoint andOptions:(NSDictionary *)options {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [[BBStandardAPI sharedManager] baseHost], endpoint]]];

    if(method == GET) [request setHTTPMethod:@"GET"];
    else if(method == POST) [request setHTTPMethod:@"POST"];
    else if(method == PUT) [request setHTTPMethod:@"PUT"];
    else if(method == DELETE) [request setHTTPMethod:@"DELETE"];

    if(options) {
    NSError *err;
    NSData *data = [NSJSONSerialization dataWithJSONObject:options options:NSJSONWritingPrettyPrinted error:&err];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:data];
    } else {
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    }

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setTimeoutInterval:20.0f];

    return request;
    }

    + (NSString *)getEndpoint:(kAPIEndpoint)endpoint {
    if(endpoint == kEndpointCategories) return @"/categories";
    else if(endpoint == kEndpointSubcategory) return @"/categories/%@";
    else if(endpoint == kEndpointCategoryProducts) return @"/categories/%@/products";
    else if(endpoint == kEndpointProduct) return @"/products/%@";
    else if(endpoint == kEndpointProductSearch) return @"/products?%@";
    else if(endpoint == kEndpointCart) return @"/checkout/cart";
    else if(endpoint == kEndpointPromo) return @"/checkout/promo";
    else if(endpoint == kEndpointCheckoutPaypal) return @"/checkout/paypal";
    else if(endpoint == kEndpointCheckoutGoogle) return @"/checkout/google";
    else if(endpoint == kEndpointCheckoutCC) return @"/checkout/cc";
    else if(endpoint == kEndpointCheckoutConfirm) return @"/checkout/confirm";
    else if(endpoint == kEndpointCheckoutReceipt) return @"/checkout/receipt";
    else if(endpoint == kEndpointCheckoutShipping) return @"/checkout/shipping_methods";
    else if(endpoint == kEndpointCheckoutDestinations) return @"/checkout/destinations";
    else if(endpoint == kEndpointSessionNew) return @"/session/new";
    else if(endpoint == kEndpointSessionDestroy) return @"/session/destroy";
    else if(endpoint == kEndpointSessionStatus) return @"/session/status";
    else if(endpoint == kEndpointAccountNew) return @"/account/new";
    else if(endpoint == kEndpointAccountOrders) return @"/account/orders";
    else if(endpoint == kEndpointAccountWishlist) return @"/account/wishlist";
    else if(endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new";
    else if(endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy";
    else if(endpoint == kEndpointAccountLoyalty) return @"/account/loyalty/show";
    else if(endpoint == kEndpointAccountLoyaltyStatements) return @"/account/loyalty/statements";
    else if(endpoint == kEndpointAccountLoyaltyTransaction) return @"/account/loyalty/transactions";
    else if(endpoint == kEndpointGiftcards) return @"/giftcards";
    else if(endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard";
    else if(endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing";
    else if(endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping";

    return @"";
    }

    - (void)setLoadingCount:(NSInteger)loadingCount {
    _loadingCount = loadingCount;
    + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]];
    NSArray *parts = [parameter componentsSeparatedByString:@"%@"];
    if(parts.count > 1) {
    parameter = [@"" mutableCopy];
    for(int index = 0; index < parts.count - 1; index++) {
    [parameter appendFormat:@"%@%@", parts[index], params[index]];
    }

    [parameter appendFormat:@"%@", parts[parts.count - 1]];
    }

    if(loadingCount > 0)
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    else
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [NSURLConnection sendAsynchronousRequest:[BBStandardAPI newRequestWithMethod:method endpoint:parameter andOptions:options] queue:[[BBStandardAPI sharedManager] queue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if(data != nil) {
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
    dispatch_async(dispatch_get_main_queue(), ^{
    completion(result, nil);
    });
    } else
    NSLog(@"-- BBStandardAPI -- Error: %@", error.description);
    }];
    }

    @end
  8. Brayden created this gist Jun 12, 2013.
    87 changes: 87 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    @interface APIWrapper : NSObject

    @property (nonatomic, strong) NSArray *categories;
    @property (nonatomic, assign) NSInteger loadingCount;

    + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion;
    + (APIWrapper *)sharedManager;

    @end









    #import "APIWrapper.h"

    @implementation APIWrapper

    + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
    [queue setName:@"com.brandingbrand.api"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", BASE_URL, endpoint]]];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [request setTimeoutInterval:15.0f];

    [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] + 1];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *resposne, NSData *data, NSError *error) {
    if(data != nil) {
    NSError *jsonError;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];

    dispatch_async(dispatch_get_main_queue(), ^{
    completion(dictionary, nil);
    });
    } else {
    NSLog(@"API Error: %@", error.description);
    }

    [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] - 1];
    }];
    }




    #pragma mark - Singleton Setup -

    + (APIWrapper *)sharedManager {
    static APIWrapper *sharedManager;
    static dispatch_once_t token = 0;

    dispatch_once(&token, ^{
    sharedManager = [[APIWrapper alloc] init];
    });

    return sharedManager;
    }

    - (id)init {
    if (self = [super init]) {
    self.categories = [[NSArray alloc] init];
    }

    return self;
    }




    #pragma mark - Setter Methods -

    - (void)setLoadingCount:(NSInteger)loadingCount {
    _loadingCount = loadingCount;

    if(loadingCount > 0)
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    else
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }

    @end