Class: AuthToken

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/auth_token.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clientObject


2
3
4
5
6
7
8
9
10
11
12
# File 'app/models/auth_token.rb', line 2

def self.client
  if @auth_token && @auth_token.high_rate_limit?
    return @auth_token.github_client
  end
  auth_token = order("RANDOM()").limit(100).sample
  if auth_token.high_rate_limit?
    @auth_token = auth_token
    return auth_token.github_client
  end
  client
end

.create_multiple(array_of_tokens) ⇒ Object


18
19
20
21
22
# File 'app/models/auth_token.rb', line 18

def self.create_multiple(array_of_tokens)
  array_of_tokens.each do |token|
    self.find_or_create_by(token: token)
  end
end

.fallback_client(token = nil) ⇒ Object


34
35
36
# File 'app/models/auth_token.rb', line 34

def self.fallback_client(token = nil)
  AuthToken.new_client(token)
end

.new_client(token) ⇒ Object


38
39
40
41
# File 'app/models/auth_token.rb', line 38

def self.new_client(token)
  token ||= AuthToken.token
  Octokit::Client.new(access_token: token, auto_paginate: true)
end

.tokenObject


14
15
16
# File 'app/models/auth_token.rb', line 14

def self.token
  client.access_token
end

Instance Method Details

#github_clientObject


30
31
32
# File 'app/models/auth_token.rb', line 30

def github_client
  AuthToken.new_client(token)
end

#high_rate_limit?Boolean


24
25
26
27
28
# File 'app/models/auth_token.rb', line 24

def high_rate_limit?
  github_client.rate_limit.remaining > 500
rescue Octokit::Unauthorized
  false
end