From 70ed84ffec97bfb04591c01304b0a413c481dd61 Mon Sep 17 00:00:00 2001 From: "bobaman@google.com" Date: Tue, 10 Aug 2010 04:44:14 +0000 Subject: [PATCH] Added some stubs for OAuth and HTTP. git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@3 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef --- lib/google/api_client.rb | 21 +++++ lib/google/api_client/auth/oauth_1.rb | 79 +++++++++++++++++++ .../api_client/transport/http_transport.rb | 21 +++++ lib/google/api_client/version.rb | 4 +- spec/google/api_client/auth/oauth_1_spec.rb | 44 +++++++++++ tasks/gem.rake | 8 +- tasks/rdoc.rake | 3 - tasks/yard.rake | 25 ++++++ 8 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 lib/google/api_client/auth/oauth_1.rb create mode 100644 lib/google/api_client/transport/http_transport.rb create mode 100644 spec/google/api_client/auth/oauth_1_spec.rb create mode 100644 tasks/yard.rake diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 684d70f6a..081ba7d54 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -13,3 +13,24 @@ # limitations under the License. require "google/api_client/version" + +module Google #:nodoc: + ## + # This class manages communication with a single API. + class APIClient + def initialize(options={}) + @options = { + # TODO: What configuration options need to go here? + }.merge(options) + unless @options[:authentication] + require "google/api_client/auth/oauth_1" + # NOTE: Do not rely on this default value, as it may change + @options[:authentication] = OAuth1.new + end + unless @options[:transport] + require "google/api_client/transport/http_transport" + @options[:transport] = HTTPTransport + end + end + end +end diff --git a/lib/google/api_client/auth/oauth_1.rb b/lib/google/api_client/auth/oauth_1.rb new file mode 100644 index 000000000..17f53feee --- /dev/null +++ b/lib/google/api_client/auth/oauth_1.rb @@ -0,0 +1,79 @@ +# Copyright 2010 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "addressable/uri" +require "oauth" + +module Google #:nodoc: + class APIClient #:nodoc: + class OAuth1 + def initialize(options={}) + @options = { + :request_token_uri => + 'https://www.google.com/accounts/OAuthGetRequestToken', + :authorization_uri => + 'https://www.google.com/accounts/OAuthAuthorizeToken', + :access_token_uri => + 'https://www.google.com/accounts/OAuthGetAccessToken', + :consumer_key => "anonymous", + :consumer_secret => "anonymous" + }.merge(options) + @options[:request_token_uri] = + Addressable::URI.parse(@options[:request_token_uri]) + @options[:authorization_uri] = + Addressable::URI.parse(@options[:authorization_uri]) + @options[:access_token_uri] = + Addressable::URI.parse(@options[:access_token_uri]) + if (@options[:request_token_uri].site != + @options[:authorization_uri].site) || + (@options[:request_token_uri].site != + @options[:authorization_uri].site) + raise ArgumentError, "All OAuth endpoints must be on the same site." + end + @oauth_consumer = ::OAuth::Consumer.new( + @options[:consumer_key], @options[:consumer_secret], { + # This is an extremely unfortunate way to configure the consumer, + # but not worth forking or patching to resolve. Yet. + :site => @options[:request_token_uri].site, + :scheme => :header, + :http_method => :post, + :request_token_path => @options[:request_token_uri].request_uri, + :access_token_path => @options[:access_token_uri].request_uri, + :authorize_path => @options[:authorization_uri].request_uri + } + ) + end + + def consumer_key + return @oauth_consumer.key + end + + def consumer_secret + return @oauth_consumer.secret + end + + def request_token_uri + return @oauth_consumer.request_token_url + end + + def authorization_uri + return @oauth_consumer.authorize_url + end + + def access_token_uri + return @oauth_consumer.access_token_url + end + end + end +end diff --git a/lib/google/api_client/transport/http_transport.rb b/lib/google/api_client/transport/http_transport.rb new file mode 100644 index 000000000..823dca714 --- /dev/null +++ b/lib/google/api_client/transport/http_transport.rb @@ -0,0 +1,21 @@ +# Copyright 2010 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module Google #:nodoc: + class APIClient #:nodoc: + class HTTPTransport + + end + end +end diff --git a/lib/google/api_client/version.rb b/lib/google/api_client/version.rb index dd8e8a806..6b93ae8be 100644 --- a/lib/google/api_client/version.rb +++ b/lib/google/api_client/version.rb @@ -14,8 +14,8 @@ # Used to prevent the class/module from being loaded more than once unless defined? Google::APIClient::VERSION - module Google - module APIClient #:nodoc: + module Google #:nodoc: + class APIClient #:nodoc: module VERSION #:nodoc: MAJOR = 0 MINOR = 1 diff --git a/spec/google/api_client/auth/oauth_1_spec.rb b/spec/google/api_client/auth/oauth_1_spec.rb new file mode 100644 index 000000000..ec6d151c9 --- /dev/null +++ b/spec/google/api_client/auth/oauth_1_spec.rb @@ -0,0 +1,44 @@ +# Copyright 2010 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "google/api_client/auth/oauth_1" + +describe Google::APIClient::OAuth1, "in the default configuration" do + before do + @oauth = Google::APIClient::OAuth1.new + end + + it "should have the correct request_token_uri" do + @oauth.request_token_uri.should == + "https://www.google.com/accounts/OAuthGetRequestToken" + end + + it "should have the correct authorization_uri" do + @oauth.authorization_uri.should == + "https://www.google.com/accounts/OAuthAuthorizeToken" + end + + it "should have the correct access_token_uri" do + @oauth.access_token_uri.should == + "https://www.google.com/accounts/OAuthGetAccessToken" + end + + it "should have the correct consumer_key" do + @oauth.consumer_key.should == "anonymous" + end + + it "should have the correct consumer_secret" do + @oauth.consumer_secret.should == "anonymous" + end +end diff --git a/tasks/gem.rake b/tasks/gem.rake index e6f9c74e6..627a9cb28 100644 --- a/tasks/gem.rake +++ b/tasks/gem.rake @@ -18,9 +18,13 @@ namespace :gem do s.extra_rdoc_files = %w( README ) s.rdoc_options.concat ["--main", "README"] - s.add_development_dependency("rake", ">= 0.8.3") - s.add_development_dependency("rspec", ">= 1.1.11") + s.add_runtime_dependency("oauth", ">= 0.4.1") + s.add_runtime_dependency("addressable", ">= 2.2.0") + + s.add_development_dependency("rake", ">= 0.7.3") + s.add_development_dependency("rspec", ">= 1.0.8") s.add_development_dependency("launchy", ">= 0.3.2") + s.add_development_dependency("diff-lcs", ">= 1.1.2") s.require_path = "lib" diff --git a/tasks/rdoc.rake b/tasks/rdoc.rake index 1f636c969..00ef19be1 100644 --- a/tasks/rdoc.rake +++ b/tasks/rdoc.rake @@ -23,7 +23,4 @@ namespace :doc do end end -desc "Alias to doc:rdoc" -task "doc" => "doc:rdoc" - task "clobber" => ["doc:clobber_rdoc", "doc:clobber_ri"] diff --git a/tasks/yard.rake b/tasks/yard.rake new file mode 100644 index 000000000..62d6283d4 --- /dev/null +++ b/tasks/yard.rake @@ -0,0 +1,25 @@ +require "rake" + +begin + require "yard/rake/yardoc_task" + + namespace :doc do + desc "Generate Yardoc documentation" + YARD::Rake::YardocTask.new do |yardoc| + yardoc.name = "yard" + yardoc.options = "-o doc/" + yardoc.files = FileList[ + "lib/**/*.rb", "ext/**/*.c", "-", "README", "CHANGELOG", "LICENSE" + ].exclude(/^[_\.]/) + end + end + + task "clobber" => ["doc:clobber_yard"] + + desc "Alias to doc:yard" + task "doc" => "doc:yard" +rescue LoadError + # If yard isn't available, it's not the end of the world + desc "Alias to doc:rdoc" + task "doc" => "doc:rdoc" +end