split storages in own adapter classes, added redis adapter

This commit is contained in:
Volker Zöpfel committed 2013-11-14 15:50:06 +01:00
1 parent bd7cf179f3
commit c2135d9dcf
4 files changed
+129 -23

No files matched your search

@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require 'json'
require 'signet/oauth_2/client'
module Google
@@ -22,34 +21,31 @@ module Google
# JSON serialized file. Meant to resemble the serialized format
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
#
class FileStorage
class Storage
AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth'
TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'
# @return [String] Path to the credentials file.
attr_accessor :path
# @return [Object] Storage object.
attr_accessor :store
# @return [Signet::OAuth2::Client] Path to the credentials file.
# @return [Signet::OAuth2::Client]
attr_reader :authorization
##
# Initializes the FileStorage object.
# Initializes the Storage object.
#
# @param [String] path
# Path to the credentials file.
def initialize(path)
@path= path
# @params [Object] Storage object
def initialize(store)
@store= store
self.authorize
end
##
# Attempt to read in credentials from the specified file.
# Attempt to read in credentials from the specified store.
def load_credentials
if File.exists?(@path) && File.readable?(@path) && File.writable?(@path)
credentials = File.open(path, 'r') { |f| JSON.parse(f.read) }
end
credentials
store.load_credentials
end
def authorize
@@ -62,12 +58,14 @@ module Google
end
##
# refresh credentials and save them to file
# refresh credentials and save them to store
def refresh_authorization
@authorization.refresh!
self.write_credentials(@authorization)
self.write_credentials
end
##
# @return [Hash] with credentials
def credentials_hash
{
:access_token => @authorization.access_token,
@@ -82,17 +80,15 @@ module Google
end
##
# Write the credentials to the specified file.
# Write the credentials to the specified store.
#
# @param [Signet::OAuth2::Client] authorization
# @params [Signet::OAuth2::Client] authorization
# Optional authorization instance. If not provided, the authorization
# already associated with this instance will be written.
def write_credentials(authorization=nil)
@authorization = authorization if authorization
if @authorization.refresh_token
File.open(self.path, 'w') do |file|
file.write(credentials_hash.to_json)
end
store.write_credentials(credentials_hash)
end
end
end
@@ -0,0 +1,59 @@
# Copyright 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'json'
module Google
class APIClient
##
# Represents cached OAuth 2 tokens stored on local disk in a
# JSON serialized file. Meant to resemble the serialized format
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
#
class FileStore
attr_accessor :path
##
# Initializes the FileStorage object.
#
# @param [String] path
# Path to the credentials file.
def initialize(path)
@path= path
end
##
# Attempt to read in credentials from the specified file.
def load_credentials
if File.exists?(path) && File.readable?(path) && File.writable?(path)
credentials = File.open(path, 'r') { |f| JSON.parse(f.read) }
end
credentials
end
##
# Write the credentials to the specified file.
#
# @param [Signet::OAuth2::Client] authorization
# Optional authorization instance. If not provided, the authorization
# already associated with this instance will be written.
def write_credentials(credentials_hash)
File.open(self.path, 'w') do |file|
file.write(credentials_hash.to_json)
end
end
end
end
end
@@ -0,0 +1,51 @@
# Copyright 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'json'
module Google
class APIClient
class RedisStore
attr_accessor :redis
##
# Initializes the RedisStore object.
#
# @params [Object] Redis instance
def initialize(redis)
@redis= redis
end
##
# Attempt to read in credentials from redis.
def load_credentials
credentials = redis.get redis_credentials_key
JSON.parse(credentials) if credentials
end
def redis_credentials_key
"google_api_credentials"
end
##
# Write the credentials to redis.
#
# @params [Hash] credentials
def write_credentials(credentials_hash)
redis.set(redis_credentials_key, credentials_hash.to_json)
end
end
end
end