PredictionIO/PredictionIO-Ruby-SDK

View on GitHub
lib/predictionio/async_response.rb

Summary

Maintainability
A
0 mins
Test Coverage
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'thread'

module PredictionIO
  # This class encapsulates an asynchronous response that will block the caller until the response is available.
  class AsyncResponse

    # The PredictionIO::AsyncRequest instance that created the current PredictionIO::AsyncResponse instance.
    attr_reader :request

    # Create the response by saving the request, and optionally the Net::HTTPResponse object.
    def initialize(request, response = nil)
      @request = request
      @response = Queue.new
      set(response) if response
    end

    # Save a Net::HTTPResponse instance to the current instance.
    # This will unblock any caller that called #get.
    def set(response)
      @response.push(response)
    end

    # Get the Net::HTTPResponse instance. This will block if the response is not yet available.
    def get
      @response.pop
    end
  end
end