Skip to content

Commit

Permalink
Adds mac target
Browse files Browse the repository at this point in the history
Gets project running on OS X
Adds ability for all encoding content types
  • Loading branch information
simonmitchell committed Sep 29, 2015
1 parent 9382fae commit 926b96a
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 4 deletions.
305 changes: 305 additions & 0 deletions ThunderRequest.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions ThunderRequest/NSDictionary+URLEncoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSDictionary+URLEncoding.h
// ThunderRequest
//
// Created by Simon Mitchell on 23/02/2015.
// Copyright (c) 2015 3 SIDED CUBE. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSDictionary (URLEncoding)

- (NSData *)urlEncodedFormData;

- (NSString *)urlEncodedFormString;

@end
43 changes: 43 additions & 0 deletions ThunderRequest/NSDictionary+URLEncoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// NSDictionary+URLEncoding.m
// ThunderRequest
//
// Created by Simon Mitchell on 23/02/2015.
// Copyright (c) 2015 3 SIDED CUBE. All rights reserved.
//

#import "NSDictionary+URLEncoding.h"

@implementation NSDictionary (URLEncoding)

static NSString *toString(id object) {
return [NSString stringWithFormat: @"%@", object];
}

// helper function: get the url encoded string form of any object
static NSString *urlEncode(id object) {
NSString *string = toString(object);
return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

- (NSString *)urlEncodedFormString
{
if (self.allKeys.count == 0) {
return nil;
}

NSMutableArray *parts = [NSMutableArray new];

[self enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
[parts addObject:[NSString stringWithFormat:@"%@=%@",urlEncode(key),urlEncode(obj)]];
}];

return [parts componentsJoinedByString:@"&"];
}

- (NSData *)urlEncodedFormData
{
return [[self urlEncodedFormString] dataUsingEncoding:NSUTF8StringEncoding];
}

@end
136 changes: 133 additions & 3 deletions ThunderRequest/TSCRequest.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#import "TSCRequest.h"
#import <CommonCrypto/CommonDigest.h>
#import "NSDictionary+URLEncoding.h"

#if TARGET_OS_IPHONE
@import UIKit;
#else
#import <AppKit/AppKit.h>
#endif

@implementation TSCRequest

Expand Down Expand Up @@ -27,6 +34,128 @@ - (void)prepareForDispatch
#pragma mark - Body building

- (nullable NSData *)HTTPBodyWithDictionary:(NSDictionary *)dictionary
{
if (dictionary) {

switch (self.contentType) {
case TSCRequestContentTypeJSON:
return [self TSC_JSONDataWithDictionary:dictionary];
break;
case TSCRequestContentTypeFormURLEncoded:
return [dictionary urlEncodedFormData];
break;
case TSCRequestContentTypeMultipartFormData:
return [self TSC_multipartFormDataWithDictionary:dictionary];
break;
case TSCRequestContentTypeXMLPlist:
return [self TSC_plistDataWithDictionary:dictionary];
break;
case TSCRequestContentTypeImageJPEG:
return [self TSC_jpgDataWithDictionary:dictionary];
break;
case TSCRequestContentTypeImagePNG:
return [self TSC_pngDataWithDictionary:dictionary];
break;
default:
break;
}
}

return nil;
}

#pragma mark - PNG Encoding

#if TARGET_OS_IPHONE
- (NSData *)TSC_pngDataWithDictionary:(NSDictionary *)dictionary
{
__block NSData *data;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if ([obj isKindOfClass:[NSData class]]) {

data = obj;
*stop = true;
} else if ([obj isKindOfClass:[UIImage class]]) {

data = UIImagePNGRepresentation(obj);
*stop = true;
}
}];
return data;
}
#else
- (NSData *)TSC_pngDataWithDictionary:(NSDictionary *)dictionary
{
__block NSData *data;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if ([obj isKindOfClass:[NSData class]]) {

data = obj;
*stop = true;
} else if ([obj isKindOfClass:[NSImage class]]) {

// data = UIImagePNGRepresentation(obj);
*stop = true;
}
}];
return data;
}
#endif

#pragma mark - JPEG Encoding

#if TARGET_OS_IPHONE
- (nullable NSData *)TSC_jpgDataWithDictionary:(NSDictionary *)dictionary
{
__block NSData *data;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if ([obj isKindOfClass:[UIImage class]]) {

data = obj;
*stop = true;
} else if ([obj isKindOfClass:[UIImage class]]) {

data = UIImageJPEGRepresentation(obj, 2.0);
*stop = true;
}
}];
return data;
}
#else
- (nullable NSData *)TSC_jpgDataWithDictionary:(NSDictionary *)dictionary
{
__block NSData *data;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if ([obj isKindOfClass:[NSData class]]) {

data = obj;
*stop = true;
} else if ([obj isKindOfClass:[NSImage class]]) {

NSBitmapImageRep *imageRep = [[obj representations] objectAtIndex:0];
data = [imageRep representationUsingType:NSJPEGFileType properties:nil];
*stop = true;
}
}];
return data;
}
#endif

#pragma mark - XML Plist Encoding

- (nullable NSData *)TSC_plistDataWithDictionary:(NSDictionary *)dictionary
{
NSData *data = [NSPropertyListSerialization dataWithPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
return data;
}

#pragma mark - JSON Encoding

- (nullable NSData *)TSC_JSONDataWithDictionary:(NSDictionary *)dictionary
{
if (dictionary) {

Expand All @@ -40,9 +169,7 @@ - (nullable NSData *)HTTPBodyWithDictionary:(NSDictionary *)dictionary
}

return encodedBody;

}

return nil;
}

Expand Down Expand Up @@ -119,7 +246,10 @@ - (nonnull NSString *)TSC_contentTypeStringForContentType:(TSCRequestContentType
return @"image/jpeg";
case TSCRequestContentTypeImagePNG:
return @"image/png";

case TSCRequestContentTypeFormURLEncoded:
return @"application/x-www-form-urlencoded";
case TSCRequestContentTypeXMLPlist:
return @"text/x-xml-plist";
default:
return @"application/json";
break;
Expand Down
5 changes: 5 additions & 0 deletions ThunderRequest/TSCRequestController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@import Foundation;

#if TARGET_OS_IPHONE
@import UIKit;
#else

#endif
#import "TSCRequestDefines.h"

@class TSCRequestResponse;
Expand Down
3 changes: 2 additions & 1 deletion ThunderRequest/TSCRequestDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ typedef NS_ENUM(NSInteger, TSCRequestContentType) {
TSCRequestContentTypeFormURLEncoded = 1,
TSCRequestContentTypeMultipartFormData = 2,
TSCRequestContentTypeImagePNG = 3,
TSCRequestContentTypeImageJPEG = 4
TSCRequestContentTypeImageJPEG = 4,
TSCRequestContentTypeXMLPlist = 5
};

/** The styles available for a `TSCErrorRecoveryOption` */
Expand Down
28 changes: 28 additions & 0 deletions ThunderRequestMac/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.threesidedcube.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015 threesidedcube. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
29 changes: 29 additions & 0 deletions ThunderRequestMac/ThunderRequestMac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ThunderRequestMac.h
// ThunderRequestMac
//
// Created by Simon Mitchell on 29/09/2015.
// Copyright (c) 2015 threesidedcube. All rights reserved.
//

#import <Cocoa/Cocoa.h>

//! Project version number for ThunderRequestMac.
FOUNDATION_EXPORT double ThunderRequestMacVersionNumber;

//! Project version string for ThunderRequestMac.
FOUNDATION_EXPORT const unsigned char ThunderRequestMacVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <ThunderRequestMac/PublicHeader.h>

#import <ThunderRequestMac/TSCRequestController.h>
#import <ThunderRequestMac/TSCRequest.h>
#import <ThunderRequestMac/TSCRequestCredential.h>
#import <ThunderRequestMacTSCRequestResponse.h>
#import <ThunderRequestMac/TSCRequestDefines.h>
#import <ThunderRequestMac/TSCRequestController.h>
#import <ThunderRequestMac/TSCRequest.h>
#import <ThunderRequestMac/TSCRequestResponse.h>
#import <ThunderRequestMac/TSCErrorRecoveryAttempter.h>
#import <ThunderRequestMac/TSCErrorRecoveryOption.h>

24 changes: 24 additions & 0 deletions ThunderRequestMacTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.threesidedcube.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
40 changes: 40 additions & 0 deletions ThunderRequestMacTests/ThunderRequestMacTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ThunderRequestMacTests.m
// ThunderRequestMacTests
//
// Created by Simon Mitchell on 29/09/2015.
// Copyright (c) 2015 threesidedcube. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <XCTest/XCTest.h>

@interface ThunderRequestMacTests : XCTestCase

@end

@implementation ThunderRequestMacTests

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}

@end

0 comments on commit 926b96a

Please sign in to comment.