jomz-google-api-client 0.7.1

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 (63) hide show
  1. data/CHANGELOG.md +144 -0
  2. data/CONTRIBUTING.md +32 -0
  3. data/Gemfile +41 -0
  4. data/LICENSE +202 -0
  5. data/README.md +192 -0
  6. data/Rakefile +46 -0
  7. data/lib/cacerts.pem +2183 -0
  8. data/lib/compat/multi_json.rb +16 -0
  9. data/lib/google/api_client.rb +672 -0
  10. data/lib/google/api_client/auth/compute_service_account.rb +28 -0
  11. data/lib/google/api_client/auth/file_storage.rb +87 -0
  12. data/lib/google/api_client/auth/installed_app.rb +122 -0
  13. data/lib/google/api_client/auth/jwt_asserter.rb +126 -0
  14. data/lib/google/api_client/auth/key_utils.rb +93 -0
  15. data/lib/google/api_client/auth/pkcs12.rb +41 -0
  16. data/lib/google/api_client/batch.rb +323 -0
  17. data/lib/google/api_client/client_secrets.rb +176 -0
  18. data/lib/google/api_client/discovery.rb +19 -0
  19. data/lib/google/api_client/discovery/api.rb +300 -0
  20. data/lib/google/api_client/discovery/media.rb +77 -0
  21. data/lib/google/api_client/discovery/method.rb +363 -0
  22. data/lib/google/api_client/discovery/resource.rb +156 -0
  23. data/lib/google/api_client/discovery/schema.rb +121 -0
  24. data/lib/google/api_client/environment.rb +42 -0
  25. data/lib/google/api_client/errors.rb +60 -0
  26. data/lib/google/api_client/gzip.rb +28 -0
  27. data/lib/google/api_client/logging.rb +32 -0
  28. data/lib/google/api_client/media.rb +259 -0
  29. data/lib/google/api_client/railtie.rb +16 -0
  30. data/lib/google/api_client/reference.rb +27 -0
  31. data/lib/google/api_client/request.rb +351 -0
  32. data/lib/google/api_client/result.rb +253 -0
  33. data/lib/google/api_client/service.rb +233 -0
  34. data/lib/google/api_client/service/batch.rb +103 -0
  35. data/lib/google/api_client/service/request.rb +144 -0
  36. data/lib/google/api_client/service/resource.rb +40 -0
  37. data/lib/google/api_client/service/result.rb +162 -0
  38. data/lib/google/api_client/service/simple_file_store.rb +151 -0
  39. data/lib/google/api_client/service/stub_generator.rb +59 -0
  40. data/lib/google/api_client/service_account.rb +18 -0
  41. data/lib/google/api_client/version.rb +31 -0
  42. data/lib/google/inflection.rb +28 -0
  43. data/spec/fixtures/files/privatekey.p12 +0 -0
  44. data/spec/fixtures/files/sample.txt +33 -0
  45. data/spec/fixtures/files/secret.pem +19 -0
  46. data/spec/google/api_client/batch_spec.rb +249 -0
  47. data/spec/google/api_client/discovery_spec.rb +652 -0
  48. data/spec/google/api_client/gzip_spec.rb +86 -0
  49. data/spec/google/api_client/media_spec.rb +179 -0
  50. data/spec/google/api_client/request_spec.rb +30 -0
  51. data/spec/google/api_client/result_spec.rb +203 -0
  52. data/spec/google/api_client/service_account_spec.rb +164 -0
  53. data/spec/google/api_client/service_spec.rb +586 -0
  54. data/spec/google/api_client/simple_file_store_spec.rb +137 -0
  55. data/spec/google/api_client_spec.rb +253 -0
  56. data/spec/spec_helper.rb +56 -0
  57. data/tasks/gem.rake +97 -0
  58. data/tasks/git.rake +45 -0
  59. data/tasks/metrics.rake +22 -0
  60. data/tasks/spec.rake +57 -0
  61. data/tasks/wiki.rake +82 -0
  62. data/tasks/yard.rake +29 -0
  63. metadata +309 -0
@@ -0,0 +1,40 @@
1
+ # Copyright 2013 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
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
19
+ # Handles an API resource.
20
+ # Simple class that contains API methods and/or child resources.
21
+ class Resource
22
+ include Google::APIClient::Service::StubGenerator
23
+
24
+ ##
25
+ # Build a resource.
26
+ # This class should not be directly instantiated in user code; resources
27
+ # are instantiated by the stub generation mechanism on Service creation.
28
+ #
29
+ # @param [Google::APIClient::Service] service
30
+ # The Service instance this resource belongs to.
31
+ # @param [Google::APIClient::API, Google::APIClient::Resource] root
32
+ # The node corresponding to this resource.
33
+ def initialize(service, root)
34
+ @service = service
35
+ generate_call_stubs(service, root)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,162 @@
1
+ # Copyright 2013 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
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
19
+ # Handles an API result.
20
+ # Wraps around the Google::APIClient::Result class, making it easier to
21
+ # handle the result (e.g. pagination) and keeping it in line with the rest
22
+ # of the Service programming interface.
23
+ class Result
24
+ extend Forwardable
25
+
26
+ ##
27
+ # Init the result.
28
+ #
29
+ # @param [Google::APIClient::Service::Request] request
30
+ # The original request
31
+ # @param [Google::APIClient::Result] base_result
32
+ # The base result to be wrapped
33
+ def initialize(request, base_result)
34
+ @request = request
35
+ @base_result = base_result
36
+ end
37
+
38
+ # @!attribute [r] status
39
+ # @return [Fixnum] HTTP status code
40
+ # @!attribute [r] headers
41
+ # @return [Hash] HTTP response headers
42
+ # @!attribute [r] body
43
+ # @return [String] HTTP response body
44
+ def_delegators :@base_result, :status, :headers, :body
45
+
46
+ # @return [Google::APIClient::Service::Request] Original request object
47
+ attr_reader :request
48
+
49
+ ##
50
+ # Get the content type of the response
51
+ # @!attribute [r] media_type
52
+ # @return [String]
53
+ # Value of content-type header
54
+ def_delegators :@base_result, :media_type
55
+
56
+ ##
57
+ # Check if request failed
58
+ #
59
+ # @!attribute [r] error?
60
+ # @return [TrueClass, FalseClass]
61
+ # true if result of operation is an error
62
+ def_delegators :@base_result, :error?
63
+
64
+ ##
65
+ # Check if request was successful
66
+ #
67
+ # @!attribute [r] success?
68
+ # @return [TrueClass, FalseClass]
69
+ # true if result of operation was successful
70
+ def_delegators :@base_result, :success?
71
+
72
+ ##
73
+ # Extracts error messages from the response body
74
+ #
75
+ # @!attribute [r] error_message
76
+ # @return [String]
77
+ # error message, if available
78
+ def_delegators :@base_result, :error_message
79
+
80
+ ##
81
+ # Check for parsable data in response
82
+ #
83
+ # @!attribute [r] data?
84
+ # @return [TrueClass, FalseClass]
85
+ # true if body can be parsed
86
+ def_delegators :@base_result, :data?
87
+
88
+ ##
89
+ # Return parsed version of the response body.
90
+ #
91
+ # @!attribute [r] data
92
+ # @return [Object, Hash, String]
93
+ # Object if body parsable from API schema, Hash if JSON, raw body if unable to parse
94
+ def_delegators :@base_result, :data
95
+
96
+ ##
97
+ # Pagination scheme used by this request/response
98
+ #
99
+ # @!attribute [r] pagination_type
100
+ # @return [Symbol]
101
+ # currently always :token
102
+ def_delegators :@base_result, :pagination_type
103
+
104
+ ##
105
+ # Name of the field that contains the pagination token
106
+ #
107
+ # @!attribute [r] page_token_param
108
+ # @return [String]
109
+ # currently always 'pageToken'
110
+ def_delegators :@base_result, :page_token_param
111
+
112
+ ##
113
+ # Get the token used for requesting the next page of data
114
+ #
115
+ # @!attribute [r] next_page_token
116
+ # @return [String]
117
+ # next page tokenx =
118
+ def_delegators :@base_result, :next_page_token
119
+
120
+ ##
121
+ # Get the token used for requesting the previous page of data
122
+ #
123
+ # @!attribute [r] prev_page_token
124
+ # @return [String]
125
+ # previous page token
126
+ def_delegators :@base_result, :prev_page_token
127
+
128
+ # @!attribute [r] resumable_upload
129
+ def resumable_upload
130
+ # TODO(sgomes): implement resumable_upload for Service::Result
131
+ raise NotImplementedError
132
+ end
133
+
134
+ ##
135
+ # Build a request for fetching the next page of data
136
+ #
137
+ # @return [Google::APIClient::Service::Request]
138
+ # API request for retrieving next page
139
+ def next_page
140
+ request = @request.clone
141
+ # Make a deep copy of the parameters.
142
+ request.parameters = Marshal.load(Marshal.dump(request.parameters))
143
+ request.parameters[page_token_param] = self.next_page_token
144
+ return request
145
+ end
146
+
147
+ ##
148
+ # Build a request for fetching the previous page of data
149
+ #
150
+ # @return [Google::APIClient::Service::Request]
151
+ # API request for retrieving previous page
152
+ def prev_page
153
+ request = @request.clone
154
+ # Make a deep copy of the parameters.
155
+ request.parameters = Marshal.load(Marshal.dump(request.parameters))
156
+ request.parameters[page_token_param] = self.prev_page_token
157
+ return request
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,151 @@
1
+ # Copyright 2013 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
+ module Google
16
+ class APIClient
17
+ class Service
18
+
19
+ # Simple file store to be used in the event no ActiveSupport cache store
20
+ # is provided. This is not thread-safe, and does not support a number of
21
+ # features (such as expiration), but it's useful for the simple purpose of
22
+ # caching discovery documents to disk.
23
+ # Implements the basic cache methods of ActiveSupport::Cache::Store in a
24
+ # limited fashion.
25
+ class SimpleFileStore
26
+
27
+ # Creates a new SimpleFileStore.
28
+ #
29
+ # @param [String] file_path
30
+ # The path to the cache file on disk.
31
+ # @param [Object] options
32
+ # The options to be used with this SimpleFileStore. Not implemented.
33
+ def initialize(file_path, options = nil)
34
+ @file_path = file_path.to_s
35
+ end
36
+
37
+ # Returns true if a key exists in the cache.
38
+ #
39
+ # @param [String] name
40
+ # The name of the key. Will always be converted to a string.
41
+ # @param [Object] options
42
+ # The options to be used with this query. Not implemented.
43
+ def exist?(name, options = nil)
44
+ read_file
45
+ @cache.nil? ? nil : @cache.include?(name.to_s)
46
+ end
47
+
48
+ # Fetches data from the cache and returns it, using the given key.
49
+ # If the key is missing and no block is passed, returns nil.
50
+ # If the key is missing and a block is passed, executes the block, sets
51
+ # the key to its value, and returns it.
52
+ #
53
+ # @param [String] name
54
+ # The name of the key. Will always be converted to a string.
55
+ # @param [Object] options
56
+ # The options to be used with this query. Not implemented.
57
+ # @yield [String]
58
+ # optional block with the default value if the key is missing
59
+ def fetch(name, options = nil)
60
+ read_file
61
+ if block_given?
62
+ entry = read(name.to_s, options)
63
+ if entry.nil?
64
+ value = yield name.to_s
65
+ write(name.to_s, value)
66
+ return value
67
+ else
68
+ return entry
69
+ end
70
+ else
71
+ return read(name.to_s, options)
72
+ end
73
+ end
74
+
75
+ # Fetches data from the cache, using the given key.
76
+ # Returns nil if the key is missing.
77
+ #
78
+ # @param [String] name
79
+ # The name of the key. Will always be converted to a string.
80
+ # @param [Object] options
81
+ # The options to be used with this query. Not implemented.
82
+ def read(name, options = nil)
83
+ read_file
84
+ @cache.nil? ? nil : @cache[name.to_s]
85
+ end
86
+
87
+ # Writes the value to the cache, with the key.
88
+ #
89
+ # @param [String] name
90
+ # The name of the key. Will always be converted to a string.
91
+ # @param [Object] value
92
+ # The value to be written.
93
+ # @param [Object] options
94
+ # The options to be used with this query. Not implemented.
95
+ def write(name, value, options = nil)
96
+ read_file
97
+ @cache = {} if @cache.nil?
98
+ @cache[name.to_s] = value
99
+ write_file
100
+ return nil
101
+ end
102
+
103
+ # Deletes an entry in the cache.
104
+ # Returns true if an entry is deleted.
105
+ #
106
+ # @param [String] name
107
+ # The name of the key. Will always be converted to a string.
108
+ # @param [Object] options
109
+ # The options to be used with this query. Not implemented.
110
+ def delete(name, options = nil)
111
+ read_file
112
+ return nil if @cache.nil?
113
+ if @cache.include? name.to_s
114
+ @cache.delete name.to_s
115
+ write_file
116
+ return true
117
+ else
118
+ return nil
119
+ end
120
+ end
121
+
122
+ protected
123
+
124
+ # Read the entire cache file from disk.
125
+ # Will avoid reading if there have been no changes.
126
+ def read_file
127
+ if !File.exists? @file_path
128
+ @cache = nil
129
+ else
130
+ # Check for changes after our last read or write.
131
+ if @last_change.nil? || File.mtime(@file_path) > @last_change
132
+ File.open(@file_path) do |file|
133
+ @cache = Marshal.load(file)
134
+ @last_change = file.mtime
135
+ end
136
+ end
137
+ end
138
+ return @cache
139
+ end
140
+
141
+ # Write the entire cache contents to disk.
142
+ def write_file
143
+ File.open(@file_path, 'w') do |file|
144
+ Marshal.dump(@cache, file)
145
+ end
146
+ @last_change = File.mtime(@file_path)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,59 @@
1
+ # Copyright 2013 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
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
19
+ # Auxiliary mixin to generate resource and method stubs.
20
+ # Used by the Service and Service::Resource classes to generate both
21
+ # top-level and nested resources and methods.
22
+ module StubGenerator
23
+ def generate_call_stubs(service, root)
24
+ metaclass = (class << self; self; end)
25
+
26
+ # Handle resources.
27
+ root.discovered_resources.each do |resource|
28
+ method_name = Google::INFLECTOR.underscore(resource.name).to_sym
29
+ if !self.respond_to?(method_name)
30
+ metaclass.send(:define_method, method_name) do
31
+ Google::APIClient::Service::Resource.new(service, resource)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Handle methods.
37
+ root.discovered_methods.each do |method|
38
+ method_name = Google::INFLECTOR.underscore(method.name).to_sym
39
+ if !self.respond_to?(method_name)
40
+ metaclass.send(:define_method, method_name) do |*args|
41
+ if args.length > 1
42
+ raise ArgumentError,
43
+ "wrong number of arguments (#{args.length} for 1)"
44
+ elsif !args.first.respond_to?(:to_hash) && !args.first.nil?
45
+ raise ArgumentError,
46
+ "expected parameter Hash, got #{args.first.class}"
47
+ else
48
+ return Google::APIClient::Service::Request.new(
49
+ service, method, args.first
50
+ )
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
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 'google/api_client/auth/pkcs12'
16
+ require 'google/api_client/auth/jwt_asserter'
17
+ require 'google/api_client/auth/key_utils'
18
+ require 'google/api_client/auth/compute_service_account'
@@ -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 = 7
25
+ TINY = 1
26
+ PATCH = nil
27
+ STRING = [MAJOR, MINOR, TINY, PATCH].compact.join('.')
28
+ end
29
+ end
30
+ end
31
+ end