evematic 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/evematic/sessions_controller.rb +21 -0
  3. data/app/controllers/evematic/sso_callbacks_controller.rb +30 -0
  4. data/app/models/evematic/access_rule.rb +17 -0
  5. data/app/models/evematic/account.rb +12 -0
  6. data/app/models/evematic/application_record.rb +2 -4
  7. data/app/models/evematic/esi/alliance.rb +25 -0
  8. data/app/models/evematic/esi/character.rb +35 -0
  9. data/app/models/evematic/esi/corporation.rb +37 -0
  10. data/app/models/evematic/identity.rb +24 -0
  11. data/config/i18n-tasks.yml +5 -4
  12. data/config/locales/en.yml +13 -0
  13. data/lib/evematic/configuration/builder.rb +74 -0
  14. data/lib/evematic/configuration.rb +20 -0
  15. data/lib/evematic/controllers/helper.rb +7 -0
  16. data/lib/evematic/controllers/helpers/authentication.rb +90 -0
  17. data/lib/evematic/controllers.rb +2 -0
  18. data/lib/evematic/engine.rb +82 -8
  19. data/lib/evematic/esi/client.rb +89 -0
  20. data/lib/evematic/esi/helper.rb +43 -0
  21. data/lib/evematic/esi.rb +2 -0
  22. data/lib/evematic/jobs/helper.rb +3 -0
  23. data/lib/evematic/models/helper.rb +31 -0
  24. data/lib/evematic/models/mixins/access_rule.rb +43 -0
  25. data/lib/evematic/models/mixins/account.rb +38 -0
  26. data/lib/evematic/models/mixins/esi/alliance.rb +34 -0
  27. data/lib/evematic/models/mixins/esi/character.rb +41 -0
  28. data/lib/evematic/models/mixins/esi/corporation.rb +41 -0
  29. data/lib/evematic/models/mixins/esi/entity.rb +55 -0
  30. data/lib/evematic/models/mixins/esi.rb +2 -0
  31. data/lib/evematic/models/mixins/identity.rb +37 -0
  32. data/lib/evematic/models/mixins.rb +2 -0
  33. data/lib/evematic/models.rb +2 -0
  34. data/lib/evematic/routes/helper.rb +5 -0
  35. data/lib/evematic/routes/router.rb +10 -0
  36. data/lib/evematic/routes/routers/authentication.rb +14 -0
  37. data/lib/evematic/routes.rb +2 -0
  38. data/lib/evematic/version.rb +1 -1
  39. data/lib/evematic/views/helper.rb +2 -0
  40. data/lib/evematic/views.rb +2 -0
  41. data/lib/evematic.rb +37 -1
  42. metadata +182 -9
  43. data/config/locales/evematic.en.yml +0 -5
  44. data/config/routes.rb +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7565c6e469317d256f491a395e804ae26cd7d5ceab1462ea7e0bc3ec92af0915
4
- data.tar.gz: 5e0a73ce00bec88e3a6519315f7c2bc56bcca59135269f5fcf5c977769e00181
3
+ metadata.gz: d16f6d328d5aea704d82573875c449e09f134461a20ff38625616b818a1b6015
4
+ data.tar.gz: c4d141a17d434effacde6518ad13b4e82811df234bf69110e114a347df825b83
5
5
  SHA512:
6
- metadata.gz: 9a3972c7130e2684a72c9576867b3fd70a2c5d9b4aac1efb9047ee2f08e67abaed435cd3370f635e6102adcc982c0393c821de9058ee3a4fe12befbe91fe14e1
7
- data.tar.gz: fd2601c9d2a6ccdf4fef2ce5b422f41a8774d95b2c0c98d487ef107b3c1312a8aef6db03bd9e2e662f9b11631c3fc2da3c2efd640d689b513129b20dc49aca94
6
+ metadata.gz: 9ffdbb85c44a34add6302b1cb206e8ccc1015306e429240164b00c2fbb91b66570abad783fb31794f1f52f22f745f5cc1f529f1571cb9dfb614ed05b53c63fd9
7
+ data.tar.gz: 42e449b89c3bca4a2075974d0e40fa6e61fcb1b42756ae6f53f80146ef8f28d00158225829595f7875bd1660412fbc9f44813f8995ac3a3a0230bd401abb506f
@@ -0,0 +1,21 @@
1
+ class Evematic::SessionsController < Evematic::ApplicationController
2
+ def new
3
+ redirect_to after_login_path if logged_in?
4
+ end
5
+
6
+ def destroy
7
+ log_out
8
+ redirect_to after_logout_path
9
+ end
10
+
11
+ private
12
+
13
+ def auth_controller?
14
+ true
15
+ end
16
+
17
+ def log_out
18
+ session[:current_identity_id] = nil
19
+ session[:current_account_id] = nil
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ class Evematic::SSOCallbacksController < Evematic::ApplicationController
2
+ def eve
3
+ auth = request.env["omniauth.auth"]
4
+ character = esi_character_model.find_or_create_by(id: auth.uid)
5
+ identity = identity_model.sso_authenticate_by(character)
6
+ if identity&.persisted?
7
+ log_in(identity)
8
+ redirect_to(stored_location || after_login_path)
9
+ else
10
+ flash[:error] = t(".forbidden", name: character.name)
11
+ redirect_to login_path
12
+ end
13
+ end
14
+
15
+ def failure
16
+ flash.now[:error] = t(".not_authorized")
17
+ redirect_to login_path
18
+ end
19
+
20
+ private
21
+
22
+ def auth_controller?
23
+ true
24
+ end
25
+
26
+ def log_in(identity)
27
+ session[:current_identity_id] = identity.id
28
+ session[:current_account_id] = identity.account_id
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: access_rules
4
+ #
5
+ # id :integer not null, primary key
6
+ # action :string default("deny"), not null
7
+ # principal_type :string not null
8
+ # created_at :datetime not null
9
+ # principal_id :integer not null
10
+ #
11
+ # Indexes
12
+ #
13
+ # index_access_rules_on_principal (principal_type,principal_id) UNIQUE
14
+ #
15
+ class Evematic::AccessRule < Evematic::ApplicationRecord
16
+ include Evematic::Models::Mixins::AccessRule
17
+ end
@@ -0,0 +1,12 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: accounts
4
+ #
5
+ # id :integer not null, primary key
6
+ # admin :boolean
7
+ # created_at :datetime not null
8
+ # updated_at :datetime not null
9
+ #
10
+ class Evematic::Account < Evematic::ApplicationRecord
11
+ include Evematic::Models::Mixins::Account
12
+ end
@@ -1,5 +1,3 @@
1
- module Evematic
2
- class Evematic::ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
1
+ class Evematic::ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
5
3
  end
@@ -0,0 +1,25 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: esi_alliances
4
+ #
5
+ # id :integer not null, primary key
6
+ # date_founded :date not null
7
+ # esi_etag :string
8
+ # esi_expires_at :datetime
9
+ # esi_last_modified_at :datetime
10
+ # name :string not null
11
+ # ticker :string not null
12
+ # created_at :datetime not null
13
+ # updated_at :datetime not null
14
+ # creator_corporation_id :integer not null
15
+ # creator_id :integer not null
16
+ # executor_corporation_id :integer
17
+ # faction_id :integer
18
+ #
19
+ # Indexes
20
+ #
21
+ # index_esi_alliances_on_ticker (ticker) UNIQUE
22
+ #
23
+ class Evematic::ESI::Alliance < Evematic::ApplicationRecord
24
+ include Evematic::Models::Mixins::ESI::Alliance
25
+ end
@@ -0,0 +1,35 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: esi_characters
4
+ #
5
+ # id :integer not null, primary key
6
+ # birthday :date not null
7
+ # description :text
8
+ # esi_etag :string
9
+ # esi_expires_at :datetime
10
+ # esi_last_modified_at :datetime
11
+ # gender :string not null
12
+ # name :string not null
13
+ # security_status :decimal(, )
14
+ # title :string
15
+ # created_at :datetime not null
16
+ # updated_at :datetime not null
17
+ # alliance_id :integer
18
+ # bloodline_id :integer not null
19
+ # corporation_id :integer not null
20
+ # faction_id :integer
21
+ # race_id :integer not null
22
+ #
23
+ # Indexes
24
+ #
25
+ # index_esi_characters_on_alliance_id (alliance_id)
26
+ # index_esi_characters_on_corporation_id (corporation_id)
27
+ #
28
+ # Foreign Keys
29
+ #
30
+ # alliance_id (alliance_id => esi_alliances.id)
31
+ # corporation_id (corporation_id => esi_corporations.id)
32
+ #
33
+ class Evematic::ESI::Character < Evematic::ApplicationRecord
34
+ include Evematic::Models::Mixins::ESI::Character
35
+ end
@@ -0,0 +1,37 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: esi_corporations
4
+ #
5
+ # id :integer not null, primary key
6
+ # date_founded :date not null
7
+ # description :text
8
+ # esi_etag :string
9
+ # esi_expires_at :datetime
10
+ # esi_last_modified_at :datetime
11
+ # member_count :integer not null
12
+ # name :string not null
13
+ # shares :bigint
14
+ # tax_rate :decimal(, ) not null
15
+ # ticker :string not null
16
+ # url :string
17
+ # war_eligible :boolean
18
+ # created_at :datetime not null
19
+ # updated_at :datetime not null
20
+ # alliance_id :integer
21
+ # ceo_id :integer not null
22
+ # creator_id :integer not null
23
+ # faction_id :integer
24
+ # home_station_id :integer
25
+ #
26
+ # Indexes
27
+ #
28
+ # index_esi_corporations_on_alliance_id (alliance_id)
29
+ # index_esi_corporations_on_ticker (ticker) UNIQUE
30
+ #
31
+ # Foreign Keys
32
+ #
33
+ # alliance_id (alliance_id => esi_alliances.id)
34
+ #
35
+ class Evematic::ESI::Corporation < Evematic::ApplicationRecord
36
+ include Evematic::Models::Mixins::ESI::Corporation
37
+ end
@@ -0,0 +1,24 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: identities
4
+ #
5
+ # id :integer not null, primary key
6
+ # main :boolean
7
+ # created_at :datetime not null
8
+ # updated_at :datetime not null
9
+ # account_id :integer not null
10
+ # character_id :integer not null
11
+ #
12
+ # Indexes
13
+ #
14
+ # index_identities_on_account_id_and_character_id (account_id,character_id) UNIQUE
15
+ # index_identities_on_account_id_and_main (account_id,main) UNIQUE
16
+ #
17
+ # Foreign Keys
18
+ #
19
+ # account_id (account_id => accounts.id)
20
+ # character_id (character_id => esi_characters.id)
21
+ #
22
+ class Evematic::Identity < Evematic::ApplicationRecord
23
+ include Evematic::Models::Mixins::Identity
24
+ end
@@ -15,7 +15,7 @@ data:
15
15
 
16
16
  # Locale files or `File.find` patterns where translations are read from:
17
17
  read:
18
- - config/locales/**/*.%{locale}.yml
18
+ # - config/locales/{locale}.yml
19
19
  ## Default:
20
20
  # - config/locales/%{locale}.yml
21
21
  ## More files:
@@ -54,8 +54,10 @@ data:
54
54
  # Find translate calls
55
55
  search:
56
56
  ## Paths or `File.find` patterns to search in:
57
- # paths:
58
- # - app/
57
+ paths:
58
+ - app/
59
+ - lib/evematic/controllers
60
+ - lib/evematic/views
59
61
 
60
62
  ## Root directories for relative keys resolution.
61
63
  # relative_roots:
@@ -119,7 +121,6 @@ search:
119
121
 
120
122
  ## Consider these keys used:
121
123
  ignore_unused:
122
- - evematic.engine.name
123
124
  # - 'activerecord.attributes.*'
124
125
  # - '{devise,kaminari,will_paginate}.*'
125
126
  # - 'simple_form.{yes,no}'
@@ -0,0 +1,13 @@
1
+ ---
2
+ en:
3
+ evematic:
4
+ errors:
5
+ login_required: You need to log in to do that.
6
+ sessions:
7
+ new:
8
+ login_button_label: Log in with EVE Online SSO
9
+ sso_callbacks:
10
+ eve:
11
+ forbidden: "%{name} is not authorized to log in."
12
+ failure:
13
+ not_authorized: Character is not authorized to log in.
@@ -0,0 +1,74 @@
1
+ require "dry-configurable"
2
+
3
+ class Evematic::Configuration::Builder
4
+ include Dry::Configurable
5
+
6
+ delegate_missing_to :config
7
+
8
+ setting :authentication_enabled, default: true
9
+
10
+ setting :esi_base_url, default: "https://esi.evetech.net"
11
+ setting :esi_client_id, default: -> { ENV.fetch("ESI_CLIENT_ID") }
12
+ setting :esi_client_secret, default: -> { ENV.fetch("ESI_CLIENT_SECRET") }
13
+ setting :esi_detailed_logging, default: false
14
+ setting :esi_retry_options, default: {}
15
+ setting :esi_user_agent, default: "Evematic/1.0 (+https://evematic.dev)"
16
+
17
+ setting :esi_alliance_class, default: "Evematic::ESI::Alliance"
18
+ setting :esi_alliances_table, default: "esi_alliances"
19
+
20
+ setting :esi_character_class, default: "Evematic::ESI::Character"
21
+ setting :esi_characters_table, default: "esi_characters"
22
+
23
+ setting :esi_corporation_class, default: "Evematic::ESI::Corporation"
24
+ setting :esi_corporations_table, default: "esi_corporations"
25
+
26
+ setting :access_rule_class, default: "Evematic::AccessRule"
27
+ setting :access_rules_table, default: "access_rules"
28
+
29
+ setting :account_class, default: "Evematic::Account"
30
+ setting :accounts_table, default: "accounts"
31
+
32
+ setting :identity_class, default: "Evematic::Identity"
33
+ setting :identities_table, default: "identities"
34
+
35
+ setting :after_login_path, default: -> { root_path }
36
+ setting :after_logout_path, default: -> { root_path }
37
+
38
+ def clear_cache!
39
+ %i[
40
+ access_rule_model
41
+ account_model
42
+ esi_alliance_model
43
+ esi_character_model
44
+ esi_corporation_model
45
+ identity_model
46
+ ].each do |var|
47
+ remove_instance_variable("@#{var}") if instance_variable_defined?("@#{var}")
48
+ end
49
+ end
50
+
51
+ def esi_alliance_model
52
+ @esi_alliance_model ||= config.esi_alliance_class.safe_constantize
53
+ end
54
+
55
+ def esi_character_model
56
+ @esi_character_model ||= config.esi_character_class.safe_constantize
57
+ end
58
+
59
+ def esi_corporation_model
60
+ @esi_corporation_model ||= config.esi_corporation_class.safe_constantize
61
+ end
62
+
63
+ def access_rule_model
64
+ @access_rule_model ||= config.access_rule_class.safe_constantize
65
+ end
66
+
67
+ def account_model
68
+ @account_model ||= config.account_class.safe_constantize
69
+ end
70
+
71
+ def identity_model
72
+ @identity_model ||= config.identity_class.safe_constantize
73
+ end
74
+ end
@@ -0,0 +1,20 @@
1
+ module Evematic
2
+ module Configuration
3
+ require "evematic/configuration/builder"
4
+ end
5
+
6
+ class << self
7
+ def configuration_builder
8
+ @configuration_builder ||= Evematic::Configuration::Builder.new
9
+ end
10
+
11
+ def configuration
12
+ configuration_builder
13
+ end
14
+ alias_method :config, :configuration
15
+
16
+ def configure(&block)
17
+ configuration_builder.configure(&block)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Evematic::Controllers::Helper
2
+ extend ActiveSupport::Concern
3
+
4
+ def not_found
5
+ raise ActionController::RoutingError.new("Not Found")
6
+ end
7
+ end
@@ -0,0 +1,90 @@
1
+ module Evematic::Controllers::Helpers::Authentication
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ helper_method :logged_in?, :current_account, :current_identity
6
+ end
7
+
8
+ def require_login!
9
+ return true if current_identity
10
+
11
+ flash[:error] = t("evematic.errors.login_required")
12
+ store_location(request.fullpath)
13
+ redirect_to login_path
14
+ end
15
+
16
+ def require_admin!
17
+ not_found unless current_account&.admin?
18
+ end
19
+
20
+ def logged_in?
21
+ current_identity_id.present?
22
+ end
23
+
24
+ def current_account_id
25
+ session[:current_account_id]
26
+ end
27
+
28
+ def current_account
29
+ @current_account ||= account_model.find_by(id: current_account_id)
30
+ end
31
+
32
+ def current_identity_id
33
+ session[:current_identity_id]
34
+ end
35
+
36
+ def current_identity
37
+ @current_identity ||= identity_model.includes(:character, :corporation, :alliance).find_by(id: current_identity_id)
38
+ end
39
+
40
+ # Helper methods taken from Devise
41
+ # https://github.com/heartcombo/devise/blob/main/lib/devise/controllers/store_location.rb
42
+
43
+ def storable_location?
44
+ request.get? && request.format.to_sym == :html && !auth_controller? && !request.xhr?
45
+ end
46
+
47
+ def store_location(location)
48
+ path = extract_path_from_location(location)
49
+ session["return_to"] = path if path
50
+ end
51
+
52
+ def stored_location
53
+ if request.format.to_sym == :html
54
+ session.delete("return_to")
55
+ else
56
+ session["return_to"]
57
+ end
58
+ end
59
+
60
+ def parse_uri(location)
61
+ location && URI.parse(location)
62
+ rescue URI::InvalidURIError
63
+ nil
64
+ end
65
+
66
+ def extract_path_from_location(location)
67
+ uri = parse_uri(location)
68
+
69
+ if uri
70
+ path = remove_domain_from_uri(uri)
71
+ add_fragment_back_to_path(uri, path)
72
+ end
73
+ end
74
+
75
+ def remove_domain_from_uri(uri)
76
+ [uri.path.sub(/\A\/+/, "/"), uri.query].compact.join("?")
77
+ end
78
+
79
+ def add_fragment_back_to_path(uri, path)
80
+ [path, uri.fragment].compact.join("#")
81
+ end
82
+
83
+ def after_login_path
84
+ root_path
85
+ end
86
+
87
+ def after_logout_path
88
+ root_path
89
+ end
90
+ end
@@ -0,0 +1,2 @@
1
+ module Evematic::Controllers
2
+ end
@@ -1,15 +1,34 @@
1
- Gem.loaded_specs["evematic"].dependencies.each do |dep|
2
- require dep.name
1
+ require "rails"
2
+ require "omniauth"
3
+ require "omniauth/strategies/eve_online"
4
+ require "omniauth/rails_csrf_protection"
5
+ require "turbo-rails"
6
+
7
+ require "evematic/configuration"
8
+ require "evematic/esi"
9
+ require "evematic/models"
10
+
11
+ OmniAuth::Strategies::EVEOnline.class_eval do
12
+ # Hack to allow passing scope as a query parameter to /auth/eve
13
+ def callback_url
14
+ full_host + callback_path
15
+ end
3
16
  end
4
17
 
5
18
  class Evematic::Engine < ::Rails::Engine
6
- isolate_namespace Evematic
19
+ config.evematic = Evematic.config
7
20
 
8
- config.app_middleware.use(
9
- Rack::Static,
10
- urls: ["evematic-assets"],
11
- root: Evematic::Engine.root.join("public")
12
- )
21
+ config.to_prepare do
22
+ Evematic.setup
23
+ end
24
+
25
+ initializer "evematic.assets" do
26
+ config.middleware.use(
27
+ Rack::Static,
28
+ urls: ["evematic"],
29
+ root: Evematic::Engine.root.join("public")
30
+ )
31
+ end
13
32
 
14
33
  initializer "evematic.inflections" do
15
34
  ActiveSupport::Inflector.inflections(:en) do |inflect|
@@ -22,4 +41,59 @@ class Evematic::Engine < ::Rails::Engine
22
41
  inflect.acronym "UI"
23
42
  end
24
43
  end
44
+
45
+ initializer "evematic.routes" do
46
+ ActionDispatch::Routing::Mapper.include Evematic::Routes::Helper
47
+ end
48
+
49
+ initializer "evematic.action_controller" do
50
+ ActiveSupport.on_load(:action_controller) do
51
+ include Evematic::Models::Helper
52
+ include Evematic::Controllers::Helper
53
+ include Evematic::Controllers::Helpers::Authentication if Evematic.config.authentication_enabled
54
+ end
55
+ end
56
+
57
+ initializer "evematic.action_view" do
58
+ ActiveSupport.on_load(:action_view) do
59
+ include Evematic::Models::Helper
60
+ include Evematic::Views::Helper
61
+ end
62
+ end
63
+
64
+ initializer "evematic.active_job" do
65
+ ActiveSupport.on_load(:active_job) do
66
+ include Evematic::Models::Helper
67
+ include Evematic::Jobs::Helper
68
+ end
69
+ end
70
+
71
+ initializer "evematic.active_record" do
72
+ ActiveSupport.on_load(:active_record) do
73
+ include Evematic::Models::Helper
74
+ end
75
+ end
76
+
77
+ initializer "evematic.i18n" do |app|
78
+ app.config.i18n.raise_on_missing_translations = true if Rails.env.local?
79
+ end
80
+
81
+ initializer "evematic.omniauth" do |app|
82
+ next unless Evematic.config.authentication_enabled
83
+
84
+ OmniAuth.config.logger = Rails.logger
85
+
86
+ app.middleware.use OmniAuth::Builder do
87
+ provider OmniAuth::Strategies::EVEOnline,
88
+ name: :eve,
89
+ setup: lambda { |env|
90
+ req = Rack::Request.new(env)
91
+ env["omniauth.strategy"].tap do |strategy|
92
+ strategy.options[:client_id] = Evematic.config.esi_client_id
93
+ strategy.options[:client_secret] = Evematic.config.esi_client_secret
94
+ strategy.options[:scope] = req.params["scope"]
95
+ end
96
+ }
97
+ end
98
+ end
25
99
  end
@@ -0,0 +1,89 @@
1
+ require "dry-initializer"
2
+ require "faraday"
3
+ require "faraday/detailed_logger"
4
+ require "faraday/http_cache"
5
+ require "faraday/retry"
6
+ require "faraday/typhoeus"
7
+
8
+ class Evematic::ESI::Client
9
+ extend Dry::Initializer
10
+
11
+ option :base_url, default: -> { Evematic.config.esi_base_url }
12
+ option :cache, default: -> { Rails.cache }
13
+ option :detailed_logging, default: -> { Evematic.config.esi_detailed_logging }
14
+ option :logger, default: -> { Rails.logger }
15
+ option :instrumenter, default: -> { ActiveSupport::Notifications }
16
+ option :user_agent, default: -> { Evematic.config.esi_user_agent }
17
+
18
+ def delete(path, body: nil, params: {}, token: nil)
19
+ json_encoded.delete(path) do |req|
20
+ req.body = body
21
+ req.params = params
22
+ req.headers = default_headers
23
+ req.headers = headers(token)
24
+ end
25
+ end
26
+
27
+ def get(path, params: {}, token: nil)
28
+ url_encoded.get(path) do |req|
29
+ req.params = params
30
+ req.headers = headers(token)
31
+ end
32
+ end
33
+
34
+ def post(path, body: nil, params: {}, token: nil)
35
+ json_encoded.post(path) do |req|
36
+ req.body = body
37
+ req.params = params
38
+ req.headers = headers(token)
39
+ end
40
+ end
41
+
42
+ def put(path, body: nil, params: {}, token: nil)
43
+ json_encoded.put(path) do |req|
44
+ req.body = body
45
+ req.params = params
46
+ req.headers = headers(token)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def headers(token)
53
+ h = default_headers
54
+ h[:authorization] = authorization_header(token) if token
55
+ h
56
+ end
57
+
58
+ def default_headers
59
+ {user_agent: user_agent.strip}
60
+ end
61
+
62
+ def authorization_header(token)
63
+ "Authorization: Bearer #{token}"
64
+ end
65
+
66
+ def url_encoded
67
+ @url_encoded ||= Faraday.new(url: base_url) do |conn|
68
+ conn.use(:http_cache, store: cache, logger:, instrumenter:)
69
+ conn.request :url_encoded
70
+ conn.request :retry, retry_options
71
+ conn.response :detailed_logger, logger if detailed_logging
72
+ conn.adapter :typhoeus
73
+ end
74
+ end
75
+
76
+ def json_encoded
77
+ @json_encoded ||= Faraday.new(url: base_url) do |conn|
78
+ conn.use(:http_cache, store: cache, logger:, instrumenter:)
79
+ conn.request :json
80
+ conn.request :retry, retry_options
81
+ conn.response :detailed_logger, logger if detailed_logging
82
+ conn.adapter :typhoeus
83
+ end
84
+ end
85
+
86
+ def retry_options
87
+ @retry_options ||= Evematic.config.esi_retry_options.to_h.merge(retry_statuses: [503, 504])
88
+ end
89
+ end