diff --git a/app/controllers/public/api_controller.rb b/app/controllers/public/api_controller.rb index 5062771..b7fffcd 100644 --- a/app/controllers/public/api_controller.rb +++ b/app/controllers/public/api_controller.rb @@ -6,4 +6,8 @@ class ApiController < PublicController @api = Noosfero::API.api_class end + def playground + @api = Noosfero::API.api_class + end + end diff --git a/app/views/api/index.html.erb b/app/views/api/index.html.erb index 2aa83c1..bdce7c3 100644 --- a/app/views/api/index.html.erb +++ b/app/views/api/index.html.erb @@ -1,5 +1,9 @@

EndPoints

+
+<%= s_('api-playground|Try the %s') % link_to('API Playground', '/api/playground') %> +
+ <%= @api.endpoints.map do |endpoint| app = endpoint.options[:app].to_s unless app.blank? diff --git a/app/views/api/playground.html.erb b/app/views/api/playground.html.erb new file mode 100644 index 0000000..b0744d6 --- /dev/null +++ b/app/views/api/playground.html.erb @@ -0,0 +1,26 @@ +

API Playground

+ + + +
+   + +
+ + +

+
+
diff --git a/public/javascripts/api-playground.js b/public/javascripts/api-playground.js
new file mode 100644
index 0000000..c7bbe18
--- /dev/null
+++ b/public/javascripts/api-playground.js
@@ -0,0 +1,87 @@
+// Create endpoint options
+for (var endpoint,i=0; endpoint=endpoints[i]; i++) {
+  jQuery('').appendTo('#api-form select');
+}
+
+var playground = {
+
+  getToken: function() {
+    return jQuery('#api-token').val();
+  },
+
+  setToken: function(token) {
+    jQuery('#api-token').val(token);
+  },
+
+  getEndpoint: function() {
+    var endpoint = jQuery('#api-form select').val().split(' ');
+    return {
+      method: endpoint[0],
+      path: endpoint[1]
+    };
+  },
+
+  selEndpoint: function() {
+    var endpoint = this.getEndpoint();
+    jQuery('#api-form .api-param').remove();
+    if ( endpoint.path == '/api/v1/login' ) {
+      this.addFormParam('login');
+      this.addFormParam('password');
+    }
+    var pathParameters = endpoint.path.match(/:[^/]+/g);
+    if ( pathParameters ) {
+      for (var pathParam,i=0; pathParam=pathParameters[i]; i++) {
+        this.addFormParam(pathParam.substr(1));
+      }
+    }
+  },
+
+  addFormParam: function(name) {
+    if (!name) name = '';
+    jQuery('
'+ + '   '+ + ''+ + '
').appendTo('#api-form'); + }, + + run: function() { + var endpoint = this.getEndpoint(); + var rawData = jQuery('#api-form').serializeArray(); + var data = {}; + for (var i=1; i'+textStatus+'' + + 'Request to '+url+' fail.\n\n' + errorThrown + ); + } + }); + } + +}; + +playground.selEndpoint(jQuery('#api-form select')[0]); -- libgit2 0.21.2