Skip to content

Commit 61c603e

Browse files
committed
style: move multipart builder back to main target
Since code climate complains of the target + helpers complexity, let's put it back to the target to restore helpers.js rating.
1 parent 710d739 commit 61c603e

2 files changed

Lines changed: 21 additions & 35 deletions

File tree

src/targets/objc/helpers.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,5 @@ 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')
10167
}
10268
}

src/targets/objc/nsurlsession.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,27 @@ module.exports = function (source, options) {
6464
break
6565

6666
case 'multipart/form-data':
67-
code.push(objcHelpers.multipartBody(source, opts))
67+
code.push(objcHelpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
68+
code.push(util.format('NSString *boundary = @"%s";', source.postData.boundary))
69+
code.push(null)
70+
code.push('NSError *error;')
71+
code.push('NSMutableString *body = [NSMutableString string];')
72+
code.push('for (NSDictionary *param in parameters) {')
73+
code.push(indent + '[body appendFormat:@"--%@\\r\\n", boundary];')
74+
code.push(indent + 'if (param[@"fileName"]) {')
75+
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];')
76+
code.push(indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
77+
code.push(indent + indent + '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];')
78+
code.push(indent + indent + 'if (error) {')
79+
code.push(indent + indent + indent + 'NSLog(@"%@", error);')
80+
code.push(indent + indent + '}')
81+
code.push(indent + '} else {')
82+
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];')
83+
code.push(indent + indent + '[body appendFormat:@"%@", param[@"value"]];')
84+
code.push(indent + '}')
85+
code.push('}')
86+
code.push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
87+
code.push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];')
6888
break
6989

7090
default:

0 commit comments

Comments
 (0)