dm-freebase-adapter 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Yves Senn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ = dm-freebase-adapter
2
+
3
+ This adapter allows you to map any freebase resource to DataMapper models.
4
+
5
+ == Installation
6
+
7
+ To install the freebase adapter execute:
8
+
9
+ `gem install dm-freebase-adapter`
10
+
11
+ == Usage
12
+
13
+ Copyright (c) 2009 Yves Senn. See LICENSE for details.
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dm-freebase-adapter"
8
+ gem.summary = %Q{DataMapper adapter to access data stored in Freebase}
9
+ gem.description = %Q{This Adapter allows you to access any Freebase data with your DataMapper models}
10
+ gem.email = "yves.senn@gmail.com"
11
+ gem.homepage = "http://github.com/senny/dm-freebase-adapter"
12
+ gem.authors = ["Yves Senn"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem.add_development_dependency "cucumber", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ begin
37
+ require 'cucumber/rake/task'
38
+ Cucumber::Rake::Task.new(:features)
39
+
40
+ task :features => :check_dependencies
41
+ rescue LoadError
42
+ task :features do
43
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
44
+ end
45
+ end
46
+
47
+ task :default => :spec
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "dm-freebase-adapter #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'dm-freebase-adapter'
3
+
4
+ require 'spec/expectations'
@@ -0,0 +1,115 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module DataMapper
5
+ module Adapters
6
+ class FreebaseAdapter < AbstractAdapter
7
+
8
+ FREEBASE_HOST = "www.freebase.com"
9
+ FREEBASE_PATH = "/api/service/mqlread"
10
+
11
+ def query_url
12
+ host = (options[:host] && !options[:host].empty?) ? options[:host] : FREEBASE_HOST
13
+ path = (options[:path] && !options[:path].empty?) ? options[:path] : FREEBASE_PATH
14
+ "http://#{ host }#{ path }"
15
+ end
16
+
17
+ def read(query)
18
+ metaweb_query = {}
19
+ fields = query.fields
20
+ query.conditions.each do |condition|
21
+ metaweb_query[build_metaweb_condition_key(condition)] = build_metaweb_condition(condition)
22
+ looking_for = fields.reject {|field| metaweb_query.include?(field.name)}
23
+ looking_for.each do |field|
24
+ metaweb_query[field.name] = [{}]
25
+ end
26
+ end
27
+ metaweb_query["type"] = query.model.storage_name
28
+ result = metaweb_read(metaweb_query)["result"]
29
+ convert_metaweb_result(result)
30
+ end
31
+
32
+ private
33
+
34
+ def build_metaweb_condition_key(condition)
35
+ case condition
36
+ when DataMapper::Query::Conditions::LikeComparison
37
+ "#{condition.subject.name}~="
38
+ when DataMapper::Query::Conditions::InclusionComparison
39
+ "#{condition.subject.name}|="
40
+ when DataMapper::Query::Conditions::LessThanComparison
41
+ "#{condition.subject.name}<"
42
+ when DataMapper::Query::Conditions::GreaterThanComparison
43
+ "#{condition.subject.name}>"
44
+ when DataMapper::Query::Conditions::LessThanOrEqualToComparison
45
+ "#{condition.subject.name}<="
46
+ when DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
47
+ "#{condition.subject.name}>="
48
+ when DataMapper::Query::Conditions::NotOperation
49
+ "#{condition.subject.name}!="
50
+ else
51
+ condition.subject.name
52
+ end
53
+ end
54
+
55
+ def build_metaweb_condition(condition)
56
+ case condition.subject
57
+ when DataMapper::Associations::ManyToOne::Relationship
58
+ {:id => condition.value.id}
59
+ else
60
+ condition.value
61
+ end
62
+ end
63
+
64
+ def convert_metaweb_result(results)
65
+ results.each do |element|
66
+ element.each do |key, value|
67
+ element[key] = extract_value(value)
68
+ end
69
+ end
70
+ end
71
+
72
+ def extract_value(property)
73
+ case property
74
+ when Array
75
+ values = property.collect {|element| extract_value(element)}
76
+ values.size == 1 ? values.first : values
77
+ when Hash
78
+ if property.has_key?("type")
79
+ extract_typed_value(property)
80
+ else
81
+ property
82
+ end
83
+ else
84
+ property
85
+ end
86
+ end
87
+
88
+ def extract_typed_value(property)
89
+ case property["type"]
90
+ when "/type/text", "/type/id"
91
+ property["value"]
92
+ else
93
+ property["name"]
94
+ end
95
+ end
96
+
97
+ def metaweb_read(query)
98
+ metaweb_query = "?query={\"query\": [#{JSON.dump(query)}]}"
99
+ url = "#{query_url}#{URI.escape(metaweb_query)}"
100
+ response = Net::HTTP.get_response(URI.parse(url))
101
+ data = response.body
102
+
103
+ result = JSON.parse(data)
104
+
105
+ if result['code'] == '/api/status/error'
106
+ raise "web service error #{url}"
107
+ end
108
+ return result
109
+ end
110
+
111
+
112
+ end # class FreebaseAdapter
113
+ end # module Adapters
114
+ end # module DataMapper
115
+
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Artist
4
+ include DataMapper::Resource
5
+
6
+ storage_names[:default] = '/music/artist'
7
+
8
+ property :id, String, :key => true
9
+ property :guid, String
10
+ property :name, String
11
+ property :genre, String
12
+
13
+ has n, :albums, :child_key => [:artist]
14
+ end
15
+
16
+ class Album
17
+ include DataMapper::Resource
18
+
19
+ storage_names[:default] = '/music/album'
20
+
21
+ property :id, String, :key => true
22
+ property :name, String
23
+ property :track, String
24
+
25
+ belongs_to :artist, :child_key => [:artist]
26
+ end
27
+
28
+ describe "FreebaseAdapter" do
29
+
30
+ it "should fetch the given properties" do
31
+ artist = Artist.get('/en/paul_kalkbrenner')
32
+ artist.name.should == "Paul Kalkbrenner"
33
+ artist.genre.should_not be_empty
34
+ end
35
+
36
+ it "should fetch associations on demand" do
37
+ artist = Artist.get('/en/apparat')
38
+ artist.albums.size.should > 0
39
+ titles = artist.albums.collect(&:name)
40
+ ["Silizium EP", "Walls"].each do |title|
41
+ titles.should include(title)
42
+ end
43
+ end
44
+
45
+ it "should work with inclusion (in) queries" do
46
+ results = Album.all(:name => ["Berlin Calling", "Balance 005: James Holden"])
47
+ results.size.should == 2
48
+ results[0].name.should == "Balance 005: James Holden"
49
+ results[1].name.should == "Berlin Calling"
50
+ end
51
+
52
+ it "should work with like (regexp match) queries" do
53
+ results = Album.all(:name.like => "Balance 00*")
54
+ results.size.should > 0
55
+ titles = results.collect(&:name)
56
+ titles.each {|title| title.should =~ /^Balance 00.*$/}
57
+ end
58
+
59
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require "rubygems"
4
+ require "dm-core"
5
+ require 'freebase_adapter'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+
9
+ DataMapper.setup(:default, :adapter => 'freebase')
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-freebase-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yves Senn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-07 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: This Adapter allows you to access any Freebase data with your DataMapper models
26
+ email: yves.senn@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - features/dm-freebase-adapter.feature
42
+ - features/step_definitions/dm-freebase-adapter_steps.rb
43
+ - features/support/env.rb
44
+ - lib/freebase_adapter.rb
45
+ - spec/freebase_adapter_spec.rb
46
+ - spec/spec.opts
47
+ - spec/spec_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/senny/dm-freebase-adapter
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: DataMapper adapter to access data stored in Freebase
76
+ test_files:
77
+ - spec/freebase_adapter_spec.rb
78
+ - spec/spec_helper.rb