uses new initialize and authorize
[arvados.git] / lib / google / api_client / auth / file_storage.rb
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 require 'signet/oauth_2/client'
16 require_relative 'storage'
17 require_relative 'storages/file_store'
18
19 module Google
20   class APIClient
21
22     ##
23     # DEPRECATED WARNING
24     # This class is deprecated use Storage and FileStore as
25     # mentioned in the samples
26     #
27     # Represents cached OAuth 2 tokens stored on local disk in a
28     # JSON serialized file. Meant to resemble the serialized format
29     # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
30     #
31     class FileStorage
32
33       attr_accessor :storage,
34                     :path
35
36       def initialize(path)
37         Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.")
38         @path = path
39         store = Google::APIClient::FileStore.new(@path)
40         @storage = Google::APIClient::Storage.new(store)
41         @storage.authorize
42       end
43
44       def load_credentials
45         Google::ApiClient.logger("DEPRECATED: Please use Storage Class instead.")
46         storage.authorize
47       end
48
49       def authorization
50         storage.authorization
51       end
52
53       ##
54       # Write the credentials to the specified file.
55       #
56       # @param [Signet::OAuth2::Client] authorization
57       #    Optional authorization instance. If not provided, the authorization
58       #    already associated with this instance will be written.
59       def write_credentials(auth=nil)
60         self.authorization = auth unless auth.nil?
61         storage.write_credentials(self.authorization)
62       end
63     end
64   end
65 end