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
DEFAULT_VALIDATION_LIMITS =

Default validation limits applied to all configurations. Partial overrides via #validation_limits= are merged into these defaults.

{
  query_max_length: 2000,
  natural_query_max_length: 2000,
  limit_max_value: 100,
  offset_max_value: 10_000,
  array_max_size: 10,
  string_max_length: 1000,
  pre_tag_max_length: 50,
  post_tag_max_length: 50,
  pre_tag_max_size: 10,
  post_tag_max_size: 10,
  classification_query_max_length: 1000,
  classification_code_max_length: 50,
  similar_text_min_words: 50,
  similar_text_max_length: 10_000,
  similar_count_max_value: 1000,
  batch_size_max_value: 50,
  batch_ids_max_size: 1000
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize a new configuration with default values



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rospatent/configuration.rb', line 56

def initialize
  @api_url = "https://searchplatform.rospatent.gov.ru"
  @token = ENV["ROSPATENT_TOKEN"] || ENV.fetch("ROSPATENT_API_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"

  # Validation limits — start from defaults; partial overrides merge in.
  @validation_limits = DEFAULT_VALIDATION_LIMITS.dup

  load_environment_config
end

Instance Attribute Details

#api_urlObject

Base URL for the Rospatent API



33
34
35
# File 'lib/rospatent/configuration.rb', line 33

def api_url
  @api_url
end

#cache_enabledObject

Cache configuration



45
46
47
# File 'lib/rospatent/configuration.rb', line 45

def cache_enabled
  @cache_enabled
end

#cache_max_sizeObject

Cache configuration



45
46
47
# File 'lib/rospatent/configuration.rb', line 45

def cache_max_size
  @cache_max_size
end

#cache_ttlObject

Cache configuration



45
46
47
# File 'lib/rospatent/configuration.rb', line 45

def cache_ttl
  @cache_ttl
end

#connection_keep_aliveObject

Connection pooling



51
52
53
# File 'lib/rospatent/configuration.rb', line 51

def connection_keep_alive
  @connection_keep_alive
end

#connection_pool_sizeObject

Connection pooling



51
52
53
# File 'lib/rospatent/configuration.rb', line 51

def connection_pool_size
  @connection_pool_size
end

#environmentObject

Current environment



43
44
45
# File 'lib/rospatent/configuration.rb', line 43

def environment
  @environment
end

#log_levelObject

Logging configuration



47
48
49
# File 'lib/rospatent/configuration.rb', line 47

def log_level
  @log_level
end

#log_requestsObject

Logging configuration



47
48
49
# File 'lib/rospatent/configuration.rb', line 47

def log_requests
  @log_requests
end

#log_responsesObject

Logging configuration



47
48
49
# File 'lib/rospatent/configuration.rb', line 47

def log_responses
  @log_responses
end

#retry_countObject

Number of retries for failed requests



39
40
41
# File 'lib/rospatent/configuration.rb', line 39

def retry_count
  @retry_count
end

#timeoutObject

Request timeout in seconds



37
38
39
# File 'lib/rospatent/configuration.rb', line 37

def timeout
  @timeout
end

#tokenObject

JWT token for authentication



35
36
37
# File 'lib/rospatent/configuration.rb', line 35

def token
  @token
end

#token_expires_atObject

Token management



49
50
51
# File 'lib/rospatent/configuration.rb', line 49

def token_expires_at
  @token_expires_at
end

#token_refresh_callbackObject

Token management



49
50
51
# File 'lib/rospatent/configuration.rb', line 49

def token_refresh_callback
  @token_refresh_callback
end

#user_agentObject

User agent to be sent with requests



41
42
43
# File 'lib/rospatent/configuration.rb', line 41

def user_agent
  @user_agent
end

#validation_limitsObject

Validation limits — readable as a hash; assignment merges into defaults.



53
54
55
# File 'lib/rospatent/configuration.rb', line 53

def validation_limits
  @validation_limits
end

Instance Method Details

#configure_from_hash(options) ⇒ Object

Configure from hash

Parameters:

  • options (Hash)

    Configuration options



126
127
128
129
130
131
# File 'lib/rospatent/configuration.rb', line 126

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



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rospatent/configuration.rb', line 106

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



120
121
122
# File 'lib/rospatent/configuration.rb', line 120

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



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

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



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

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