From f96412cf23909df8565d2bba524bccd3c332d77e Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Sat, 18 Sep 2010 00:30:02 +0000 Subject: [PATCH] Reorganized some of the code and removed unnecessary stuff. git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@35 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef --- README | 16 +++- lib/google/api_client.rb | 38 +++++--- .../{discovery/service.rb => discovery.rb} | 0 lib/google/api_client/discovery/discovery.rb | 87 ------------------- .../{parser => parsers}/json_parser.rb | 12 ++- lib/google/api_client/request/api_request.rb | 28 ------ lib/google/api_client/request/api_response.rb | 29 ------- .../api_client/discovery/discovery_spec.rb | 46 ---------- spec/google/api_client/discovery_spec.rb | 71 +++++++++++++++ .../json_parser_spec.rb} | 21 +++-- spec/google/api_client_spec.rb | 4 +- 11 files changed, 130 insertions(+), 222 deletions(-) rename lib/google/api_client/{discovery/service.rb => discovery.rb} (100%) delete mode 100644 lib/google/api_client/discovery/discovery.rb rename lib/google/api_client/{parser => parsers}/json_parser.rb (87%) delete mode 100644 lib/google/api_client/request/api_request.rb delete mode 100644 lib/google/api_client/request/api_response.rb delete mode 100644 spec/google/api_client/discovery/discovery_spec.rb create mode 100644 spec/google/api_client/discovery_spec.rb rename spec/google/api_client/{parser/parser_spec.rb => parsers/json_parser_spec.rb} (72%) diff --git a/README b/README index 20ef1e097..9ac40b712 100644 --- a/README +++ b/README @@ -15,7 +15,21 @@ The Google API Ruby Client makes it trivial to discover and access supported API == Example Usage - # Some code goes here. + require 'google/api_client' + client = Google::APIClient.new(:service => 'buzz') + client.authorization.fetch_temporary_credential!( + :additional_parameters => { + 'scope' => 'https://www.googleapis.com/auth/buzz' + } + ) + client.authorization.authorization_uri + # Redirect user here + client.authorization.fetch_token_credential!(:verifier => '12345') + response = client.execute( + 'buzz.activities.list', + 'scope' => '@self', 'userId' => '@me', 'alt' => 'json' + ) + status, headers, body = response == Requirements diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index d2c1bb584..369db35be 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -15,7 +15,7 @@ require 'httpadapter' require 'json' -require 'google/api_client/discovery/service' +require 'google/api_client/discovery' module Google #:nodoc: ## @@ -32,9 +32,9 @@ module Google #:nodoc: # Returns the parser used by the client. def parser unless @options[:parser] - require 'google/api_client/parser/json_parser' + require 'google/api_client/parsers/json_parser' # NOTE: Do not rely on this default value, as it may change - @options[:parser] = JSONParser.new + @options[:parser] = JSONParser end return @options[:parser] end @@ -72,9 +72,9 @@ module Google #:nodoc: return @options[:discovery_uri] ||= (begin if @options[:service] service_id = @options[:service] - service_version = @options[:service_version] || '1.0' + service_version = @options[:service_version] || 'v1' "http://www.googleapis.com/discovery/0.1/describe" + - "?api=#{service_id}&apiVersion=#{service_version}" + "?api=#{service_id}" else raise ArgumentError, 'Missing required configuration value, :discovery_uri.' @@ -107,10 +107,9 @@ module Google #:nodoc: services = [] for service_name in service_names versions = self.discovery_document['data'][service_name] - for version_name in versions.keys() + for service_version in versions.keys() service_description = - self.discovery_document['data'][service_name][version_name] - service_version = "%.1f" % version_name.gsub(/^v/, '').to_f + self.discovery_document['data'][service_name][service_version] services << ::Google::APIClient::Service.new( service_name, service_version, @@ -122,7 +121,7 @@ module Google #:nodoc: end) end - def discovered_service(service_name, service_version='1.0') + def discovered_service(service_name, service_version='v1') for service in self.discovered_services if service.name == service_name && service.version.to_s == service_version.to_s @@ -132,7 +131,7 @@ module Google #:nodoc: return nil end - def discovered_method(rpc_name, service_version='1.0') + def discovered_method(rpc_name, service_version='v1') for service in self.discovered_services # This looks kinda weird, but is not a real problem because there's # almost always only one service, and this is memoized anyhow. @@ -143,11 +142,24 @@ module Google #:nodoc: return nil end + def latest_service(service_name) + versions = {} + for service in self.discovered_services + next if service.name != service_name + sortable_version = service.version.gsub(/^v/, '').split('.').map do |v| + v.to_i + end + versions[sortable_version] = service + end + return versions[versions.keys.sort.last] + end + def generate_request( api_method, parameters={}, body='', headers=[], options={}) options={ :signed => true, - :service_version => '1.0' + :parser => self.parser, + :service_version => 'v1' }.merge(options) if api_method.kind_of?(String) api_method = self.discovered_method( @@ -172,8 +184,8 @@ module Google #:nodoc: return self.transmit_request(request) end - def transmit_request(request) - ::HTTPAdapter.transmit(request, self.http_adapter) + def transmit_request(request, adapter=self.http_adapter) + ::HTTPAdapter.transmit(request, adapter) end def sign_request(request) diff --git a/lib/google/api_client/discovery/service.rb b/lib/google/api_client/discovery.rb similarity index 100% rename from lib/google/api_client/discovery/service.rb rename to lib/google/api_client/discovery.rb diff --git a/lib/google/api_client/discovery/discovery.rb b/lib/google/api_client/discovery/discovery.rb deleted file mode 100644 index 882e631f7..000000000 --- a/lib/google/api_client/discovery/discovery.rb +++ /dev/null @@ -1,87 +0,0 @@ -# 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 'json' -require 'addressable/template' - -module Google #:nodoc: - class APIClient #:nodoc: - ## - # A discovery document handler. - 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 - # :service:: - # The name of the service. - # :service_version:: - # The version of the service. - # :discovery_uri:: - # The URI of the discovery document. - # - # @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) - if @options[:service] && !@options[:discovery_uri] - service_id = @options[:service] - service_version = @options[:service_version] || '1.0' - @options[:discovery_uri] = - "http://www.googleapis.com/discovery/0.1/describe" + - "?api=#{service_id}&apiVersion=#{service_version}" - end - unless @options[:discovery_uri] - raise ArgumentError, - 'Missing required configuration value, :discovery_uri.' - end - # 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 - - ## - # Returns the URI of the discovery document. - # - # @return [String] The URI of the discovery document. - def discovery_uri - return @options[:discovery_uri] - end - end - end -end diff --git a/lib/google/api_client/parser/json_parser.rb b/lib/google/api_client/parsers/json_parser.rb similarity index 87% rename from lib/google/api_client/parser/json_parser.rb rename to lib/google/api_client/parsers/json_parser.rb index 15b616901..f2f00d7ab 100644 --- a/lib/google/api_client/parser/json_parser.rb +++ b/lib/google/api_client/parsers/json_parser.rb @@ -16,26 +16,24 @@ require 'json' module Google #:nodoc: class APIClient #:nodoc: - ## # Provides a consistent interface by which to parse request and response # content. # TODO(mattpok): ensure floats, URLs, dates are parsed correctly - class JSONParser + module JSONParser - def generate(hash) + def self.serialize(hash) # JSON parser used can accept arrays as well, but we will limit # to only allow hash to JSON string parsing to keep a simple interface unless hash.instance_of? Hash raise ArgumentError, "JSON generate expected a Hash but got a #{hash.class}." end - - return JSON.generate hash + return JSON.generate(hash) end - def parse(json_string) - return JSON.parse json_string + def self.parse(json_string) + return JSON.parse(json_string) end end end diff --git a/lib/google/api_client/request/api_request.rb b/lib/google/api_client/request/api_request.rb deleted file mode 100644 index d7ecf66ec..000000000 --- a/lib/google/api_client/request/api_request.rb +++ /dev/null @@ -1,28 +0,0 @@ -# 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: - - ## - # A simple request abstraction for API requests. This is the primary - # request object that users will interact with. - # - class APIRequest - - def execute - end - end - end -end diff --git a/lib/google/api_client/request/api_response.rb b/lib/google/api_client/request/api_response.rb deleted file mode 100644 index e9819378b..000000000 --- a/lib/google/api_client/request/api_response.rb +++ /dev/null @@ -1,29 +0,0 @@ -# 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: - - ## - # A simple response abstraction for API requests. Encapsulates raw repsonse - # data and allows user to parse response as a hash or model class. - # This is the primary response object that users will interact with. - # - class APIResponse - - def parse - end - end - end -end diff --git a/spec/google/api_client/discovery/discovery_spec.rb b/spec/google/api_client/discovery/discovery_spec.rb deleted file mode 100644 index 1f7e3795b..000000000 --- a/spec/google/api_client/discovery/discovery_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# 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 'spec_helper' - -require 'oauth' -require 'google/api_client/discovery/discovery' - -describe Google::APIClient::Discovery, 'configured for use with a service' do - before do - @discovery = Google::APIClient::Discovery.new(:service => :magic) - end - - it 'should have the correct discovery document URI' do - @discovery.discovery_uri.should == - 'http://www.googleapis.com/discovery/0.1/describe' + - '?api=magic&apiVersion=1.0' - end -end - -describe Google::APIClient::Discovery, - 'configured for use with a specific service version' do - before do - @discovery = Google::APIClient::Discovery.new( - :service => :magic, - :service_version => 42.0 - ) - end - - it 'should have the correct discovery document URI' do - @discovery.discovery_uri.should == - 'http://www.googleapis.com/discovery/0.1/describe' + - '?api=magic&apiVersion=42.0' - end -end diff --git a/spec/google/api_client/discovery_spec.rb b/spec/google/api_client/discovery_spec.rb new file mode 100644 index 000000000..b168ee89c --- /dev/null +++ b/spec/google/api_client/discovery_spec.rb @@ -0,0 +1,71 @@ +# 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 'spec_helper' + +require 'signet/oauth_1/client' +require 'httpadapter/adapters/net_http' + +require 'google/api_client' +require 'google/api_client/version' +require 'google/api_client/parsers/json_parser' + +describe Google::APIClient, 'configured for the prediction API' do + before do + @client = Google::APIClient.new(:service => 'prediction') + end + + it 'should have multiple versions available' do + @client.discovered_services.size.should > 1 + end + + it 'should correctly determine the latest version' do + @client.latest_service('prediction').version.should_not == 'v1' + end + + it 'should correctly determine the latest version' do + # Sanity check the algorithm + @client.discovered_services.clear + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v1', {}) + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v1.1', {}) + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v1.10', {}) + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v10.1', {}) + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v2.1', {}) + @client.discovered_services << + Google::APIClient::Service.new('magic', 'v10.0', {}) + @client.latest_service('magic').version.should == 'v10.1' + end + + it 'should correctly determine the latest version' do + # Sanity check the algorithm + @client.discovered_services.clear + @client.discovered_services << + Google::APIClient::Service.new('one', 'v3', {}) + @client.discovered_services << + Google::APIClient::Service.new('two', 'v1', {}) + @client.discovered_services << + Google::APIClient::Service.new('two', 'v2', {}) + @client.latest_service('two').version.should == 'v2' + end + + it 'should return nil for bogus service names' do + # Sanity check the algorithm + @client.latest_service('bogus').should == nil + end +end diff --git a/spec/google/api_client/parser/parser_spec.rb b/spec/google/api_client/parsers/json_parser_spec.rb similarity index 72% rename from spec/google/api_client/parser/parser_spec.rb rename to spec/google/api_client/parsers/json_parser_spec.rb index 8b4b17031..d30242233 100644 --- a/spec/google/api_client/parser/parser_spec.rb +++ b/spec/google/api_client/parsers/json_parser_spec.rb @@ -15,25 +15,28 @@ require 'spec_helper' require 'json' -require 'google/api_client/parser/json_parser' +require 'google/api_client/parsers/json_parser' describe Google::APIClient::JSONParser, 'generates json from hash' do before do - @parser = Google::APIClient::JSONParser.new + @parser = Google::APIClient::JSONParser end it 'should translate simple hash to JSON string' do - @parser.generate('test' => 23).should == "{\"test\":23}" + @parser.serialize('test' => 23).should == '{"test":23}' end + it 'should translate simple nested into to nested JSON string' do - @parser.generate({'test' => 23, 'test2' => {'foo' => 'baz', 12 => 3.14 }}).should == - "{\"test2\":{\"12\":3.14,\"foo\":\"baz\"},\"test\":23}" + @parser.serialize({ + 'test' => 23, 'test2' => {'foo' => 'baz', 12 => 3.14 } + }).should == + '{"test2":{"12":3.14,"foo":"baz"},"test":23}' end end describe Google::APIClient::JSONParser, 'parses json string into hash' do before do - @parser = Google::APIClient::JSONParser.new + @parser = Google::APIClient::JSONParser end it 'should parse simple json string into hash' do @@ -41,8 +44,8 @@ describe Google::APIClient::JSONParser, 'parses json string into hash' do end it 'should parse nested json object into hash' do - @parser.parse('{"test":23, "test2":{"bar":"baz", "foo":3.14}}').should == - {'test' => 23, 'test2' => {'bar' => 'baz', 'foo' => 3.14}} + @parser.parse('{"test":23, "test2":{"bar":"baz", "foo":3.14}}').should == { + 'test' => 23, 'test2' => {'bar' => 'baz', 'foo' => 3.14} + } end end - diff --git a/spec/google/api_client_spec.rb b/spec/google/api_client_spec.rb index a38dafc80..e76c10e0e 100644 --- a/spec/google/api_client_spec.rb +++ b/spec/google/api_client_spec.rb @@ -19,7 +19,7 @@ require 'httpadapter/adapters/net_http' require 'google/api_client' require 'google/api_client/version' -require 'google/api_client/parser/json_parser' +require 'google/api_client/parsers/json_parser' describe Google::APIClient, 'with default configuration' do before do @@ -31,7 +31,7 @@ describe Google::APIClient, 'with default configuration' do end it 'should use the default JSON parser' do - @client.parser.should be_instance_of(Google::APIClient::JSONParser) + @client.parser.should be(Google::APIClient::JSONParser) end it 'should use the default OAuth1 client configuration' do