michael-ken 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{ken}
5
- s.version = "0.1.1"
5
+ s.version = "0.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["michael"]
9
- s.date = %q{2009-07-24}
9
+ s.date = %q{2009-08-23}
10
10
  s.email = %q{ma[at]zive[dot]at}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/lib/ken.rb CHANGED
@@ -2,7 +2,6 @@ require 'pathname'
2
2
  require 'rubygems'
3
3
  require 'net/http'
4
4
  require 'json'
5
- require 'extlib'
6
5
  require 'extlib/assertions'
7
6
  require 'addressable/uri'
8
7
 
@@ -41,6 +40,7 @@ module Ken
41
40
 
42
41
  FETCH_DATA_QUERY = {
43
42
  # :id => id, # needs to be merge!d in instance method
43
+ :guid => nil,
44
44
  :name => nil,
45
45
  :"ken:type" => [{
46
46
  :id => nil,
@@ -59,7 +59,8 @@ module Ken
59
59
  :id => nil,
60
60
  :link => nil,
61
61
  :name => nil,
62
- :optional => true
62
+ :optional => true,
63
+ :limit => 999999
63
64
  }
64
65
  ],
65
66
  :"/type/reflect/any_reverse" => [
@@ -67,14 +68,16 @@ module Ken
67
68
  :id => nil,
68
69
  :link => nil,
69
70
  :name => nil,
70
- :optional => true
71
+ :optional => true,
72
+ :limit => 999999
71
73
  }
72
74
  ],
73
75
  :"/type/reflect/any_value" => [
74
76
  {
75
77
  :link => nil,
76
78
  :value => nil,
77
- :optional => true
79
+ :optional => true,
80
+ :limit => 999999
78
81
  # TODO: support multiple language
79
82
  # :lang => "/lang/en",
80
83
  # :type => "/type/text"
@@ -82,7 +85,6 @@ module Ken
82
85
  ]
83
86
  }
84
87
 
85
-
86
88
  # Executes an Mql Query against the Freebase API and returns the result as
87
89
  # a <tt>Collection</tt> of <tt>Resources</tt>.
88
90
  #
@@ -2,48 +2,7 @@ module Ken
2
2
  class Resource
3
3
  include Extlib::Assertions
4
4
 
5
- FETCH_DATA_QUERY = {
6
- # :id => id, # needs to be merge!d in instance method
7
- :name => nil,
8
- :"ken:type" => [{
9
- :id => nil,
10
- :name => nil,
11
- :properties => [{
12
- :id => nil,
13
- :name => nil,
14
- :expected_type => nil,
15
- :unique => nil,
16
- :reverse_property => nil,
17
- :master_property => nil,
18
- }]
19
- }],
20
- :"/type/reflect/any_master" => [
21
- {
22
- :id => nil,
23
- :link => nil,
24
- :name => nil,
25
- :optional => true
26
- }
27
- ],
28
- :"/type/reflect/any_reverse" => [
29
- {
30
- :id => nil,
31
- :link => nil,
32
- :name => nil,
33
- :optional => true
34
- }
35
- ],
36
- :"/type/reflect/any_value" => [
37
- {
38
- :link => nil,
39
- :value => nil,
40
- :optional => true
41
- # TODO: support multiple language
42
- # :lang => "/lang/en",
43
- # :type => "/type/text"
44
- }
45
- ]
46
- }
5
+ attr_reader :data
47
6
 
48
7
  # initializes a resource using a json result
49
8
  def initialize(data)
@@ -58,6 +17,12 @@ module Ken
58
17
  @data["id"] || ""
59
18
  end
60
19
 
20
+ # resource guid
21
+ # @api public
22
+ def guid
23
+ @data['guid'] || ""
24
+ end
25
+
61
26
  # resource name
62
27
  # @api public
63
28
  def name
@@ -87,6 +52,20 @@ module Ken
87
52
  @views ||= Ken::Collection.new(types.map { |type| Ken::View.new(self, type) })
88
53
  end
89
54
 
55
+ # returns individual view based on the requested type id
56
+ # @api public
57
+ def view(type)
58
+ views.each { |v| return v if v.type.id =~ /^#{Regexp.escape(type)}$/}
59
+ nil
60
+ end
61
+
62
+ # returns individual type based on the requested type id
63
+ # @api public
64
+ def type(type)
65
+ types.each { |t| return t if t.id =~ /^#{Regexp.escape(type)}$/}
66
+ nil
67
+ end
68
+
90
69
  # returns all the properties from all assigned types
91
70
  # @api public
92
71
  def properties
@@ -104,6 +83,13 @@ module Ken
104
83
  @attributes.values
105
84
  end
106
85
 
86
+ # search for an attribute by name and return it
87
+ # @api public
88
+ def attribute(name)
89
+ attributes.each { |a| return a if a.property.id == name }
90
+ nil
91
+ end
92
+
107
93
  # returns true if type information is already loaded
108
94
  # @api public
109
95
  def schema_loaded?
@@ -1,5 +1,5 @@
1
1
  module Ken
2
- class << self #:nodoc:
2
+ class << self
3
3
  attr_accessor :session
4
4
  end
5
5
 
@@ -16,14 +16,14 @@ module Ken
16
16
  end
17
17
  end
18
18
 
19
- class AttributeNotFound < StandardError; end
20
- class PropertyNotFound < StandardError; end
21
- class ResourceNotFound < StandardError; end
19
+ class AttributeNotFound < StandardError ; end
20
+ class PropertyNotFound < StandardError ; end
21
+ class ResourceNotFound < StandardError ; end
22
+ class ViewNotFound < StandardError ; end
22
23
 
23
24
  # partially taken from chris eppstein's freebase api
24
25
  # http://github.com/chriseppstein/freebase/tree
25
26
  class Session
26
-
27
27
  public
28
28
  # Initialize a new Ken Session
29
29
  # Ken::Session.new(host{String, IO}, username{String}, password{String})
@@ -69,11 +69,10 @@ module Ken
69
69
  end
70
70
  end # handle_read_error
71
71
 
72
-
73
- # perform a mqlread and return the results
72
+ # Perform a mqlread and return the results
73
+ # Specify :cursor => true to batch the results of a query, sending multiple requests if necessary.
74
74
  # TODO: should support multiple queries
75
75
  # you should be able to pass an array of queries
76
- # Specify :cursor => true to batch the results of a query, sending multiple requests if necessary.
77
76
  def mqlread(query, options = {})
78
77
  Ken.logger.info ">>> Sending Query: #{query.to_json}"
79
78
  cursor = options[:cursor]
@@ -126,6 +125,5 @@ module Ken
126
125
  end
127
126
  Ken.logger.info("Wrote response to #{fname}")
128
127
  end
129
-
130
128
  end # class Session
131
129
  end # module Ken
@@ -16,12 +16,3 @@ module Ken
16
16
  end
17
17
  end
18
18
 
19
- class Object
20
- # nice for debugging
21
- # usage: print_call_stack(:method_name, 2, 10)
22
- def print_call_stack(from = 2, to = nil, html = false)
23
- (from..(to ? to : caller.length)).each do |idx|
24
- p "[#{idx}]: #{caller[idx]}#{html ? '<br />' : ''}"
25
- end
26
- end
27
- end
@@ -34,23 +34,23 @@ module Ken
34
34
  @resource.attributes.select { |a| a.property.type == @type}
35
35
  end
36
36
 
37
+ # search for an attribute by name and return it
38
+ # @api public
39
+ def attribute(name)
40
+ attributes.each { |a| return a if a.property.id =~ /\/#{name}$/ }
41
+ nil
42
+ end
43
+
37
44
  # returns properties which are member of the view's type
38
45
  # @api public
39
46
  def properties
40
47
  @resource.properties.select { |p| p.type == @type}
41
48
  end
42
49
 
43
- # delegate to attribute_get
50
+ # delegate to attribute
44
51
  def method_missing sym
45
- attribute_get(sym.to_s)
52
+ attribute(sym.to_s)
46
53
  end
47
54
 
48
- private
49
- # search for an attribute by name and return it
50
- # @api private
51
- def attribute_get(name)
52
- attributes.each { |a| return a if a.property.id =~ /\/#{name}$/ }
53
- raise AttributeNotFound
54
- end
55
55
  end
56
56
  end
@@ -5,7 +5,7 @@ class KenTest < Test::Unit::TestCase
5
5
  setup do
6
6
  Ken::Session.new('http://www.freebase.com', 'ma', 'xxxxx')
7
7
  end
8
-
8
+
9
9
  should 'return a Ken::Resource' do
10
10
  the_police = Ken.get("/en/the_police")
11
11
  the_police.should be_kind_of(Ken::Resource)
@@ -48,7 +48,7 @@ class KenTest < Test::Unit::TestCase
48
48
  ],
49
49
  :type => "/film/film"
50
50
  }
51
-
51
+
52
52
  resources = Ken.all(query)
53
53
  resources.length.should == 3
54
54
  resources.first.name.should == "Star Wars Episode IV: A New Hope"
@@ -48,4 +48,14 @@ class Test::Unit::TestCase
48
48
  matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
49
49
  receiver.eql?(false)
50
50
  end
51
+ end
52
+
53
+ class Object
54
+ # nice for debugging
55
+ # usage: print_call_stack(:method_name, 2, 10)
56
+ def print_call_stack(from = 2, to = nil, html = false)
57
+ (from..(to ? to : caller.length)).each do |idx|
58
+ p "[#{idx}]: #{caller[idx]}#{html ? '<br />' : ''}"
59
+ end
60
+ end
51
61
  end
@@ -18,6 +18,16 @@ class ResourceTest < Test::Unit::TestCase
18
18
  @the_police.views.first.type.should be_kind_of(Ken::Type)
19
19
  end
20
20
 
21
+ should "return individual view based requested type id" do
22
+ @the_police.view('/music/artist').should be_kind_of(Ken::View)
23
+ @the_police.view('/location/location').should be_nil # not existent view
24
+ end
25
+
26
+ should "return individual type based requested type id" do
27
+ @the_police.type('/music/artist').should be_kind_of(Ken::Type)
28
+ @the_police.type('/location/location').should be_nil # not existent type
29
+ end
30
+
21
31
  should 'have a full set of attributes' do
22
32
  @the_police.attributes.should_not be_nil
23
33
  end
@@ -9,7 +9,7 @@ class SessionTest < Test::Unit::TestCase
9
9
 
10
10
  should 'return the correct set of types' do
11
11
  result = Ken.session.mqlread({:id => "/en/the_police", :type => []})
12
- result['type'].length.should == 6
12
+ result['type'].length.should >= 1
13
13
  end
14
14
 
15
15
  should 'raise a Ken::MqlReadError if node does not exist' do
@@ -26,7 +26,7 @@ class ViewTest < Test::Unit::TestCase
26
26
 
27
27
  context "when accessing a direct attribute" do
28
28
  setup do
29
- @genre = @view.genre
29
+ @genre = @view.attribute('genre')
30
30
  @album = @view.album
31
31
  end
32
32
 
@@ -41,7 +41,7 @@ class ViewTest < Test::Unit::TestCase
41
41
  end
42
42
 
43
43
  should "raise AttributeNotFound when invalid propertyname is supplied" do
44
- lambda { @view.not_existing_attribute }.should raise_error(Ken::AttributeNotFound)
44
+ @view.not_existing_attribute.should be_nil
45
45
  end
46
46
  end # context
47
47
  end # context
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: michael-ken
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - michael
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-24 00:00:00 -07:00
12
+ date: 2009-08-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -89,6 +89,7 @@ files:
89
89
  - test/unit/view_test.rb
90
90
  has_rdoc: false
91
91
  homepage: http://github.com/michael/ken
92
+ licenses:
92
93
  post_install_message:
93
94
  rdoc_options:
94
95
  - --charset=UTF-8
@@ -109,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  requirements: []
110
111
 
111
112
  rubyforge_project:
112
- rubygems_version: 1.2.0
113
+ rubygems_version: 1.3.5
113
114
  signing_key:
114
115
  specification_version: 3
115
116
  summary: Ruby API for Accessing the Freebase