basuco 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/basuco.rb +0 -82
- data/lib/basuco/api.rb +1 -0
- data/lib/basuco/request.rb +81 -0
- data/lib/basuco/search.rb +2 -1
- data/lib/basuco/trans.rb +1 -0
- data/lib/basuco/version.rb +1 -1
- metadata +3 -3
data/lib/basuco.rb
CHANGED
@@ -5,69 +5,6 @@ Dir[File.dirname(__FILE__) + '/basuco/*.rb'].each {|file| require file }
|
|
5
5
|
|
6
6
|
module Basuco
|
7
7
|
|
8
|
-
SERVICES = {
|
9
|
-
:mqlread => '/api/service/mqlread',
|
10
|
-
:mqlwrite => '/api/service/mqlwrite',
|
11
|
-
:blurb => '/api/trans/blurb/guid/',
|
12
|
-
:raw => '/api/trans/raw/guid/',
|
13
|
-
:login => '/api/account/login', #not done
|
14
|
-
:logout => '/api/account/logout', #not done
|
15
|
-
:upload => '/api/service/upload',
|
16
|
-
:topic => '/experimental/topic',
|
17
|
-
:search => '/api/service/search',
|
18
|
-
:status => '/api/status', #not done
|
19
|
-
:thumb => 'api/trans/image_thumb'
|
20
|
-
|
21
|
-
}
|
22
|
-
|
23
|
-
def service_url(svc)
|
24
|
-
"#{@host}#{SERVICES[svc]}"
|
25
|
-
end
|
26
|
-
|
27
|
-
SERVICES.each_key do |k|
|
28
|
-
define_method("#{k}_service_url") do
|
29
|
-
service_url(k)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# raise an error if the inner response envelope is encoded as an error
|
34
|
-
def handle_read_error(inner)
|
35
|
-
unless inner['code'][0, '/api/status/ok'.length] == '/api/status/ok'
|
36
|
-
error = inner['messages'][0]
|
37
|
-
raise ReadError.new(error['code'], error['message'])
|
38
|
-
end
|
39
|
-
end # handle_read_error
|
40
|
-
|
41
|
-
# returns parsed json response from freebase mqlread service
|
42
|
-
def get_query_response(query, cursor=nil)
|
43
|
-
envelope = { :qname => {:query => query, :escape => false }}
|
44
|
-
envelope[:qname][:cursor] = cursor if cursor
|
45
|
-
|
46
|
-
response = http_request mqlread_service_url, :queries => envelope.to_json
|
47
|
-
result = JSON.parse response
|
48
|
-
inner = result['qname']
|
49
|
-
handle_read_error(inner)
|
50
|
-
inner
|
51
|
-
end
|
52
|
-
|
53
|
-
# encode parameters
|
54
|
-
def params_to_string(parameters)
|
55
|
-
parameters.keys.map {|k| "#{URI.encode(k.to_s)}=#{URI.encode(parameters[k].to_s)}" }.join('&')
|
56
|
-
end
|
57
|
-
|
58
|
-
# does the dirty work
|
59
|
-
def http_request(url, parameters = {})
|
60
|
-
params = params_to_string(parameters)
|
61
|
-
url << '?'+params unless params !~ /\S/
|
62
|
-
|
63
|
-
return Net::HTTP.get_response(::URI.parse(url)).body
|
64
|
-
|
65
|
-
fname = "#{MD5.md5(params)}.mql"
|
66
|
-
open(fname,"w") do |f|
|
67
|
-
f << response
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
8
|
#hash of all statuses
|
72
9
|
def self.check_statuses
|
73
10
|
response = http_request status_service_url
|
@@ -75,25 +12,6 @@ module Basuco
|
|
75
12
|
result
|
76
13
|
end
|
77
14
|
|
78
|
-
# A class for returing errors from the freebase api.
|
79
|
-
# For more infomation see the freebase documentation:
|
80
|
-
class ReadError < ArgumentError
|
81
|
-
attr_accessor :code, :msg
|
82
|
-
def initialize(code,msg)
|
83
|
-
self.code = code
|
84
|
-
self.msg = msg
|
85
|
-
end
|
86
|
-
def message
|
87
|
-
"#{code}: #{msg}"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class AttributeNotFound < StandardError ; end
|
92
|
-
class PropertyNotFound < StandardError ; end
|
93
|
-
class ResourceNotFound < StandardError ; end
|
94
|
-
class TopicNotFound < StandardError ; end
|
95
|
-
class ViewNotFound < StandardError ; end
|
96
|
-
|
97
15
|
end # module Basuco
|
98
16
|
|
99
17
|
|
data/lib/basuco/api.rb
CHANGED
data/lib/basuco/request.rb
CHANGED
@@ -1,4 +1,85 @@
|
|
1
1
|
module Request
|
2
2
|
|
3
|
+
SERVICES = {
|
4
|
+
:mqlread => '/api/service/mqlread',
|
5
|
+
:mqlwrite => '/api/service/mqlwrite',
|
6
|
+
:blurb => '/api/trans/blurb/guid/',
|
7
|
+
:raw => '/api/trans/raw/guid/',
|
8
|
+
:login => '/api/account/login', #not done
|
9
|
+
:logout => '/api/account/logout', #not done
|
10
|
+
:upload => '/api/service/upload',
|
11
|
+
:topic => '/experimental/topic',
|
12
|
+
:search => '/api/service/search',
|
13
|
+
:status => '/api/status', #not done
|
14
|
+
:thumb => 'api/trans/image_thumb'
|
3
15
|
|
16
|
+
}
|
17
|
+
|
18
|
+
def service_url(svc)
|
19
|
+
"#{@host}#{SERVICES[svc]}"
|
20
|
+
end
|
21
|
+
|
22
|
+
SERVICES.each_key do |k|
|
23
|
+
define_method("#{k}_service_url") do
|
24
|
+
service_url(k)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# A class for returing errors from the freebase api.
|
30
|
+
# For more infomation see the freebase documentation:
|
31
|
+
class ReadError < ArgumentError
|
32
|
+
attr_accessor :code, :msg
|
33
|
+
def initialize(code,msg)
|
34
|
+
self.code = code
|
35
|
+
self.msg = msg
|
36
|
+
end
|
37
|
+
def message
|
38
|
+
"#{code}: #{msg}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class AttributeNotFound < StandardError ; end
|
43
|
+
class PropertyNotFound < StandardError ; end
|
44
|
+
class ResourceNotFound < StandardError ; end
|
45
|
+
class TopicNotFound < StandardError ; end
|
46
|
+
class ViewNotFound < StandardError ; end
|
47
|
+
|
48
|
+
# raise an error if the inner response envelope is encoded as an error
|
49
|
+
def handle_read_error(inner)
|
50
|
+
unless inner['code'][0, '/api/status/ok'.length] == '/api/status/ok'
|
51
|
+
error = inner['messages'][0]
|
52
|
+
raise ReadError.new(error['code'], error['message'])
|
53
|
+
end
|
54
|
+
end # handle_read_error
|
55
|
+
|
56
|
+
# returns parsed json response from freebase mqlread service
|
57
|
+
def get_query_response(query, cursor=nil)
|
58
|
+
envelope = { :qname => {:query => query, :escape => false }}
|
59
|
+
envelope[:qname][:cursor] = cursor if cursor
|
60
|
+
|
61
|
+
response = http_request mqlread_service_url, :queries => envelope.to_json
|
62
|
+
result = JSON.parse response
|
63
|
+
inner = result['qname']
|
64
|
+
handle_read_error(inner)
|
65
|
+
inner
|
66
|
+
end
|
67
|
+
|
68
|
+
# encode parameters
|
69
|
+
def params_to_string(parameters)
|
70
|
+
parameters.keys.map {|k| "#{URI.encode(k.to_s)}=#{URI.encode(parameters[k].to_s)}" }.join('&')
|
71
|
+
end
|
72
|
+
|
73
|
+
# does the dirty work
|
74
|
+
def http_request(url, parameters = {})
|
75
|
+
params = params_to_string(parameters)
|
76
|
+
url << '?'+params unless params !~ /\S/
|
77
|
+
|
78
|
+
return Net::HTTP.get_response(::URI.parse(url)).body
|
79
|
+
|
80
|
+
fname = "#{MD5.md5(params)}.mql"
|
81
|
+
open(fname,"w") do |f|
|
82
|
+
f << response
|
83
|
+
end
|
84
|
+
end
|
4
85
|
end #Request
|
data/lib/basuco/search.rb
CHANGED
@@ -6,7 +6,8 @@ module Basuco
|
|
6
6
|
# partially taken from chris eppstein's freebase api
|
7
7
|
# http://github.com/chriseppstein/freebase/tree
|
8
8
|
class Search
|
9
|
-
|
9
|
+
include Request
|
10
|
+
|
10
11
|
def initialize(options = {:host => 'http://www.freebase.com', :username => 'un', :password => 'pw'})
|
11
12
|
@host = options[:host]
|
12
13
|
@username = options[:username]
|
data/lib/basuco/trans.rb
CHANGED
@@ -6,6 +6,7 @@ module Basuco
|
|
6
6
|
# partially taken from chris eppstein's freebase api
|
7
7
|
# http://github.com/chriseppstein/freebase/tree
|
8
8
|
class Trans
|
9
|
+
include Request
|
9
10
|
|
10
11
|
def initialize(options = {:host => 'http://www.freebase.com', :username => 'un', :password => 'pw'})
|
11
12
|
@host = options[:host]
|
data/lib/basuco/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basuco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-25 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &73363010 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *73363010
|
25
25
|
description: Transfer information from freebase.com through ruby.
|
26
26
|
email:
|
27
27
|
- ''
|