Commit b9cfd5f095c016acfb88b0e34ed9a99fd682c74c

Authored by Rodrigo Souto
Committed by Daniela Feitosa
1 parent 9d383ef1

api: ensure relative root is prefixed to the api path

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