Sha256: b8675a059a82d045d7ce87a2a5e89322ead994a0d4089849505cc43dbcea93d1

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module GoogleApis
  class Api
    module Base
      module InstanceMethods

        attr_accessor :connection, :discovered_api

        def initialize(options = {})
          options = options.symbolize_keys

          config, default_params = options.partition{|(k, v)| [:email_address, :private_key].include?(k)}.collect{|x| Hash[x] unless x.empty?}
          @connection = config ? GoogleApis::Connection.new(config) : GoogleApis.connection
          raise Error, "Please ensure a Google API connection" unless @connection

          @discovered_api = connection.discover_api self.class.api, self.class.version
          @default_params = default_params || {}
        end

        def execute(api_method, *params)
          params[0] = (params[0] || {}).symbolize_keys
          params[0].reverse_merge!(@default_params)
          connection.execute self.class, api_method, *params
        end

        def inspect
          "#<#{self.class}:#{object_hexid} #{discovered_api.version}:[#{discovered_api.discovered_resources.collect(&:name).sort.join(",")}] {#{@default_params.collect{|k, v| "#{k}:#{v.inspect}"}.join(",")}}>"
        end

        def method_missing(name, *args)
          if resource = find(name)
            Resource.new self, resource
          else
            super
          end
        end

      private

        def find(name)
          discovered_api.discovered_resources.detect{|x| x.name == name.to_s}
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
google-apis-0.1.1 lib/google_apis/api/base/instance_methods.rb
google-apis-0.1.0 lib/google_apis/api/base/instance_methods.rb