Merge pull request #4 from vapir/baseURI_and_fixups
Base uri and fixups
This commit is contained in:
		
						commit
						49ca24c16f
					
				| 
						 | 
				
			
			@ -47,8 +47,8 @@ module Google
 | 
			
		|||
    #     <li><code>:oauth_1</code></li>
 | 
			
		||||
    #     <li><code>:oauth_2</code></li>
 | 
			
		||||
    #   </ul>
 | 
			
		||||
    # @option options [String] :host ("www.googleapis.com")
 | 
			
		||||
    #   The API hostname used by the client.  This rarely needs to be changed.
 | 
			
		||||
    # @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1")
 | 
			
		||||
    #   The base API URI used by the client.  This rarely needs to be changed.
 | 
			
		||||
    # @option options [String] :application_name
 | 
			
		||||
    #   The name of the application using the client.
 | 
			
		||||
    # @option options [String] :application_version
 | 
			
		||||
| 
						 | 
				
			
			@ -63,8 +63,9 @@ module Google
 | 
			
		|||
        accu[key.to_s] = value
 | 
			
		||||
        accu
 | 
			
		||||
      end
 | 
			
		||||
      # Almost all API usage will have a host of 'www.googleapis.com'.
 | 
			
		||||
      self.host = options["host"] || 'www.googleapis.com'
 | 
			
		||||
      # Almost all API usage will have this base URI 
 | 
			
		||||
      self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1"
 | 
			
		||||
 | 
			
		||||
      # Most developers will want to leave this value alone and use the
 | 
			
		||||
      # application_name option.
 | 
			
		||||
      application_string = (
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +81,7 @@ module Google
 | 
			
		|||
      ).strip
 | 
			
		||||
      # The writer method understands a few Symbols and will generate useful
 | 
			
		||||
      # default authentication mechanisms.
 | 
			
		||||
      self.authorization = options["authorization"] || :oauth_2
 | 
			
		||||
      self.authorization = options.key?("authorization") ? options["authorization"] : :oauth_2
 | 
			
		||||
      self.key = options["key"]
 | 
			
		||||
      self.user_ip = options["user_ip"]
 | 
			
		||||
      @discovery_uris = {}
 | 
			
		||||
| 
						 | 
				
			
			@ -165,7 +166,7 @@ module Google
 | 
			
		|||
    #
 | 
			
		||||
    # @return [String]
 | 
			
		||||
    #   The API hostname.  Should almost always be 'www.googleapis.com'.
 | 
			
		||||
    attr_accessor :host
 | 
			
		||||
    attr_accessor :baseURI
 | 
			
		||||
 | 
			
		||||
    ##
 | 
			
		||||
    # The user agent used by the client.
 | 
			
		||||
| 
						 | 
				
			
			@ -174,15 +175,16 @@ module Google
 | 
			
		|||
    #   The user agent string used in the User-Agent header.
 | 
			
		||||
    attr_accessor :user_agent
 | 
			
		||||
    
 | 
			
		||||
    def relative_uri(path, expand={})
 | 
			
		||||
      Addressable::Template.new(baseURI+path).expand(expand)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    ##
 | 
			
		||||
    # Returns the URI for the directory document.
 | 
			
		||||
    #
 | 
			
		||||
    # @return [Addressable::URI] The URI of the directory document.
 | 
			
		||||
    def directory_uri
 | 
			
		||||
      template = Addressable::Template.new(
 | 
			
		||||
        "https://{host}/discovery/v1/apis"
 | 
			
		||||
      )
 | 
			
		||||
      return template.expand({"host" => self.host})
 | 
			
		||||
      relative_uri('/apis')
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    ##
 | 
			
		||||
| 
						 | 
				
			
			@ -208,15 +210,7 @@ module Google
 | 
			
		|||
      api = api.to_s
 | 
			
		||||
      version = version || 'v1'
 | 
			
		||||
      return @discovery_uris["#{api}:#{version}"] ||= (begin
 | 
			
		||||
        template = Addressable::Template.new(
 | 
			
		||||
          "https://{host}/discovery/v1/apis/" +
 | 
			
		||||
          "{api}/{version}/rest"
 | 
			
		||||
        )
 | 
			
		||||
        template.expand({
 | 
			
		||||
          "host" => self.host,
 | 
			
		||||
          "api" => api,
 | 
			
		||||
          "version" => version
 | 
			
		||||
        })
 | 
			
		||||
        relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version)
 | 
			
		||||
      end)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -596,7 +590,7 @@ module Google
 | 
			
		|||
        unless headers.kind_of?(Enumerable)
 | 
			
		||||
          # We need to use some Enumerable methods, relying on the presence of
 | 
			
		||||
          # the #each method.
 | 
			
		||||
          class <<headers
 | 
			
		||||
          class << headers
 | 
			
		||||
            include Enumerable
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,7 +43,7 @@ module Google
 | 
			
		|||
      def initialize(document_base, discovery_document)
 | 
			
		||||
        @document_base = Addressable::URI.parse(document_base)
 | 
			
		||||
        @discovery_document = discovery_document
 | 
			
		||||
        metaclass = (class <<self; self; end)
 | 
			
		||||
        metaclass = (class << self; self; end)
 | 
			
		||||
        self.discovered_resources.each do |resource|
 | 
			
		||||
          method_name = Google::INFLECTOR.underscore(resource.name).to_sym
 | 
			
		||||
          if !self.respond_to?(method_name)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -172,12 +172,14 @@ module Google
 | 
			
		|||
        query_parameters = parameters.reject do |k, v|
 | 
			
		||||
          template_variables.include?(k)
 | 
			
		||||
        end
 | 
			
		||||
        if query_parameters.size > 0
 | 
			
		||||
          uri.query_values = (uri.query_values || []) + query_parameters
 | 
			
		||||
        # encode all non-template parameters
 | 
			
		||||
        params = ""
 | 
			
		||||
        unless query_parameters.empty?
 | 
			
		||||
          params = "?" + Addressable::URI.form_encode(query_parameters)
 | 
			
		||||
        end
 | 
			
		||||
        # Normalization is necessary because of undesirable percent-escaping
 | 
			
		||||
        # during URI template expansion
 | 
			
		||||
        return uri.normalize
 | 
			
		||||
        return uri.normalize + params
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      ##
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -47,7 +47,7 @@ module Google
 | 
			
		|||
          # and excess object creation, but this hopefully shouldn't be an
 | 
			
		||||
          # issue since it should only be called only once per schema per
 | 
			
		||||
          # process.
 | 
			
		||||
          if data.kind_of?(Hash) && data['$ref']
 | 
			
		||||
          if data.kind_of?(Hash) && data['$ref'].is_a?(String)
 | 
			
		||||
            reference = data['$ref']
 | 
			
		||||
            reference = '#' + reference if reference[0..0] != '#'
 | 
			
		||||
            data.merge({
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,15 +16,19 @@
 | 
			
		|||
module Google
 | 
			
		||||
  class APIClient
 | 
			
		||||
    module ENV
 | 
			
		||||
      OS_VERSION = if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
 | 
			
		||||
        # TODO(bobaman)
 | 
			
		||||
        # Confirm that all of these Windows environments actually have access
 | 
			
		||||
        # to the `ver` command.
 | 
			
		||||
        `ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
 | 
			
		||||
      elsif RUBY_PLATFORM =~ /darwin/i
 | 
			
		||||
        "Mac OS X/#{`sw_vers -productVersion`}"
 | 
			
		||||
      else
 | 
			
		||||
        `uname -sr`.sub(' ', '/')
 | 
			
		||||
      OS_VERSION = begin
 | 
			
		||||
        if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
 | 
			
		||||
          # TODO(bobaman)
 | 
			
		||||
          # Confirm that all of these Windows environments actually have access
 | 
			
		||||
          # to the `ver` command.
 | 
			
		||||
          `ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
 | 
			
		||||
        elsif RUBY_PLATFORM =~ /darwin/i
 | 
			
		||||
          "Mac OS X/#{`sw_vers -productVersion`}"
 | 
			
		||||
        else
 | 
			
		||||
          `uname -sr`.sub(' ', '/')
 | 
			
		||||
        end
 | 
			
		||||
      rescue Exception
 | 
			
		||||
        RUBY_PLATFORM
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue