Sha256: 457bdbb99905140b6f39b354bae1b542650194a29b9d4e8bbcb70f75367cc37b

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

module RestPack::Service
  class Response
    attr_accessor :result, :errors, :status

    def initialize
      @result = {}
      @errors = {}
    end

    def success?
      @errors.empty? and @status == :ok
    end

    def code
      Status.from_status(status)
    end

    def add_error(field, message)
      @errors[field] ||= []
      @errors[field] << message
    end

    def self.from_rest(rest_response)
      response = new
      response.status = Status.from_code(rest_response.code)
      response.result = Yajl::Parser.parse(rest_response.body, :symbolize_keys => true)

      if response.result[:errors]
        response.result[:errors].each do |field, errors|
          response.errors[field.to_sym] = errors
        end

        response.result.delete :errors
      end

      response
    end

    class Status
      @@map = {
        200 => :ok,
        404 => :not_found,
        422 => :unprocessable_entity,
        500 => :internal_service_error
      }

      def self.from_code(code)
        if @@map.has_key?(code)
          @@map[code]
        else
          raise "Invalid Status Code: #{code}"
        end
      end

      def self.from_status(status)
        if @@map.has_value?(status)
          @@map.key(status)
        else
          raise "Invalid Status: #{status}"
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
restpack_service-0.0.32 lib/restpack_service/response.rb
restpack_service-0.0.31 lib/restpack_service/response.rb
restpack_service-0.0.30 lib/restpack_service/response.rb
restpack_service-0.0.29 lib/restpack_service/response.rb
restpack_service-0.0.28 lib/restpack_service/response.rb
restpack_service-0.0.27 lib/restpack_service/response.rb
restpack_service-0.0.26 lib/restpack_service/response.rb
restpack_service-0.0.25 lib/restpack_service/response.rb