@@ -23,16 +23,16 @@ module.exports = {
2323 * @return {string } A valid Objective-C declaration and initialization of an Objective-C object litteral.
2424 *
2525 * @example
26- * nsDeclarationBuilder ('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
26+ * nsDeclaration ('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
2727 * // returns:
2828 * NSDictionary *params = @{ @"a": @"b",
2929 * @"c": @"d" };
3030 *
31- * nsDeclarationBuilder ('NSDictionary', 'params', {a: 'b', c: 'd'})
31+ * nsDeclaration ('NSDictionary', 'params', {a: 'b', c: 'd'})
3232 * // returns:
3333 * NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
3434 */
35- nsDeclarationBuilder : function ( nsClass , name , parameters , indent ) {
35+ nsDeclaration : function ( nsClass , name , parameters , indent ) {
3636 var opening = nsClass + ' *' + name + ' = '
3737 var literal = this . literalRepresentation ( parameters , indent ? opening . length : undefined )
3838 return opening + literal + ';'
@@ -64,5 +64,39 @@ module.exports = {
6464 default :
6565 return '@"' + value . replace ( / " / g, '\\"' ) + '"'
6666 }
67+ } ,
68+
69+ /**
70+ * By appending multipart parameters one by one in the resulting snippet,
71+ * we make it easier for the user to edit it according to his or her needs after pasting.
72+ * The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
73+ */
74+ multipartBody : function ( source , opts ) {
75+ var code = [ ]
76+ var indent = opts . indent
77+
78+ code . push ( this . nsDeclaration ( 'NSArray' , 'parameters' , source . postData . params , opts . pretty ) )
79+ code . push ( util . format ( 'NSString *boundary = @"%s";' , source . postData . boundary ) )
80+ code . push ( null )
81+ code . push ( 'NSError *error;' )
82+ code . push ( 'NSMutableString *body = [NSMutableString string];' )
83+ code . push ( 'for (NSDictionary *param in parameters) {' )
84+ code . push ( indent + '[body appendFormat:@"--%@\\r\\n", boundary];' )
85+ code . push ( indent + 'if (param[@"fileName"]) {' )
86+ code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];' )
87+ code . push ( indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];' )
88+ code . push ( indent + indent + '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];' )
89+ code . push ( indent + indent + 'if (error) {' )
90+ code . push ( indent + indent + indent + 'NSLog(@"%@", error);' )
91+ code . push ( indent + indent + '}' )
92+ code . push ( indent + '} else {' )
93+ code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];' )
94+ code . push ( indent + indent + '[body appendFormat:@"%@", param[@"value"]];' )
95+ code . push ( indent + '}' )
96+ code . push ( '}' )
97+ code . push ( '[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];' )
98+ code . push ( 'NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];' )
99+
100+ return code . join ( '\n' )
67101 }
68102}
0 commit comments