evri 0.05 → 0.06
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/evri/entity.rb +26 -30
- data/lib/evri/relation.rb +2 -2
- data/lib/evri/zeitgeist.rb +2 -2
- data/lib/evri.rb +12 -61
- data/test/test_entity.rb +1 -2
- data/test/test_traverse.rb +5 -8
- metadata +2 -2
data/lib/evri/entity.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Evri
|
2
2
|
# Represents an Evri Entity.
|
3
3
|
class Entity
|
4
|
-
attr_reader :properties
|
4
|
+
attr_reader :properties, :source_url
|
5
5
|
|
6
6
|
# Finds a specific Entity, given an ID. An Entity is a person, company, product, etc.
|
7
7
|
#
|
@@ -13,7 +13,7 @@ module Evri
|
|
13
13
|
@results ||= {}
|
14
14
|
id = "/" + id
|
15
15
|
return @results[id] if @results[id]
|
16
|
-
@results[id] =
|
16
|
+
@results[id] = create_one do
|
17
17
|
Evri.query(:type => :uri, :query => id)
|
18
18
|
end
|
19
19
|
end
|
@@ -22,8 +22,8 @@ module Evri
|
|
22
22
|
# Entity.search "Barack Obama"
|
23
23
|
# Entity.search "Forgetting Sarah Marshall"
|
24
24
|
def self.search name
|
25
|
-
|
26
|
-
Evri.query(:type => :search, :query => name)[
|
25
|
+
create_many do
|
26
|
+
Evri.query(:type => :search, :query => name)["entities/entity"]
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -33,8 +33,8 @@ module Evri
|
|
33
33
|
#
|
34
34
|
# will likely return an Entity representing Barack Obama.
|
35
35
|
def self.search_by_prefix prefix
|
36
|
-
|
37
|
-
Evri.query(:type => :prefix, :query => prefix)[
|
36
|
+
create_many do
|
37
|
+
Evri.query(:type => :prefix, :query => prefix)["entities/entity"]
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -53,16 +53,12 @@ module Evri
|
|
53
53
|
raise ArgumentError, "Must specify URI via the :uri option" unless uri
|
54
54
|
raise ArgumentError, "Must specify some text via the :text option" unless text
|
55
55
|
|
56
|
-
|
56
|
+
create_many do
|
57
57
|
json = Evri.query(:type => :from_media, :uri => uri, :text => text)
|
58
|
-
json[
|
58
|
+
json["graph/entities/entity"]
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
# Returns the URL where the information for this entity came from.
|
63
|
-
def source_url
|
64
|
-
@source_url
|
65
|
-
end
|
66
62
|
|
67
63
|
# Returns information about an entity.
|
68
64
|
#
|
@@ -112,14 +108,13 @@ module Evri
|
|
112
108
|
|
113
109
|
# Returns the Evri URI of the entity
|
114
110
|
def href
|
115
|
-
#p @parsed_json if !@parsed_json[:href]
|
116
111
|
@parsed_json[:href] || target_href
|
117
112
|
end
|
118
113
|
|
119
114
|
alias id href
|
120
115
|
alias uri href
|
121
116
|
|
122
|
-
# TODO
|
117
|
+
# TODO document this
|
123
118
|
def target_href
|
124
119
|
@parsed_json[:targetHref]
|
125
120
|
end
|
@@ -137,13 +132,19 @@ module Evri
|
|
137
132
|
end
|
138
133
|
end
|
139
134
|
json = Evri.query(:type => :relations, :query => id, :from_domains => from_domains)
|
140
|
-
json["relations
|
135
|
+
json["relations/relation"].each do |json|
|
141
136
|
@relations << Relation.new(json)
|
142
137
|
end
|
143
138
|
@relations
|
144
139
|
end
|
145
140
|
|
146
|
-
# Returns which entities are related to the current entity
|
141
|
+
# Returns which entities are related to the current entity.
|
142
|
+
# You can pass in :entity, :uri, or a <insert description of verbs, etc>. You can also specify the type of result to return (image, video, etc).
|
143
|
+
#
|
144
|
+
# @obama.related_by(:verb => :kill) # Returns the entities that are related to Obama by the verb 'kill'
|
145
|
+
# @obama.related_by(:entity => @mccain) # Returns the entities that are related to Obama by McCain
|
146
|
+
# @obama.related_by(:uri => "http://cnn.com/article/here") # Returns the entities that are related to Obama by the given URI
|
147
|
+
# @obama.related_by(:facet => "politician", :entity => @mccain)
|
147
148
|
def related_by options={}
|
148
149
|
media, verb, verb_value, entity, uri = nil, nil, nil, nil, nil
|
149
150
|
options.each do |key, value|
|
@@ -159,8 +160,8 @@ module Evri
|
|
159
160
|
end
|
160
161
|
end
|
161
162
|
json = Evri.query(:type => :related_by, :media => media, :query => id, :uri => uri, :entity => entity, :verb => verb, :value => verb_value)
|
162
|
-
Entity.
|
163
|
-
json[
|
163
|
+
Entity.create_many do
|
164
|
+
json["relations/relation/targets/entity"]
|
164
165
|
end
|
165
166
|
end
|
166
167
|
|
@@ -178,11 +179,8 @@ module Evri
|
|
178
179
|
end
|
179
180
|
|
180
181
|
json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image')
|
181
|
-
|
182
|
-
|
183
|
-
json["mediaResult"]["imageList"]["image"].each do |media_json|
|
184
|
-
medias << Image.new(media_json)
|
185
|
-
end
|
182
|
+
json["mediaResult/imageList/image"].each do |media_json|
|
183
|
+
medias << Image.new(media_json)
|
186
184
|
end
|
187
185
|
medias
|
188
186
|
end
|
@@ -202,10 +200,8 @@ module Evri
|
|
202
200
|
|
203
201
|
type = options[:type] ? options[:type] : nil
|
204
202
|
json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type)
|
205
|
-
|
206
|
-
|
207
|
-
medias << Article.new(media_json)
|
208
|
-
end
|
203
|
+
json["mediaResult/articleList/article"].each do |media_json|
|
204
|
+
medias << Article.new(media_json)
|
209
205
|
end
|
210
206
|
medias
|
211
207
|
end
|
@@ -213,7 +209,7 @@ module Evri
|
|
213
209
|
|
214
210
|
private
|
215
211
|
|
216
|
-
def self.
|
212
|
+
def self.create_one json=nil, &block
|
217
213
|
begin
|
218
214
|
json = block.call if block
|
219
215
|
Entity.new json["entity"]
|
@@ -222,7 +218,7 @@ module Evri
|
|
222
218
|
end
|
223
219
|
end
|
224
220
|
|
225
|
-
def self.
|
221
|
+
def self.create_many jsons=nil, &block
|
226
222
|
jsons = block.call if block
|
227
223
|
return [] unless jsons
|
228
224
|
if jsons.multiple?
|
@@ -236,7 +232,7 @@ module Evri
|
|
236
232
|
@properties = {}
|
237
233
|
return unless json[:property]
|
238
234
|
json[:property].each do |prop|
|
239
|
-
|
235
|
+
unless prop[:name].empty?
|
240
236
|
@properties[prop[:name].to_sym] = prop[:value]
|
241
237
|
end
|
242
238
|
end
|
data/lib/evri/relation.rb
CHANGED
@@ -14,8 +14,8 @@ module Evri
|
|
14
14
|
return @entities unless @entities.empty?
|
15
15
|
json = Evri.query(:type => :uri, :query => @href)
|
16
16
|
@entities =
|
17
|
-
Entity.
|
18
|
-
json["relations
|
17
|
+
Entity.create_many do
|
18
|
+
json["relations/relation/targets/entity"]
|
19
19
|
end
|
20
20
|
@entities
|
21
21
|
end
|
data/lib/evri/zeitgeist.rb
CHANGED
@@ -17,8 +17,8 @@ module Evri
|
|
17
17
|
eval %(
|
18
18
|
def self.#{t}_#{et}
|
19
19
|
json = Evri.query :type => :zeitgeist, :query => "#{et}/#{t}"
|
20
|
-
Entity.
|
21
|
-
json["zeitgeist
|
20
|
+
Entity.create_many do
|
21
|
+
json["zeitgeist/#{t}/entities/entity"]
|
22
22
|
end
|
23
23
|
end
|
24
24
|
)
|
data/lib/evri.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
$: << File.dirname(__FILE__)
|
2
1
|
|
3
2
|
require 'uri'
|
4
3
|
require 'net/http'
|
5
4
|
require 'cgi'
|
6
|
-
require 'traverse'
|
7
5
|
|
8
6
|
begin
|
9
7
|
require 'rubygems'
|
@@ -17,6 +15,8 @@ for file in Dir[File.join(File.dirname(__FILE__), 'evri', '*')]
|
|
17
15
|
require file
|
18
16
|
end
|
19
17
|
|
18
|
+
require File.join(File.dirname(__FILE__), 'traverse')
|
19
|
+
|
20
20
|
module Evri
|
21
21
|
|
22
22
|
# A general error.
|
@@ -25,7 +25,7 @@ module Evri
|
|
25
25
|
# Raised whenever the entity you are searching for cannot be found.
|
26
26
|
class EntityNotFound < Error; end
|
27
27
|
|
28
|
-
VERSION = "0.
|
28
|
+
VERSION = "0.06"
|
29
29
|
@@api_host = "api.evri.com"
|
30
30
|
@@source_host = @@api_host
|
31
31
|
@@source_url = nil
|
@@ -60,75 +60,26 @@ module Evri
|
|
60
60
|
begin
|
61
61
|
Traverse.new JSON.parse(json)
|
62
62
|
rescue JSON::ParserError => e
|
63
|
-
# puts "Error!"
|
64
|
-
# puts e.message
|
65
|
-
# puts json
|
66
63
|
raise Error.new(e)
|
67
64
|
end
|
68
65
|
end
|
69
66
|
|
70
67
|
# TODO Rewrite
|
68
|
+
# Build the path and query string, get a response from the api server, and parse the data.
|
69
|
+
# Not used by clients. Maybe use :nodoc: here.
|
71
70
|
def self.query options={}
|
72
|
-
query =
|
73
|
-
|
74
|
-
when :uri
|
75
|
-
path = options[:query]
|
76
|
-
when :relations
|
77
|
-
path = options[:query] + "/relations"
|
78
|
-
if options[:from_domains]
|
79
|
-
query = "includeDomain=#{options[:from_domains]}"
|
80
|
-
end
|
81
|
-
when :related_by
|
82
|
-
if options[:uri]
|
83
|
-
path = options[:query] + "/related/entities"
|
84
|
-
query = "uri=#{escape(options[:uri])}"
|
85
|
-
else
|
86
|
-
path = options[:query] + "/relations"
|
87
|
-
if options[:verb]
|
88
|
-
path += "/#{options[:verb]}/#{escape(options[:value])}"
|
89
|
-
if options[:entity]
|
90
|
-
path += options[:entity].href
|
91
|
-
end
|
92
|
-
end
|
93
|
-
if options[:media]
|
94
|
-
query += "media=#{options[:media]}"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
when :zeitgeist
|
98
|
-
path = "/zeitgeist/entities/" + options[:query]
|
99
|
-
when :related_medias
|
100
|
-
path = options[:query] + "/media/related"
|
101
|
-
if options[:entities]
|
102
|
-
query = options[:entities].map do |e|
|
103
|
-
"entityURI=#{e}&"
|
104
|
-
end
|
105
|
-
query = query.join
|
106
|
-
end
|
107
|
-
query += "type=#{options[:media]}" if options[:media]
|
108
|
-
when :from_media
|
109
|
-
path = "/media/entities"
|
110
|
-
query = "uri=#{escape(options[:uri])}&text=#{escape(options[:text])}"
|
111
|
-
when :search
|
112
|
-
path = "/entities/find"
|
113
|
-
query = "name=#{escape(options[:query])}"
|
114
|
-
when :prefix
|
115
|
-
path = "/entities/find"
|
116
|
-
query = "prefix=#{escape(options[:query])}"
|
117
|
-
else
|
118
|
-
raise ArgumentError, "unexpected type #{ options[:type] }"
|
119
|
-
end
|
120
|
-
query = nil if query.empty?
|
71
|
+
path, query = Evri::URLs.generate options
|
72
|
+
|
121
73
|
uri = URI::HTTP.build :host => self.api_host, :path => path + '.json', :query => query
|
122
|
-
# puts "getting #{ uri }"
|
123
74
|
response = Net::HTTP.get_response(uri)
|
75
|
+
|
124
76
|
raise Error.new("unexpected http response: #{ response.code }") unless response.code == "200"
|
77
|
+
|
78
|
+
# Remember the source URL for this request.
|
125
79
|
@@source_url = @@source_host + uri.request_uri
|
126
|
-
parse_json(response.body)
|
127
|
-
end
|
128
80
|
|
129
|
-
|
130
|
-
|
131
|
-
text ? CGI.escape(text.to_s) : ''
|
81
|
+
# Return the parsed JSON
|
82
|
+
parse_json(response.body)
|
132
83
|
end
|
133
84
|
|
134
85
|
private
|
data/test/test_entity.rb
CHANGED
@@ -141,8 +141,7 @@ class TestEntity < Test::Unit::TestCase
|
|
141
141
|
uri = "http://www.reuters.com/article/industryNews/idUSTRE4981RO20081009"
|
142
142
|
dreamworks = Evri::Entity.find "/organization/dreamworks-0x3c510"
|
143
143
|
entities = Evri::Entity.from_media(:uri => uri, :text => "Dreamworks")
|
144
|
-
|
145
|
-
#assert entities.include?(dreamworks)
|
144
|
+
assert entities.include?(dreamworks)
|
146
145
|
|
147
146
|
text = %(He starred in "Forgetting Sarah Marshall" and was picked as one of Variety's 10 comics to watch, but Russell Brand's latest performance is no laughing matter for the BBC.)
|
148
147
|
uri = "http://www.variety.com/article/VR1117994787.html?categoryid=14&cs=1&nid=2565"
|
data/test/test_traverse.rb
CHANGED
@@ -1,19 +1,16 @@
|
|
1
1
|
require 'pp'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'lib/evri'
|
4
|
-
require 'lib/traverse'
|
5
4
|
require 'mocha'
|
6
5
|
|
7
6
|
class TestTraverse < Test::Unit::TestCase
|
8
|
-
BARACK_ID = "/person/barack-obama-0x16f69"
|
9
|
-
|
10
7
|
def setup
|
11
|
-
@json =
|
8
|
+
@json = JSON.parse(File.read("test/barack.txt"))
|
12
9
|
end
|
13
10
|
|
14
11
|
def test_go
|
15
12
|
result = Traverse.new(@json)["entity"]
|
16
|
-
assert_equal
|
13
|
+
assert_equal "/person/barack-obama-0x16f69", result[:href]
|
17
14
|
assert_equal "Barack Obama", result[:name]
|
18
15
|
assert_equal "PERSON", result["type"]
|
19
16
|
assert_equal "Democratic Party", result["properties"]["property"].find { |p| p[:name] == "political_party" }[:value]
|
@@ -25,9 +22,9 @@ class TestTraverse < Test::Unit::TestCase
|
|
25
22
|
"#{ property[:name] } => #{ property[:value] }"
|
26
23
|
end
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
assert_equal result["properties/property"].size, result[:properties][:property].size
|
26
|
+
|
27
|
+
assert result["does/not/exist"].blank?
|
31
28
|
end
|
32
29
|
end
|
33
30
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: evri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.06"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Van Dyk
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-06 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|