Class: JPSClient::ApiConfig
- Inherits:
-
Object
- Object
- JPSClient::ApiConfig
- Defined in:
- lib/jpsclient/base/api_config.rb
Overview
API 配置管理类
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
Instance Method Summary collapse
-
#get_endpoint(category, action) ⇒ Object
获取API端点信息.
-
#get_error_message(code) ⇒ Object
获取错误描述.
-
#get_method(category, action) ⇒ Object
获取API方法.
-
#get_parameters(category, action) ⇒ Object
获取参数定义.
-
#get_url(category, action) ⇒ Object
获取完整的API URL.
-
#initialize(config_json) ⇒ ApiConfig
constructor
A new instance of ApiConfig.
-
#requires_auth?(category, action) ⇒ Boolean
是否需要认证.
-
#validate_params(category, action, params) ⇒ Object
验证参数.
Constructor Details
#initialize(config_json) ⇒ ApiConfig
Returns a new instance of ApiConfig.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jpsclient/base/api_config.rb', line 9 def initialize(config_json) @base_url = config_json["pgyerapps_base_url"] # 兼容旧配置格式 if config_json["api_path_config"] @legacy_config = config_json["api_path_config"] @endpoints = convert_legacy_config(@legacy_config) elsif config_json["api_endpoints"] @endpoints = config_json["api_endpoints"] else raise ExceptionError, "API配置格式错误:缺少api_path_config或api_endpoints" end @error_codes = config_json["error_codes"] || default_error_codes @rate_limits = config_json["rate_limits"] || default_rate_limits end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/jpsclient/base/api_config.rb', line 7 def base_url @base_url end |
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
7 8 9 |
# File 'lib/jpsclient/base/api_config.rb', line 7 def endpoints @endpoints end |
Instance Method Details
#get_endpoint(category, action) ⇒ Object
获取API端点信息
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jpsclient/base/api_config.rb', line 27 def get_endpoint(category, action) if @legacy_config # 兼容旧格式 path = @legacy_config["#{category}_#{action}"] || @legacy_config[action] return nil unless path { "path" => path, "method" => guess_method(action), "requires_auth" => !action.include?("login"), "description" => "#{category}.#{action}" } else # 新格式 @endpoints.dig(category.to_s, action.to_s) end end |
#get_error_message(code) ⇒ Object
获取错误描述
100 101 102 |
# File 'lib/jpsclient/base/api_config.rb', line 100 def (code) @error_codes[code.to_s] || "未知错误 (#{code})" end |
#get_method(category, action) ⇒ Object
获取API方法
54 55 56 57 |
# File 'lib/jpsclient/base/api_config.rb', line 54 def get_method(category, action) endpoint = get_endpoint(category, action) endpoint ? endpoint['method'] : 'GET' end |
#get_parameters(category, action) ⇒ Object
获取参数定义
66 67 68 69 |
# File 'lib/jpsclient/base/api_config.rb', line 66 def get_parameters(category, action) endpoint = get_endpoint(category, action) endpoint ? endpoint.fetch('parameters', {}) : {} end |
#get_url(category, action) ⇒ Object
获取完整的API URL
46 47 48 49 50 51 |
# File 'lib/jpsclient/base/api_config.rb', line 46 def get_url(category, action) endpoint = get_endpoint(category, action) return nil unless endpoint "#{@base_url}#{endpoint['path']}" end |
#requires_auth?(category, action) ⇒ Boolean
是否需要认证
60 61 62 63 |
# File 'lib/jpsclient/base/api_config.rb', line 60 def requires_auth?(category, action) endpoint = get_endpoint(category, action) endpoint ? endpoint.fetch('requires_auth', true) : true end |
#validate_params(category, action, params) ⇒ Object
验证参数
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jpsclient/base/api_config.rb', line 72 def validate_params(category, action, params) param_definitions = get_parameters(category, action) errors = [] # 检查必需参数 param_definitions.each do |name, definition| if definition['required'] && !params.key?(name.to_sym) && !params.key?(name.to_s) errors << "缺少必需参数: #{name}" end # 类型检查(如果提供了参数) value = params[name.to_sym] || params[name.to_s] if value && definition['type'] unless check_type(value, definition['type']) errors << "参数 #{name} 类型错误,期望 #{definition['type']}" end end # 枚举值检查 if value && definition['enum'] && !definition['enum'].include?(value) errors << "参数 #{name} 值无效,必须是: #{definition['enum'].join(', ')}" end end errors end |