Class: Ken::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/ken/session.rb

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

Constructor Details

#initialize(host, username, password) ⇒ Session

Initialize a new Ken Session

Ken::Session.new(host{String, IO}, username{String}, password{String})

Parameters:

  • host (String)

    the API host

  • username (String)

    freebase username

  • password (String)

    user password



34
35
36
37
38
39
40
41
42
43
# File 'lib/ken/session.rb', line 34

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



64
65
66
67
68
69
70
# File 'lib/ken/session.rb', line 64

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 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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ken/session.rb', line 76

def mqlread(query, options = {})
  Ken.logger.info ">>> Sending Query: #{query.to_json}"
  cursor = options[: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

#service_url(svc) ⇒ Object

get the service url for the specified service.



53
54
55
# File 'lib/ken/session.rb', line 53

def service_url(svc)
  "#{@host}#{SERVICES[svc]}"
end