Sha256: de5502524503ef43c5d74ee3ede36f5e1832d7296fc256ca6c56c91d2c2c7813

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Cts
  module Mpx
    module Driver
      #
      # Class to contain a response from the services, has a few helper methods to make reading the data easier.
      #
      class Response
        include Creatable

        # @!attribute original
        #   @return [Excon::Response] copy of the original excon response.
        attribute name: 'original', kind_of: Excon::Response

        #
        # Hash output of the data returned from the services.
        #
        # @return [Hash] Hash including keys specific to the service and type of service.
        #
        def data
          return @data if @data

          raise 'response does not appear to be healthy' unless healthy?

          begin
            @data = Oj.load(original.body)
          rescue Oj::ParseError => e
            raise "could not parse data: #{e}"
          end
          @data
        end

        #
        # Is the response healthy?   did it have a status code outside 2xx or 3xx.
        #
        # @return [TrueFalse] false if status <= 199 or => 400, otherwise true.
        #
        def healthy?
          return false if status <= 199 || status >= 400
          true
        end

        #
        # Does this response contain a service exception
        #
        # @return [TrueFalse] true if it does, false if it does not.
        #
        def service_exception?
          data['isException'] == true
        end

        #
        # a page of data, processes the response.data for any entries.
        #
        # @return [Cts::Mpx::Driver::Page] a page of data.
        #
        def page
          raise 'response does not appear to be healthy' unless healthy?
          Cts::Mpx::Driver::Page.create entries: data['entries'], xmlns: data['$xmlns']
        end

        #
        # Status code of the response
        #
        # @return [Fixnum] http status code
        #
        def status
          original.status || nil
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cts-mpx-1.0.3 lib/cts/mpx/driver/response.rb
cts-mpx-1.0.2 lib/cts/mpx/driver/response.rb