Skip to content

Commit b0a0b10

Browse files
committed
Adapted to JavaScript + jQuery.
1 parent e189f19 commit b0a0b10

File tree

6 files changed

+70
-105
lines changed

6 files changed

+70
-105
lines changed

Cakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{print} = require 'sys'
22
{spawn} = require 'child_process'
33

4-
file = 'PythonRequestsCodeGenerator.coffee'
4+
file = 'JavaScriptjQueryCodeGenerator.coffee'
55

66
task 'build', ->
77
coffee = spawn 'coffee', ['-c', file]

PythonRequestsCodeGenerator.coffee renamed to JavaScriptjQueryCodeGenerator.coffee

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require "URI.js"
44
addslashes = (str) ->
55
("#{str}").replace(/[\\"]/g, '\\$&')
66

7-
PythonRequestsCodeGenerator = ->
7+
JavaScriptjQueryCodeGenerator = ->
88

99
@url = (request) ->
1010
url_params_object = (() ->
@@ -41,7 +41,7 @@ PythonRequestsCodeGenerator = ->
4141
if json_body
4242
return {
4343
"has_json_body":true
44-
"json_body_object":@json_body_object json_body, 2
44+
"json_body_object":@json_body_object json_body, 0
4545
}
4646

4747
url_encoded_body = request.urlEncodedBody
@@ -54,16 +54,6 @@ PythonRequestsCodeGenerator = ->
5454
} for name, value of url_encoded_body)
5555
}
5656

57-
multipart_body = request.multipartBody
58-
if multipart_body
59-
return {
60-
"has_multipart_body":true
61-
"multipart_body": ({
62-
"name": addslashes name
63-
"value": addslashes value
64-
} for name, value of multipart_body)
65-
}
66-
6757
raw_body = request.body
6858
if raw_body
6959
if raw_body.length < 5000
@@ -78,13 +68,13 @@ PythonRequestsCodeGenerator = ->
7868

7969
@json_body_object = (object, indent = 0) ->
8070
if object == null
81-
s = "None"
71+
s = "null"
8272
else if typeof(object) == 'string'
8373
s = "\"#{addslashes object}\""
8474
else if typeof(object) == 'number'
8575
s = "#{object}"
8676
else if typeof(object) == 'boolean'
87-
s = "#{if object then "True" else "False"}"
77+
s = "#{if object then "true" else "false"}"
8878
else if typeof(object) == 'object'
8979
indent_str = Array(indent + 2).join(' ')
9080
indent_str_children = Array(indent + 3).join(' ')
@@ -104,20 +94,19 @@ PythonRequestsCodeGenerator = ->
10494

10595
view =
10696
"request": context.getCurrentRequest()
107-
"method": request.method.toLowerCase()
10897
"url": @url request
10998
"headers": @headers request
11099
"body": @body request
111100

112-
template = readFile "python.mustache"
101+
template = readFile "javascript.mustache"
113102
Mustache.render template, view
114103

115104
return
116105

117106

118-
PythonRequestsCodeGenerator.identifier =
119-
"com.luckymarmot.PawExtensions.PythonRequestsCodeGenerator";
120-
PythonRequestsCodeGenerator.title =
121-
"Python (Requests)";
107+
JavaScriptjQueryCodeGenerator.identifier =
108+
"com.luckymarmot.PawExtensions.JavaScriptjQueryCodeGenerator";
109+
JavaScriptjQueryCodeGenerator.title =
110+
"JavaScript (jQuery)";
122111

123-
registerCodeGenerator PythonRequestsCodeGenerator
112+
registerCodeGenerator JavaScriptjQueryCodeGenerator

PythonRequestsCodeGenerator.js renamed to JavaScriptjQueryCodeGenerator.js

Lines changed: 10 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Python + Requests Code Generator (Paw Extension)
1+
#JavaScript jQuery Code Generator (Paw Extension)
22

3-
This a Paw Extension that generates code for Python using the [Requests](http://docs.python-requests.org/en/latest/) library.
3+
This a Paw Extension that generates code for JavaScript AJAX requests with jQuery.
44

55
##License
66

javascript.mustache

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// {{{request.name}}} ({{{request.method}}} {{{url.base}}})
2+
3+
$.ajax({
4+
url: "{{{url.base}}}",
5+
type: "{{{request.method}}}",
6+
{{#url.has_params}}
7+
data:{
8+
{{#url.params}}
9+
"{{{name}}}":"{{{value}}}",
10+
{{/url.params}}
11+
},
12+
{{/url.has_params}}
13+
{{#headers.has_headers}}
14+
headers:{
15+
{{#headers.header_list}}
16+
"{{{header_name}}}":"{{{header_value}}}",
17+
{{/headers.header_list}}
18+
},
19+
{{/headers.has_headers}}
20+
{{#body.has_raw_body}}
21+
processData:false,
22+
data:"{{{body.raw_body}}}",
23+
{{/body.has_raw_body}}
24+
{{#body.has_long_body}}
25+
processData:false,
26+
data:"", // set your body string
27+
{{/body.has_long_body}}
28+
{{#body.has_url_encoded_body}}
29+
contentType:"application/x-www-form-urlencoded",
30+
data:{
31+
{{#body.url_encoded_body}}
32+
"{{{name}}}":"{{{value}}}",
33+
{{/body.url_encoded_body}}
34+
},
35+
{{/body.has_url_encoded_body}}
36+
{{#body.has_json_body}}
37+
contentType:"application/json",
38+
data:JSON.stringify({{{body.json_body_object}}}),
39+
{{/body.has_json_body}}
40+
success:function(data, textStatus, jqXHR){
41+
console.log("HTTP Request Succeeded: " + jqXHR.status);
42+
console.log(data);
43+
},
44+
error:function(jqXHR, textStatus, errorThrown){
45+
console.log("HTTP Request Failed");
46+
}
47+
});

python.mustache

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)