Class: Geoloqi::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/geoloqi/batch.rb

Overview

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 Also:

Defined Under Namespace

Classes: NotImplementedError

Constant Summary collapse

PER_REQUEST_LIMIT =

This keeps the batcher from going overboard.

200

Instance Method Summary collapse

Constructor Details

#initialize(session, &block) ⇒ Batch

Returns a new instance of Batch.



11
12
13
14
15
# File 'lib/geoloqi/batch.rb', line 11

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

Instance Method Details

#build_request(path, query = nil, headers = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/geoloqi/batch.rb', line 26

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

#get(path, query = nil, headers = {}) ⇒ Object



21
22
23
24
# File 'lib/geoloqi/batch.rb', line 21

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

#post(path, query = nil, headers = {}) ⇒ Object



17
18
19
# File 'lib/geoloqi/batch.rb', line 17

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

#run!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/geoloqi/batch.rb', line 34

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