Class: DeepL::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/deepl/configuration.rb

Constant Summary collapse

ATTRIBUTES =
%i[auth_key host logger max_doc_status_queries max_network_retries user_agent
version].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, app_info_name = nil, app_info_version = nil, send_platform_info = true) ⇒ Configuration

rubocop:disable all



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/deepl/configuration.rb', line 17

def initialize(data = {}, app_info_name = nil, app_info_version = nil, send_platform_info = true) # rubocop:disable all
  data.each { |key, value| send("#{key}=", value) }
  @auth_key ||= ENV.fetch('DEEPL_AUTH_KEY', nil)
  @host ||= ENV.fetch('DEEPL_SERVER_URL', nil)
  @host ||= if self.class.(@auth_key)
              DEEPL_SERVER_URL_FREE
            else
              DEEPL_SERVER_URL
            end
  @version ||= 'v2'
  @user_agent ||= construct_user_agent(send_platform_info, app_info_name, app_info_version)
  @max_network_retries ||= 5
  @logger ||= nil
end

Class Method Details

.free_account_auth_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/deepl/configuration.rb', line 44

def self.(key)
  key&.end_with?(':fx')
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/deepl/configuration.rb', line 40

def ==(other)
  attributes == other.attributes
end

#attributesObject



36
37
38
# File 'lib/deepl/configuration.rb', line 36

def attributes
  ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
end

#construct_user_agent(send_platform_info, app_info_name, app_info_version) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/deepl/configuration.rb', line 48

def construct_user_agent(send_platform_info, app_info_name, app_info_version)
  library_info_str = 'deepl-ruby/3.0.2'
  if send_platform_info
    library_info_str += " (#{RbConfig::CONFIG['host_os']}) ruby/#{RUBY_VERSION}"
  end
  if app_info_name && app_info_version
    library_info_str += " #{app_info_name}/#{app_info_version}"
  end
  library_info_str
end

#validate!Object

Raises:



32
33
34
# File 'lib/deepl/configuration.rb', line 32

def validate!
  raise Exceptions::Error, 'auth_key not provided' if auth_key.nil? || auth_key.empty?
end