Commit 014031e1f34ab5508ce9d9b421f8b98f517111c7

Authored by Rodrigo Souto
1 parent 4e700990

api: ensure relative root is prefixed to the api path

app/views/api/playground.html.erb
1 <h1>API Playground</h1> 1 <h1>API Playground</h1>
2 2
3 <script> 3 <script>
  4 +<% prefix = Noosfero::API::API.prefix %>
  5 +var prefix = <%= prefix.to_json %>;
4 var endpoints = <%= 6 var endpoints = <%=
5 endpoints.map do |endpoint| 7 endpoints.map do |endpoint|
6 app = endpoint.options[:app].to_s 8 app = endpoint.options[:app].to_s
@@ -13,8 +15,8 @@ endpoints.map do |endpoint| @@ -13,8 +15,8 @@ endpoints.map do |endpoint|
13 end 15 end
14 end 16 end
15 end.flatten.compact.sort{|a,b| 17 end.flatten.compact.sort{|a,b|
16 - a[:path]=='/api/v1/login' ? -1 :  
17 - b[:path]=='/api/v1/login' ? 1 : 18 + a[:path]=="#{prefix}/v1/login" ? -1 :
  19 + b[:path]=="#{prefix}/v1/login" ? 1 :
18 a[:path] <=> b[:path] 20 a[:path] <=> b[:path]
19 }.to_json %>; 21 }.to_json %>;
20 </script> 22 </script>
@@ -33,4 +35,4 @@ end.flatten.compact.sort{|a,b| @@ -33,4 +35,4 @@ end.flatten.compact.sort{|a,b|
33 </div> 35 </div>
34 <pre id="api-response" class="empty"></pre> 36 <pre id="api-response" class="empty"></pre>
35 37
36 -<script src="/javascripts/api-playground.js"></script> 38 +<%= javascript_include_tag 'api-playground' %>
lib/noosfero/api/api.rb
@@ -32,7 +32,7 @@ module Noosfero @@ -32,7 +32,7 @@ module Noosfero
32 after { set_session_cookie } 32 after { set_session_cookie }
33 33
34 version 'v1' 34 version 'v1'
35 - prefix "api" 35 + prefix [ENV['RAILS_RELATIVE_URL_ROOT'], "api"].compact.join('/')
36 format :json 36 format :json
37 content_type :txt, "text/plain" 37 content_type :txt, "text/plain"
38 38
public/javascripts/api-playground.js
@@ -24,7 +24,7 @@ var playground = { @@ -24,7 +24,7 @@ var playground = {
24 selEndpoint: function() { 24 selEndpoint: function() {
25 var endpoint = this.getEndpoint(); 25 var endpoint = this.getEndpoint();
26 jQuery('#api-form .api-param').remove(); 26 jQuery('#api-form .api-param').remove();
27 - if ( endpoint.path == '/api/v1/login' ) { 27 + if ( endpoint.path == prefix+'/v1/login' ) {
28 this.addFormParam('login'); 28 this.addFormParam('login');
29 this.addFormParam('password'); 29 this.addFormParam('password');
30 } 30 }
@@ -51,7 +51,7 @@ var playground = { @@ -51,7 +51,7 @@ var playground = {
51 for (var i=1; i<rawData.length; i+=2) { 51 for (var i=1; i<rawData.length; i+=2) {
52 data[ rawData[i].value ] = rawData[i+1].value; 52 data[ rawData[i].value ] = rawData[i+1].value;
53 } 53 }
54 - if ( endpoint.path != '/api/v1/login' ) { 54 + if ( endpoint.path != prefix+'/v1/login' ) {
55 data.private_token = this.getToken(); 55 data.private_token = this.getToken();
56 } 56 }
57 jQuery('#api-response').empty()[0].className = 'empty'; 57 jQuery('#api-response').empty()[0].className = 'empty';
@@ -71,7 +71,7 @@ var playground = { @@ -71,7 +71,7 @@ var playground = {
71 jQuery('#api-response').text( 71 jQuery('#api-response').text(
72 JSON.stringify(data, null, ' ') 72 JSON.stringify(data, null, ' ')
73 )[0].className = 'full'; 73 )[0].className = 'full';
74 - if ( endpoint.path == '/api/v1/login' ) { 74 + if ( endpoint.path == prefix+'/v1/login' ) {
75 playground.setToken(data.private_token) 75 playground.setToken(data.private_token)
76 } 76 }
77 }, 77 },