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
- #api_client ⇒ Object
- #authorization_header ⇒ Object
- #current_access_token ⇒ Object
- #refresh_token! ⇒ Object
- #refresh_token_body ⇒ Object
- #refresh_token_headers ⇒ Object
- #refresh_token_request ⇒ Object
- #update_tokens(credentials) ⇒ Object
- #wallet_api ⇒ Object
Instance Method Details
#api_client ⇒ Object
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_header ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/generators/rails_eve/install/templates/esi_character_api.rb', line 40 def 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_token ⇒ Object
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_body ⇒ Object
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_headers ⇒ Object
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' => } end |
#refresh_token_request ⇒ Object
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_api ⇒ Object
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 |