Sha256: aaee38550c3a5481633aa0d51c97666fcd0bded886558e4e61f4f0be5264f747

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

module Geoloqi
  # This class is a builder/DSL class used to construct batch queries against the Geoloqi API. The best way to use this is
  # to use it from the Session class.
  # @see Session#batch
  class Batch
    # This keeps the batcher from going overboard.
    PER_REQUEST_LIMIT = 200 

    class NotImplementedError < StandardError; end

    def initialize(session, &block)
      @jobs = []
      @session = session
      self.instance_eval(&block)
    end

    def post(path, query=nil, headers={})
       build_request path, query, headers
    end

    def get(path, query=nil, headers={})
      path += "?#{Rack::Utils.parse_query query}" if query.is_a?(String)
      build_request path, nil, headers
    end

    def build_request(path, query=nil, headers={})
      @jobs << {
        :relative_url => path,
        :body => query,
        :headers => headers
      }
    end

    def run!
      results = []

      until @jobs.empty?
        queued_jobs = @jobs.slice! 0, PER_REQUEST_LIMIT

        results << @session.post('batch/run', {
          :access_token => @session.access_token,
          :batch => queued_jobs
        })[:result]
      end

      results.flatten!
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
geoloqi-0.9.44 lib/geoloqi/batch.rb
geoloqi-0.9.43 lib/geoloqi/batch.rb
geoloqi-0.9.41 lib/geoloqi/batch.rb
geoloqi-0.9.40 lib/geoloqi/batch.rb
geoloqi-0.9.39 lib/geoloqi/batch.rb
geoloqi-0.9.38 lib/geoloqi/batch.rb