Class: Leash::Provider::AuthCode

Inherits:
Ohm::Model
  • Object
show all
Defined in:
app/models/leash/provider/auth_code.rb

Constant Summary collapse

MAX_ASSIGN_TRIES =
20

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign!(app_name, owner, redirect_uri) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/leash/provider/auth_code.rb', line 15

def self.assign!(app_name, owner, redirect_uri)
  tries = 0
  auth_code = nil

  loop do
    begin
      auth_code = SecureRandom.urlsafe_base64(32)
      timestamp = Time.now.to_i
      self.create app_name: app_name, owner: owner_key(owner), auth_code: auth_code, redirect_uri: redirect_uri, created_at: timestamp
      break

    rescue Ohm::UniqueIndexViolation => e
      tries += 1

      fail if tries > MAX_ASSIGN_TRIES
    end
  end

  auth_code
end

.find_by_auth_code(auth_code) ⇒ Object



42
43
44
# File 'app/models/leash/provider/auth_code.rb', line 42

def self.find_by_auth_code(auth_code)
  self.find(auth_code: auth_code).first
end

.owner_key(owner) ⇒ Object



47
48
49
50
51
52
53
# File 'app/models/leash/provider/auth_code.rb', line 47

def self.owner_key(owner)
  if owner.is_a? ActiveRecord::Base
    "#{owner.class.name}##{owner.id}"
  else
    owner
  end
end

.present?(auth_code) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/leash/provider/auth_code.rb', line 37

def self.present?(auth_code)
  self.find(auth_code: auth_code).size != 0
end

Instance Method Details

#owner_instanceObject



56
57
58
59
60
# File 'app/models/leash/provider/auth_code.rb', line 56

def owner_instance
  owner_klass, owner_id = owner.split("#", 2)

  owner_klass.classify.constantize.find(owner_id)
end