Skip to content

Commit

Permalink
Updated batch refactoring to make specs pass and process errors corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
Chris Baclig committed Jun 24, 2011
1 parent 084353e commit d9aef90
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/koala.rb
Expand Up @@ -12,7 +12,7 @@
require 'koala/http_services/net_http_service'
require 'koala/oauth'
require 'koala/graph_api'
require 'koala/graph_batch_invoker'
require 'koala/graph_batch_api'
require 'koala/batch_operation'
require 'koala/graph_collection'
require 'koala/rest_api'
Expand Down Expand Up @@ -71,7 +71,7 @@ class GraphAPI < API
end

class GraphBatchAPI < GraphAPI
include GraphBatchInvoker
include GraphBatchAPIMethods
end

class RestAPI < API
Expand Down
3 changes: 2 additions & 1 deletion lib/koala/graph_api.rb
Expand Up @@ -214,7 +214,8 @@ def graph_call(path, args = {}, verb = "get", options = {}, &post_processing)
# Direct access to the Facebook API
# see any of the above methods for example invocations
result = api(path, args, verb, options) do |response|
raise error if error = check_response(response)
error = check_response(response)
raise error if error
end

# now process as appropriate (get picture header, make GraphCollection, etc.)
Expand Down
28 changes: 19 additions & 9 deletions lib/koala/graph_batch_invoker.rb → lib/koala/graph_batch_api.rb
@@ -1,14 +1,21 @@
module Koala
module Facebook
module GraphBatchInvoker
module GraphBatchAPIMethods

def self.included(base)
base.class_eval do
alias_method :graph_call_outside_batch, :graph_call
alias_method :graph_call, :graph_call_in_batch

alias_method :check_graph_api_response, :check_response
alias_method :check_response, :check_graph_batch_api_response
end
end

def batch_calls
@batch_calls ||= []
end

def graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing)
# for batch APIs, we queue up the call details (incl. post-processing)
batch_calls << BatchOperation.new(
Expand All @@ -22,19 +29,22 @@ def graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_proce
nil # batch operations return nothing immediately
end

def batch_calls
@batch_calls ||= []
def check_graph_batch_api_response(response)
if response.is_a?(Hash) && response["error"] && !response["error"].is_a?(Hash)
APIError.new("type" => "Error #{response["error"]}", "message" => response["error_description"])
else
check_graph_api_response(response)
end
end

def execute(http_options = {})
return [] unless batch_calls.length > 0
# Turn the call args collected into what facebook expects
args = {
"batch" => batch_calls.map { |batch_op|
args.merge!(batch_op.files) if batch_op.files
batch_op.to_batch_params(access_token)
}.to_json
}
args = {}
args["batch"] = batch_calls.map { |batch_op|
args.merge!(batch_op.files) if batch_op.files
batch_op.to_batch_params(access_token)
}.to_json

graph_call_outside_batch('/', args, 'post', http_options) do |response|
# map the results with post-processing included
Expand Down

0 comments on commit d9aef90

Please sign in to comment.