Use ActiveSupport instead of extlib
[arvados.git] / lib / google / api_client / discovery / resource.rb
index 6d2d6bddb65d877f9561615250b713db2b1df872..9b757c684da02aed78a8905e1a8ea0d22fbc3e85 100644 (file)
 
 require 'addressable/uri'
 
-require 'google/inflection'
+require 'active_support/inflector'
 require 'google/api_client/discovery/method'
 
+
 module Google
   class APIClient
     ##
@@ -27,63 +28,70 @@ module Google
       ##
       # Creates a description of a particular version of a resource.
       #
-      # @param [Addressable::URI] base
+      # @param [Google::APIClient::API] api
+      #   The API this resource belongs to.
+      # @param [Addressable::URI] method_base
       #   The base URI for the service.
       # @param [String] resource_name
       #   The identifier for the resource.
-      # @param [Hash] resource_description
+      # @param [Hash] discovery_document
       #   The section of the discovery document that applies to this resource.
       #
       # @return [Google::APIClient::Resource] The constructed resource object.
-      def initialize(method_base, resource_name, discovery_document)
+      def initialize(api, method_base, resource_name, discovery_document)
+        @api = api
         @method_base = method_base
         @name = resource_name
         @discovery_document = discovery_document
         metaclass = (class <<self; self; end)
-        self.resources.each do |resource|
-          method_name = Google::INFLECTOR.underscore(resource.name).to_sym
+        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 resource
+      attr_reader :discovery_document
+
       ##
       # Returns the identifier for the resource.
       #
       # @return [String] The resource identifier.
       attr_reader :name
 
-      ##
-      # Returns the parsed section of the discovery document that applies to
-      # this resource.
-      #
-      # @return [Hash] The resource description.
-      attr_reader :description
-
       ##
       # Returns the base URI for this resource.
       #
       # @return [Addressable::URI] The base URI that methods are joined to.
       attr_reader :method_base
 
+      ##
+      # Returns a human-readable description of the resource.
+      #
+      # @return [Hash] The API description.
+      def description
+        return @discovery_document['description']
+      end
+
       ##
       # 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 resource.
       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
@@ -92,10 +100,12 @@ module Google
       # A list of sub-resources available on this resource.
       #
       # @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.method_base, k, v)
+            accu << Google::APIClient::Resource.new(
+              @api, self.method_base, k, v
+            )
             accu
           end
         )
@@ -105,10 +115,10 @@ module Google
       # A list of methods available on this resource.
       #
       # @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.method_base, k, v)
+            accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
             accu
           end
         )
@@ -122,10 +132,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