Class: Ken::Session
Overview
partially taken from chris eppstein’s freebase api github.com/chriseppstein/freebase/tree
Constant Summary collapse
- SERVICES =
{ :mqlread => '/api/service/mqlread', :mqlwrite => '/api/service/mqlwrite', :login => '/api/account/login', :upload => '/api/service/upload' }
Instance Method Summary collapse
-
#handle_read_error(inner) ⇒ Object
raise an error if the inner response envelope is encoded as an error.
-
#initialize(host, username, password) ⇒ Session
constructor
Initialize a new Ken Session Ken::Session.new(hostIO, usernameString, passwordString).
-
#mqlread(query, options = {}) ⇒ Object
perform a mqlread and return the results TODO: should support multiple queries you should be able to pass an array of queries.
-
#service_url(svc) ⇒ Object
get the service url for the specified service.
Constructor Details
#initialize(host, username, password) ⇒ Session
Initialize a new Ken Session
Ken::Session.new(host{String, IO}, username{String}, password{String})
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ken/session.rb', line 31 def initialize(host, username, password) @host = host @username = username @password = password Ken.session = self # TODO: check connection Ken.logger.info("connection established.") end |
Instance Method Details
#handle_read_error(inner) ⇒ Object
raise an error if the inner response envelope is encoded as an error
62 63 64 65 66 67 68 |
# File 'lib/ken/session.rb', line 62 def handle_read_error(inner) unless inner['code'][0, '/api/status/ok'.length] == '/api/status/ok' Ken.logger.error "Read Error #{inner.inspect}" error = inner['messages'][0] raise ReadError.new(error['code'], error['message']) end end |
#mqlread(query, options = {}) ⇒ Object
perform a mqlread and return the results TODO: should support multiple queries
you should be able to pass an array of queries
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ken/session.rb', line 73 def mqlread(query, = {}) Ken.logger.info ">>> Sending Query: #{query.to_json}" envelope = { :qname => {:query => query }} response = http_request mqlread_service_url, :queries => envelope.to_json result = JSON.parse response inner = result['qname'] handle_read_error(inner) Ken.logger.info "<<< Received Response: #{inner['result'].inspect}" # will always return the converted ruby hash (from json) inner['result'] end |