Class: RubyApiPackCloudways::Connection::CwToken
- Inherits:
-
Object
- Object
- RubyApiPackCloudways::Connection::CwToken
- Defined in:
- lib/ruby_api_pack_cloudways/connection/cw_token.rb
Constant Summary collapse
- TOKEN_EXPIRATION_BUFFER =
Buffer time before token expiration to fetch a new token
300
Instance Attribute Summary collapse
-
#cw_api_url_base ⇒ Object
readonly
Returns the value of attribute cw_api_url_base.
-
#cw_url_path_auth ⇒ Object
readonly
Returns the value of attribute cw_url_path_auth.
-
#cw_user_email ⇒ Object
readonly
Returns the value of attribute cw_user_email.
-
#cw_user_key ⇒ Object
readonly
Returns the value of attribute cw_user_key.
Instance Method Summary collapse
-
#cw_api_token ⇒ Object
Fetch the API token, using caching to avoid unnecessary requests.
-
#initialize ⇒ CwToken
constructor
A new instance of CwToken.
Constructor Details
#initialize ⇒ CwToken
Returns a new instance of CwToken.
11 12 13 14 15 16 17 18 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 11 def initialize @cw_api_url_base = RubyApiPackCloudways.configuration.api_url @cw_url_path_auth = RubyApiPackCloudways.configuration.api_path_token @cw_user_email = RubyApiPackCloudways.configuration.api_email @cw_user_key = RubyApiPackCloudways.configuration.api_key @cached_token = nil @token_expiry = nil end |
Instance Attribute Details
#cw_api_url_base ⇒ Object (readonly)
Returns the value of attribute cw_api_url_base.
9 10 11 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 9 def cw_api_url_base @cw_api_url_base end |
#cw_url_path_auth ⇒ Object (readonly)
Returns the value of attribute cw_url_path_auth.
9 10 11 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 9 def cw_url_path_auth @cw_url_path_auth end |
#cw_user_email ⇒ Object (readonly)
Returns the value of attribute cw_user_email.
9 10 11 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 9 def cw_user_email @cw_user_email end |
#cw_user_key ⇒ Object (readonly)
Returns the value of attribute cw_user_key.
9 10 11 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 9 def cw_user_key @cw_user_key end |
Instance Method Details
#cw_api_token ⇒ Object
Fetch the API token, using caching to avoid unnecessary requests
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_api_pack_cloudways/connection/cw_token.rb', line 21 def cw_api_token # Use cached token if valid and not near expiration return @cached_token if valid_cached_token? # Fetch a new token response = HTTParty.post( "#{@cw_api_url_base}#{@cw_url_path_auth}", headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, body: { email: @cw_user_email, api_key: @cw_user_key }, debug_output: $stdout # Enable debug output for troubleshooting ) if response.code == 200 # Parse the response and cache the token parsed_response = parse_response(response) @cached_token = parsed_response['access_token'] @token_expiry = Time.now + parsed_response['expires_in'] @cached_token else handle_error_response(response) end end |