Class: Baloo::BatchRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/baloo/batch_request.rb

Constant Summary collapse

Limit =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatchRequest

Returns a new instance of BatchRequest.



9
10
11
12
# File 'lib/baloo/batch_request.rb', line 9

def initialize
  @requests = []
  @responses = []
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



7
8
9
# File 'lib/baloo/batch_request.rb', line 7

def requests
  @requests
end

#responsesObject

Returns the value of attribute responses.



7
8
9
# File 'lib/baloo/batch_request.rb', line 7

def responses
  @responses
end

Instance Method Details

#add(method, path, body = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/baloo/batch_request.rb', line 14

def add(method, path, body = {})
  request = { :method => method, :relative_url => path }
  unless body.empty?
    request[:body] = HTTParty::HashConversions.to_params(body)
  end
  @requests << request
end

#perform(&block) ⇒ Object

peform the batch request the passed block will recieve each response as it is parsed

Raises:

  • (Exception)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/baloo/batch_request.rb', line 24

def perform(&block)
  raise Exception.new("You must have at least 2 requests") unless @requests.length > 1
  @responses.clear
  requests.each_slice(Limit).to_a.each do |batch|
    body = {
      :batch => Yajl::Encoder.encode(batch),
      :access_token => Baloo.client_credentials
    }
    Client.post("/", :body => body).each do |response|
      # response['headers'] = Yajl::Parser.parse(response['headers'])
      response['body'] = Yajl::Parser.parse(response['body'])
      yield response
    end
  end
end