2020-05-18 17:45:01 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# OpenAPI::Client keeps track of the authorized accesses to the 3-rd party API (aka. OpenAPI)
|
2020-03-25 10:16:47 +01:00
|
|
|
class OpenAPI::Client < ApplicationRecord
|
2016-05-04 11:48:14 +02:00
|
|
|
has_many :calls_count_tracings, foreign_key: :open_api_client_id, dependent: :destroy
|
2020-05-18 17:45:01 +02:00
|
|
|
|
2016-05-04 11:48:14 +02:00
|
|
|
validates :name, presence: true
|
2020-05-18 17:45:01 +02:00
|
|
|
validates_uniqueness_of :token
|
|
|
|
|
|
|
|
before_create :set_initial_token
|
2016-05-04 11:48:14 +02:00
|
|
|
|
|
|
|
def increment_calls_count
|
|
|
|
update_column(:calls_count, calls_count+1)
|
|
|
|
end
|
2020-05-18 17:45:01 +02:00
|
|
|
|
|
|
|
def regenerate_token
|
|
|
|
update_attributes(token: generate_unique_secure_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_initial_token
|
|
|
|
self.token = generate_unique_secure_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_unique_secure_token
|
|
|
|
SecureRandom.base58(24)
|
|
|
|
end
|
2016-05-04 11:48:14 +02:00
|
|
|
end
|