Class: Purtea::FFLogs::API

Inherits:
Object
  • Object
show all
Defined in:
lib/purtea/fflogs/api.rb

Overview

Contains methods to interact with the FF Logs API.

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ API

Returns a new instance of API.



16
17
18
19
20
21
22
23
# File 'lib/purtea/fflogs/api.rb', line 16

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  @oa_client = OAuth2::Client.new(
    @client_id, @client_secret, site: BASE_URL
  )
  authorize!
end

Instance Method Details

#authorize!(force_refresh: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/purtea/fflogs/api.rb', line 56

def authorize!(force_refresh: false)
  Purtea.logger.debug 'Authorize FF Logs API'

  unless force_refresh
    load_token!
    return unless token_expired?
  end

  refresh_token!
  save_token
end

#dump_schema(is_retry: false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/purtea/fflogs/api.rb', line 25

def dump_schema(is_retry: false)
  result = GraphQL::Client.dump_schema(
    Purtea::FFLogs::HTTP,
    SCHEMA_FILE,
    context: { access_token: @token.token }
  )

  err_code = result&.dig('errors', 0, 'message')&.[](0..2)
  if err_code && err_code == '401'
    return result if is_retry

    Purtea.logger.info 'FF Logs API token expired or revoked, getting new'

    authorize! true
    return dump_schema true
  end

  result
end

#fights(code) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/purtea/fflogs/api.rb', line 45

def fights(code)
  result = CLIENT.query(
    GET_FIGHTS_QUERY,
    variables: { code: code },
    context: { access_token: @token.token }
  )

  report = result.data.report_data.report
  report.fights.map { |d| Fight.new d, report.start_time }
end

#load_token!Object



77
78
79
80
81
82
83
# File 'lib/purtea/fflogs/api.rb', line 77

def load_token!
  return unless File.exist? TOKEN_FILE

  Purtea.logger.debug 'Loading FF Logs API token from file'
  token_hash = JSON.load_file TOKEN_FILE
  @token = OAuth2::AccessToken.from_hash(@oa_client, token_hash)
end

#refresh_token!Object



85
86
87
88
89
# File 'lib/purtea/fflogs/api.rb', line 85

def refresh_token!
  # FF Logs does not issue refresh tokens
  Purtea.logger.debug 'Refreshing FF Logs API token'
  @token = @oa_client.client_credentials.get_token
end

#save_tokenObject



68
69
70
71
72
73
74
75
# File 'lib/purtea/fflogs/api.rb', line 68

def save_token
  return if @token.nil?

  Purtea.logger.debug 'Saving FF Logs API token to file'
  token_hash = @token.to_hash
  token_json = token_hash.to_json
  File.open(TOKEN_FILE, 'w') { |f| f.write token_json }
end

#token_expired?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/purtea/fflogs/api.rb', line 91

def token_expired?
  return true if @token.nil?

  expires_at = Time.at(@token.expires_at)
  time_until_expire = expires_at - Time.now
  time_until_expire < EXPIRATION_THRESHOLD
end