Module: EsiCharacterApi

Extended by:
ActiveSupport::Concern
Included in:
Character
Defined in:
lib/generators/rails_eve/install/templates/esi_character_api.rb

Instance Method Summary collapse

Instance Method Details

#api_clientObject



48
49
50
51
52
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 48

def api_client
  client = ESI::ApiClient.new
  client.config.access_token = current_access_token
  client
end

#authorization_headerObject



40
41
42
43
44
45
46
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 40

def authorization_header
  secrets = OpenStruct.new Rails.application.config_for(:secrets)
  app_id = secrets.eve_api_app_id
  app_secret = secrets.eve_api_app_secret
  auth = Base64.strict_encode64("#{app_id}:#{app_secret}")
  "Basic #{auth}"
end

#current_access_tokenObject



35
36
37
38
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 35

def current_access_token
  refresh_token!
  access_token
end

#refresh_token!Object



13
14
15
16
17
18
19
20
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 13

def refresh_token!
  return false if Time.zone.now < token_expires

  self.access_token = refresh_token_request.access_token
  self.refresh_token = refresh_token_request.refresh_token
  self.token_expires = Time.zone.now + refresh_token_request.expires_in
  save
end

#refresh_token_bodyObject



31
32
33
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 31

def refresh_token_body
  { grant_type: 'refresh_token', refresh_token: refresh_token }.to_json
end

#refresh_token_headersObject



27
28
29
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 27

def refresh_token_headers
  { 'Content-Type' => 'application/json', 'Authorization' => authorization_header }
end

#refresh_token_requestObject



22
23
24
25
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 22

def refresh_token_request
  request = Typhoeus.post('https://login.eveonline.com/oauth/token', headers: refresh_token_headers, body: refresh_token_body)
  OpenStruct.new(JSON.parse(request.body))
end

#update_tokens(credentials) ⇒ Object



6
7
8
9
10
11
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 6

def update_tokens(credentials)
  self.access_token = credentials['token']
  self.refresh_token = credentials['refresh_token']
  self.token_expires = Time.zone.at credentials['expires_at']
  save
end

#wallet_apiObject



54
55
56
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 54

def wallet_api
  ESI::WalletApi.new(api_client)
end