Sha256: 10055d90bd2a24bf8fb178c62bc6ecaeaf9726740508c4fe42cfa21766704017

Contents?: true

Size: 838 Bytes

Versions: 6

Compression:

Stored size: 838 Bytes

Contents

require 'time'
require 'json'

module CopyleaksApi
  class AccessToken
    attr_reader :created_at, :expire_at

    # constructor
    def initialize(cloud, email, api_key)
      @cloud = cloud
      @email = email
      @api_key = api_key
      login
    end

    # predicate method to check if token is not expired
    def fresh?
      DateTime.now.new_offset(0) < @expire_at
    end

    # return token string
    def token
      return @token if fresh?
      login
    end

    # get token for given email and api_key pair
    def login
      res = @cloud.api.post('account/login-api', { Email: @email, ApiKey: @api_key }.to_json)
      @token = res['access_token']
      @created_at = DateTime.parse(res['.issued'])
      @expire_at = DateTime.parse(res['.expires'])
      @token
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
plagiarism-checker-2.1.2 lib/copyleaks_api/access_token.rb
plagiarism-checker-2.1.1 lib/copyleaks_api/access_token.rb
plagiarism-checker-2.1.0 lib/copyleaks_api/access_token.rb
plagiarism-checker-2.0.0 lib/copyleaks_api/access_token.rb
plagiarism-checker-1.0.0 lib/copyleaks_api/access_token.rb
mkisilenko-test-0.1.0 lib/copyleaks_api/access_token.rb