Skip to content

Commit c0a967d

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 044a2cb commit c0a967d

2 files changed

Lines changed: 30 additions & 37 deletions

File tree

‎src/targets/swift/helpers.js‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,5 @@ module.exports = {
7474
default:
7575
return '"' + value.replace(/"/g, '\\"') + '"'
7676
}
77-
},
78-
79-
/**
80-
* By appending multipart parameters one by one in the resulting snippet,
81-
* we make it easier for the user to edit it according to his or her needs after pasting.
82-
* The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
83-
*/
84-
multipartBody: function (source, opts) {
85-
var code = []
86-
var indent = opts.indent
87-
88-
code.push(this.literalDeclaration('parameters', source.postData.params, opts))
89-
code.push(null)
90-
code.push(util.format('let boundary = "%s"', source.postData.boundary))
91-
code.push(null)
92-
code.push('var body = ""')
93-
code.push('var error: NSError? = nil')
94-
code.push('for param in parameters {')
95-
code.push(indent + 'let paramName = param["name"]!')
96-
code.push(indent + 'body += "--\\(boundary)\\r\\n"')
97-
code.push(indent + 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""')
98-
code.push(indent + 'if let filename = param["fileName"] {')
99-
code.push(indent + indent + 'let contentType = param["content-type"]!')
100-
code.push(indent + indent + 'let fileContent = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error)')
101-
code.push(indent + indent + 'if (error != nil) {')
102-
code.push(indent + indent + indent + 'println(error)')
103-
code.push(indent + indent + '}')
104-
code.push(indent + indent + 'body += "; filename=\\"\\(filename)\\"\\r\\n"')
105-
code.push(indent + indent + 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"')
106-
code.push(indent + indent + 'body += fileContent!')
107-
code.push(indent + '} else if let paramValue = param["value"] {')
108-
code.push(indent + indent + 'body += "\\r\\n\\r\\n\\(paramValue)"')
109-
code.push(indent + '}')
110-
code.push('}')
111-
112-
return code.join('\n')
11377
}
11478
}

‎src/targets/swift/nsurlsession.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = function (source, options) {
2828
hasBody: false
2929
}
3030

31+
var indent = opts.indent
32+
3133
// We just want to make sure people understand that is the only dependency
3234
code.push('import Foundation')
3335

@@ -61,7 +63,34 @@ module.exports = function (source, options) {
6163
break
6264

6365
case 'multipart/form-data':
64-
code.push(swiftHelpers.multipartBody(source, opts))
66+
/**
67+
* By appending multipart parameters one by one in the resulting snippet,
68+
* we make it easier for the user to edit it according to his or her needs after pasting.
69+
* The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
70+
*/
71+
code.push(swiftHelpers.literalDeclaration('parameters', source.postData.params, opts))
72+
code.push(null)
73+
code.push(util.format('let boundary = "%s"', source.postData.boundary))
74+
code.push(null)
75+
code.push('var body = ""')
76+
code.push('var error: NSError? = nil')
77+
code.push('for param in parameters {')
78+
code.push(indent + 'let paramName = param["name"]!')
79+
code.push(indent + 'body += "--\\(boundary)\\r\\n"')
80+
code.push(indent + 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""')
81+
code.push(indent + 'if let filename = param["fileName"] {')
82+
code.push(indent + indent + 'let contentType = param["content-type"]!')
83+
code.push(indent + indent + 'let fileContent = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error)')
84+
code.push(indent + indent + 'if (error != nil) {')
85+
code.push(indent + indent + indent + 'println(error)')
86+
code.push(indent + indent + '}')
87+
code.push(indent + indent + 'body += "; filename=\\"\\(filename)\\"\\r\\n"')
88+
code.push(indent + indent + 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"')
89+
code.push(indent + indent + 'body += fileContent!')
90+
code.push(indent + '} else if let paramValue = param["value"] {')
91+
code.push(indent + indent + 'body += "\\r\\n\\r\\n\\(paramValue)"')
92+
code.push(indent + '}')
93+
code.push('}')
6594
break
6695

6796
default:

0 commit comments

Comments
 (0)