Sha256: 9e31d679fd7dec3f047ebeb038a44727b7208b0e0e1bf30b63df4ea4b72d8e45

Contents?: true

Size: 1.5 KB

Versions: 16

Compression:

Stored size: 1.5 KB

Contents

require "net/http"
require "json"

require "processout/errors/authentication_error"
require "processout/errors/generic_error"
require "processout/errors/internal_error"
require "processout/errors/notfound_error"
require "processout/errors/validation_error"

module ProcessOut
  class Response
  attr_reader :body

    def initialize(resp)
      @resp = resp
      @status = resp.code.to_i
      @body = JSON.parse(resp.body)
      self.check
    end

    # Success returns whether or not the response returned a successful message
    def success
      if @body.include? "success"
        return @body["success"]
      end
      false
    end

    # Message returns the error message contained in the response, if any
    def message
      if @body.include? "message"
        return @body["message"]
      end
      ""
    end

    # Check checks the response didn't contain any error, or raises an
    # error if one was found
    def check
      unless self.success
        if @status == 404
          raise NotFoundError, self.message
        end
        if @status == 401
          raise AuthenticationError, self.message
        end
        if @status == 400
          raise ValidationError, self.message
        end
        if @status >= 500
          raise InternalError, "ProcessOut returned an internal error (" +
            @status.to_s + "): " + self.message
        end

        raise GenericError, "ProcessOut returned an error (" +
          @status.to_s + "): " + self.message
      end
    end
    protected :check
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
processout-1.0.12 lib/processout/networking/response.rb
processout-1.0.11 lib/processout/networking/response.rb
processout-1.0.10 lib/processout/networking/response.rb
processout-1.0.9 lib/processout/networking/response.rb
processout-1.0.8 lib/processout/networking/response.rb
processout-1.0.7 lib/processout/networking/response.rb
processout-1.0.6 lib/processout/networking/response.rb
processout-1.0.5 lib/processout/networking/response.rb
processout-1.0.4 lib/processout/networking/response.rb
processout-1.0.3 lib/processout/networking/response.rb
processout-1.0.2 lib/processout/networking/response.rb
processout-1.0.1 lib/processout/networking/response.rb
processout-1.0.0 lib/processout/networking/response.rb
processout-0.3.0 lib/processout/networking/response.rb
processout-0.2.1 lib/processout/networking/response.rb
processout-0.2.0 lib/processout/networking/response.rb