-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObjCNSURLSessionCodeGenerator.coffee
135 lines (116 loc) · 4.21 KB
/
ObjCNSURLSessionCodeGenerator.coffee
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# in API v0.2.0 and below (Paw 2.2.2 and below), require had no return value
((root) ->
if root.bundle?.minApiVersion('0.2.0')
root.URI = require("./URI")
root.Mustache = require("./mustache")
else
require("URI.min.js")
require("mustache.js")
)(this)
addslashes = (str) ->
("#{str}").replace(/[\\"]/g, '\\$&')
ObjCNSURLSessionCodeGenerator = ->
@url = (request) ->
url_params_object = (() ->
_uri = URI request.url
_uri.search true
)()
url_params = ({
"name": addslashes name
"value": addslashes value
} for name, value of url_params_object)
return {
"base": addslashes (() ->
_uri = URI request.url
_uri.search("")
_uri
)()
"params": url_params
"has_params": url_params.length > 0
}
@headers = (request) ->
headers = request.headers
return {
"has_headers": Object.keys(headers).length > 0
"header_list": ({
"header_name": addslashes header_name
"header_value": addslashes header_value
} for header_name, header_value of headers)
}
@body = (request) ->
json_body = request.jsonBody
if json_body
return {
"has_json_body":true
"json_body_object":@json_body_object json_body, 1
}
url_encoded_body = request.urlEncodedBody
if url_encoded_body
return {
"has_url_encoded_body":true
"url_encoded_body": ({
"name": addslashes name
"value": addslashes value
} for name, value of url_encoded_body)
}
raw_body = request.body
if raw_body
if raw_body.length < 5000
return {
"has_raw_body":true
"raw_body": addslashes raw_body
}
else
return {
"has_long_body":true
}
@json_body_object = (object, indent = 0) ->
if object == null
s = "[NSNull null]"
else if typeof(object) == 'string'
s = "@\"#{addslashes object}\""
else if typeof(object) == 'number'
s = "@#{object}"
else if typeof(object) == 'boolean'
s = "@#{if object then "YES" else "NO"}"
else if typeof(object) == 'object'
indent_str = Array(indent + 1).join(' ')
indent_str_children = Array(indent + 2).join(' ')
if object.length?
s = "@[\n" +
("#{indent_str_children}#{@json_body_object(value, indent+1)}" for value in object).join(',\n') +
"\n#{indent_str}]"
else
s = "@{\n" +
("#{indent_str_children}@\"#{addslashes key}\": #{@json_body_object(value, indent+1)}" for key, value of object).join(',\n') +
"\n#{indent_str}}"
if indent <= 1
if typeof(object) == 'object'
# NSArray
if object.length?
s = "NSArray* bodyObject = #{s};"
# NSDictionary
else
s = "NSDictionary* bodyObject = #{s};"
else
s = "id bodyObject = #{s};"
return s
@generate = (context) ->
request = context.getCurrentRequest()
view =
"request": context.getCurrentRequest()
"url": @url request
"headers": @headers request
"body": @body request
if view.url.has_params or (view.body and view.body.has_url_encoded_body)
view["has_utils_query_string"] = true
template = readFile "objc.mustache"
Mustache.render template, view
return
ObjCNSURLSessionCodeGenerator.identifier =
"com.luckymarmot.PawExtensions.ObjCNSURLSessionCodeGenerator"
ObjCNSURLSessionCodeGenerator.title =
"Objective-C (NSURLSession)"
ObjCNSURLSessionCodeGenerator.fileExtension = "m"
ObjCNSURLSessionCodeGenerator.languageHighlighter = "objectivec"
registerCodeGenerator ObjCNSURLSessionCodeGenerator