Sha256: 6e6457d8d3e8ecf99a4078c63d730bc4e7a1ee67c8dd33fd16f160242bfcffa9

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module HttpStore
  module Helpers
    module Responseable
      # check response message is right, if is right
      def response_valid?
        status_code >= 200 && status_code < 400
      end

      def need_retry?
        !response_valid?
      end

      def json_response?
        response_headers_hash['content_type'].to_s =~ /json/
      end

      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

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

      def build_response_code
        response_valid? ? 200 : 422
      end

      def cache_response?
        response_valid?
      end

      # callback
      def after_response; end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
http_store-0.6.19 lib/http_store/helpers/responseable.rb
http_store-0.6.18 lib/http_store/helpers/responseable.rb
http_store-0.6.17 lib/http_store/helpers/responseable.rb