Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f45cbea066
Fetching contributors…

Cannot retrieve contributors at this time

file 51 lines (44 sloc) 1.745 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//
// AEURLResponseProcessors.m
// Turntable
//
// Created by Adam Ernst on 11/10/11.
// Copyright (c) 2011 cosmicsoft. All rights reserved.
//

#import "AEURLResponseProcessors.h"

NSString *AEURLResponseProcessorsErrorDomain = @"AEURLResponseProcessorsErrorDomain";

@implementation AEURLResponseProcessors

+ (AEURLResponseProcessor)imageResponseProcessor {
    return [[^id(NSURLResponse *response, id data, NSError **error) {
        UIImage *image = [[[UIImage alloc] initWithData:data] autorelease];
        if (!image) {
            if (error) {
                *error = [NSError errorWithDomain:AEURLResponseProcessorsErrorDomain
                                             code:AEURLResponseProcessorsErrorImageDecodingFailed
                                         userInfo:nil];
            }
            return nil;
        }
        return image;
    } copy] autorelease];
}

+ (AEURLResponseProcessor)chainedResponseProcessor:(AEURLResponseProcessor)firstProcessor, ... {
    NSMutableArray *processors = [NSMutableArray array];
    va_list args;
    va_start(args, firstProcessor);
    for (AEURLResponseProcessor processor = firstProcessor; processor != nil; processor = va_arg(args, AEURLResponseProcessor)) {
        [processors addObject:[[processor copy] autorelease]];
    }
    
    return [[^id(NSURLResponse *response, id data, NSError **error) {
        id newData = data;
        for (AEURLResponseProcessor processor in processors) {
            newData = processor(response, newData, error);
            if (*error) {
                NSAssert(newData == nil, @"Expected data or error but not both");
                return nil;
            }
        }
        return newData;
    } copy] autorelease];
}

@end
Something went wrong with that request. Please try again.