Sha256: 67ebcc3a2f20bba8572b6c384809c43b67ad7071f99b629d9fc7163582acb20f

Contents?: true

Size: 761 Bytes

Versions: 3

Compression:

Stored size: 761 Bytes

Contents

module LazyResource
  class ResourceQueue
    include LazyResource::UrlGeneration

    def initialize
      @queue = []
    end

    def queue(relation)
      @queue.push(relation)
    end

    def flush!
      @queue = []
    end

    def request_queue
      Thread.current[:request_queue] ||= Typhoeus::Hydra.new
    end

    def run
      send_to_request_queue!
      request_queue.run
    end

    def send_to_request_queue!
      while(relation = @queue.pop)
        request = Request.new(url_for(relation), relation)
        request_queue.queue(request)
      end
    end

    def url_for(relation)
      url = ''
      url << relation.klass.site
      url << self.class.collection_path(relation.to_params, nil, relation.from)
      url
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lazy_resource-0.3.0 lib/lazy_resource/resource_queue.rb
lazy_resource-0.2.0 lib/lazy_resource/resource_queue.rb
lazy_resource-0.1.0 lib/lazy_resource/resource_queue.rb