Skip to content

Commit

Permalink
Added FQL multiquery support
Browse files Browse the repository at this point in the history
  • Loading branch information
simianarmy authored and mmangino committed Mar 2, 2011
1 parent c4cced8 commit 1c70682
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/mogli/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def fql_path
"https://api.facebook.com/method/fql.query"
end

def fql_multiquery_path
"https://api.facebook.com/method/fql.multiquery"
end

def initialize(access_token = nil,expiration=nil)
@access_token = access_token
# nil expiration means extended access
Expand Down Expand Up @@ -108,6 +112,20 @@ def fql_query(query,klass=nil,format="json")
map_data(data,klass)
end

def fql_multiquery(queries,klass=nil,format="json")
data = self.class.post(fql_multiquery_path,:body=>default_params.merge({:queries=>queries.to_json,:format=>format}))
return data unless format=="json"
if response = map_data(data,klass)
# Following Facebooker convention & parsing results into constituent results
# map subquery names to result set in hash
# Returns: {'fql-query-name1' => [fql-query-name1-values], ...}
queries.each_key.inject({}) do |res, q|
res[q] = response.find{|r| r['name'] == q.to_s}.fetch('fql_result_set')
res
end
end
end

def get_and_map(path,klass=nil,body_args = {})
data = self.class.get(api_path(path),:query=>default_params.merge(body_args))
data = data.values if body_args.key?(:ids) && !data.key?('error')
Expand Down
30 changes: 30 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@
end
end

describe "fql multiqueries" do
it "defaults to json" do
fql = {"query1"=>"select uid from user"}
Mogli::Client.should_receive(:post).with("https://api.facebook.com/method/fql.multiquery",:body=>{:format=>"json",:queries=>fql.to_json,:access_token=>"1234"})
client = Mogli::Client.new("1234")
client.fql_multiquery(fql)
end

it "supports xml" do
fql = {"query1"=>"select uid from user"}
Mogli::Client.should_receive(:post).with("https://api.facebook.com/method/fql.multiquery",:body=>{:format=>"xml",:queries=>fql.to_json,:access_token=>"1234"})
client = Mogli::Client.new("1234")
client.fql_multiquery(fql,nil,"xml")
end

it "returns hash with key names used in query mapping to return values" do
# contrived multiquery
fql = {"query1"=>"SELECT fromid FROM comment WHERE post_id = 123343434",
"query2"=>"SELECT uid, first_name, last_name FROM user WHERE uid IN (SELECT fromid FROM #query1)"
}
Mogli::Client.should_receive(:post).and_return([{"name"=>"query2", "fql_result_set"=>[{"uid"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino"}]},
{"name"=>"query1","fql_result_set"=>[{"fromid"=>12451752}]}])
client = Mogli::Client.new("1234")
res = client.fql_multiquery(fql)
res.should be_an_instance_of(Hash)
res['query1'].should == [{"fromid"=>12451752}]
res['query2'].should == [{"uid"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino"}]
end
end

describe "result mapping" do

let :user_data do
Expand Down

0 comments on commit 1c70682

Please sign in to comment.