Module: Freebase::Api

Included in:
Base
Defined in:
lib/freebase/api.rb

Defined Under Namespace

Classes: Configuration, FreebaseResult, Logger, MqlReadError

Constant Summary collapse

SERVICES =
{ :mqlread => '/api/service/mqlread',
  :mqlwrite => '/api/service/mqlwrite',
  :login => '/api/account/login',
  :upload => '/api/service/upload'
}

Instance Method Summary collapse

Instance Method Details

#handle_read_error(inner) ⇒ Object

raise an error if the inner response envelope is encoded as an error



178
179
180
181
182
183
184
# File 'lib/freebase/api.rb', line 178

def handle_read_error(inner)
  unless inner['code'].starts_with?('/api/status/ok')
    Logger.error "<<< Received Error: #{inner.inspect}"
    error = inner['messages'][0]
    raise MqlReadError.new(error['code'], error['message'])
  end
end

#mqlread(query, options = {}) ⇒ Object

perform a mqlread and return the results Specify :raw => true if you don’t want the results converted into a FreebaseResult object.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/freebase/api.rb', line 190

def mqlread(query, options = {})
  Logger.trace {">>> Sending Query: #{query.inspect}"}
  envelope = { :qname => {:query => query }}
  response = http_request mqlread_service_url, :queries => envelope.to_json
  result = ActiveSupport::JSON.decode(response)
  inner = result['qname']
  handle_read_error(inner)
  Logger.trace {"<<< Received Response: #{inner['result'].inspect}"}
  if options[:raw]
    inner['result']
  else
    inner['result'] ? FreebaseResult.new(inner['result']) : nil
  end
end

#service_url(svc) ⇒ Object

get the service url for the specified service.



167
168
169
# File 'lib/freebase/api.rb', line 167

def service_url(svc)
  "http://#{Configuration.instance[:host]}#{SERVICES[svc]}"
end