Class: CompanionApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/companion_api/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_name) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/companion_api/base.rb', line 8

def initialize(profile_name)
  @profile = Profile.new(profile_name)
  @account = CompanionApi::Resources::Account.new(@profile)
  @login = CompanionApi::Resources::Login.new(@profile)
  @market = CompanionApi::Resources::Market.new(@profile)
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



4
5
6
# File 'lib/companion_api/base.rb', line 4

def 
  @account
end

#loginObject

Returns the value of attribute login.



5
6
7
# File 'lib/companion_api/base.rb', line 5

def 
  @login
end

#marketObject

Returns the value of attribute market.



6
7
8
# File 'lib/companion_api/base.rb', line 6

def market
  @market
end

#profileObject

Returns the value of attribute profile.



3
4
5
# File 'lib/companion_api/base.rb', line 3

def profile
  @profile
end

Instance Method Details

#loggedin?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/companion_api/base.rb', line 31

def loggedin?
  @login.character.present?
end

#login!(username, password) ⇒ Object



15
16
17
18
19
# File 'lib/companion_api/base.rb', line 15

def login!(username, password)
  raise CompanionApi::Error, 'no username or password specified' if username.blank? || password.blank?

  @account.auto_login!(username, password)
end

#token_auth!Object



21
22
23
24
25
26
27
28
29
# File 'lib/companion_api/base.rb', line 21

def token_auth!
  @account.token_login!

  url = @account.
  CompanionApi.config.logger.info("please visit #{url} and hit enter afterwards")
  gets
  res = @login.post_auth
  raise CompanionApi::Error, 'invalid response received' if res['status'] != 200
end

#valid_token?Boolean

Returns:

  • (Boolean)


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

def valid_token?
   = @profile.get("lastLogin")
  return false if .blank?

  diff = Time.now.to_i - 
  # we use 12 hours for now to refresh tokens a bit more often and prevent expiring
  diff < 12 * 60 * 60
end