Class: CompanionApi::Base
- Inherits:
-
Object
- Object
- CompanionApi::Base
- Defined in:
- lib/companion_api/base.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#login ⇒ Object
Returns the value of attribute login.
-
#market ⇒ Object
Returns the value of attribute market.
-
#profile ⇒ Object
Returns the value of attribute profile.
Instance Method Summary collapse
-
#initialize(profile_name) ⇒ Base
constructor
A new instance of Base.
- #loggedin? ⇒ Boolean
- #login!(username, password) ⇒ Object
- #token_auth! ⇒ Object
- #valid_token? ⇒ Boolean
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
#account ⇒ Object
Returns the value of attribute account.
4 5 6 |
# File 'lib/companion_api/base.rb', line 4 def account @account end |
#login ⇒ Object
Returns the value of attribute login.
5 6 7 |
# File 'lib/companion_api/base.rb', line 5 def login @login end |
#market ⇒ Object
Returns the value of attribute market.
6 7 8 |
# File 'lib/companion_api/base.rb', line 6 def market @market end |
#profile ⇒ Object
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
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.login_url 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
35 36 37 38 39 40 41 42 |
# File 'lib/companion_api/base.rb', line 35 def valid_token? last_login = @profile.get("lastLogin") return false if last_login.blank? diff = Time.now.to_i - last_login # we use 12 hours for now to refresh tokens a bit more often and prevent expiring diff < 12 * 60 * 60 end |