1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'addressable/uri'
18 require 'google/api_client/discovery'
23 def initialize(options={})
24 # We only need this to do lookups on method ID String values
25 # It's optional, but method ID lookups will fail if the client is
27 @client = options[:client]
28 @version = options[:version] || 'v1'
30 self.api_method = options[:api_method]
31 self.parameters = options[:parameters] || {}
32 # These parameters are handled differently because they're not
33 # parameters to the API method, but rather to the API system.
34 self.parameters['key'] ||= options[:key] if options[:key]
35 self.parameters['user_ip'] ||= options[:user_ip] if options[:user_ip]
36 self.headers = options[:headers] || []
38 self.body = options[:body]
39 elsif options[:merged_body]
40 self.merged_body = options[:merged_body]
41 elsif options[:body_object]
42 if options[:body_object].respond_to?(:to_json)
43 serialized_body = options[:body_object].to_json
44 elsif options[:body_object].respond_to?(:to_hash)
45 serialized_body = JSON.generate(options[:body_object].to_hash)
48 'Could not convert body object to JSON.' +
49 'Must respond to :to_json or :to_hash.'
51 self.merged_body = serialized_body
55 unless self.api_method
56 self.http_method = options[:http_method] || 'GET'
57 self.uri = options[:uri]
65 def api_method=(new_api_method)
66 if new_api_method.kind_of?(Google::APIClient::Method) ||
68 @api_method = new_api_method
69 elsif new_api_method.respond_to?(:to_str) ||
70 new_api_method.kind_of?(Symbol)
73 "API method lookup impossible without client instance."
75 new_api_method = new_api_method.to_s
76 # This method of guessing the API is unreliable. This will fail for
77 # APIs where the first segment of the RPC name does not match the
78 # service name. However, this is a fallback mechanism anyway.
79 # Developers should be passing in a reference to the method, rather
80 # than passing in a string or symbol. This should raise an error
81 # in the case of a mismatch.
82 api = new_api_method[/^([^.]+)\./, 1]
83 @api_method = @client.discovered_method(
84 new_api_method, api, @version
87 # Ditch the client reference, we won't need it again.
90 raise ArgumentError, "API method could not be found."
94 "Expected Google::APIClient::Method, got #{new_api_method.class}."
102 def parameters=(new_parameters)
103 # No type-checking needed, the Method class handles this.
104 @parameters = new_parameters
112 if new_body.respond_to?(:each)
115 raise TypeError, "Expected body to respond to :each."
120 return (self.body.inject(StringIO.new) do |accu, chunk|
126 def merged_body=(new_merged_body)
127 if new_merged_body.respond_to?(:string)
128 new_merged_body = new_merged_body.string
129 elsif new_merged_body.respond_to?(:to_str)
130 new_merged_body = new_merged_body.to_str
133 "Expected String or StringIO, got #{new_merged_body.class}."
135 self.body = [new_merged_body]
139 return @headers ||= []
142 def headers=(new_headers)
143 if new_headers.kind_of?(Array) || new_headers.kind_of?(Hash)
144 @headers = new_headers
146 raise TypeError, "Expected Hash or Array, got #{new_headers.class}."
151 return @http_method ||= self.api_method.http_method
154 def http_method=(new_http_method)
155 if new_http_method.kind_of?(Symbol)
156 @http_method = new_http_method.to_s.upcase
157 elsif new_http_method.respond_to?(:to_str)
158 @http_method = new_http_method.to_str.upcase
161 "Expected String or Symbol, got #{new_http_method.class}."
166 return @uri ||= self.api_method.generate_uri(self.parameters)
170 @uri = Addressable::URI.parse(new_uri)
175 return self.api_method.generate_request(
176 self.parameters, self.merged_body, self.headers
179 return [self.http_method, self.uri, self.headers, self.body]
186 options[:api_method] = self.api_method
187 options[:parameters] = self.parameters
189 options[:http_method] = self.http_method
190 options[:uri] = self.uri
192 options[:headers] = self.headers
193 options[:body] = self.body