Sha256: fe6fa062d139e387ac9bd61754bfd27cf9c32e303b1c16791b9da817e7fcc685

Contents?: true

Size: 940 Bytes

Versions: 33

Compression:

Stored size: 940 Bytes

Contents

module Unimatrix

  class Response

    attr_reader :code
    attr_reader :body
    attr_reader :resources

    def initialize( http_response )
      @success        = http_response.is_a?( Net::HTTPOK )
      @code           = http_response.code
      @resources      = []
      @body           = decode_response_body( http_response )

      if ( @body && @body.respond_to?( :keys ) )
        Parser.new( @body ) do | parser |
          @resources = parser.resources
          @success   = !( parser.type_name == 'error' )
        end
      else
        @success  = false
        @resources << Error.new(
          message: "#{ @code }: #{ http_response.message }."
        )
      end
    end

    def success?
      @success
    end

    protected; def decode_response_body( http_response )
      body = http_response.body

      if body.present?
        JSON.parse( body ) rescue nil
      else
        nil
      end
    end

  end

end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
unimatrix-3.4.0 lib/unimatrix/response.rb
unimatrix-3.3.4 lib/unimatrix/response.rb
unimatrix-3.3.3 lib/unimatrix/response.rb
unimatrix-3.3.2 lib/unimatrix/response.rb
unimatrix-3.3.1 lib/unimatrix/response.rb
unimatrix-3.3.0 lib/unimatrix/response.rb
unimatrix-3.2.0 lib/unimatrix/response.rb
unimatrix-3.1.0 lib/unimatrix/response.rb
unimatrix-3.0.0 lib/unimatrix/response.rb
unimatrix-2.9.0 lib/unimatrix/response.rb
unimatrix-2.8.0 lib/unimatrix/response.rb
unimatrix-2.7.1 lib/unimatrix/response.rb
unimatrix-2.7.0 lib/unimatrix/response.rb
unimatrix-2.6.1 lib/unimatrix/response.rb
unimatrix-2.6.0 lib/unimatrix/response.rb
unimatrix-2.5.5 lib/unimatrix/response.rb
unimatrix-2.5.4 lib/unimatrix/response.rb
unimatrix-2.5.3 lib/unimatrix/response.rb
unimatrix-2.5.2 lib/unimatrix/response.rb
unimatrix-2.5.1 lib/unimatrix/response.rb