From a6d7aa570e8c4ff1bbb5891c48c406ef1d5a1529 Mon Sep 17 00:00:00 2001 From: "bobaman@google.com" Date: Sat, 14 Aug 2010 01:16:35 +0000 Subject: [PATCH] Updated the configuration handling code. git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@10 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef --- lib/google/api_client/auth/oauth_1.rb | 28 ++++++++++++++- lib/google/api_client/discovery/discovery.rb | 35 +++++++++++++++++++ spec/google/api_client/auth/oauth_1_spec.rb | 25 +++++++++++++ .../auth/services/buzz_slow_spec.rb | 7 ++++ 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/google/api_client/auth/oauth_1.rb b/lib/google/api_client/auth/oauth_1.rb index 36271718c..cca3d3363 100644 --- a/lib/google/api_client/auth/oauth_1.rb +++ b/lib/google/api_client/auth/oauth_1.rb @@ -76,7 +76,7 @@ module Google #:nodoc: # # @return [Google::APIClient::OAuth1] The OAuth 1.0a handler. def initialize(options={}) - if options[:service] + if options[:service] && SERVICE_DEFAULTS[options[:service]] @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]]) else @options = DEFAULTS.clone @@ -107,6 +107,15 @@ module Google #:nodoc: } ) end + + ## + # Returns the configuration of the handler. Configuration options that + # are not recognized by the handler are ignored. + # + # @return [Hash] The configuration options. + def options + return @options + end ## # Returns the current request token. Obtains a new request token if @@ -174,6 +183,23 @@ module Google #:nodoc: return @options[:scopes] end + ## + # Returns the callback for the handler. + # + # @return [String] The OAuth 1.0a callback for the consumer. + def callback + return @options[:callback] + end + + ## + # Returns a human-readable service name to present to the user when they + # visit the :authorization_uri. + # + # @return [String] The display name for the consumer. + def display_name + return @options[:display_name] + end + ## # Returns the consumer key. # diff --git a/lib/google/api_client/discovery/discovery.rb b/lib/google/api_client/discovery/discovery.rb index 171a586d8..64fb0705c 100644 --- a/lib/google/api_client/discovery/discovery.rb +++ b/lib/google/api_client/discovery/discovery.rb @@ -18,7 +18,42 @@ require "addressable/template" module Google #:nodoc: class APIClient #:nodoc: class Discovery + ## + # The default discovery configuration values. These may be overrided + # simply by passing in the same key to the constructor. + DEFAULTS = { + } + + ## + # A set of default configuration values specific to each service. These + # may be overrided simply by passing in the same key to the constructor. + SERVICE_DEFAULTS = { + } + ## + # Creates a new API discovery handler. + # + # @param [Hash] options + # + # @return [Google::APIClient::Discovery] The API discovery handler. + def initialize(options={}) + if options[:service] && SERVICE_DEFAULTS[options[:service]] + @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]]) + else + @options = DEFAULTS.clone + end + @options.merge!(options) + # Handle any remaining configuration here + end + + ## + # Returns the configuration of the handler. Configuration options that + # are not recognized by the handler are ignored. + # + # @return [Hash] The configuration options. + def options + return @options + end end end end diff --git a/spec/google/api_client/auth/oauth_1_spec.rb b/spec/google/api_client/auth/oauth_1_spec.rb index 984834e97..d3f79f1d6 100644 --- a/spec/google/api_client/auth/oauth_1_spec.rb +++ b/spec/google/api_client/auth/oauth_1_spec.rb @@ -57,3 +57,28 @@ describe Google::APIClient::OAuth1, "in the default configuration" do end).should raise_error(TypeError) end end + +describe Google::APIClient::OAuth1, "configured for use with bogus service" do + before do + @oauth = Google::APIClient::OAuth1.new(:service => :bogus) + end + + it "should have the default configuration" do + @oauth.request_token_uri.should == + Google::APIClient::OAuth1::DEFAULTS[:request_token_uri] + @oauth.authorization_endpoint_uri.should == + Google::APIClient::OAuth1::DEFAULTS[:authorization_uri] + @oauth.access_token_uri.should == + Google::APIClient::OAuth1::DEFAULTS[:access_token_uri] + @oauth.scopes.should == + Google::APIClient::OAuth1::DEFAULTS[:scopes] + @oauth.callback.should == + Google::APIClient::OAuth1::DEFAULTS[:callback] + @oauth.display_name.should == + Google::APIClient::OAuth1::DEFAULTS[:display_name] + @oauth.consumer_key.should == + Google::APIClient::OAuth1::DEFAULTS[:consumer_key] + @oauth.consumer_secret.should == + Google::APIClient::OAuth1::DEFAULTS[:consumer_secret] + end +end diff --git a/spec/google/api_client/auth/services/buzz_slow_spec.rb b/spec/google/api_client/auth/services/buzz_slow_spec.rb index 903f5d502..bc30de51a 100644 --- a/spec/google/api_client/auth/services/buzz_slow_spec.rb +++ b/spec/google/api_client/auth/services/buzz_slow_spec.rb @@ -23,6 +23,13 @@ describe Google::APIClient::OAuth1, "configured for use with Buzz" do @oauth = Google::APIClient::OAuth1.new(:service => :buzz) end + it "should not have the default configuration" do + @oauth.authorization_endpoint_uri.should_not == + Google::APIClient::OAuth1::DEFAULTS[:authorization_uri] + @oauth.scopes.should_not == + Google::APIClient::OAuth1::DEFAULTS[:scopes] + end + it "should have the correct authorization_uri" do @oauth.authorization_endpoint_uri.should == "https://www.google.com/buzz/api/auth/OAuthAuthorizeToken"