Class: Pathfinder::StateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pathfinder_dnd/state_manager.rb

Overview

Sets up the OAuth connection to Google Drive and manages user settings such as the Google Drive key/id of the character sheet document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage = Pathfinder::STORAGE) ⇒ StateManager

create a StateManager with a optional storage backend. Loads data including OAuth tokens and the document identifier from the PStore By default all StateMangers read/write to the Pathfinder settings PStore in ~/.config/pathfinder.pstore



27
28
29
30
31
32
33
34
35
# File 'lib/pathfinder_dnd/state_manager.rb', line 27

def initialize(storage = Pathfinder::STORAGE)
    @storage = storage
    @auth = Pathfinder::OAuth.new(@storage)
    @token = @auth.load_token

    @storage.transaction do
        @doc_id = @storage[:doc]
    end
end

Instance Attribute Details

#authObject

for ease of use



20
21
22
# File 'lib/pathfinder_dnd/state_manager.rb', line 20

def auth
  @auth
end

#doc_idObject

Returns the value of attribute doc_id.



21
22
23
# File 'lib/pathfinder_dnd/state_manager.rb', line 21

def doc_id
  @doc_id
end

#sessionObject

for ease of use



20
21
22
# File 'lib/pathfinder_dnd/state_manager.rb', line 20

def session
  @session
end

#tokenObject

for ease of use



20
21
22
# File 'lib/pathfinder_dnd/state_manager.rb', line 20

def token
  @token
end

Instance Method Details

#authorizeObject

Round-trip the user through Google’s OAuth process



38
39
40
41
42
# File 'lib/pathfinder_dnd/state_manager.rb', line 38

def authorize
    @auth.authorize
    @auth.save_token
    @token = @auth.access_token
end

#get_character_sheetObject

Perform all necessary input to get a character sheet from a user’s Google Drive.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pathfinder_dnd/state_manager.rb', line 52

def get_character_sheet
    if @token.nil?
        self.authorize()
    end

    if @doc_id.nil?
        puts "\n\nPaste the `key=` parameter from your character's Google Drive URL here:"
        self.doc_id = gets.chomp
    end

    @session = GoogleDrive.(@token)

    return Pathfinder::CharacterSheet.new(@session, @doc_id)
end