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



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rospatent/configuration.rb', line 34

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
  @validation_limits = {
    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
  }

  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

#validation_limitsObject

Validation limits



31
32
33
# File 'lib/rospatent/configuration.rb', line 31

def validation_limits
  @validation_limits
end

Instance Method Details

#configure_from_hash(options) ⇒ Object

Configure from hash

Parameters:

  • options (Hash)

    Configuration options



122
123
124
125
126
127
# File 'lib/rospatent/configuration.rb', line 122

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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rospatent/configuration.rb', line 102

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



116
117
118
# File 'lib/rospatent/configuration.rb', line 116

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



88
89
90
91
92
# File 'lib/rospatent/configuration.rb', line 88

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



96
97
98
# File 'lib/rospatent/configuration.rb', line 96

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