Class: GSquire::Accounts

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Accounts

Returns a new instance of Accounts.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :path (String)

    Path to GSquire database. Required and must exist.

  • :logger (Logger)

    Logger instance to be used for logging.



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 = options[:logger] || DummyLogger.new
  @tokens = Tokens.new :path => options[:path], :client => oauth_client
  @clients = {}
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



35
36
37
# File 'lib/gsquire/accounts.rb', line 35

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/gsquire/accounts.rb', line 35

def options
  @options
end

#tokensObject (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

Parameters:

  • name (String)

    Account name

Returns:

Raises:



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

Parameters:

  • name (String)

    Account name

  • code (String)

    Authorization code provided by Google

Returns:

  • (String)

    OAuth token string



80
81
82
83
# File 'lib/gsquire/accounts.rb', line 80

def authorize!(name, code)
  token = oauth_client.auth_code.get_token code, redirect_uri: OAUTH_OUTOFBAND
  tokens[name] = token
end

#authorize_urlString

Use this method to get the URL to show user to authorize GSquire

Returns:

  • (String)

    authorization URL



134
135
136
137
138
# File 'lib/gsquire/accounts.rb', line 134

def authorize_url
  oauth_client.auth_code.authorize_url \
    redirect_uri: OAUTH_OUTOFBAND,
    scope: GOOGLE_TASKS_SCOPE
end

#defaultString

Returns default account name

Returns:

  • (String)

    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

Parameters:

  • name (String)

    Account name



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

Parameters:

  • name (String)

    Account name

Returns:

  • (OAuth2::AccessToken)

    Access token associated to that account



89
90
91
# File 'lib/gsquire/accounts.rb', line 89

def delete(name)
  tokens.delete name
end

#eachObject



50
51
52
# File 'lib/gsquire/accounts.rb', line 50

def each
  tokens.keys.each {|key| yield key }
end

#inspectObject



140
141
142
# File 'lib/gsquire/accounts.rb', line 140

def inspect
  tokens.keys.inspect
end

#to_sObject



144
145
146
# File 'lib/gsquire/accounts.rb', line 144

def to_s
  tokens.keys.to_s
end