Sha256: 321c044a2943cb6288c2f1048183e9e72d27819f9c9d3f5425139702c1f2a4f7

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Syncano
  class Response
    class << self
      def handle(raw_response)
        code = unify_code(raw_response)

        case code
          when Status.no_content
          when Status.successful
            parse_response raw_response
          when Status.not_found
            raise NotFound.new(raw_response.env.url, raw_response.env.method)
          when Status.client_error
            raise ClientError.new raw_response.body, raw_response
          when Status.server_error
            raise ServerError.new raw_response.body, raw_response
          else
            raise UnsupportedStatusError.new raw_response
        end
      end

      def parse_response(raw_response)
        JSON.parse(raw_response.body)
      end

      def unify_code(raw_response)
        raw_response.status.code
      end
    end

    class Status
      class << self
        def successful
          ->(code) { (200...300).include? code }
        end

        def client_error
          ->(code) { (400...500).include? code }
        end

        def no_content
          ->(code) { code == 204 }
        end

        def server_error
          ->(code) { code >= 500 }
        end

        def not_found
          ->(code) { code == 404 }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
syncano-4.0.0.alpha4 lib/syncano/response.rb
syncano-4.0.0.alpha3 lib/syncano/response.rb
syncano-4.0.0.alpha2 lib/syncano/response.rb