Class: FreebaseAPI::Session

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/freebase_api/session.rb

Constant Summary collapse

API_URL =

URIs of the Freebase API

"https://www.googleapis.com/freebase/v1"
SANDBOX_API_URL =
"https://www.googleapis.com/freebase/v1sandbox"
DEFAULT_LIMIT =

Freebase default values

10
DEFAULT_LANGUAGE =
:en

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



19
20
21
22
23
24
25
# File 'lib/freebase_api/session.rb', line 19

def initialize(options={})
  options = { :env => :stable, :query_options => { :limit => DEFAULT_LIMIT, :lang => DEFAULT_LANGUAGE } }.deep_merge(options)
  @env = options[:env]
  @key = options[:key] || ENV['GOOGLE_API_KEY']
  @query_options = options[:query_options]
  @proxy_options = get_proxy_options(options[:proxy])
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/freebase_api/session.rb', line 9

def env
  @env
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/freebase_api/session.rb', line 9

def key
  @key
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



9
10
11
# File 'lib/freebase_api/session.rb', line 9

def query_options
  @query_options
end

Instance Method Details

#image(id, options = {}) ⇒ Hash

Execute a Image Service query

Parameters:

  • id (String)

    the topic ID

  • options (Hash) (defaults to: {})

    the options

Returns:

  • (Hash)

    the response

See Also:



68
69
70
71
# File 'lib/freebase_api/session.rb', line 68

def image(id, options={})
  params = options
  get(surl('image') + id, params).body
end

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

Execute a MQL read query

Parameters:

  • query (Hash)

    the MQL query

  • options (Hash) (defaults to: {})

    the MQL query options

Returns:

  • (Hash)

    the response

See Also:



33
34
35
36
37
# File 'lib/freebase_api/session.rb', line 33

def mqlread(query, options={})
  params = { :query => query.to_json, :lang => "/lang/#{@query_options[:lang]}", :limit => @query_options[:limit] }.merge(options)
  response = get(surl('mqlread'), params, format: :json)
  response['result']
end

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

Execute a Search query

Parameters:

  • query (String)

    the query to search

  • options (Hash) (defaults to: {})

    the options

Returns:

  • (Hash)

    the response

See Also:



45
46
47
48
49
# File 'lib/freebase_api/session.rb', line 45

def search(query, options={})
  params = { :query => query, :lang => @query_options[:lang], :limit => @query_options[:limit] }.merge(options)
  response = get(surl('search'), params, format: :json)
  response['result']
end

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

Execute a Topic query

Parameters:

  • id (String)

    the topic ID

  • options (Hash) (defaults to: {})

    the options

Returns:

  • (Hash)

    the response

See Also:



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

def topic(id, options={})
  params = { :lang => @query_options[:lang], :limit => @query_options[:limit] }.merge(options)
  get(surl('topic') + id, params, format: :json).parsed_response
end