Module: Basuco

Defined in:
lib/basuco/view.rb,
lib/basuco.rb,
lib/basuco/api.rb,
lib/basuco/type.rb,
lib/basuco/util.rb,
lib/basuco/topic.rb,
lib/basuco/trans.rb,
lib/basuco/search.rb,
lib/basuco/version.rb,
lib/basuco/property.rb,
lib/basuco/resource.rb,
lib/basuco/attribute.rb,
lib/basuco/collection.rb

Overview

provides an interface to view a subject (resource or topic) as a specific type provides an interface for working with attributes, properties

Defined Under Namespace

Modules: Util Classes: Api, Attribute, AttributeNotFound, Collection, Property, PropertyNotFound, ReadError, Resource, ResourceNotFound, Search, Topic, TopicNotFound, Trans, Type, View, ViewNotFound

Constant Summary collapse

SERVICES =
{
  :mqlread => '/api/service/mqlread',
  :mqlwrite => '/api/service/mqlwrite',
  :blurb => '/api/trans/blurb/guid/',
  :raw => '/api/trans/raw/guid/',
  :login => '/api/account/login', #not done
  :logout => '/api/account/logout', #not done
  :upload => '/api/service/upload',
  :topic => '/experimental/topic',
  :search => '/api/service/search',
  :status => '/api/status', #not done
  :thumb => 'api/trans/image_thumb'

}
VERSION =
"0.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.apiObject

Returns the value of attribute api.



3
4
5
# File 'lib/basuco/api.rb', line 3

def api
  @api
end

.searchObject

Returns the value of attribute search.



3
4
5
# File 'lib/basuco/search.rb', line 3

def search
  @search
end

.transObject

Returns the value of attribute trans.



3
4
5
# File 'lib/basuco/trans.rb', line 3

def trans
  @trans
end

Class Method Details

.check_statusesObject

hash of all statuses



72
73
74
75
76
# File 'lib/basuco.rb', line 72

def self.check_statuses
  response = http_request status_service_url
  result = JSON.parse response
  result
end

Instance Method Details

#get_query_response(query, cursor = nil) ⇒ Object

returns parsed json response from freebase mqlread service



42
43
44
45
46
47
48
49
50
51
# File 'lib/basuco.rb', line 42

def get_query_response(query, cursor=nil)
  envelope = { :qname => {:query => query, :escape => false }}
  envelope[:qname][:cursor] = cursor if cursor
  
  response = http_request mqlread_service_url, :queries => envelope.to_json
  result = JSON.parse response
  inner = result['qname']
  handle_read_error(inner)
  inner
end

#handle_read_error(inner) ⇒ Object

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



34
35
36
37
38
39
# File 'lib/basuco.rb', line 34

def handle_read_error(inner)
  unless inner['code'][0, '/api/status/ok'.length] == '/api/status/ok'
    error = inner['messages'][0]
    raise ReadError.new(error['code'], error['message'])
  end
end

#http_request(url, parameters = {}) ⇒ Object

does the dirty work



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/basuco.rb', line 59

def http_request(url, parameters = {})
  params = params_to_string(parameters)
  url << '?'+params unless params !~ /\S/
        
  return Net::HTTP.get_response(::URI.parse(url)).body
  
  fname = "#{MD5.md5(params)}.mql"
  open(fname,"w") do |f|
    f << response
  end
end

#params_to_string(parameters) ⇒ Object

encode parameters



54
55
56
# File 'lib/basuco.rb', line 54

def params_to_string(parameters)
  parameters.keys.map {|k| "#{URI.encode(k.to_s)}=#{URI.encode(parameters[k].to_s)}" }.join('&')
end

#service_url(svc) ⇒ Object



23
24
25
# File 'lib/basuco.rb', line 23

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