lib/koala/graph_api.rb in koala-1.2.0beta3 vs lib/koala/graph_api.rb in koala-1.2.0beta4
- old
+ new
@@ -69,13 +69,11 @@
# Connections
def get_connections(id, connection_name, args = {}, options = {})
# Fetchs the connections for given object.
- graph_call("#{id}/#{connection_name}", args, "get", options) do |result|
- result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
- end
+ graph_call("#{id}/#{connection_name}", args, "get", options)
end
def put_connections(id, connection_name, args = {}, options = {})
# Posts a certain connection
raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token
@@ -172,13 +170,11 @@
# Search
def search(search_terms, args = {}, options = {})
args.merge!({:q => search_terms}) unless search_terms.nil?
- graph_call("search", args, "get", options) do |result|
- result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
- end
+ graph_call("search", args, "get", options)
end
# Convenience Methods
def get_page_access_token(object_id)
@@ -197,55 +193,48 @@
# GraphCollection support
def get_page(params)
# Pages through a set of results stored in a GraphCollection
# Used for connections and search results
- graph_call(*params) do |result|
- result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
- end
+ graph_call(*params)
end
# Batch API
def batch(http_options = {}, &block)
- batch_client = GraphBatchAPI.new(access_token)
+ batch_client = GraphBatchAPI.new(access_token, self)
if block
yield batch_client
batch_client.execute(http_options)
else
batch_client
end
end
-
- def self.included(base)
- base.class_eval do
- def self.batch
- raise NoMethodError, "The BatchAPI signature has changed (the original implementation was not thread-safe). Please see https://github.com/arsduo/koala/wiki/Batch-requests. (This message will be removed in the final 1.1 release.)"
- end
- end
- end
-
+
# Direct access to the Facebook API
# see any of the above methods for example invocations
def graph_call(path, args = {}, verb = "get", options = {}, &post_processing)
result = api(path, args, verb, options) do |response|
error = check_response(response)
raise error if error
end
- # now process as appropriate (get picture header, make GraphCollection, etc.)
+ # turn this into a GraphCollection if it's pageable
+ result = GraphCollection.evaluate(result, self)
+
+ # now process as appropriate for the given call (get picture header, etc.)
post_processing ? post_processing.call(result) : result
end
+ private
+
def check_response(response)
# check for Graph API-specific errors
# this returns an error, which is immediately raised (non-batch)
# or added to the list of batch results (batch)
if response.is_a?(Hash) && error_details = response["error"]
APIError.new(error_details)
end
end
-
- private
def parse_media_args(media_args, method)
# photo and video uploads can accept different types of arguments (see above)
# so here, we parse the arguments into a form directly usable in put_object
raise KoalaError.new("Wrong number of arguments for put_#{method == "photos" ? "picture" : "video"}") unless media_args.size.between?(1, 5)