Sha256: 317c9d959b59d51ae08cf8b9b47646675cc00d4083436949dd7d62f5386316d3

Contents?: true

Size: 636 Bytes

Versions: 4

Compression:

Stored size: 636 Bytes

Contents

# frozen_string_literal: true

require 'securerandom'
module Apicasso
  # A model to abstract API access, with scope options, token generation, request limiting
  class Key < Apicasso::ApplicationRecord
    before_create :set_auth_token

    private

    # Method that generates `SecureRandom.uuid` as token until
    # an unique one has been acquired
    def set_auth_token
      loop do
        self.token = generate_auth_token
        break unless self.class.exists?(token: token)
      end
    end

    # RFC4122 style token
    def generate_auth_token
      SecureRandom.uuid.delete('-')
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apicasso-0.4.5 app/models/apicasso/key.rb
apicasso-0.4.4 app/models/apicasso/key.rb
apicasso-0.4.3 app/models/apicasso/key.rb
apicasso-0.4.2 app/models/apicasso/key.rb