Class: Baloo::BatchRequest
- Inherits:
-
Object
- Object
- Baloo::BatchRequest
- Defined in:
- lib/baloo/batch_request.rb
Constant Summary collapse
- Limit =
20
Instance Attribute Summary collapse
-
#requests ⇒ Object
Returns the value of attribute requests.
-
#responses ⇒ Object
Returns the value of attribute responses.
Instance Method Summary collapse
- #add(method, path, body = {}) ⇒ Object
-
#initialize ⇒ BatchRequest
constructor
A new instance of BatchRequest.
-
#perform(&block) ⇒ Object
peform the batch request the passed block will recieve each response as it is parsed.
Constructor Details
#initialize ⇒ BatchRequest
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
#requests ⇒ Object
Returns the value of attribute requests.
7 8 9 |
# File 'lib/baloo/batch_request.rb', line 7 def requests @requests end |
#responses ⇒ Object
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
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 |