Use ActiveSupport instead of extlib
[arvados.git] / lib / google / api_client / discovery / api.rb
index 6f3bb10f1fd9ec04ca78b44067a3bf1451fb5c42..be83f6279fdb2765f774bff8e9f2a39c60d762d4 100644 (file)
 
 
 require 'addressable/uri'
-
-require 'google/inflection'
+require 'multi_json'
+require 'active_support/inflector'
 require 'google/api_client/discovery/resource'
 require 'google/api_client/discovery/method'
+require 'google/api_client/discovery/media'
 
 module Google
   class APIClient
@@ -28,13 +29,9 @@ module Google
       ##
       # Creates a description of a particular version of a service.
       #
-      # @param [String] api
-      #   The identifier for the service.  Note that while this frequently
-      #   matches the first segment of all of the service's RPC names, this
-      #   should not be assumed.  There is no requirement that these match.
-      # @param [String] version
-      #   The identifier for the service version.
-      # @param [Hash] api_description
+      # @param [String] document_base
+      #   Base URI for the service
+      # @param [Hash] discovery_document
       #   The section of the discovery document that applies to this service
       #   version.
       #
@@ -42,27 +39,33 @@ module Google
       def initialize(document_base, discovery_document)
         @document_base = Addressable::URI.parse(document_base)
         @discovery_document = discovery_document
-        metaclass = (class <<self; self; end)
-        self.resources.each do |resource|
-          method_name = Google::INFLECTOR.underscore(resource.name).to_sym
+        metaclass = (class << self; self; end)
+        self.discovered_resources.each do |resource|
+          method_name = ActiveSupport::Inflector.underscore(resource.name).to_sym
           if !self.respond_to?(method_name)
             metaclass.send(:define_method, method_name) { resource }
           end
         end
-        self.methods.each do |method|
-          method_name = Google::INFLECTOR.underscore(method.name).to_sym
+        self.discovered_methods.each do |method|
+          method_name = ActiveSupport::Inflector.underscore(method.name).to_sym
           if !self.respond_to?(method_name)
             metaclass.send(:define_method, method_name) { method }
           end
         end
       end
+      
+      # @return [String] unparsed discovery document for the API
+      attr_reader :discovery_document
 
       ##
       # Returns the id of the service.
       #
       # @return [String] The service id.
       def id
-        return @discovery_document['id']
+        return (
+          @discovery_document['id'] ||
+          "#{self.name}:#{self.version}"
+        )
       end
 
       ##
@@ -82,14 +85,29 @@ module Google
       end
 
       ##
-      # Returns the parsed section of the discovery document that applies to
-      # this version of the service.
+      # Returns a human-readable title for the API.
       #
-      # @return [Hash] The service description.
+      # @return [Hash] The API title.
+      def title
+        return @discovery_document['title']
+      end
+
+      ##
+      # Returns a human-readable description of the API.
+      #
+      # @return [Hash] The API description.
       def description
         return @discovery_document['description']
       end
 
+      ##
+      # Returns a URI for the API documentation.
+      #
+      # @return [Hash] The API documentation.
+      def documentation
+        return Addressable::URI.parse(@discovery_document['documentationLink'])
+      end
+
       ##
       # Returns true if this is the preferred version of this API.
       #
@@ -130,8 +148,7 @@ module Google
       def method_base
         if @discovery_document['basePath']
           return @method_base ||= (
-            self.document_base +
-            Addressable::URI.parse(@discovery_document['basePath'])
+            self.document_base.join(Addressable::URI.parse(@discovery_document['basePath']))
           ).normalize
         else
           return nil
@@ -141,18 +158,33 @@ module Google
       ##
       # Updates the hierarchy of resources and methods with the new base.
       #
-      # @param [Addressable::URI, #to_str, String] new_base
+      # @param [Addressable::URI, #to_str, String] new_method_base
       #   The new base URI to use for the service.
       def method_base=(new_method_base)
         @method_base = Addressable::URI.parse(new_method_base)
-        self.resources.each do |resource|
+        self.discovered_resources.each do |resource|
           resource.method_base = @method_base
         end
-        self.methods.each do |method|
+        self.discovered_methods.each do |method|
           method.method_base = @method_base
         end
       end
 
+      ##
+      # Returns the base URI for batch calls to this service.
+      #
+      # @return [Addressable::URI] The base URI that methods are joined to.
+      def batch_path
+        if @discovery_document['batchPath']
+          return @batch_path ||= (
+            self.document_base.join(Addressable::URI.parse('/' +
+                @discovery_document['batchPath']))
+          ).normalize
+        else
+          return nil
+        end
+      end
+
       ##
       # A list of schemas available for this version of the API.
       #
@@ -160,9 +192,7 @@ module Google
       def schemas
         return @schemas ||= (
           (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
-            accu[k] = Google::APIClient::Schema.new(
-              self, self.name, self.version, v
-            )
+            accu[k] = Google::APIClient::Schema.parse(self, v)
             accu
           end
         )
@@ -190,8 +220,8 @@ module Google
       # API.
       #
       # @return [Array] A list of {Google::APIClient::Resource} objects.
-      def resources
-        return @resources ||= (
+      def discovered_resources
+        return @discovered_resources ||= (
           (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
             accu << Google::APIClient::Resource.new(
               self, self.method_base, k, v
@@ -206,8 +236,8 @@ module Google
       # API.
       #
       # @return [Array] A list of {Google::APIClient::Method} objects.
-      def methods
-        return @methods ||= (
+      def discovered_methods
+        return @discovered_methods ||= (
           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
             accu << Google::APIClient::Method.new(self, self.method_base, k, v)
             accu
@@ -215,6 +245,12 @@ module Google
         )
       end
 
+      ##
+      # Allows deep inspection of the discovery document.
+      def [](key)
+        return @discovery_document[key]
+      end
+
       ##
       # Converts the service to a flat mapping of RPC names and method objects.
       #
@@ -226,10 +262,10 @@ module Google
       def to_h
         return @hash ||= (begin
           methods_hash = {}
-          self.methods.each do |method|
+          self.discovered_methods.each do |method|
             methods_hash[method.id] = method
           end
-          self.resources.each do |resource|
+          self.discovered_resources.each do |resource|
             methods_hash.merge!(resource.to_h)
           end
           methods_hash
@@ -245,6 +281,20 @@ module Google
           "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
         )
       end
+      
+      ##
+      # Marshalling support - serialize the API to a string (doc base + original 
+      # discovery document).
+      def _dump(level)
+        MultiJson.dump([@document_base.to_s, @discovery_document])
+      end
+      
+      ##
+      # Marshalling support - Restore an API instance from serialized form
+      def self._load(obj)
+        new(*MultiJson.load(obj)) 
+      end
+
     end
   end
 end