freebase-api 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +204 -0
- data/Rakefile +8 -0
- data/freebase-api.gemspec +19 -0
- data/lib/freebase-api.rb +32 -0
- data/lib/freebase_api/attribute.rb +29 -0
- data/lib/freebase_api/exceptions.rb +25 -0
- data/lib/freebase_api/ext/hash.rb +15 -0
- data/lib/freebase_api/image.rb +44 -0
- data/lib/freebase_api/session.rb +114 -0
- data/lib/freebase_api/topic.rb +186 -0
- data/lib/freebase_api/version.rb +3 -0
- data/spec/attribute_spec.rb +42 -0
- data/spec/fixtures/img.jpg +0 -0
- data/spec/fixtures/search.json +214 -0
- data/spec/fixtures/topic.json +477 -0
- data/spec/freebase_api_spec.rb +13 -0
- data/spec/image_spec.rb +57 -0
- data/spec/session_spec.rb +165 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/helpers.rb +12 -0
- data/spec/topic_spec.rb +166 -0
- data/travis.yml +8 -0
- metadata +95 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FreebaseAPI do
|
4
|
+
|
5
|
+
it "should have a default logger" do
|
6
|
+
FreebaseAPI.logger.should be_kind_of(Logger)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have a default session" do
|
10
|
+
FreebaseAPI.session.should be_kind_of(FreebaseAPI::Session)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/spec/image_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe FreebaseAPI::Image do
|
5
|
+
|
6
|
+
let(:options) { { :maxheight => 20, :maxwidth => 20 } }
|
7
|
+
let(:image) { FreebaseAPI::Image.get('/en/bob_dylan', options) }
|
8
|
+
|
9
|
+
let(:data) {
|
10
|
+
load_data 'img.jpg'
|
11
|
+
}
|
12
|
+
|
13
|
+
before {
|
14
|
+
stubbed_session = mock('session')
|
15
|
+
FreebaseAPI.stub(:session).and_return(stubbed_session)
|
16
|
+
stubbed_session.stub(:image).and_return(data)
|
17
|
+
}
|
18
|
+
|
19
|
+
describe ".get" do
|
20
|
+
let(:stubbed_session) { mock('session').as_null_object }
|
21
|
+
|
22
|
+
before {
|
23
|
+
FreebaseAPI.stub(:session).and_return(stubbed_session)
|
24
|
+
}
|
25
|
+
|
26
|
+
it "should make a Image API call" do
|
27
|
+
stubbed_session.should_receive(:image).with('/en/bob_dylan', :maxheight => 20, :maxwidth => 20).and_return(data)
|
28
|
+
image
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return an image" do
|
32
|
+
image.should be_kind_of FreebaseAPI::Image
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#id" do
|
37
|
+
it "should return the topic related ID" do
|
38
|
+
image.id.should == '/en/bob_dylan'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#size" do
|
43
|
+
it "should return the topic related ID" do
|
44
|
+
image.size.should == data.size
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#store" do
|
49
|
+
it "should store the image" do
|
50
|
+
Dir::Tmpname.create 'freebase-api' do |file|
|
51
|
+
image.store(file)
|
52
|
+
File.size(file).should == data.size
|
53
|
+
File.unlink(file)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FreebaseAPI::Session do
|
4
|
+
|
5
|
+
let(:session) { FreebaseAPI::Session.new }
|
6
|
+
let(:dummy_session) { FreebaseAPI::Session.new(:key => "GOOGLE_API_KEY", :env => :sandbox, :query_options => { :lang => :fr, :limit => 1 }) }
|
7
|
+
|
8
|
+
it "should include HTTParty" do
|
9
|
+
FreebaseAPI::Session.should include(HTTParty)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#key" do
|
13
|
+
context "when key has been set manually" do
|
14
|
+
it "should return the API key" do
|
15
|
+
dummy_session.key.should == 'GOOGLE_API_KEY'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with the env variables defined" do
|
20
|
+
before { ENV['GOOGLE_API_KEY'] = 'ENV_GOOGLE_API_KEY' }
|
21
|
+
it "should return the API key" do
|
22
|
+
session.key.should == 'ENV_GOOGLE_API_KEY'
|
23
|
+
end
|
24
|
+
after { ENV['GOOGLE_API_KEY'] = nil }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#env" do
|
29
|
+
it "should return the environnment" do
|
30
|
+
dummy_session.env.should == :sandbox
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#query_options" do
|
35
|
+
it "should return the environnment" do
|
36
|
+
dummy_session.query_options.should == { :lang => :fr, :limit => 1 }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#surl" do
|
41
|
+
context "in a sandbox environnment" do
|
42
|
+
it "should return the sandbox service URL" do
|
43
|
+
dummy_session.send(:surl, 'service').should == 'https://www.googleapis.com/freebase/v1sandbox/service'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "in a stable environnment" do
|
48
|
+
it "should return the sandbox service URL" do
|
49
|
+
session.send(:surl, 'service').should == 'https://www.googleapis.com/freebase/v1/service'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#mqlread" do
|
55
|
+
let(:query) {
|
56
|
+
{
|
57
|
+
:type => '/internet/website',
|
58
|
+
:id => '/en/github',
|
59
|
+
:'/common/topic/official_website' => nil
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
let(:request) {
|
64
|
+
session.mqlread(query)
|
65
|
+
}
|
66
|
+
|
67
|
+
context "when the query is successful" do
|
68
|
+
it "should run a MQL query and return the result" do
|
69
|
+
request.should == {
|
70
|
+
"/common/topic/official_website" => "http://github.com/",
|
71
|
+
"id" => "/en/github",
|
72
|
+
"type" => "/internet/website"
|
73
|
+
}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when the query has failed" do
|
78
|
+
let(:session) { FreebaseAPI::Session.new :key => 'nil' }
|
79
|
+
|
80
|
+
it "should raise en error" do
|
81
|
+
expect {
|
82
|
+
request
|
83
|
+
}.to raise_error(FreebaseAPI::Error)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#image" do
|
89
|
+
let(:request) {
|
90
|
+
session.image('/en/bob_dylan')
|
91
|
+
}
|
92
|
+
|
93
|
+
context "when the query is successful" do
|
94
|
+
it "should not raise any error" do
|
95
|
+
expect {
|
96
|
+
request
|
97
|
+
}.to_not raise_error(FreebaseAPI::Error)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return some data" do
|
101
|
+
request.size.should > 1000
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "when the query has failed" do
|
106
|
+
let(:session) { FreebaseAPI::Session.new :key => 'nil' }
|
107
|
+
|
108
|
+
it "should raise en error" do
|
109
|
+
expect {
|
110
|
+
request
|
111
|
+
}.to raise_error(FreebaseAPI::Error)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "#search" do
|
117
|
+
let(:request) {
|
118
|
+
session.search('bob dylan')
|
119
|
+
}
|
120
|
+
|
121
|
+
context "when the query is successful" do
|
122
|
+
it "should not raise any error" do
|
123
|
+
expect {
|
124
|
+
request
|
125
|
+
}.to_not raise_error(FreebaseAPI::Error)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should return some topics" do
|
129
|
+
request.should_not be_empty
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "when the query has failed" do
|
134
|
+
let(:session) { FreebaseAPI::Session.new :key => 'nil' }
|
135
|
+
|
136
|
+
it "should raise en error" do
|
137
|
+
expect {
|
138
|
+
request
|
139
|
+
}.to raise_error(FreebaseAPI::Error)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "#topic" do
|
145
|
+
let(:request) {
|
146
|
+
session.topic('/en/github', :filter => '/common/topic/official_website')
|
147
|
+
}
|
148
|
+
|
149
|
+
context "when the query is successful" do
|
150
|
+
it "should run a topic query and return the result" do
|
151
|
+
request['property'].should have(1).elements
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "when the query has failed" do
|
156
|
+
let(:session) { FreebaseAPI::Session.new :key => 'nil' }
|
157
|
+
|
158
|
+
it "should raise en error" do
|
159
|
+
expect {
|
160
|
+
request
|
161
|
+
}.to raise_error(FreebaseAPI::Error)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
|
+
]
|
8
|
+
SimpleCov.start
|
9
|
+
|
10
|
+
require 'rspec'
|
11
|
+
require 'freebase-api'
|
12
|
+
require 'support/helpers'
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.include FreebaseAPI::Helpers
|
16
|
+
end
|
17
|
+
|
18
|
+
FreebaseAPI.logger.level = Logger::FATAL
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FreebaseAPI
|
2
|
+
# Helpers for Rspec
|
3
|
+
module Helpers
|
4
|
+
def load_fixture(fixture)
|
5
|
+
JSON.parse(IO.read(File.join(File.dirname(__FILE__), "..", "fixtures", "#{fixture}.json")))
|
6
|
+
end
|
7
|
+
|
8
|
+
def load_data(file)
|
9
|
+
IO.read(File.join(File.dirname(__FILE__), "..", "fixtures", file))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/topic_spec.rb
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FreebaseAPI::Topic do
|
4
|
+
|
5
|
+
let(:options) { { :lang => :en } }
|
6
|
+
let(:topic) { FreebaseAPI::Topic.get('/en/github', options) }
|
7
|
+
let(:new_topic) { FreebaseAPI::Topic.new('/en/github') }
|
8
|
+
|
9
|
+
let(:data) { load_fixture 'topic' }
|
10
|
+
|
11
|
+
before {
|
12
|
+
stubbed_session = mock('session')
|
13
|
+
FreebaseAPI.stub(:session).and_return(stubbed_session)
|
14
|
+
stubbed_session.stub(:topic).and_return(data)
|
15
|
+
stubbed_session.stub(:image).and_return(nil)
|
16
|
+
}
|
17
|
+
|
18
|
+
describe ".get" do
|
19
|
+
let(:stubbed_session) { mock('session').as_null_object }
|
20
|
+
|
21
|
+
before {
|
22
|
+
FreebaseAPI::Topic.any_instance.stub(:build)
|
23
|
+
FreebaseAPI.stub(:session).and_return(stubbed_session)
|
24
|
+
}
|
25
|
+
|
26
|
+
it "should make a Topic API call" do
|
27
|
+
stubbed_session.should_receive(:topic).with('/en/github', :lang => :en, :filter => 'commons').and_return(data)
|
28
|
+
topic
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return a topic" do
|
32
|
+
topic.should be_kind_of FreebaseAPI::Topic
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe ".search" do
|
37
|
+
let(:stubbed_session) { mock('session') }
|
38
|
+
let(:topic_search) { FreebaseAPI::Topic.search('dylan') }
|
39
|
+
let(:data) { load_fixture 'search' }
|
40
|
+
let(:item) { topic_search.values.first }
|
41
|
+
|
42
|
+
before {
|
43
|
+
FreebaseAPI.stub(:session).and_return(stubbed_session)
|
44
|
+
stubbed_session.stub(:search).and_return(data)
|
45
|
+
}
|
46
|
+
|
47
|
+
it "should make a Search API call" do
|
48
|
+
stubbed_session.should_receive(:search).with('dylan', {}).and_return(data)
|
49
|
+
topic_search
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return an hash" do
|
53
|
+
topic_search.should be_kind_of Hash
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return ordered scores" do
|
57
|
+
topic_search.keys.first.should == 72.587578
|
58
|
+
topic_search.keys.last.should == 20.738529
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return topics" do
|
62
|
+
item.should be_kind_of(FreebaseAPI::Topic)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should store the id" do
|
66
|
+
item.id.should == '/m/01vrncs'
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should store some properties" do
|
70
|
+
item.properties.keys.should == ["/type/object/name", "/common/topic/notable_for"]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#id" do
|
75
|
+
context "when the topic has been sync yet" do
|
76
|
+
it "should return the topic ID" do
|
77
|
+
new_topic.id.should == '/en/github'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when the topic has been sync" do
|
82
|
+
it "should return the topic ID" do
|
83
|
+
topic.id.should == '/m/04g0kcw'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#text" do
|
89
|
+
it "should return the topic text" do
|
90
|
+
topic.text.should == "GitHub"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "#lang" do
|
95
|
+
it "should return the topic language" do
|
96
|
+
topic.lang.should == 'en'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "#name" do
|
101
|
+
it "should return the topic name" do
|
102
|
+
topic.name.should == 'GitHub'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#types" do
|
107
|
+
it "should return all the topic types" do
|
108
|
+
topic.types.should == ["/common/topic", "/internet/website", "/base/technologyofdoing/proposal_agent"]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#description" do
|
113
|
+
it "should return the topic description" do
|
114
|
+
topic.description.should start_with "GitHub is a web-based hosting service for software development projects that use the Git revision control system."
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#properties" do
|
119
|
+
it "should return the properties values which are topics" do
|
120
|
+
link = topic.properties['/internet/website/category'].first
|
121
|
+
link.should be_kind_of(FreebaseAPI::Topic)
|
122
|
+
link.name.should == 'Revision control'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return the properties values which are data" do
|
126
|
+
link = topic.properties['/common/topic/official_website'].first
|
127
|
+
link.should be_kind_of(FreebaseAPI::Attribute)
|
128
|
+
link.value.should == 'http://github.com/'
|
129
|
+
end
|
130
|
+
|
131
|
+
context "with a property exclusion constraint" do
|
132
|
+
let(:topic) { FreebaseAPI::Topic.get('/en/github', :exclude => '/internet') }
|
133
|
+
|
134
|
+
it "should not return these properties" do
|
135
|
+
topic.properties.should_not have_key('/internet/website/category')
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should return the properties that do not match this exclusion" do
|
139
|
+
topic.properties.should have_key('/common/topic/official_website')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "#property" do
|
145
|
+
it "should return the asked property" do
|
146
|
+
property = topic.property('/type/object/type')
|
147
|
+
property.should have(3).elements
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "#properties domains" do
|
152
|
+
it "should return an hash containing the properties domains" do
|
153
|
+
topic.properties_domains.should == { 'common' => 16, 'internet' => 1,'type' => 30 }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "#image" do
|
158
|
+
it "should return an Image" do
|
159
|
+
topic.image.should be_kind_of(FreebaseAPI::Image)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should be the topic image" do
|
163
|
+
topic.image.id.should == topic.id
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
data/travis.yml
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: freebase-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aymeric Brisse
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &1 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.10'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *1
|
25
|
+
description: A library to use the Freebase API
|
26
|
+
email:
|
27
|
+
- aymeric.brisse@mperfect-memory.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- Gemfile
|
35
|
+
- Gemfile.lock
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.markdown
|
38
|
+
- Rakefile
|
39
|
+
- freebase-api.gemspec
|
40
|
+
- lib/freebase-api.rb
|
41
|
+
- lib/freebase_api/attribute.rb
|
42
|
+
- lib/freebase_api/exceptions.rb
|
43
|
+
- lib/freebase_api/ext/hash.rb
|
44
|
+
- lib/freebase_api/image.rb
|
45
|
+
- lib/freebase_api/session.rb
|
46
|
+
- lib/freebase_api/topic.rb
|
47
|
+
- lib/freebase_api/version.rb
|
48
|
+
- spec/attribute_spec.rb
|
49
|
+
- spec/fixtures/img.jpg
|
50
|
+
- spec/fixtures/search.json
|
51
|
+
- spec/fixtures/topic.json
|
52
|
+
- spec/freebase_api_spec.rb
|
53
|
+
- spec/image_spec.rb
|
54
|
+
- spec/session_spec.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- spec/support/helpers.rb
|
57
|
+
- spec/topic_spec.rb
|
58
|
+
- travis.yml
|
59
|
+
homepage: https://github.com/PerfectMemory/freebase-api
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.10
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Provides access to both a raw-access and an abstract-layer to the Freebase
|
83
|
+
API
|
84
|
+
test_files:
|
85
|
+
- spec/attribute_spec.rb
|
86
|
+
- spec/fixtures/img.jpg
|
87
|
+
- spec/fixtures/search.json
|
88
|
+
- spec/fixtures/topic.json
|
89
|
+
- spec/freebase_api_spec.rb
|
90
|
+
- spec/image_spec.rb
|
91
|
+
- spec/session_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/support/helpers.rb
|
94
|
+
- spec/topic_spec.rb
|
95
|
+
has_rdoc:
|