Class: Basuco::Search
Overview
partially taken from chris eppstein’s freebase api github.com/chriseppstein/freebase/tree
Constant Summary
Constants included from Request
Instance Method Summary collapse
-
#all(options = {}) ⇒ Object
a
Collection
ofResources
. -
#get(id) ⇒ Object
Executes an Mql Query against the Freebase API and returns the result wrapped in a
Resource
Object. -
#initialize(options = {:host => 'http://www.freebase.com', :username => 'un', :password => 'pw'}) ⇒ Search
constructor
A new instance of Search.
-
#mqlread(query, options = {}) ⇒ Object
Perform a mqlread and return the results Specify :cursor => true to batch the results of a query, sending multiple requests if necessary.
- #search(query, options = {}) ⇒ Object
-
#status? ⇒ Boolean
yes or no.
- #topic(id, options = {}) ⇒ Object
Methods included from Request
#get_query_response, #handle_read_error, #http_request, #params_to_string, #service_url
Constructor Details
#initialize(options = {:host => 'http://www.freebase.com', :username => 'un', :password => 'pw'}) ⇒ Search
Returns a new instance of Search.
12 13 14 15 16 17 18 19 |
# File 'lib/basuco/search.rb', line 12 def initialize( = {:host => 'http://www.freebase.com', :username => 'un', :password => 'pw'}) @host = [:host] @username = [:username] @password = [:password] Basuco.search = self # TODO: check connection end |
Instance Method Details
#all(options = {}) ⇒ Object
a Collection
of Resources
.
45 46 47 48 49 50 |
# File 'lib/basuco/search.rb', line 45 def all( = {}) #assert_kind_of 'options', options, Hash query = { :name => nil }.merge!().merge!(:id => nil) result = mqlread([ query ], :cursor => ![:limit]) Basuco::Collection.new(result.map { |r| Basuco::Resource.new(r) }) end |
#get(id) ⇒ Object
Executes an Mql Query against the Freebase API and returns the result wrapped in a Resource
Object.
Examples
Basuco.get('/en/the_police') => #<Resource id="/en/the_police" name="The Police">
60 61 62 |
# File 'lib/basuco/search.rb', line 60 def get(id) Basuco::Resource.get(id) end |
#mqlread(query, options = {}) ⇒ Object
Perform a mqlread and return the results Specify :cursor => true to batch the results of a query, sending multiple requests if necessary. TODO: should support multiple queries
you should be able to pass an array of queries
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/basuco/search.rb', line 25 def mqlread(query, = {}) cursor = [:cursor] if cursor query_result = [] while cursor response = get_query_response(query, cursor) query_result += response['result'] cursor = response['cursor'] end else response = get_query_response(query, cursor) cursor = response['cursor'] query_result = response['result'] end query_result end |
#search(query, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/basuco/search.rb', line 76 def search(query, = {}) .merge!({:query => query}) response = http_request search_service_url, result = JSON.parse response handle_read_error(result) result['result'] end |
#status? ⇒ Boolean
yes or no
89 90 91 92 93 |
# File 'lib/basuco/search.rb', line 89 def status? response = http_request status_service_url result = JSON.parse response return result["code"] == "/api/status/ok" end |
#topic(id, options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/basuco/search.rb', line 65 def topic(id, = {}) .merge!({:id => id}) response = http_request topic_service_url+"/standard", --debugger result = JSON.parse response inner = result[id] handle_read_error(inner) inner['result'] end |