Class: GSquire::Accounts
- Inherits:
-
Object
- Object
- GSquire::Accounts
- Includes:
- Enumerable
- Defined in:
- lib/gsquire/accounts.rb,
lib/gsquire/accounts/tokens.rb,
lib/gsquire/accounts/tasks_api_middleware.rb
Overview
Main class for GSquire.
It handles multiple accounts and maintains a list of Client instances, each one associated with one Google account. Each client can then perform actions on tasklists and tasks (read, create, update, delete).
Defined Under Namespace
Classes: NotAuthorized, NotFound, TasksApiMiddleware, Tokens
Constant Summary collapse
- CLIENT_ID =
'365482102803.apps.googleusercontent.com'
- CLIENT_SECRET =
'Y3UeEPOUEc60d_DbJOzFsr2Y'
- OAUTH_OUTOFBAND =
'urn:ietf:wg:oauth:2.0:oob'
- GOOGLE_TASKS_SCOPE =
'https://www.googleapis.com/auth/tasks'
- GOOGLE_OAUTH2_AUTH =
'https://accounts.google.com/o/oauth2/auth'
- GOOGLE_OAUTH2_TOKEN =
'https://accounts.google.com/o/oauth2/token'
- DEFAULT_FILE =
'default'
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#[](name) ⇒ Client
Returns an account client.
-
#authorize!(name, code) ⇒ String
(also: #[]=)
Authorizes GSquire with the token Google gave to user.
-
#authorize_url ⇒ String
Use this method to get the URL to show user to authorize GSquire.
-
#default ⇒ String
Returns default account name.
-
#default=(name) ⇒ Object
Sets default account.
-
#delete(name) ⇒ OAuth2::AccessToken
Removes account.
- #each ⇒ Object
-
#initialize(opts = {}) ⇒ Accounts
constructor
A new instance of Accounts.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Accounts
Returns a new instance of Accounts.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gsquire/accounts.rb', line 39 def initialize(opts = {}) if opts[:path].nil? || !File.exist?(opts[:path]) raise ArgumentError, ":path option is required and must exist" end @options = opts @logger = [:logger] || DummyLogger.new @tokens = Tokens.new :path => [:path], :client => oauth_client @clients = {} end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
35 36 37 |
# File 'lib/gsquire/accounts.rb', line 35 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
35 36 37 |
# File 'lib/gsquire/accounts.rb', line 35 def @options end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
35 36 37 |
# File 'lib/gsquire/accounts.rb', line 35 def tokens @tokens end |
Instance Method Details
#[](name) ⇒ Client
Returns an account client
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gsquire/accounts.rb', line 57 def [](name) raise NotFound, "Account #{name} not found" unless tokens.include? name token = tokens[name] if token.expired? and token.refresh_token.to_s.strip.empty? @clients.delete name raise NotAuthorized, "Token for account #{name} is expired and cannot be renewed" end if token.expired? logger.debug "Token for account #{name} expired, renewing" token = tokens.store name, token.refresh! @clients[name] = nil end @clients[name] ||= Client.new token end |
#authorize!(name, code) ⇒ String Also known as: []=
Authorizes GSquire with the token Google gave to user
80 81 82 83 |
# File 'lib/gsquire/accounts.rb', line 80 def (name, code) token = oauth_client.auth_code.get_token code, redirect_uri: OAUTH_OUTOFBAND tokens[name] = token end |
#authorize_url ⇒ String
Use this method to get the URL to show user to authorize GSquire
134 135 136 137 138 |
# File 'lib/gsquire/accounts.rb', line 134 def oauth_client.auth_code. \ redirect_uri: OAUTH_OUTOFBAND, scope: GOOGLE_TASKS_SCOPE end |
#default ⇒ String
Returns default account name
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gsquire/accounts.rb', line 95 def default @default ||= begin name = begin File.read(default_path).strip rescue Errno::ENOENT "" end if name.empty? if tokens.size == 1 self.default = tokens.first.first else nil end else if tokens.include?(name) name else self.default = nil self.default end end end end |
#default=(name) ⇒ Object
Sets default account
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gsquire/accounts.rb', line 121 def default=(name) if name.nil? File.truncate default_path, 0 nil else raise NotFound, "Account #{name} not found" unless tokens.include?(name) File.open(default_path, 'w') {|f| f.write(name) } name end end |
#delete(name) ⇒ OAuth2::AccessToken
Removes account
89 90 91 |
# File 'lib/gsquire/accounts.rb', line 89 def delete(name) tokens.delete name end |
#each ⇒ Object
50 51 52 |
# File 'lib/gsquire/accounts.rb', line 50 def each tokens.keys.each {|key| yield key } end |
#inspect ⇒ Object
140 141 142 |
# File 'lib/gsquire/accounts.rb', line 140 def inspect tokens.keys.inspect end |
#to_s ⇒ Object
144 145 146 |
# File 'lib/gsquire/accounts.rb', line 144 def to_s tokens.keys.to_s end |