Sha256: e917ceb45bf801b655c0ee2d10dbc924fd54ea4e85a8f1a9e6022817ccd46f85

Contents?: true

Size: 909 Bytes

Versions: 2

Compression:

Stored size: 909 Bytes

Contents

module Lifen
  class Token
    include Virtus.model(finalize: false)

    attribute :user, "Lifen::User"

    attribute :value, String
    attribute :expires_at, Integer

    def to_s
      value
    end

    def active?
      valid? and !has_expired?
    end

    def valid?
      !value.nil? and value.length > 0 and !expires_at.nil?
    end

    def has_expired?
      return true if expires_at.nil?

      return expires_at < Time.now.to_i
    end

    def refresh
      json = client.post("/oauth/admin/third_party/access_token?accountUuid=#{user.uuid}")

      self.value = json["access_token"]
      self.expires_at = Time.now.to_i + json["expires_in"].to_i
    end

    def refresh_once_if_needed
      return if active?

      refresh

      raise Error, "Token can't be refreshed" if !active?
    end

    private

      def client
        @client ||= AppAuthenticatedClient.new
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lifen-1.0.2 lib/lifen/token.rb
lifen-1.0.1 lib/lifen/token.rb