Sha256: 553094ce38adcae57ac16c263f49e7cc9abeb8a134beb31f0fd73247377108d4

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

module WssAgent
  class Response
    SUCCESS_STATUS = 1
    BAD_REQUEST_STATUS = 2
    SERVER_ERROR_STATUS = 3

    attr_reader :response, :status, :message, :response_data, :data

    def initialize(response)
      @response = response
      if response.is_a?(Faraday::Error::ClientError)
        parse_error
      else
        parse_response
      end
    end

    def parse_error
      @status = SERVER_ERROR_STATUS
      @message = response.message
    end

    def parse_response
      if response.success?
        begin
          @response_data = MultiJson.load(response.body)
          @status = @response_data['status'].to_i
          @message = @response_data['message']
        rescue
          @status = SERVER_ERROR_STATUS
          @message = response.body
        end
      else
        @status = SERVER_ERROR_STATUS
        @message = response.body
      end
    end

    def response_success?
      if response.is_a?(Faraday::Error::ClientError)
        false
      else
        response.success?
      end
    end

    def success?
      response_success? && status == SUCCESS_STATUS
    end

    def data
      @data ||= MultiJson.load(response_data['data'])
    rescue
      response_data && response_data.key?('data') ? response_data['data'] : nil
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
wss_agent-18.10.2 lib/wss_agent/response.rb
wss_agent-18.6.2 lib/wss_agent/response.rb
wss_agent-18.5.2 lib/wss_agent/response.rb
wss_agent-17.12.2 lib/wss_agent/response.rb
wss_agent-0.0.26 lib/wss_agent/response.rb
wss_agent-0.0.25 lib/wss_agent/response.rb
wss_agent-0.0.24 lib/wss_agent/response.rb
wss_agent-0.0.23 lib/wss_agent/response.rb
wss_agent-0.0.22 lib/wss_agent/response.rb
wss_agent-0.0.21 lib/wss_agent/response.rb