Class: JPSClient::Response
- Inherits:
-
Object
- Object
- JPSClient::Response
- Defined in:
- lib/jpsclient/http/http_client.rb
Overview
JPS API 响应封装类 - 简化版
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.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/jpsclient/http/http_client.rb', line 229 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.
225 226 227 |
# File 'lib/jpsclient/http/http_client.rb', line 225 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
226 227 228 |
# File 'lib/jpsclient/http/http_client.rb', line 226 def data @data end |
#msg ⇒ Object (readonly)
Returns the value of attribute msg.
227 228 229 |
# File 'lib/jpsclient/http/http_client.rb', line 227 def msg @msg end |
Instance Method Details
#need_login? ⇒ Boolean
20001: 登录token失效
257 258 259 |
# File 'lib/jpsclient/http/http_client.rb', line 257 def need_login? @need_login || @code.to_s == '20001' end |
#no_permission? ⇒ Boolean
20011: 没有权限访问项目
262 263 264 |
# File 'lib/jpsclient/http/http_client.rb', line 262 def @code.to_s == '20011' end |
#success? ⇒ Boolean
251 252 253 254 |
# File 'lib/jpsclient/http/http_client.rb', line 251 def success? # 成功码从 200 改为 0,同时向后兼容 200 @success && (@code.to_s == '0' || @code.to_s == '200') end |
#to_h ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/jpsclient/http/http_client.rb', line 266 def to_h { 'code' => @code, 'data' => @data, 'msg' => @msg } end |
#to_h_with_meta ⇒ Object
添加新方法返回 meta 格式响应
275 276 277 278 279 280 281 282 283 |
# File 'lib/jpsclient/http/http_client.rb', line 275 def { 'meta' => { 'code' => @code, 'message' => @msg }, 'data' => @data } end |