Sha256: 2bac9364b981700701a06a45495e6895fa5e7c2e1e8ea8460a9102013a6246ed

Contents?: true

Size: 870 Bytes

Versions: 11

Compression:

Stored size: 870 Bytes

Contents

class Marty::ApiAuth < Marty::Base
  has_mcfly

  KEY_SIZE = 19

  validates :app_name, :api_key, :script_name, presence: true

  class ApiAuthValidator < ActiveModel::Validator
    def validate(api)
      api.errors.add(:base, "API Key length must be #{KEY_SIZE * 2}") if
        api.api_key && api.api_key.length != KEY_SIZE * 2

      api.errors.add(:base, 'Script Name must reference a valid script') unless
        Marty::Script.find_script(api.script_name, nil)
    end
  end

  validates_with ApiAuthValidator

  mcfly_validates_uniqueness_of :api_key, scope: [:script_name]
  validates :app_name, uniqueness: { scope: [:script_name,
                                             :obsoleted_dt] }

  before_validation do
    self.api_key = Marty::ApiAuth.generate_key if
      api_key.blank?
  end

  def self.generate_key
    SecureRandom.hex(KEY_SIZE)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
marty-14.3.0 app/models/marty/api_auth.rb
marty-14.0.0 app/models/marty/api_auth.rb
marty-13.0.2 app/models/marty/api_auth.rb
marty-11.0.0 app/models/marty/api_auth.rb
marty-10.0.3 app/models/marty/api_auth.rb
marty-10.0.2 app/models/marty/api_auth.rb
marty-10.0.0 app/models/marty/api_auth.rb
marty-9.5.1 app/models/marty/api_auth.rb
marty-9.5.0 app/models/marty/api_auth.rb
marty-9.3.3 app/models/marty/api_auth.rb
marty-9.3.2 app/models/marty/api_auth.rb