Skip to content

Commit

Permalink
mqlread should auto-create arrays of FreebaseResults when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan del Strother authored and Jonathan del Strother committed Jan 2, 2009
1 parent 0dab288 commit 1f99bb8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/freebase/api.rb
Expand Up @@ -207,10 +207,17 @@ def mqlread(query, options = {})
inner = result['qname']
handle_read_error(inner)
Logger.trace {"<<< Received Response: #{inner['result'].inspect}"}
if options[:raw]
inner['result']
query_result = inner['result']

return query_result if options[:raw]

case query_result
when Array
query_result.map{|r| FreebaseResult.new(r)}
when Hash
FreebaseResult.new(query_result)
else
inner['result'] ? FreebaseResult.new(inner['result']) : nil
nil
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/api_test.rb
Expand Up @@ -14,8 +14,15 @@ def test_find_raw_data
assert_equal "The Polyphonic Spree", data['name']
end
def test_find_multiple_records
data = mqlread([{:type => '/music/artist', :name => nil, :'name~=' => '^Tori '}])
assert_instance_of Array, data
assert_instance_of FreebaseResult, data.first
assert data.map{|artist| artist.name}.include?('Tori Amos')
end
def test_find_multiple_raw_data
data = mqlread([{:type => '/music/artist', :name => nil, :'name~=' => '^Tori '}], :raw => true)
assert_instance_of Array, data
assert_instance_of Hash, data.first
assert data.map{|artist| artist['name']}.include?('Tori Amos')
end
end

0 comments on commit 1f99bb8

Please sign in to comment.