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',
  :blurb => '/api/trans/blurb',
  :raw => '/api/trans/raw',
  :login => '/api/account/login',
  :upload => '/api/service/upload',
  :topic => '/experimental/topic',
  :search => '/api/service/search'
}

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



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

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

#blurb_content(id, options = {}) ⇒ Object



105
106
107
108
109
# File 'lib/ken/session.rb', line 105

def blurb_content(id, options = {})
  response = http_request blurb_service_url+id, options
  Ken.logger.info "<<< Received Blurb Content Response: #{response}"
  response
end

#handle_read_error(inner) ⇒ Object

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



69
70
71
72
73
74
75
# File 'lib/ken/session.rb', line 69

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ken/session.rb', line 81

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

#raw_content(id, options = {}) ⇒ Object



99
100
101
102
103
# File 'lib/ken/session.rb', line 99

def raw_content(id, options = {})
  response = http_request raw_service_url+id, options
  Ken.logger.info "<<< Received Raw Content Response: #{response}"
  response
end

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ken/session.rb', line 122

def search(query, options = {})
  Ken.logger.info ">>> Sending Search Query: #{query}"
  options.merge!({:query => query})
  
  response = http_request search_service_url, options
  result = JSON.parse response
  
  handle_read_error(result)
  
  Ken.logger.info "<<< Received Topic Response: #{result['result'].inspect}"
  result['result']
end

#service_url(svc) ⇒ Object

get the service url for the specified service.



58
59
60
# File 'lib/ken/session.rb', line 58

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

#topic(id, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/ken/session.rb', line 111

def topic(id, options = {})
  options.merge!({:id => id})
  
  response = http_request topic_service_url+"/standard", options
  result = JSON.parse response
  inner = result[id]
  handle_read_error(inner)
  Ken.logger.info "<<< Received Topic Response: #{inner['result'].inspect}"
  inner['result']
end