If a GET fails while looking up a friendly name, skip the hyperlink
[arvados.git] / services / api / app / controllers / application_controller.rb
index 434b09572b40b64af026e27a43962b422aeea7c6..708defede671ac6fc60af73fd83485fcdfd31545 100644 (file)
@@ -101,7 +101,7 @@ class ApplicationController < ActionController::Base
       @where = params[:where]
     elsif params[:where].is_a? String
       begin
-        @where = Oj.load(params[:where])
+        @where = Oj.load(params[:where], symbol_keys: true)
       rescue
         raise ArgumentError.new("Could not parse \"where\" param as an object")
       end
@@ -125,7 +125,7 @@ class ApplicationController < ActionController::Base
     if !@where.empty?
       conditions = ['1=1']
       @where.each do |attr,value|
-        if attr == 'any' or attr == :any
+        if attr == :any
           if value.is_a?(Array) and
               value[0] == 'contains' and
               model_class.columns.collect(&:name).index('name') then
@@ -191,7 +191,7 @@ class ApplicationController < ActionController::Base
     return @attrs if @attrs
     @attrs = params[resource_name]
     if @attrs.is_a? String
-      @attrs = Oj.load @attrs
+      @attrs = Oj.load @attrs, symbol_keys: true
     end
     unless @attrs.is_a? Hash
       message = "No #{resource_name}"
@@ -241,6 +241,7 @@ class ApplicationController < ActionController::Base
   end
 
   def thread_with_auth_info
+    Thread.current[:request_starttime] = Time.now
     Thread.current[:api_url_base] = root_url.sub(/\/$/,'') + '/arvados/v1'
     begin
       user = nil
@@ -309,7 +310,9 @@ class ApplicationController < ActionController::Base
     if params[:id] and params[:id].match /\D/
       params[:uuid] = params.delete :id
     end
-    @object = model_class.where('uuid=?', params[:uuid]).first
+    @where = { uuid: params[:uuid] }
+    find_objects_for_index
+    @object = @objects.first
   end
 
   def self.accept_attribute_as_json(attr, force_class=nil)
@@ -318,7 +321,8 @@ class ApplicationController < ActionController::Base
   def accept_attribute_as_json(attr, force_class)
     if params[resource_name].is_a? Hash
       if params[resource_name][attr].is_a? String
-        params[resource_name][attr] = Oj.load params[resource_name][attr]
+        params[resource_name][attr] = Oj.load(params[resource_name][attr],
+                                              symbol_keys: true)
         if force_class and !params[resource_name][attr].is_a? force_class
           raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
         end
@@ -328,13 +332,16 @@ class ApplicationController < ActionController::Base
 
   def render_list
     @object_list = {
-      :kind  => "arvados##{resource_name}List",
+      :kind  => "arvados##{(@response_resource_name || resource_name).camelize(:lower)}List",
       :etag => "",
       :self_link => "",
       :next_page_token => "",
       :next_link => "",
       :items => @objects.as_api_response(nil)
     }
+    if @objects.respond_to? :except
+      @object_list[:items_available] = @objects.except(:limit).count
+    end
     render json: @object_list
   end
 
@@ -360,4 +367,16 @@ class ApplicationController < ActionController::Base
     (request.headers['Accept'].split(' ') &
      ['text/plain', '*/*']).count > 0
   end
+
+  def render *opts
+    response = opts.first[:json]
+    if response.is_a?(Hash) &&
+        params[:_profile] &&
+        Thread.current[:request_starttime]
+      response[:_profile] = {
+         request_time: Time.now - Thread.current[:request_starttime]
+      }
+    end
+    super *opts
+  end
 end