Sha256: 2579e547da8aaca781ae97fcf5b99309468015e4ac73391c881c935b0eca9dd2

Contents?: true

Size: 866 Bytes

Versions: 12

Compression:

Stored size: 866 Bytes

Contents

class Marty::ApiAuth < Marty::Base
  has_mcfly

  KEY_SIZE = 19

  validates_presence_of :app_name, :api_key, :script_name

  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_uniqueness_of :app_name, scope: [:script_name,
                                             :obsoleted_dt]


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

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
marty-2.5.2 app/models/marty/api_auth.rb
marty-2.5.1 app/models/marty/api_auth.rb
marty-2.5.0 app/models/marty/api_auth.rb
marty-2.4.9 app/models/marty/api_auth.rb
marty-2.4.8 app/models/marty/api_auth.rb
marty-2.4.7 app/models/marty/api_auth.rb
marty-2.4.6 app/models/marty/api_auth.rb
marty-2.4.5 app/models/marty/api_auth.rb
marty-2.4.4 app/models/marty/api_auth.rb
marty-2.4.3 app/models/marty/api_auth.rb
marty-2.4.2 app/models/marty/api_auth.rb
marty-2.4.1 app/models/marty/api_auth.rb