ghost_google-api-client 0.4.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG.md +77 -0
  2. data/Gemfile +30 -0
  3. data/Gemfile.lock +80 -0
  4. data/LICENSE +202 -0
  5. data/README.md +71 -0
  6. data/Rakefile +42 -0
  7. data/bin/google-api +545 -0
  8. data/lib/compat/multi_json.rb +17 -0
  9. data/lib/google/api_client.rb +802 -0
  10. data/lib/google/api_client/batch.rb +296 -0
  11. data/lib/google/api_client/client_secrets.rb +106 -0
  12. data/lib/google/api_client/discovery.rb +19 -0
  13. data/lib/google/api_client/discovery/api.rb +287 -0
  14. data/lib/google/api_client/discovery/media.rb +77 -0
  15. data/lib/google/api_client/discovery/method.rb +369 -0
  16. data/lib/google/api_client/discovery/resource.rb +150 -0
  17. data/lib/google/api_client/discovery/schema.rb +119 -0
  18. data/lib/google/api_client/environment.rb +42 -0
  19. data/lib/google/api_client/errors.rb +49 -0
  20. data/lib/google/api_client/media.rb +172 -0
  21. data/lib/google/api_client/reference.rb +305 -0
  22. data/lib/google/api_client/result.rb +161 -0
  23. data/lib/google/api_client/service_account.rb +134 -0
  24. data/lib/google/api_client/version.rb +31 -0
  25. data/lib/google/inflection.rb +28 -0
  26. data/spec/fixtures/files/sample.txt +33 -0
  27. data/spec/google/api_client/batch_spec.rb +241 -0
  28. data/spec/google/api_client/discovery_spec.rb +670 -0
  29. data/spec/google/api_client/media_spec.rb +143 -0
  30. data/spec/google/api_client/result_spec.rb +185 -0
  31. data/spec/google/api_client/service_account_spec.rb +58 -0
  32. data/spec/google/api_client_spec.rb +139 -0
  33. data/spec/spec_helper.rb +7 -0
  34. data/tasks/gem.rake +97 -0
  35. data/tasks/git.rake +45 -0
  36. data/tasks/metrics.rake +22 -0
  37. data/tasks/spec.rake +57 -0
  38. data/tasks/wiki.rake +82 -0
  39. data/tasks/yard.rake +29 -0
  40. metadata +253 -0
@@ -0,0 +1,161 @@
1
+ # Copyright 2010 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+
16
+ module Google
17
+ class APIClient
18
+ ##
19
+ # This class wraps a result returned by an API call.
20
+ class Result
21
+ def initialize(reference, request, response)
22
+ @reference = reference
23
+ @request = request
24
+ @response = response
25
+ end
26
+
27
+ attr_reader :reference
28
+
29
+ attr_reader :request
30
+
31
+ attr_reader :response
32
+
33
+ def status
34
+ return @response.status
35
+ end
36
+
37
+ def headers
38
+ return @response.headers
39
+ end
40
+
41
+ def body
42
+ return @response.body
43
+ end
44
+
45
+ def resumable_upload
46
+ @media_upload ||= Google::APIClient::ResumableUpload.new(self, reference.media, self.headers['location'])
47
+ end
48
+
49
+ def media_type
50
+ _, content_type = self.headers.detect do |h, v|
51
+ h.downcase == 'Content-Type'.downcase
52
+ end
53
+ content_type[/^([^;]*);?.*$/, 1].strip.downcase
54
+ end
55
+
56
+ def error?
57
+ return self.response.status >= 400
58
+ end
59
+
60
+ def success?
61
+ return !self.error?
62
+ end
63
+
64
+ def error_message
65
+ if self.data?
66
+ if self.data.respond_to?(:error) &&
67
+ self.data.error.respond_to?(:message)
68
+ # You're going to get a terrible error message if the response isn't
69
+ # parsed successfully as an error.
70
+ return self.data.error.message
71
+ elsif self.data['error'] && self.data['error']['message']
72
+ return self.data['error']['message']
73
+ end
74
+ end
75
+ return self.body
76
+ end
77
+
78
+ def data?
79
+ self.media_type == 'application/json'
80
+ end
81
+
82
+ def data
83
+ return @data ||= (begin
84
+ media_type = self.media_type
85
+ data = self.body
86
+ case media_type
87
+ when 'application/json'
88
+ data = MultiJson.load(data)
89
+ # Strip data wrapper, if present
90
+ data = data['data'] if data.has_key?('data')
91
+ else
92
+ raise ArgumentError,
93
+ "Content-Type not supported for parsing: #{media_type}"
94
+ end
95
+ if @reference.api_method && @reference.api_method.response_schema
96
+ # Automatically parse using the schema designated for the
97
+ # response of this API method.
98
+ data = @reference.api_method.response_schema.new(data)
99
+ data
100
+ else
101
+ # Otherwise, return the raw unparsed value.
102
+ # This value must be indexable like a Hash.
103
+ data
104
+ end
105
+ end)
106
+ end
107
+
108
+ def pagination_type
109
+ return :token
110
+ end
111
+
112
+ def page_token_param
113
+ return "pageToken"
114
+ end
115
+
116
+ def next_page_token
117
+ if self.data.respond_to?(:next_page_token)
118
+ return self.data.next_page_token
119
+ elsif self.data.respond_to?(:[])
120
+ return self.data["nextPageToken"]
121
+ else
122
+ raise TypeError, "Data object did not respond to #next_page_token."
123
+ end
124
+ end
125
+
126
+ def next_page
127
+ merged_parameters = Hash[self.reference.parameters].merge({
128
+ self.page_token_param => self.next_page_token
129
+ })
130
+ # Because References can be coerced to Hashes, we can merge them,
131
+ # preserving all context except the API method parameters that we're
132
+ # using for pagination.
133
+ return Google::APIClient::Reference.new(
134
+ Hash[self.reference].merge(:parameters => merged_parameters)
135
+ )
136
+ end
137
+
138
+ def prev_page_token
139
+ if self.data.respond_to?(:prev_page_token)
140
+ return self.data.prev_page_token
141
+ elsif self.data.respond_to?(:[])
142
+ return self.data["prevPageToken"]
143
+ else
144
+ raise TypeError, "Data object did not respond to #next_page_token."
145
+ end
146
+ end
147
+
148
+ def prev_page
149
+ merged_parameters = Hash[self.reference.parameters].merge({
150
+ self.page_token_param => self.prev_page_token
151
+ })
152
+ # Because References can be coerced to Hashes, we can merge them,
153
+ # preserving all context except the API method parameters that we're
154
+ # using for pagination.
155
+ return Google::APIClient::Reference.new(
156
+ Hash[self.reference].merge(:parameters => merged_parameters)
157
+ )
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,134 @@
1
+ # Copyright 2010 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ require 'jwt'
16
+ require 'signet/oauth_2/client'
17
+
18
+ module Google
19
+ class APIClient
20
+ ##
21
+ # Helper for loading keys from the PKCS12 files downloaded when
22
+ # setting up service accounts at the APIs Console.
23
+ module PKCS12
24
+
25
+ ##
26
+ # Loads a key from PKCS12 file, assuming a single private key
27
+ # is present.
28
+ #
29
+ # @param [String] keyfile
30
+ # Path of the PKCS12 file to load. If not a path to an actual file,
31
+ # assumes the string is the content of the file itself.
32
+ # @param [String] passphrase
33
+ # Passphrase for unlocking the private key
34
+ #
35
+ # @return [OpenSSL::PKey] The private key for signing assertions.
36
+ def self.load_key(keyfile, passphrase)
37
+ begin
38
+ if File.exists?(keyfile)
39
+ content = File.read(keyfile)
40
+ else
41
+ content = keyfile
42
+ end
43
+ pkcs12 = OpenSSL::PKCS12.new(content, passphrase)
44
+ return pkcs12.key
45
+ rescue OpenSSL::PKCS12::PKCS12Error
46
+ raise ArgumentError.new("Invalid keyfile or passphrase")
47
+ end
48
+ end
49
+ end
50
+
51
+ ##
52
+ # Generates access tokens using the JWT assertion profile. Requires a
53
+ # service account & access to the private key.
54
+ class JWTAsserter
55
+ attr_accessor :issuer, :expiry
56
+ attr_reader :scope
57
+ attr_writer :key
58
+
59
+ ##
60
+ # Initializes the asserter for a service account.
61
+ #
62
+ # @param [String] issuer
63
+ # Name/ID of the client issuing the assertion
64
+ # @param [String or Array] scope
65
+ # Scopes to authorize. May be a space delimited string or array of strings
66
+ # @param [OpenSSL::PKey]
67
+ # RSA private key for signing assertions
68
+ def initialize(issuer, scope, key)
69
+ self.issuer = issuer
70
+ self.scope = scope
71
+ self.expiry = 60 # 1 min default
72
+ self.key = key
73
+ end
74
+
75
+ ##
76
+ # Set the scopes to authorize
77
+ #
78
+ # @param [String or Array] scope
79
+ # Scopes to authorize. May be a space delimited string or array of strings
80
+ def scope=(new_scope)
81
+ case new_scope
82
+ when Array
83
+ @scope = new_scope.join(' ')
84
+ when String
85
+ @scope = new_scope
86
+ when nil
87
+ @scope = ''
88
+ else
89
+ raise TypeError, "Expected Array or String, got #{new_scope.class}"
90
+ end
91
+ end
92
+
93
+ ##
94
+ # Builds & signs the assertion.
95
+ #
96
+ # @param [String] person
97
+ # Email address of a user, if requesting a token to act on their behalf
98
+ # @return [String] Encoded JWT
99
+ def to_jwt(person=nil)
100
+ now = Time.new
101
+ assertion = {
102
+ "iss" => @issuer,
103
+ "scope" => self.scope,
104
+ "aud" => "https://accounts.google.com/o/oauth2/token",
105
+ "exp" => (now + expiry).to_i,
106
+ "iat" => now.to_i
107
+ }
108
+ assertion['prn'] = person unless person.nil?
109
+ return JWT.encode(assertion, @key, "RS256")
110
+ end
111
+
112
+ ##
113
+ # Request a new access token.
114
+ #
115
+ # @param [String] person
116
+ # Email address of a user, if requesting a token to act on their behalf
117
+ # @param [Hash] options
118
+ # Pass through to Signet::OAuth2::Client.fetch_access_token
119
+ # @return [Signet::OAuth2::Client] Access token
120
+ #
121
+ # @see Signet::OAuth2::Client.fetch_access_token
122
+ def authorize(person = nil, options={})
123
+ assertion = self.to_jwt(person)
124
+ authorization = Signet::OAuth2::Client.new(
125
+ :token_credential_uri => 'https://accounts.google.com/o/oauth2/token'
126
+ )
127
+ authorization.grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
128
+ authorization.extension_parameters = { :assertion => assertion }
129
+ authorization.fetch_access_token!(options)
130
+ return authorization
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright 2010 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+
16
+ # Used to prevent the class/module from being loaded more than once
17
+ if !defined?(::Google::APIClient::VERSION)
18
+
19
+
20
+ module Google
21
+ class APIClient
22
+ module VERSION
23
+ MAJOR = 0
24
+ MINOR = 4
25
+ TINY = 6
26
+
27
+ STRING = [MAJOR, MINOR, TINY].join('.')
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ # Copyright 2010 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+
16
+ module Google
17
+ if defined?(ActiveSupport::Inflector)
18
+ INFLECTOR = ActiveSupport::Inflector
19
+ else
20
+ begin
21
+ require 'extlib/inflection'
22
+ INFLECTOR = Extlib::Inflection
23
+ rescue LoadError
24
+ require 'active_support/inflector'
25
+ INFLECTOR = ActiveSupport::Inflector
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus posuere urna bibendum diam vulputate fringilla. Fusce elementum fermentum justo id aliquam. Integer vel felis ut arcu elementum lacinia. Duis congue urna eget nisl dapibus tristique molestie turpis sollicitudin. Vivamus in justo quam. Proin condimentum mollis tortor at molestie. Cras luctus, nunc a convallis iaculis, est risus consequat nisi, sit amet sollicitudin metus mi a urna. Aliquam accumsan, massa quis condimentum varius, sapien massa faucibus nibh, a dignissim magna nibh a lacus. Nunc aliquet, nunc ac pulvinar consectetur, sapien lacus hendrerit enim, nec dapibus lorem mi eget risus. Praesent vitae justo eget dolor blandit ullamcorper. Duis id nibh vitae sem aliquam vehicula et ac massa. In neque elit, molestie pulvinar viverra at, vestibulum quis velit.
2
+
3
+ Mauris sit amet placerat enim. Duis vel tellus ac dui auctor tincidunt id nec augue. Donec ut blandit turpis. Mauris dictum urna id urna vestibulum accumsan. Maecenas sagittis urna vitae erat facilisis gravida. Phasellus tellus augue, commodo ut iaculis vitae, interdum ut dolor. Proin at dictum lorem. Quisque pellentesque neque ante, vitae rutrum elit. Pellentesque sit amet erat orci. Praesent justo diam, tristique eu tempus ut, vestibulum eget dui. Maecenas et elementum justo. Cras a augue a elit porttitor placerat eget ut magna.
4
+
5
+ Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam adipiscing tellus in arcu bibendum volutpat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed laoreet faucibus tristique. Duis metus eros, molestie eget dignissim in, imperdiet fermentum nulla. Vestibulum laoreet lorem eu justo vestibulum lobortis. Praesent pharetra leo vel mauris rhoncus commodo sollicitudin ante auctor. Ut sagittis, tortor nec placerat rutrum, neque ipsum cursus nisl, ut lacinia magna risus ac risus. Sed volutpat commodo orci, sodales fermentum dui accumsan eu. Donec egestas ullamcorper elit at condimentum. In euismod sodales posuere. Nullam lacinia tempus molestie. Etiam vitae ullamcorper dui. Fusce congue suscipit arcu, at consectetur diam gravida id. Quisque augue urna, commodo eleifend volutpat vitae, tincidunt ac ligula. Curabitur eget orci nisl, vel placerat ipsum.
6
+
7
+ Curabitur rutrum euismod nisi, consectetur varius tortor condimentum non. Pellentesque rhoncus nisi eu purus ultricies suscipit. Morbi ante nisi, varius nec molestie bibendum, pharetra quis enim. Proin eget nunc ante. Cras aliquam enim vel nunc laoreet ut facilisis nunc interdum. Fusce libero ipsum, posuere eget blandit quis, bibendum vitae quam. Integer dictum faucibus lacus eget facilisis. Duis adipiscing tortor magna, vel tincidunt risus. In non augue eu nisl sodales cursus vel eget nisi. Maecenas dignissim lectus elementum eros fermentum gravida et eget leo. Aenean quis cursus arcu. Mauris posuere purus non diam mattis vehicula. Integer nec orci velit.
8
+
9
+ Integer ac justo ac magna adipiscing condimentum vitae tincidunt dui. Morbi augue arcu, blandit nec interdum sit amet, condimentum vel nisl. Nulla vehicula tincidunt laoreet. Aliquam ornare elementum urna, sed vehicula magna porta id. Vestibulum dictum ultrices tortor sit amet tincidunt. Praesent bibendum, metus vel volutpat interdum, nisl nunc cursus libero, vel congue ligula mi et felis. Nulla mollis elementum nulla, in accumsan risus consequat at. Suspendisse potenti. Vestibulum enim lorem, dignissim ut porta vestibulum, porta eget mi. Fusce a elit ac dui sodales gravida. Pellentesque sed elit at dui dapibus mattis a non arcu.
10
+
11
+ Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In nec posuere augue. Praesent non suscipit arcu. Sed nibh risus, lacinia ut molestie vitae, tristique eget turpis. Sed pretium volutpat arcu, non rutrum leo volutpat sed. Maecenas quis neque nisl, sit amet ornare dolor. Nulla pharetra pulvinar tellus sed eleifend. Aliquam eget mattis nulla. Nulla dictum vehicula velit, non facilisis lorem volutpat id. Fusce scelerisque sem vitae purus dapibus lobortis. Mauris ac turpis nec nibh consequat porttitor. Ut sit amet iaculis lorem. Vivamus blandit erat ac odio venenatis fringilla a sit amet ante. Quisque ut urna sed augue laoreet sagittis.
12
+
13
+ Integer nisl urna, bibendum id lobortis in, tempor non velit. Fusce sed volutpat quam. Suspendisse eu placerat purus. Maecenas quis feugiat lectus. Sed accumsan malesuada dui, a pretium purus facilisis quis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc ac purus id lacus malesuada placerat et in nunc. Ut imperdiet tincidunt est, at consectetur augue egestas hendrerit. Pellentesque eu erat a dui dignissim adipiscing. Integer quis leo non felis placerat eleifend. Fusce luctus mi a lorem mattis eget accumsan libero posuere. Sed pellentesque, odio id pharetra tempus, enim quam placerat metus, auctor aliquam elit mi facilisis quam. Nam at velit et eros rhoncus accumsan.
14
+
15
+ Donec tellus diam, fringilla ac viverra fringilla, rhoncus sit amet purus. Cras et ligula sed nibh tempor gravida. Aliquam id tempus mauris. Ut convallis quam sed arcu varius eget mattis magna tincidunt. Aliquam et suscipit est. Sed metus augue, tristique sed accumsan eget, euismod et augue. Nam augue sapien, placerat vel facilisis eu, tempor id risus. Aliquam mollis egestas mi. Fusce scelerisque convallis mauris quis blandit. Mauris nec ante id lacus sagittis tincidunt ornare vehicula dui. Curabitur tristique mattis nunc, vel cursus libero viverra feugiat. Suspendisse at sapien velit, a lacinia dolor. Vivamus in est non odio feugiat lacinia sodales ut magna.
16
+
17
+ Donec interdum ligula id ipsum dapibus consectetur. Pellentesque vitae posuere ligula. Morbi rhoncus bibendum eleifend. Suspendisse fringilla nunc at elit malesuada vitae ullamcorper lorem laoreet. Suspendisse a ante at ipsum iaculis cursus. Duis accumsan ligula quis nibh luctus pretium. Duis ultrices scelerisque dolor, et vulputate lectus commodo ut.
18
+
19
+ Vestibulum ac tincidunt lorem. Vestibulum lorem massa, dictum a scelerisque ut, convallis vitae eros. Morbi ipsum nisl, lacinia non tempor nec, lobortis id diam. Fusce quis magna nunc. Proin ultricies congue justo sed mattis. Vestibulum sit amet arcu tellus. Quisque ultricies porta massa iaculis vehicula. Vestibulum sollicitudin tempor urna vel sodales. Pellentesque ultricies tellus vel metus porta nec iaculis sapien mollis. Maecenas ullamcorper, metus eget imperdiet sagittis, odio orci dapibus neque, in vulputate nunc nibh non libero. Donec velit quam, lobortis quis tempus a, hendrerit id arcu.
20
+
21
+ Donec nec ante at tortor dignissim mattis. Curabitur vehicula tincidunt magna id sagittis. Proin euismod dignissim porta. Curabitur non turpis purus, in rutrum nulla. Nam turpis nulla, tincidunt et hendrerit non, posuere nec enim. Curabitur leo enim, lobortis ut placerat id, condimentum nec massa. In bibendum, lectus sit amet molestie commodo, felis massa rutrum nisl, ac fermentum ligula lacus in ipsum.
22
+
23
+ Pellentesque mi nulla, scelerisque vitae tempus id, consequat a augue. Quisque vel nisi sit amet ipsum faucibus laoreet sed vitae lorem. Praesent nunc tortor, volutpat ac commodo non, pharetra sed neque. Curabitur nec felis at mi blandit aliquet eu ornare justo. Mauris dignissim purus quis nisl porttitor interdum. Aenean id ipsum enim, blandit commodo justo. Quisque facilisis elit quis velit commodo scelerisque lobortis sapien condimentum. Cras sit amet porttitor velit. Praesent nec tempor arcu.
24
+
25
+ Donec varius mi adipiscing elit semper vel feugiat ipsum dictum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec non quam nisl, ac mattis justo. Vestibulum sed massa eget velit tristique auctor ut ac sapien. Curabitur aliquet ligula eget dui ornare at scelerisque mauris faucibus. Vestibulum id mauris metus, sed vestibulum nibh. Nulla egestas dictum blandit. Mauris vitae nibh at dui mollis lobortis. Phasellus sem leo, euismod at fringilla quis, mollis in nibh. Aenean vel lacus et elit pharetra elementum. Aliquam at ligula id sem bibendum volutpat. Pellentesque quis elit a massa dapibus viverra ut et lorem. Donec nulla eros, iaculis nec commodo vel, suscipit sit amet tortor. Integer tempor, elit at viverra imperdiet, velit sapien laoreet nunc, id laoreet ligula risus vel risus. Nullam sed tortor metus.
26
+
27
+ In nunc orci, tempor vulputate pretium vel, suscipit quis risus. Suspendisse accumsan facilisis felis eget posuere. Donec a faucibus felis. Proin nibh erat, sollicitudin quis vestibulum id, tincidunt quis justo. In sed purus eu nisi dignissim condimentum. Sed mattis dapibus lorem id vulputate. Suspendisse nec elit a augue interdum consequat quis id magna. In eleifend aliquam tempor. In in lacus augue.
28
+
29
+ Ut euismod sollicitudin lorem, id aliquam magna dictum sed. Nunc fringilla lobortis nisi sed consectetur. Nulla facilisi. Aenean nec lobortis augue. Curabitur ullamcorper dapibus libero, vel pellentesque arcu sollicitudin non. Praesent varius, turpis nec sollicitudin bibendum, elit tortor rhoncus lacus, gravida luctus leo nisi in felis. Ut metus eros, molestie non faucibus vel, condimentum ac elit.
30
+
31
+ Suspendisse nisl justo, lacinia sit amet interdum nec, tincidunt placerat urna. Suspendisse potenti. In et odio sed purus malesuada cursus sed nec lectus. Cras commodo, orci sit amet hendrerit iaculis, nunc urna facilisis tellus, vel laoreet odio nulla quis nibh. Maecenas ut justo ut lacus posuere sodales. Vestibulum facilisis fringilla diam at volutpat. Proin a hendrerit urna. Aenean placerat pulvinar arcu, sit amet lobortis neque eleifend in. Aenean risus nulla, facilisis ut tincidunt vitae, fringilla at ligula. Praesent eleifend est at sem lacinia auctor. Nulla ornare nunc in erat laoreet blandit.
32
+
33
+ Suspendisse pharetra leo ac est porta consequat. Nunc sem nibh, gravida vel aliquam a, ornare in tortor. Nulla vel sapien et felis placerat pellentesque id scelerisque nisl. Praesent et posuere.
@@ -0,0 +1,241 @@
1
+ # Copyright 2012 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ require 'spec_helper'
16
+
17
+ require 'google/api_client'
18
+ require 'google/api_client/version'
19
+
20
+ describe Google::APIClient::BatchRequest do
21
+ CLIENT ||= Google::APIClient.new
22
+
23
+ after do
24
+ # Reset client to not-quite-pristine state
25
+ CLIENT.key = nil
26
+ CLIENT.user_ip = nil
27
+ end
28
+
29
+ it 'should raise an error if making an empty batch request' do
30
+ batch = Google::APIClient::BatchRequest.new
31
+
32
+ (lambda do
33
+ CLIENT.execute(batch)
34
+ end).should raise_error(Google::APIClient::BatchError)
35
+ end
36
+
37
+ describe 'with the discovery API' do
38
+ before do
39
+ CLIENT.authorization = nil
40
+ @discovery = CLIENT.discovered_api('discovery', 'v1')
41
+ end
42
+
43
+ describe 'with two valid requests' do
44
+ before do
45
+ @call1 = {
46
+ :api_method => @discovery.apis.get_rest,
47
+ :parameters => {
48
+ 'api' => 'adsense',
49
+ 'version' => 'v1'
50
+ }
51
+ }
52
+
53
+ @call2 = {
54
+ :api_method => @discovery.apis.get_rest,
55
+ :parameters => {
56
+ 'api' => 'discovery',
57
+ 'version' => 'v1'
58
+ }
59
+ }
60
+ end
61
+
62
+ it 'should execute both when using a global callback' do
63
+ block_called = 0
64
+ ids = ['first_call', 'second_call']
65
+ expected_ids = ids.clone
66
+ batch = Google::APIClient::BatchRequest.new do |result|
67
+ block_called += 1
68
+ result.status.should == 200
69
+ expected_ids.should include(result.response.call_id)
70
+ expected_ids.delete(result.response.call_id)
71
+ end
72
+
73
+ batch.add(@call1, ids[0])
74
+ batch.add(@call2, ids[1])
75
+
76
+ CLIENT.execute(batch)
77
+ block_called.should == 2
78
+ end
79
+
80
+ it 'should execute both when using individual callbacks' do
81
+ batch = Google::APIClient::BatchRequest.new
82
+
83
+ call1_returned, call2_returned = false, false
84
+ batch.add(@call1) do |result|
85
+ call1_returned = true
86
+ result.status.should == 200
87
+ end
88
+ batch.add(@call2) do |result|
89
+ call2_returned = true
90
+ result.status.should == 200
91
+ end
92
+
93
+ CLIENT.execute(batch)
94
+ call1_returned.should == true
95
+ call2_returned.should == true
96
+ end
97
+
98
+ it 'should raise an error if using the same call ID more than once' do
99
+ batch = Google::APIClient::BatchRequest.new
100
+
101
+ (lambda do
102
+ batch.add(@call1, 'my_id')
103
+ batch.add(@call2, 'my_id')
104
+ end).should raise_error(Google::APIClient::BatchError)
105
+ end
106
+ end
107
+
108
+ describe 'with a valid request and an invalid one' do
109
+ before do
110
+ @call1 = {
111
+ :api_method => @discovery.apis.get_rest,
112
+ :parameters => {
113
+ 'api' => 'adsense',
114
+ 'version' => 'v1'
115
+ }
116
+ }
117
+
118
+ @call2 = {
119
+ :api_method => @discovery.apis.get_rest,
120
+ :parameters => {
121
+ 'api' => 0,
122
+ 'version' => 1
123
+ }
124
+ }
125
+ end
126
+
127
+ it 'should execute both when using a global callback' do
128
+ block_called = 0
129
+ ids = ['first_call', 'second_call']
130
+ expected_ids = ids.clone
131
+ batch = Google::APIClient::BatchRequest.new do |result|
132
+ block_called += 1
133
+ expected_ids.should include(result.response.call_id)
134
+ expected_ids.delete(result.response.call_id)
135
+ if result.response.call_id == ids[0]
136
+ result.status.should == 200
137
+ else
138
+ result.status.should >= 400
139
+ result.status.should < 500
140
+ end
141
+ end
142
+
143
+ batch.add(@call1, ids[0])
144
+ batch.add(@call2, ids[1])
145
+
146
+ CLIENT.execute(batch)
147
+ block_called.should == 2
148
+ end
149
+
150
+ it 'should execute both when using individual callbacks' do
151
+ batch = Google::APIClient::BatchRequest.new
152
+
153
+ call1_returned, call2_returned = false, false
154
+ batch.add(@call1) do |result|
155
+ call1_returned = true
156
+ result.status.should == 200
157
+ end
158
+ batch.add(@call2) do |result|
159
+ call2_returned = true
160
+ result.status.should >= 400
161
+ result.status.should < 500
162
+ end
163
+
164
+ CLIENT.execute(batch)
165
+ call1_returned.should == true
166
+ call2_returned.should == true
167
+ end
168
+ end
169
+ end
170
+
171
+ describe 'with the calendar API' do
172
+ before do
173
+ CLIENT.authorization = nil
174
+ @calendar = CLIENT.discovered_api('calendar', 'v3')
175
+ end
176
+
177
+ describe 'with two valid requests' do
178
+ before do
179
+ event1 = {
180
+ 'summary' => 'Appointment 1',
181
+ 'location' => 'Somewhere',
182
+ 'start' => {
183
+ 'dateTime' => '2011-01-01T10:00:00.000-07:00'
184
+ },
185
+ 'end' => {
186
+ 'dateTime' => '2011-01-01T10:25:00.000-07:00'
187
+ },
188
+ 'attendees' => [
189
+ {
190
+ 'email' => 'myemail@mydomain.tld'
191
+ }
192
+ ]
193
+ }
194
+
195
+ event2 = {
196
+ 'summary' => 'Appointment 2',
197
+ 'location' => 'Somewhere as well',
198
+ 'start' => {
199
+ 'dateTime' => '2011-01-02T10:00:00.000-07:00'
200
+ },
201
+ 'end' => {
202
+ 'dateTime' => '2011-01-02T10:25:00.000-07:00'
203
+ },
204
+ 'attendees' => [
205
+ {
206
+ 'email' => 'myemail@mydomain.tld'
207
+ }
208
+ ]
209
+ }
210
+
211
+ @call1 = {
212
+ :api_method => @calendar.events.insert,
213
+ :parameters => {'calendarId' => 'myemail@mydomain.tld'},
214
+ :body => MultiJson.dump(event1),
215
+ :headers => {'Content-Type' => 'application/json'}
216
+ }
217
+
218
+ @call2 = {
219
+ :api_method => @calendar.events.insert,
220
+ :parameters => {'calendarId' => 'myemail@mydomain.tld'},
221
+ :body => MultiJson.dump(event2),
222
+ :headers => {'Content-Type' => 'application/json'}
223
+ }
224
+ end
225
+
226
+ it 'should convert to a correct HTTP request' do
227
+ batch = Google::APIClient::BatchRequest.new { |result| }
228
+ batch.add(@call1, '1').add(@call2, '2')
229
+ method, uri, headers, body = batch.to_http_request
230
+ boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
231
+ method.to_s.downcase.should == 'post'
232
+ uri.to_s.should == 'https://www.googleapis.com/batch'
233
+ headers.should == {
234
+ "Content-Type"=>"multipart/mixed; boundary=#{boundary}"
235
+ }
236
+ expected_body = /--#{Regexp.escape(boundary)}\nContent-Type: +application\/http\nContent-ID: +<[\w-]+\+1>\n\nPOST +https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/myemail@mydomain.tld\/events +HTTP\/1.1\nContent-Type: +application\/json\n\n#{Regexp.escape(@call1[:body])}\n\n--#{boundary}\nContent-Type: +application\/http\nContent-ID: +<[\w-]+\+2>\n\nPOST +https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/myemail@mydomain.tld\/events HTTP\/1.1\nContent-Type: +application\/json\n\n#{Regexp.escape(@call2[:body])}\n\n--#{Regexp.escape(boundary)}--/
237
+ body.gsub("\r", "").should =~ expected_body
238
+ end
239
+ end
240
+ end
241
+ end