google-api-client 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +13 -5
  2. data/CHANGELOG.md +14 -0
  3. data/Gemfile +0 -36
  4. data/README.md +12 -1
  5. data/Rakefile +1 -8
  6. data/google-api-client.gemspec +40 -0
  7. data/lib/google/api_client.rb +98 -30
  8. data/lib/google/api_client/auth/compute_service_account.rb +1 -1
  9. data/lib/google/api_client/auth/file_storage.rb +19 -44
  10. data/lib/google/api_client/auth/installed_app.rb +11 -7
  11. data/lib/google/api_client/auth/storage.rb +101 -0
  12. data/lib/google/api_client/auth/storages/file_store.rb +58 -0
  13. data/lib/google/api_client/auth/storages/redis_store.rb +54 -0
  14. data/lib/google/api_client/batch.rb +13 -11
  15. data/lib/google/api_client/charset.rb +33 -0
  16. data/lib/google/api_client/client_secrets.rb +9 -6
  17. data/lib/google/api_client/discovery/api.rb +3 -3
  18. data/lib/google/api_client/discovery/resource.rb +3 -3
  19. data/lib/google/api_client/discovery/schema.rb +3 -5
  20. data/lib/google/api_client/errors.rb +5 -0
  21. data/lib/google/api_client/railtie.rb +2 -1
  22. data/lib/google/api_client/request.rb +1 -2
  23. data/lib/google/api_client/result.rb +4 -2
  24. data/lib/google/api_client/service.rb +2 -2
  25. data/lib/google/api_client/service/batch.rb +7 -0
  26. data/lib/google/api_client/service/stub_generator.rb +4 -2
  27. data/lib/google/api_client/service_account.rb +3 -0
  28. data/lib/google/api_client/version.rb +8 -13
  29. data/spec/google/api_client/auth/storage_spec.rb +122 -0
  30. data/spec/google/api_client/auth/storages/file_store_spec.rb +40 -0
  31. data/spec/google/api_client/auth/storages/redis_store_spec.rb +70 -0
  32. data/spec/google/api_client/batch_spec.rb +29 -30
  33. data/spec/google/api_client/client_secrets_spec.rb +53 -0
  34. data/spec/google/api_client/discovery_spec.rb +101 -91
  35. data/spec/google/api_client/gzip_spec.rb +21 -9
  36. data/spec/google/api_client/media_spec.rb +31 -32
  37. data/spec/google/api_client/request_spec.rb +3 -4
  38. data/spec/google/api_client/result_spec.rb +51 -47
  39. data/spec/google/api_client/service_account_spec.rb +40 -35
  40. data/spec/google/api_client/service_spec.rb +144 -112
  41. data/spec/google/api_client/simple_file_store_spec.rb +30 -34
  42. data/spec/google/api_client_spec.rb +139 -40
  43. data/spec/spec_helper.rb +9 -1
  44. metadata +111 -88
  45. data/CONTRIBUTING.md +0 -32
  46. data/lib/cacerts.pem +0 -2183
  47. data/lib/google/inflection.rb +0 -28
  48. data/spec/fixtures/files/privatekey.p12 +0 -0
  49. data/spec/fixtures/files/sample.txt +0 -33
  50. data/spec/fixtures/files/secret.pem +0 -19
  51. data/tasks/gem.rake +0 -97
  52. data/tasks/git.rake +0 -45
  53. data/tasks/metrics.rake +0 -22
  54. data/tasks/spec.rake +0 -57
  55. data/tasks/wiki.rake +0 -82
  56. data/tasks/yard.rake +0 -29
@@ -1,3 +1,4 @@
1
+ # Encoding: utf-8
1
2
  # Copyright 2012 Google Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,12 +16,12 @@
15
16
  require 'spec_helper'
16
17
 
17
18
  require 'google/api_client'
18
- require 'google/api_client/version'
19
19
 
20
- describe Google::APIClient::Gzip do
20
+ RSpec.describe Google::APIClient::Gzip do
21
21
 
22
22
  def create_connection(&block)
23
23
  Faraday.new do |b|
24
+ b.response :charset
24
25
  b.response :gzip
25
26
  b.adapter :test do |stub|
26
27
  stub.get '/', &block
@@ -33,7 +34,7 @@ describe Google::APIClient::Gzip do
33
34
  [200, {}, 'Hello world']
34
35
  end
35
36
  result = conn.get('/')
36
- result.body.should == "Hello world"
37
+ expect(result.body).to eq("Hello world")
37
38
  end
38
39
 
39
40
  it 'should decompress gziped content' do
@@ -41,9 +42,20 @@ describe Google::APIClient::Gzip do
41
42
  [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
42
43
  end
43
44
  result = conn.get('/')
44
- result.body.should == "Hello world\n"
45
+ expect(result.body).to eq("Hello world\n")
45
46
  end
46
47
 
48
+ it 'should inflate with the correct charset encoding' do
49
+ conn = create_connection do |env|
50
+ [200,
51
+ { 'Content-Encoding' => 'deflate', 'Content-Type' => 'application/json;charset=BIG5'},
52
+ Base64.decode64('eJxb8nLp7t2VAA8fBCI=')]
53
+ end
54
+ result = conn.get('/')
55
+ expect(result.body.encoding).to eq(Encoding::BIG5)
56
+ expect(result.body).to eq('日本語'.encode("BIG5"))
57
+ end
58
+
47
59
  describe 'with API Client' do
48
60
 
49
61
  before do
@@ -55,8 +67,8 @@ describe Google::APIClient::Gzip do
55
67
  it 'should send gzip in user agent' do
56
68
  conn = create_connection do |env|
57
69
  agent = env[:request_headers]['User-Agent']
58
- agent.should_not be_nil
59
- agent.should include 'gzip'
70
+ expect(agent).not_to be_nil
71
+ expect(agent).to include 'gzip'
60
72
  [200, {}, 'Hello world']
61
73
  end
62
74
  @client.execute(:uri => 'http://www.example.com/', :connection => conn)
@@ -65,8 +77,8 @@ describe Google::APIClient::Gzip do
65
77
  it 'should send gzip in accept-encoding' do
66
78
  conn = create_connection do |env|
67
79
  encoding = env[:request_headers]['Accept-Encoding']
68
- encoding.should_not be_nil
69
- encoding.should include 'gzip'
80
+ expect(encoding).not_to be_nil
81
+ expect(encoding).to include 'gzip'
70
82
  [200, {}, 'Hello world']
71
83
  end
72
84
  @client.execute(:uri => 'http://www.example.com/', :connection => conn)
@@ -75,7 +87,7 @@ describe Google::APIClient::Gzip do
75
87
  it 'should not send gzip in accept-encoding if disabled for request' do
76
88
  conn = create_connection do |env|
77
89
  encoding = env[:request_headers]['Accept-Encoding']
78
- encoding.should_not include('gzip') unless encoding.nil?
90
+ expect(encoding).not_to include('gzip') unless encoding.nil?
79
91
  [200, {}, 'Hello world']
80
92
  end
81
93
  response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
@@ -15,15 +15,14 @@
15
15
  require 'spec_helper'
16
16
 
17
17
  require 'google/api_client'
18
- require 'google/api_client/version'
19
18
 
20
19
  fixtures_path = File.expand_path('../../../fixtures', __FILE__)
21
20
 
22
- describe Google::APIClient::UploadIO do
21
+ RSpec.describe Google::APIClient::UploadIO do
23
22
  it 'should reject invalid file paths' do
24
- (lambda do
23
+ expect(lambda do
25
24
  media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
26
- end).should raise_error
25
+ end).to raise_error
27
26
  end
28
27
 
29
28
  describe 'with a file' do
@@ -33,11 +32,11 @@ describe Google::APIClient::UploadIO do
33
32
  end
34
33
 
35
34
  it 'should report the correct file length' do
36
- @media.length.should == File.size(@file)
35
+ expect(@media.length).to eq(File.size(@file))
37
36
  end
38
37
 
39
38
  it 'should have a mime type' do
40
- @media.content_type.should == 'text/plain'
39
+ expect(@media.content_type).to eq('text/plain')
41
40
  end
42
41
  end
43
42
 
@@ -48,64 +47,64 @@ describe Google::APIClient::UploadIO do
48
47
  end
49
48
 
50
49
  it 'should report the correct file length' do
51
- @media.length.should == @content.length
50
+ expect(@media.length).to eq(@content.length)
52
51
  end
53
52
 
54
53
  it 'should have a mime type' do
55
- @media.content_type.should == 'text/plain'
54
+ expect(@media.content_type).to eq('text/plain')
56
55
  end
57
56
  end
58
57
  end
59
58
 
60
- describe Google::APIClient::RangedIO do
59
+ RSpec.describe Google::APIClient::RangedIO do
61
60
  before do
62
61
  @source = StringIO.new("1234567890abcdef")
63
62
  @io = Google::APIClient::RangedIO.new(@source, 1, 5)
64
63
  end
65
64
 
66
65
  it 'should return the correct range when read entirely' do
67
- @io.read.should == "23456"
66
+ expect(@io.read).to eq("23456")
68
67
  end
69
68
 
70
69
  it 'should maintain position' do
71
- @io.read(1).should == '2'
72
- @io.read(2).should == '34'
73
- @io.read(2).should == '56'
70
+ expect(@io.read(1)).to eq('2')
71
+ expect(@io.read(2)).to eq('34')
72
+ expect(@io.read(2)).to eq('56')
74
73
  end
75
74
 
76
75
  it 'should allow rewinds' do
77
- @io.read(2).should == '23'
76
+ expect(@io.read(2)).to eq('23')
78
77
  @io.rewind()
79
- @io.read(2).should == '23'
78
+ expect(@io.read(2)).to eq('23')
80
79
  end
81
80
 
82
81
  it 'should allow setting position' do
83
82
  @io.pos = 3
84
- @io.read.should == '56'
83
+ expect(@io.read).to eq('56')
85
84
  end
86
85
 
87
86
  it 'should not allow position to be set beyond range' do
88
87
  @io.pos = 10
89
- @io.read.should == ''
88
+ expect(@io.read).to eq('')
90
89
  end
91
90
 
92
91
  it 'should return empty string when read amount is zero' do
93
- @io.read(0).should == ''
92
+ expect(@io.read(0)).to eq('')
94
93
  end
95
94
 
96
95
  it 'should return empty string at EOF if amount is nil' do
97
96
  @io.read
98
- @io.read.should == ''
97
+ expect(@io.read).to eq('')
99
98
  end
100
99
 
101
100
  it 'should return nil at EOF if amount is positive int' do
102
101
  @io.read
103
- @io.read(1).should == nil
102
+ expect(@io.read(1)).to eq(nil)
104
103
  end
105
104
 
106
105
  end
107
106
 
108
- describe Google::APIClient::ResumableUpload do
107
+ RSpec.describe Google::APIClient::ResumableUpload do
109
108
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
110
109
 
111
110
  after do
@@ -127,26 +126,26 @@ describe Google::APIClient::ResumableUpload do
127
126
  it 'should consider 20x status as complete' do
128
127
  request = @uploader.to_http_request
129
128
  @uploader.process_http_response(mock_result(200))
130
- @uploader.complete?.should == true
129
+ expect(@uploader.complete?).to eq(true)
131
130
  end
132
131
 
133
132
  it 'should consider 30x status as incomplete' do
134
133
  request = @uploader.to_http_request
135
134
  @uploader.process_http_response(mock_result(308))
136
- @uploader.complete?.should == false
137
- @uploader.expired?.should == false
135
+ expect(@uploader.complete?).to eq(false)
136
+ expect(@uploader.expired?).to eq(false)
138
137
  end
139
138
 
140
139
  it 'should consider 40x status as fatal' do
141
140
  request = @uploader.to_http_request
142
141
  @uploader.process_http_response(mock_result(404))
143
- @uploader.expired?.should == true
142
+ expect(@uploader.expired?).to eq(true)
144
143
  end
145
144
 
146
145
  it 'should detect changes to location' do
147
146
  request = @uploader.to_http_request
148
147
  @uploader.process_http_response(mock_result(308, 'location' => 'https://www.googleapis.com/upload/drive/v1/files/abcdef'))
149
- @uploader.uri.to_s.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef'
148
+ expect(@uploader.uri.to_s).to eq('https://www.googleapis.com/upload/drive/v1/files/abcdef')
150
149
  end
151
150
 
152
151
  it 'should resume from the saved range reported by the server' do
@@ -154,8 +153,8 @@ describe Google::APIClient::ResumableUpload do
154
153
  @uploader.to_http_request # Send bytes 0-199, only 0-99 saved
155
154
  @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
156
155
  method, url, headers, body = @uploader.to_http_request # Send bytes 100-299
157
- headers['Content-Range'].should == "bytes 100-299/#{@media.length}"
158
- headers['Content-length'].should == "200"
156
+ expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
157
+ expect(headers['Content-length']).to eq("200")
159
158
  end
160
159
 
161
160
  it 'should resync the offset after 5xx errors' do
@@ -163,12 +162,12 @@ describe Google::APIClient::ResumableUpload do
163
162
  @uploader.to_http_request
164
163
  @uploader.process_http_response(mock_result(500)) # Invalidates range
165
164
  method, url, headers, body = @uploader.to_http_request # Resync
166
- headers['Content-Range'].should == "bytes */#{@media.length}"
167
- headers['Content-length'].should == "0"
165
+ expect(headers['Content-Range']).to eq("bytes */#{@media.length}")
166
+ expect(headers['Content-length']).to eq("0")
168
167
  @uploader.process_http_response(mock_result(308, 'range' => '0-99'))
169
168
  method, url, headers, body = @uploader.to_http_request # Send next chunk at correct range
170
- headers['Content-Range'].should == "bytes 100-299/#{@media.length}"
171
- headers['Content-length'].should == "200"
169
+ expect(headers['Content-Range']).to eq("bytes 100-299/#{@media.length}")
170
+ expect(headers['Content-length']).to eq("200")
172
171
  end
173
172
 
174
173
  def mock_result(status, headers = {})
@@ -15,16 +15,15 @@
15
15
  require 'spec_helper'
16
16
 
17
17
  require 'google/api_client'
18
- require 'google/api_client/version'
19
18
 
20
- describe Google::APIClient::Request do
19
+ RSpec.describe Google::APIClient::Request do
21
20
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22
21
 
23
22
  it 'should normalize parameter names to strings' do
24
23
  request = Google::APIClient::Request.new(:uri => 'https://www.google.com', :parameters => {
25
24
  :a => '1', 'b' => '2'
26
25
  })
27
- request.parameters['a'].should == '1'
28
- request.parameters['b'].should == '2'
26
+ expect(request.parameters['a']).to eq('1')
27
+ expect(request.parameters['b']).to eq('2')
29
28
  end
30
29
  end
@@ -15,9 +15,8 @@
15
15
  require 'spec_helper'
16
16
 
17
17
  require 'google/api_client'
18
- require 'google/api_client/version'
19
18
 
20
- describe Google::APIClient::Result do
19
+ RSpec.describe Google::APIClient::Result do
21
20
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22
21
 
23
22
  describe 'with the plus API' do
@@ -36,8 +35,8 @@ describe Google::APIClient::Result do
36
35
 
37
36
  # Response double
38
37
  @response = double("response")
39
- @response.stub(:status).and_return(200)
40
- @response.stub(:headers).and_return({
38
+ allow(@response).to receive(:status).and_return(200)
39
+ allow(@response).to receive(:headers).and_return({
41
40
  'etag' => '12345',
42
41
  'x-google-apiary-auth-scopes' =>
43
42
  'https://www.googleapis.com/auth/plus.me',
@@ -51,7 +50,7 @@ describe Google::APIClient::Result do
51
50
 
52
51
  describe 'with a next page token' do
53
52
  before do
54
- @response.stub(:body).and_return(
53
+ allow(@response).to receive(:body).and_return(
55
54
  <<-END_OF_STRING
56
55
  {
57
56
  "kind": "plus#activityFeed",
@@ -70,46 +69,49 @@ describe Google::APIClient::Result do
70
69
  end
71
70
 
72
71
  it 'should indicate a successful response' do
73
- @result.error?.should be_false
72
+ expect(@result.error?).to be_falsey
74
73
  end
75
74
 
76
75
  it 'should return the correct next page token' do
77
- @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
76
+ expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
78
77
  end
79
78
 
80
79
  it 'should escape the next page token when calling next_page' do
81
80
  reference = @result.next_page
82
- Hash[reference.parameters].should include('pageToken')
83
- Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
81
+ expect(Hash[reference.parameters]).to include('pageToken')
82
+ expect(Hash[reference.parameters]['pageToken']).to eq('NEXT+PAGE+TOKEN')
84
83
  url = reference.to_env(CLIENT.connection)[:url]
85
- url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
84
+ expect(url.to_s).to include('pageToken=NEXT%2BPAGE%2BTOKEN')
86
85
  end
87
86
 
88
87
  it 'should return content type correctly' do
89
- @result.media_type.should == 'application/json'
88
+ expect(@result.media_type).to eq('application/json')
90
89
  end
91
90
 
92
91
  it 'should return the result data correctly' do
93
- @result.data?.should be_true
94
- @result.data.class.to_s.should ==
92
+ expect(@result.data?).to be_truthy
93
+ expect(@result.data.class.to_s).to eq(
95
94
  'Google::APIClient::Schema::Plus::V1::ActivityFeed'
96
- @result.data.kind.should == 'plus#activityFeed'
97
- @result.data.etag.should == 'FOO'
98
- @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
99
- @result.data.selfLink.should ==
95
+ )
96
+ expect(@result.data.kind).to eq('plus#activityFeed')
97
+ expect(@result.data.etag).to eq('FOO')
98
+ expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
99
+ expect(@result.data.selfLink).to eq(
100
100
  'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
101
- @result.data.nextLink.should ==
101
+ )
102
+ expect(@result.data.nextLink).to eq(
102
103
  'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
103
104
  'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
104
- @result.data.title.should == 'Plus Public Activity Feed for '
105
- @result.data.id.should == "123456790"
106
- @result.data.items.should be_empty
105
+ )
106
+ expect(@result.data.title).to eq('Plus Public Activity Feed for ')
107
+ expect(@result.data.id).to eq("123456790")
108
+ expect(@result.data.items).to be_empty
107
109
  end
108
110
  end
109
111
 
110
112
  describe 'without a next page token' do
111
113
  before do
112
- @response.stub(:body).and_return(
114
+ allow(@response).to receive(:body).and_return(
113
115
  <<-END_OF_STRING
114
116
  {
115
117
  "kind": "plus#activityFeed",
@@ -126,30 +128,32 @@ describe Google::APIClient::Result do
126
128
  end
127
129
 
128
130
  it 'should not return a next page token' do
129
- @result.next_page_token.should == nil
131
+ expect(@result.next_page_token).to eq(nil)
130
132
  end
131
133
 
132
134
  it 'should return content type correctly' do
133
- @result.media_type.should == 'application/json'
135
+ expect(@result.media_type).to eq('application/json')
134
136
  end
135
137
 
136
138
  it 'should return the result data correctly' do
137
- @result.data?.should be_true
138
- @result.data.class.to_s.should ==
139
+ expect(@result.data?).to be_truthy
140
+ expect(@result.data.class.to_s).to eq(
139
141
  'Google::APIClient::Schema::Plus::V1::ActivityFeed'
140
- @result.data.kind.should == 'plus#activityFeed'
141
- @result.data.etag.should == 'FOO'
142
- @result.data.selfLink.should ==
142
+ )
143
+ expect(@result.data.kind).to eq('plus#activityFeed')
144
+ expect(@result.data.etag).to eq('FOO')
145
+ expect(@result.data.selfLink).to eq(
143
146
  'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
144
- @result.data.title.should == 'Plus Public Activity Feed for '
145
- @result.data.id.should == "123456790"
146
- @result.data.items.should be_empty
147
+ )
148
+ expect(@result.data.title).to eq('Plus Public Activity Feed for ')
149
+ expect(@result.data.id).to eq("123456790")
150
+ expect(@result.data.items).to be_empty
147
151
  end
148
152
  end
149
-
153
+
150
154
  describe 'with JSON error response' do
151
155
  before do
152
- @response.stub(:body).and_return(
156
+ allow(@response).to receive(:body).and_return(
153
157
  <<-END_OF_STRING
154
158
  {
155
159
  "error": {
@@ -166,37 +170,37 @@ describe Google::APIClient::Result do
166
170
  }
167
171
  END_OF_STRING
168
172
  )
169
- @response.stub(:status).and_return(400)
173
+ allow(@response).to receive(:status).and_return(400)
170
174
  @result = Google::APIClient::Result.new(@reference, @response)
171
175
  end
172
-
176
+
173
177
  it 'should return error status correctly' do
174
- @result.error?.should be_true
178
+ expect(@result.error?).to be_truthy
175
179
  end
176
180
 
177
181
  it 'should return the correct error message' do
178
- @result.error_message.should == 'Parse Error'
182
+ expect(@result.error_message).to eq('Parse Error')
179
183
  end
180
184
  end
181
-
185
+
182
186
  describe 'with 204 No Content response' do
183
187
  before do
184
- @response.stub(:body).and_return('')
185
- @response.stub(:status).and_return(204)
186
- @response.stub(:headers).and_return({})
188
+ allow(@response).to receive(:body).and_return('')
189
+ allow(@response).to receive(:status).and_return(204)
190
+ allow(@response).to receive(:headers).and_return({})
187
191
  @result = Google::APIClient::Result.new(@reference, @response)
188
192
  end
189
193
 
190
194
  it 'should indicate no data is available' do
191
- @result.data?.should be_false
195
+ expect(@result.data?).to be_falsey
192
196
  end
193
-
197
+
194
198
  it 'should return nil for data' do
195
- @result.data.should == nil
199
+ expect(@result.data).to eq(nil)
196
200
  end
197
-
201
+
198
202
  it 'should return nil for media_type' do
199
- @result.media_type.should == nil
203
+ expect(@result.media_type).to eq(nil)
200
204
  end
201
205
  end
202
206
  end