-
Notifications
You must be signed in to change notification settings - Fork 129
Endpoint authentication
sanchojaf edited this page Nov 1, 2014
·
5 revisions
The endpoint authentication use a pair (key, authentication_token).
For key generate is used NumberGenerator module.
module NumberGenerator
extend ActiveSupport::Concern
NUMBER_LENGTH = 9
NUMBER_LETTERS = false
NUMBER_PREFIX = 'N'
included do
field :number, type: String
validates :number, uniqueness: true
before_validation :generate_number, on: :create
end
def self.by_number(number)
where(number: number)
end
def generate_number(options = {})
options[:length] ||= NUMBER_LENGTH
options[:letters] ||= NUMBER_LETTERS
options[:prefix] ||= NUMBER_PREFIX
possible = (0..9).to_a
possible += ('A'..'Z').to_a if options[:letters]
self.number ||= loop do
# Make a random number.
random = "#{options[:prefix]}#{(0...options[:length]).map { possible.shuffle.first }.join}"
# Use the random number if no other order exists with it.
if self.number.present? && self.number == random
# If over half of all possible options are taken add another digit.
options[:length] += 1 if self.class.count > (10 ** options[:length] / 2)
else
break random
end
end
end
end
The module decorate Connection class.
module Setup
class Connection < Base
include NumberGenerator
...
end
end
Mongoid provide alias functionality, we use alias key for number
field :number, as: :key, type: String
In the above example you can reference the field by Connection#key
, but the field in the database is stored as number
.