Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 195 lines (167 sloc) 7.705 kb
5f98cc5 @mansilladev Initial commit.
mansilladev authored
1 <html>
2 <head>
3 <title>I/O Wraps - JSON Schema Converter</title>
4 <script src="http://code.jquery.com/jquery-1.7.2.js" type="text/javascript"></script>
5 <link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" media="screen" />
6 </head>
7 <body>
8 <div class="container" id="mainwrap">
9 <header>
10 <h1>Mashery I/O Wraps - JSON Schema Converter</h1>
11 <h4 style="line-height: 20px;">Use this script to convert a Mashery I/O Docs JSON Schema configuration to the Google Discovery Format, for use with client
12 library generator. Note that the acceptable schema format is that of the Mashery product version, not the I/O Docs Node.js (Github) project. A converter
13 from that schema format will be provided in the near future.</h4>
14 <br />
15 </header>
16 <span style="font-size:12pt; font-weight:bold;">I/O Docs JSON Schema</span> -
17 <input type="button" class="btn btn-primary" id="php_to_goog" onclick="iodocs_to_goog();" value="Convert to Google Discovery Format" />
18 <button class="btn btn-danger" onclick="$('#iodocs_src').val('');">
19 Reset
20 </button>
21 <br />
22 <br />
23 <textarea id="iodocs_src" name="iodocs_src" class="input-large" style="width:800px;height:300px;">
24 </textarea>
25 <br />
26 <br />
27 <span style="font-size:12pt; font-weight:bold;">Google Discovery Format</span> -
28 <button class="btn btn-danger" onclick="$('#goog_src').val('');">
29 Reset
30 </button>
31 <br />
32 <br />
33 <textarea id="goog_src" name="goog_src" class="input-large" style="width:800px;height:300px;" readonly="readonly">
34 </textarea>
35
36
37 <script type="text/javascript">
38 // validateName
39 // Resource, method and parameter names must conform to this regex.
40 // See name_validator.py in the Google Client Library Generator for all
41 // regex details. The schema specification is not explicit about naming
42 // conventions; however, the generator code does enforce the regex below
43 // most likely for native language compatibility.
44 var nameRegex = /(^[a-zA-Z]$)|(^[a-zA-Z_$]{1}[a-zA-Z0-9_.-]*[a-zA-Z0-9_]{1})$/;
45 function validateName(varName, locRef) {
46 if (nameRegex.test(varName)) {
47 return true;
48 } else {
49 alert('Variable "' + varName + '" does not conform to style guide. ' + locRef);
50 return false;
51 }
52 }
53
54 // iodocs_to_good
55 function iodocs_to_goog() {
56 var iodocsJSON = jQuery.parseJSON(jQuery('#iodocs_src').val());
57 var googJSON = {};
58
59 googJSON.kind = "discovery#restDescription";
60 googJSON.discoveryVersion = "v1";
61 googJSON.title = iodocsJSON.title;
62 googJSON.description = iodocsJSON.description;
63 googJSON.protocol = iodocsJSON.protocol;
64 googJSON.name = (iodocsJSON.title.replace(new RegExp(' ', 'g'), '')).toLowerCase();
65 if ( typeof iodocsJSON.version == 'undefined') {
66 googJSON.version = "1";
67 } else {
68 googJSON.version = iodocsJSON.version;
69 }
70 googJSON.auth = iodocsJSON.auth;
71
72 // Parse the basePath into "uri" object
73 var uri = parseUri(iodocsJSON.basePath);
74 // Set basePath, rootUrl, servicePat and baseUrl from URI paths.
75 // Additionally, strip out trailing slashes from values.
76 googJSON.basePath = uri.path;
77 googJSON.rootUrl = uri.protocol + "://" + uri.authority + "/";
78 googJSON.rootUrl = (googJSON.rootUrl.replace(new RegExp('[\/]{1,}$', ''), ''));
79 googJSON.servicePath = uri.path.substr(1, uri.path.length - 1) + "/";
80 googJSON.servicePath = (googJSON.servicePath.replace(new RegExp('[\/]{1,}$', ''), ''));
81 googJSON.baseUrl = googJSON.rootUrl + googJSON.servicePath;
82 googJSON.baseUrl = (googJSON.baseUrl.replace(new RegExp('[\/]{1,}$', ''), ''));
83
84 // googJSON.resources = iodocsJSON.resources -- but first, we strip out the spaces in
85 // the names and add "id" if that doesn't exist.
86 var googResources = {};
87 for (resource in iodocsJSON.resources) {
88
89 if (!validateName(resource, '(see resources)')) {
90 return false;
91 }
92 resourceCompressed = resource.replace(" ", "");
93 phpResource = iodocsJSON.resources[resource];
94 googResources[resourceCompressed] = {};
95 googResources[resourceCompressed]["methods"] = {};
96 for (method in phpResource.methods) {
97 if (!validateName(method, '(see resources.' + resource + '.methods)')) {
98 return false;
99 }
100 methodCompressed = method.replace(new RegExp(' ', 'g'), '');
101 googResources[resourceCompressed]["methods"][methodCompressed] = phpResource.methods[method];
102 googResources[resourceCompressed]["methods"][methodCompressed]["id"] = resourceCompressed + "." + methodCompressed;
103 }
104 }
105 googJSON.resources = googResources;
106
107 // Iterate through googJSON.resources for transformations
108 for (resource in googJSON.resources) {
109
110 for (method in googJSON.resources[resource].methods) {
111
112 // methodCompressed = method.replace(new RegExp(' ','g'),'');
113 googMethod = googJSON.resources[resource].methods[method];
114
115 // Remove leading slash from method path
116 googMethod.path = googMethod.path.replace(/^\//, "");
117
118 // Change pathReplace to just path
119 if (googMethod.location == "pathReplace") {
120 googMethod.location = "path";
121 }
122
123 // Check for "id" -- if not there, then add it.
124 if (!( typeof googMethod.id != 'undefined')) {
125 googMethod.id = method
126 }
127
128 // Iterate through all methods params
129 for (param in googMethod.parameters) {
130 if (!validateName(param, '(see resources.' + resource + '.methods.' + method + '.parameters)')) {
131 return false;
132 }
133 googParam = googJSON.resources[resource].methods[method].parameters[param];
134
135 // Replicate enum into enumDescriptions for Google format
136 if (( typeof googParam.enum != 'undefined') && (!( typeof googParam.enumDescriptions != 'undefined'))) {
137 googParam.enumDescriptions = googParam.enum;
138 }
139
140 // Replace "pathReplace" with "path"
141 if (googParam.location == 'pathReplace') {
142 googParam.location = 'path';
143
144 // Encapsulate the parameter name with curly brackets in method's path
145 googMethod.path = (googMethod.path.replace(new RegExp(param, 'g'), '{' + param + '}'));
146 }
147 }
148 }
149 // Format the output with 4-space padding
150 var googJSONstring = JSON.stringify(googJSON, null, 4);
151 jQuery('#goog_src').val(googJSONstring);
152 }
153 }
154
155 // parseUri 1.2.2
156 // (c) Steven Levithan <stevenlevithan.com>
157 // MIT License
158
159 function parseUri(str) {
160 var o = parseUri.options, m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), uri = {}, i = 14;
161
162 while (i--)
163 uri[o.key[i]] = m[i] || "";
164
165 uri[o.q.name] = {};
166 uri[o.key[12]].replace(o.q.parser, function($0, $1, $2) {
167 if ($1)
168 uri[o.q.name][$1] = $2;
169 });
170
171 return uri;
172 };
173
174 parseUri.options = {
175 strictMode : false,
176 key : ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
177 q : {
178 name : "queryKey",
179 parser : /(?:^|&)([^&=]*)=?([^&]*)/g
180 },
181 parser : {
182 strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
183 loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
184 }
185 };
186
187 // end parseUri
188
189 </script>
190 </div>
191 </body>
192 </html>
193
194
Something went wrong with that request. Please try again.