Sha256: bee6407611a3b22fa2a9e54d067c2ad5432cf27696110e26012da3de391a7703

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'json'

module Github
  module Authentication
    class Token
      attr_reader :expires_at

      def self.from_json(data)
        return nil if data.nil?

        token, expires_at = JSON.parse(data).values_at('token', 'expires_at')
        new(token, Time.iso8601(expires_at))
      rescue JSON::ParserError
        nil
      end

      def initialize(token, expires_at)
        @token = token
        @expires_at = expires_at
      end

      def expires_in
        (@expires_at - Time.now.utc).to_i
      end

      def expired?(seconds_ttl: 300)
        @expires_at < Time.now.utc + seconds_ttl
      end

      def valid_for?(ttl)
        !expired?(seconds_ttl: ttl)
      end

      def inspect
        # Truncating the token should be enough not to leak it in error messages etc
        "#<#{self.class.name} @token=#{truncate(@token, 10)} @expires_at=#{@expires_at}>"
      end

      def to_json
        JSON.dump(token: @token, expires_at: @expires_at.iso8601)
      end

      def to_s
        @token
      end
      alias_method :to_str, :to_s

      private

      def truncate(string, max)
        string.length > max ? "#{string[0...max]}..." : string
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github-authentication-0.1.2 lib/github/authentication/token.rb