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
This commit is contained in:
Bob Aman committed 2010-09-18 00:30:02 +00:00
1 parent 3a9d58108a
commit f96412cf23
11 files changed
+130 -222

No files matched your search

+25 -13
View File
@@ -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)
@@ -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
# <code>:service</code>::
# The name of the service.
# <code>:service_version</code>::
# The version of the service.
# <code>:discovery_uri</code>::
# 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
@@ -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
@@ -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
@@ -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