Module: HttpStore::Helpers::Responseable

Included in:
Client
Defined in:
lib/http_store/helpers/responseable.rb

Instance Method Summary collapse

Instance Method Details

#after_responseObject

callback



47
# File 'lib/http_store/helpers/responseable.rb', line 47

def after_response; end

#build_responseObject



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_codeObject



38
39
40
# File 'lib/http_store/helpers/responseable.rb', line 38

def build_response_code
  response_valid? ? 200 : 422
end

#build_response_dataObject



34
35
36
# File 'lib/http_store/helpers/responseable.rb', line 34

def build_response_data
  response_valid ? 'success' : 'error'
end

#cache_response?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/http_store/helpers/responseable.rb', line 42

def cache_response?
  response_valid?
end

#json_response?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


5
6
7
# File 'lib/http_store/helpers/responseable.rb', line 5

def response_valid?
  status_code >= 200 && status_code < 400
end