Class: Rospatent::Configuration

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

Overview

Configuration class for Rospatent API client

Constant Summary collapse

ENVIRONMENTS =
%w[development staging production].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize a new configuration with default values



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rospatent/configuration.rb', line 32

def initialize
  @api_url = "https://searchplatform.rospatent.gov.ru"
  @token = nil
  @timeout = 30
  @retry_count = 3
  @user_agent = "Rospatent Ruby Client/#{Rospatent::VERSION}"

  # Environment configuration
  @environment = ENV.fetch("ROSPATENT_ENV", "development")

  # Cache configuration
  @cache_enabled = ENV.fetch("ROSPATENT_CACHE_ENABLED", "true") == "true"
  @cache_ttl = ENV.fetch("ROSPATENT_CACHE_TTL", "300").to_i
  @cache_max_size = ENV.fetch("ROSPATENT_CACHE_MAX_SIZE", "1000").to_i

  # Logging configuration
  @log_level = ENV.fetch("ROSPATENT_LOG_LEVEL", "info").to_sym
  @log_requests = ENV.fetch("ROSPATENT_LOG_REQUESTS", "false") == "true"
  @log_responses = ENV.fetch("ROSPATENT_LOG_RESPONSES", "false") == "true"

  # Token management
  @token_expires_at = nil
  @token_refresh_callback = nil

  # Connection pooling
  @connection_pool_size = ENV.fetch("ROSPATENT_POOL_SIZE", "5").to_i
  @connection_keep_alive = ENV.fetch("ROSPATENT_KEEP_ALIVE", "true") == "true"

  load_environment_config
end

Instance Attribute Details

#api_urlObject

Base URL for the Rospatent API



11
12
13
# File 'lib/rospatent/configuration.rb', line 11

def api_url
  @api_url
end

#cache_enabledObject

Cache configuration



23
24
25
# File 'lib/rospatent/configuration.rb', line 23

def cache_enabled
  @cache_enabled
end

#cache_max_sizeObject

Cache configuration



23
24
25
# File 'lib/rospatent/configuration.rb', line 23

def cache_max_size
  @cache_max_size
end

#cache_ttlObject

Cache configuration



23
24
25
# File 'lib/rospatent/configuration.rb', line 23

def cache_ttl
  @cache_ttl
end

#connection_keep_aliveObject

Connection pooling



29
30
31
# File 'lib/rospatent/configuration.rb', line 29

def connection_keep_alive
  @connection_keep_alive
end

#connection_pool_sizeObject

Connection pooling



29
30
31
# File 'lib/rospatent/configuration.rb', line 29

def connection_pool_size
  @connection_pool_size
end

#environmentObject

Current environment



21
22
23
# File 'lib/rospatent/configuration.rb', line 21

def environment
  @environment
end

#log_levelObject

Logging configuration



25
26
27
# File 'lib/rospatent/configuration.rb', line 25

def log_level
  @log_level
end

#log_requestsObject

Logging configuration



25
26
27
# File 'lib/rospatent/configuration.rb', line 25

def log_requests
  @log_requests
end

#log_responsesObject

Logging configuration



25
26
27
# File 'lib/rospatent/configuration.rb', line 25

def log_responses
  @log_responses
end

#retry_countObject

Number of retries for failed requests



17
18
19
# File 'lib/rospatent/configuration.rb', line 17

def retry_count
  @retry_count
end

#timeoutObject

Request timeout in seconds



15
16
17
# File 'lib/rospatent/configuration.rb', line 15

def timeout
  @timeout
end

#tokenObject

JWT token for authentication



13
14
15
# File 'lib/rospatent/configuration.rb', line 13

def token
  @token
end

#token_expires_atObject

Token management



27
28
29
# File 'lib/rospatent/configuration.rb', line 27

def token_expires_at
  @token_expires_at
end

#token_refresh_callbackObject

Token management



27
28
29
# File 'lib/rospatent/configuration.rb', line 27

def token_refresh_callback
  @token_refresh_callback
end

#user_agentObject

User agent to be sent with requests



19
20
21
# File 'lib/rospatent/configuration.rb', line 19

def user_agent
  @user_agent
end

Instance Method Details

#configure_from_hash(options) ⇒ Object

Configure from hash

Parameters:

  • options (Hash)

    Configuration options



99
100
101
102
103
104
# File 'lib/rospatent/configuration.rb', line 99

def configure_from_hash(options)
  options.each do |key, value|
    setter = "#{key}="
    send(setter, value) if respond_to?(setter)
  end
end

#effective_api_urlString

Get environment-specific API URL if needed

Returns:

  • (String)

    API URL for current environment



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rospatent/configuration.rb', line 79

def effective_api_url
  case @environment
  when "development"
    ENV.fetch("ROSPATENT_DEV_API_URL", @api_url)
  when "staging"
    ENV.fetch("ROSPATENT_STAGING_API_URL", @api_url)
  when "production"
    @api_url
  else
    @api_url
  end
end

#reset!Object

Reset configuration to defaults



93
94
95
# File 'lib/rospatent/configuration.rb', line 93

def reset!
  initialize
end

#token_valid?Boolean

Check if the current token is still valid

Returns:

  • (Boolean)

    true if token is valid or no expiration is set



65
66
67
68
69
# File 'lib/rospatent/configuration.rb', line 65

def token_valid?
  return true unless @token_expires_at

  Time.now < @token_expires_at
end

#valid_environment?Boolean

Validate the current environment

Returns:

  • (Boolean)

    true if environment is valid



73
74
75
# File 'lib/rospatent/configuration.rb', line 73

def valid_environment?
  ENVIRONMENTS.include?(@environment)
end