Class: JPSClient::Response
- Inherits:
-
Object
- Object
- JPSClient::Response
- Defined in:
- lib/jpsclient/http/http_client.rb
Overview
JPS API 响应封装类 - 简化版
Constant Summary collapse
- NEED_LOGIN_CODE =
登录 token 失效的业务码。调用方需要按它区分「认证失败」和「网络/服务端故障」, 提成常量避免各处硬编码
'20001'.freeze
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#msg ⇒ Object
readonly
Returns the value of attribute msg.
Instance Method Summary collapse
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
-
#need_login? ⇒ Boolean
20001: 登录token失效.
-
#no_permission? ⇒ Boolean
20011: 没有权限访问项目.
- #success? ⇒ Boolean
- #to_h ⇒ Object
-
#to_h_with_meta ⇒ Object
添加新方法返回 meta 格式响应.
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/jpsclient/http/http_client.rb', line 247 def initialize(http_response) @success = http_response[:success] @need_login = http_response[:need_login] if http_response[:body].is_a?(Hash) # 处理 Meta 格式响应: {meta: {code: 0, message: "..."}, data: {...}} if http_response[:body]['meta'] @code = http_response[:body]['meta']['code'] @msg = http_response[:body]['meta']['message'] @data = http_response[:body]['data'] # 处理直接格式响应: {code: 0, data: {...}, msg: "..."} else @code = http_response[:body]['code'] || http_response[:code] @data = http_response[:body]['data'] @msg = http_response[:body]['msg'] || http_response[:body]['message'] end else @code = http_response[:code] @data = http_response[:body] end end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
243 244 245 |
# File 'lib/jpsclient/http/http_client.rb', line 243 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
244 245 246 |
# File 'lib/jpsclient/http/http_client.rb', line 244 def data @data end |
#msg ⇒ Object (readonly)
Returns the value of attribute msg.
245 246 247 |
# File 'lib/jpsclient/http/http_client.rb', line 245 def msg @msg end |
Instance Method Details
#need_login? ⇒ Boolean
20001: 登录token失效
275 276 277 |
# File 'lib/jpsclient/http/http_client.rb', line 275 def need_login? @need_login || @code.to_s == NEED_LOGIN_CODE end |
#no_permission? ⇒ Boolean
20011: 没有权限访问项目
280 281 282 |
# File 'lib/jpsclient/http/http_client.rb', line 280 def @code.to_s == '20011' end |
#success? ⇒ Boolean
269 270 271 272 |
# File 'lib/jpsclient/http/http_client.rb', line 269 def success? # 成功码从 200 改为 0,同时向后兼容 200 @success && (@code.to_s == '0' || @code.to_s == '200') end |
#to_h ⇒ Object
284 285 286 287 288 289 290 |
# File 'lib/jpsclient/http/http_client.rb', line 284 def to_h { 'code' => @code, 'data' => @data, 'msg' => @msg } end |
#to_h_with_meta ⇒ Object
添加新方法返回 meta 格式响应
293 294 295 296 297 298 299 300 301 |
# File 'lib/jpsclient/http/http_client.rb', line 293 def { 'meta' => { 'code' => @code, 'message' => @msg }, 'data' => @data } end |