Class: BooticCli::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bootic_cli/session.rb

Defined Under Namespace

Classes: NullCacheStore

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Session

Returns a new instance of Session.



7
8
9
10
# File 'lib/bootic_cli/session.rb', line 7

def initialize(store)
  @store = store
  upgrade!
end

Instance Method Details

#clientObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bootic_cli/session.rb', line 91

def client
  @client ||= begin
    raise "First setup credentials and log in" unless ready?
    BooticClient.configure do |c|
      c.auth_host = config[:auth_host] if config[:auth_host]
      c.api_root = config[:api_root] if config[:api_root]
      c.client_id = config[:client_id]
      c.client_secret = config[:client_secret]
      c.logger = Logger.new(STDOUT)
      c.logging = false
      c.cache_store = NullCacheStore.new
    end

    BooticClient.client(:authorized, access_token: config[:access_token]) do |new_token|
      store.transaction{ store['access_token'] = new_token }
    end
  end
end

#configObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bootic_cli/session.rb', line 62

def config
  @config ||= store.transaction do
    h = {
      client_id: store['client_id'],
      client_secret: store['client_secret'],
      access_token: store['access_token']
    }
    h[:auth_host] = store['auth_host'] if store['auth_host']
    h[:api_root] = store['api_root'] if store['api_root']
    h
  end
end

#erase!Object



52
53
54
# File 'lib/bootic_cli/session.rb', line 52

def erase!
  store.erase
end

#logged_in?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/bootic_cli/session.rb', line 27

def logged_in?
  store.transaction{ store['access_token'] }
end

#login(username, pwd, scope) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/bootic_cli/session.rb', line 44

def (username, pwd, scope)
  token = oauth_client.password.get_token(username, pwd, 'scope' => scope)

  store.transaction do
    store['access_token'] = token.token
  end
end

#logout!Object



56
57
58
59
60
# File 'lib/bootic_cli/session.rb', line 56

def logout!
  store.transaction do
    store['access_token'] = nil
  end
end

#needs_upgrade?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/bootic_cli/session.rb', line 12

def needs_upgrade?
  store.needs_upgrade?
end

#ready?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/bootic_cli/session.rb', line 31

def ready?
  setup? && logged_in?
end

#setup(client_id, client_secret, auth_host: nil, api_root: nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/bootic_cli/session.rb', line 35

def setup(client_id, client_secret, auth_host: nil, api_root: nil)
  store.transaction do
    store['client_id'] = client_id
    store['client_secret'] = client_secret
    store['auth_host'] = auth_host if auth_host
    store['api_root'] = api_root if api_root
  end
end

#setup?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/bootic_cli/session.rb', line 21

def setup?
  store.transaction do
    store['client_id'] && store['client_secret']
  end
end

#upgrade!Object



16
17
18
19
# File 'lib/bootic_cli/session.rb', line 16

def upgrade!
  store.upgrade!
  self
end