Module: HttpStore::Helpers::Responseable
- Included in:
- Client
- Defined in:
- lib/http_store/helpers/responseable.rb
Instance Method Summary collapse
-
#after_response ⇒ Object
callback.
- #build_response ⇒ Object
- #build_response_code ⇒ Object
- #build_response_data ⇒ Object
- #cache_response? ⇒ Boolean
- #json_response? ⇒ Boolean
- #need_retry? ⇒ Boolean
-
#response_valid? ⇒ Boolean
check response message is right, if is right.
Instance Method Details
#after_response ⇒ Object
callback
47 |
# File 'lib/http_store/helpers/responseable.rb', line 47 def after_response; end |
#build_response ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/http_store/helpers/responseable.rb', line 17 def build_response response = json_safe_parse(response_obj.body) response = { original: response } if response.is_a?(String) @meta.response = response @meta.status_code = response_obj.code @meta.response_headers = response_obj.headers @meta.response_valid = response_valid? @meta.response_code = build_response_code @meta.response_data = build_response_data @meta.cache_response = cache_response? raise HttpStore::RequestError, '三方请求异常, 请与管理员联系' if response_data.nil? Rails.logger.info "#{uri}: response: #{response}" Rails.logger.info "#{uri}: response_data: #{response_data}" end |
#build_response_code ⇒ Object
38 39 40 |
# File 'lib/http_store/helpers/responseable.rb', line 38 def build_response_code response_valid? ? 200 : 422 end |
#build_response_data ⇒ Object
34 35 36 |
# File 'lib/http_store/helpers/responseable.rb', line 34 def build_response_data response_valid ? 'success' : 'error' end |
#cache_response? ⇒ Boolean
42 43 44 |
# File 'lib/http_store/helpers/responseable.rb', line 42 def cache_response? response_valid? end |
#json_response? ⇒ Boolean
13 14 15 |
# File 'lib/http_store/helpers/responseable.rb', line 13 def json_response? response_headers_hash['content_type'].to_s =~ /json/ end |
#need_retry? ⇒ Boolean
9 10 11 |
# File 'lib/http_store/helpers/responseable.rb', line 9 def need_retry? !response_valid? end |
#response_valid? ⇒ Boolean
check response message is right, if is right
5 6 7 |
# File 'lib/http_store/helpers/responseable.rb', line 5 def response_valid? status_code >= 200 && status_code < 400 end |