evri 0.04 → 0.05

Sign up to get free protection for your applications and to get access to all the features.
data/lib/evri/entity.rb CHANGED
@@ -8,9 +8,10 @@ module Evri
8
8
  # IDs look like <tt>/person/barack-obama-0x16f69</tt> or <tt>product/forgetting-sarah-marshall-0x1e0d1</tt>
9
9
  # Entity.find "/person/barack-obama-0x16f69"
10
10
  def self.find id
11
+ raise "Must provide an id" unless id
11
12
  @entended_properties = true
12
13
  @results ||= {}
13
- id = "/" + id unless id =~ /\A\//
14
+ id = "/" + id
14
15
  return @results[id] if @results[id]
15
16
  @results[id] = create_from_json do
16
17
  Evri.query(:type => :uri, :query => id)
@@ -22,7 +23,7 @@ module Evri
22
23
  # Entity.search "Forgetting Sarah Marshall"
23
24
  def self.search name
24
25
  create_from_jsons do
25
- Evri.query(:type => :search, :query => name)
26
+ Evri.query(:type => :search, :query => name)[:entities][:entity]
26
27
  end
27
28
  end
28
29
 
@@ -33,7 +34,7 @@ module Evri
33
34
  # will likely return an Entity representing Barack Obama.
34
35
  def self.search_by_prefix prefix
35
36
  create_from_jsons do
36
- Evri.query(:type => :prefix, :query => prefix)
37
+ Evri.query(:type => :prefix, :query => prefix)[:entities][:entity]
37
38
  end
38
39
  end
39
40
 
@@ -52,9 +53,9 @@ module Evri
52
53
  raise ArgumentError, "Must specify URI via the :uri option" unless uri
53
54
  raise ArgumentError, "Must specify some text via the :text option" unless text
54
55
 
55
- json = Evri.parse_json(Evri.query(:type => :from_media, :uri => uri, :text => text))
56
- json["graph"]["entities"]["entity"].map do |entity_json|
57
- Entity.new entity_json
56
+ create_from_jsons do
57
+ json = Evri.query(:type => :from_media, :uri => uri, :text => text)
58
+ json[:graph][:entities][:entity]
58
59
  end
59
60
  end
60
61
 
@@ -83,35 +84,36 @@ module Evri
83
84
  end
84
85
  end
85
86
 
86
- def to_s
87
+ def to_s # :nodoc:
87
88
  "#{name} (#{ id })"
88
89
  end
89
90
 
90
- def inspect
91
+ def inspect # :nodoc:
91
92
  "#<Evri::Entity:#{to_s}>"
92
93
  end
93
94
 
94
95
  # Returns the name of the entity.
95
96
  def name
96
- @parsed_json["name"]["$"] || @parsed_json["name"]
97
+ @parsed_json["name"]
97
98
  end
98
99
 
99
- def initialize json
100
+ def initialize json # :nodoc:
100
101
  @parsed_json = json
101
102
  @relations = []
102
103
  @source_url = Evri.source_url
103
- if json["properties"]
104
+ if json and json["properties"]
104
105
  set_properties json["properties"]
105
106
  end
106
107
  end
107
108
 
108
- def == b
109
+ def == b # :nodoc:
109
110
  self.id == b.id
110
111
  end
111
112
 
112
113
  # Returns the Evri URI of the entity
113
114
  def href
114
- @parsed_json["@href"]
115
+ #p @parsed_json if !@parsed_json[:href]
116
+ @parsed_json[:href] || target_href
115
117
  end
116
118
 
117
119
  alias id href
@@ -119,7 +121,7 @@ module Evri
119
121
 
120
122
  # TODO
121
123
  def target_href
122
- @parsed_json["@targetHref"]
124
+ @parsed_json[:targetHref]
123
125
  end
124
126
 
125
127
  # Returns relationships for the entity
@@ -135,11 +137,8 @@ module Evri
135
137
  end
136
138
  end
137
139
  json = Evri.query(:type => :relations, :query => id, :from_domains => from_domains)
138
- Evri.parse_json(json)["relations"].each do |type, relation_json|
139
- next if type != 'relation'
140
- relation_json.each do |r|
141
- @relations << Relation.new(r)
142
- end
140
+ json["relations"]["relation"].each do |json|
141
+ @relations << Relation.new(json)
143
142
  end
144
143
  @relations
145
144
  end
@@ -159,21 +158,10 @@ module Evri
159
158
  verb_value = value
160
159
  end
161
160
  end
162
- @entities = []
163
161
  json = Evri.query(:type => :related_by, :media => media, :query => id, :uri => uri, :entity => entity, :verb => verb, :value => verb_value)
164
- Evri.parse_json(json)["relations"].each do |type, relation_json|
165
- next if type != 'relation'
166
- relation_json["targets"].each do |type, r|
167
- r.each do |entity|
168
- if entity.class == Hash
169
- if entity["@href"]
170
- @entities << Entity.new(entity)
171
- end
172
- end
173
- end
174
- end
162
+ Entity.create_from_jsons do
163
+ json[:relations][:relation][:targets][:entity]
175
164
  end
176
- @entities
177
165
  end
178
166
 
179
167
  # Returns images relating to the current entity
@@ -189,7 +177,7 @@ module Evri
189
177
  end
190
178
  end
191
179
 
192
- json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image'))
180
+ json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image')
193
181
  # TODO handle images here
194
182
  if json["mediaResult"]["imageList"]
195
183
  json["mediaResult"]["imageList"]["image"].each do |media_json|
@@ -213,7 +201,7 @@ module Evri
213
201
  end
214
202
 
215
203
  type = options[:type] ? options[:type] : nil
216
- json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type))
204
+ json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type)
217
205
  if json["mediaResult"]["articleList"]
218
206
  json["mediaResult"]["articleList"]["article"].each do |media_json|
219
207
  medias << Article.new(media_json)
@@ -228,7 +216,7 @@ module Evri
228
216
  def self.create_from_json json=nil, &block
229
217
  begin
230
218
  json = block.call if block
231
- Entity.new Evri.parse_json(json)["entity"]
219
+ Entity.new json["entity"]
232
220
  rescue Evri::Error => e
233
221
  raise Evri::EntityNotFound.new(e.message)
234
222
  end
@@ -236,26 +224,20 @@ module Evri
236
224
 
237
225
  def self.create_from_jsons jsons=nil, &block
238
226
  jsons = block.call if block
239
- result = []
240
- Evri.parse_json(jsons)["entities"].each do |type, entity|
241
- if entity.class == Array
242
- entity.each do |e|
243
- result << Entity.new(e)
244
- end
245
- else
246
- result << Entity.new(entity)
247
- end
227
+ return [] unless jsons
228
+ if jsons.multiple?
229
+ jsons.map { |e| Entity.new e }
230
+ else
231
+ [Entity.new(jsons)]
248
232
  end
249
- result
250
233
  end
251
234
 
252
235
  def set_properties json
253
236
  @properties = {}
254
- json["property"].each do |prop|
255
- if prop.class == Array
256
- @properties[prop.first.to_sym] = prop.last["$"]
257
- else
258
- @properties[prop["name"]["$"].to_sym] = prop["value"]["$"]
237
+ return unless json[:property]
238
+ json[:property].each do |prop|
239
+ if prop[:name]
240
+ @properties[prop[:name].to_sym] = prop[:value]
259
241
  end
260
242
  end
261
243
  end
data/lib/evri/media.rb CHANGED
@@ -7,42 +7,42 @@ module Evri
7
7
  # Represents an Article.
8
8
  class Article < Media
9
9
  attr_accessor :title, :href, :uri, :author, :published_at, :content
10
- def initialize json
11
- @title = json["title"]["$"]
12
- @author = json["author"]["$"]
13
- @content = json["content"] ? json["content"]["$"] : "No content provided"
14
- @published_at = json["published"]["$"]
15
- @href = Evri.api_host + json["link"]["@href"]
16
- @uri = json["link"]["@hostName"] + json["link"]["@path"]
10
+ def initialize json # :nodoc:
11
+ @title = json[:title]
12
+ @author = json[:author]
13
+ @content = (json[:content] || "No content provided")
14
+ @published_at = json[:published]
15
+ @href = Evri.api_host + json[:link][:href]
16
+ @uri = json[:link][:hostName] + json[:link][:path]
17
17
  end
18
18
  end
19
19
 
20
20
  # Represents an Image.
21
21
  class Image < Media
22
22
  attr_accessor :size, :title, :article_href, :mime_type, :thumbnail, :date, :content, :width, :url, :click_url, :height
23
- def initialize json
24
- @title = json["title"]
25
- @width = json["@width"]
26
- @height = json["@height"]
27
- @url = json["@url"]
28
- @size = json["size"]
29
- @title = json["title"]
30
- @article_href = json["articleHref"]["$"]
31
- @mime_type = json["mimeType"]
32
- @date = Date.parse(json["date"]["$"])
33
- @content = json["content"]["$"]
34
- @click_url = json["clickUrl"]["$"]
35
- @thumbnail = Thumbnail.new(json["thumbnail"])
23
+ def initialize json # :nodoc:
24
+ @title = json[:title]
25
+ @width = json[:width]
26
+ @height = json[:height]
27
+ @url = json[:url]
28
+ @size = json[:size]
29
+ @title = json[:title]
30
+ @article_href = json[:articleHref]
31
+ @mime_type = json[:mimeType]
32
+ @date = Date.parse(json[:date])
33
+ @content = json[:content]
34
+ @click_url = json[:clickUrl]
35
+ @thumbnail = Thumbnail.new(json[:thumbnail])
36
36
  end
37
37
 
38
38
  # Represents an Thumbnail for an Image.
39
39
  class Thumbnail
40
40
  attr_accessor :size, :width, :height, :url
41
- def initialize json
42
- @size = json["size"]
43
- @width = json["@width"]
44
- @height = json["@height"]
45
- @url = json["@url"]
41
+ def initialize json # :nodoc:
42
+ @size = json[:size]
43
+ @width = json[:width]
44
+ @height = json[:height]
45
+ @url = json[:url]
46
46
  end
47
47
  end
48
48
  end
data/lib/evri/relation.rb CHANGED
@@ -2,20 +2,21 @@ module Evri
2
2
  # Represents a relation.
3
3
  class Relation
4
4
  attr_accessor :name, :href, :type
5
- def initialize json
6
- @name = json["name"]["$"]
7
- @href = json["@href"]
8
- @type = json["type"]["$"]
5
+ def initialize json # :nodoc:
6
+ @name = json[:name]
7
+ @href = json[:href]
8
+ @type = json[:type]
9
9
  @entities = []
10
10
  end
11
11
 
12
12
  # Returns the entities in this relation.
13
13
  def entities
14
14
  return @entities unless @entities.empty?
15
- json = JSON.parse(Evri.query(:type => :uri, :query => @href))
16
- json["relations"]["relation"]["targets"]["entity"].each do |entity_json|
17
- @entities << Entity.new(entity_json)
18
- end
15
+ json = Evri.query(:type => :uri, :query => @href)
16
+ @entities =
17
+ Entity.create_from_jsons do
18
+ json["relations"]["relation"]["targets"]["entity"]
19
+ end
19
20
  @entities
20
21
  end
21
22
  end
@@ -3,13 +3,10 @@ module Evri
3
3
  #
4
4
  # To access the information, combine a TREND with a TYPE to form a class method.
5
5
  #
6
- # i.e.
6
+ # Zeitgeist.all_chemical
7
+ # Zeitgeist.falling_person
8
+ # Zeitgeist.popular_organization
7
9
  #
8
- # * Zeitgeist.all_chemical
9
- # * Zeitgeist.falling_person
10
- # * Zeitgeist.popular_organization
11
- #
12
- # etc
13
10
  class Zeitgeist
14
11
 
15
12
  TYPES = %w( animal backterium chemical concept disorder event location organization person plant product virus )
@@ -19,9 +16,9 @@ module Evri
19
16
  TYPES.each do |et|
20
17
  eval %(
21
18
  def self.#{t}_#{et}
22
- json = Evri.parse_json(Evri.query :type => :zeitgeist, :query => "#{et}/#{t}")
23
- json["zeitgeist"]["#{t}"]["entities"]["entity"].map do |entity_json|
24
- Entity.new(entity_json)
19
+ json = Evri.query :type => :zeitgeist, :query => "#{et}/#{t}"
20
+ Entity.create_from_jsons do
21
+ json["zeitgeist"]["#{t}"]["entities"]["entity"]
25
22
  end
26
23
  end
27
24
  )
data/lib/evri.rb CHANGED
@@ -3,6 +3,7 @@ $: << File.dirname(__FILE__)
3
3
  require 'uri'
4
4
  require 'net/http'
5
5
  require 'cgi'
6
+ require 'traverse'
6
7
 
7
8
  begin
8
9
  require 'rubygems'
@@ -24,7 +25,7 @@ module Evri
24
25
  # Raised whenever the entity you are searching for cannot be found.
25
26
  class EntityNotFound < Error; end
26
27
 
27
- VERSION = "0.04"
28
+ VERSION = "0.05"
28
29
  @@api_host = "api.evri.com"
29
30
  @@source_host = @@api_host
30
31
  @@source_url = nil
@@ -57,7 +58,7 @@ module Evri
57
58
  # Parses JSON data
58
59
  def self.parse_json json
59
60
  begin
60
- JSON.parse(json)
61
+ Traverse.new JSON.parse(json)
61
62
  rescue JSON::ParserError => e
62
63
  # puts "Error!"
63
64
  # puts e.message
@@ -122,7 +123,7 @@ module Evri
122
123
  response = Net::HTTP.get_response(uri)
123
124
  raise Error.new("unexpected http response: #{ response.code }") unless response.code == "200"
124
125
  @@source_url = @@source_host + uri.request_uri
125
- response.body
126
+ parse_json(response.body)
126
127
  end
127
128
 
128
129
  # Escapes CGI text
data/test/test_entity.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'lib/evri'
3
3
  require 'mocha'
4
+ require 'pp'
4
5
 
5
6
  class TestEntity < Test::Unit::TestCase
6
7
  BARACK_ID = "/person/barack-obama-0x16f69"
@@ -32,6 +33,7 @@ class TestEntity < Test::Unit::TestCase
32
33
  end
33
34
 
34
35
  def test_failed_search_returns_empty_array
36
+ # p Evri::Entity.search("asdfasdfasdfasdfjlskdjflkasdjfkljasdklfjkladsjfklajsdl;kfjlaksdjfadklsjfkladsjlfkjasdfkljas#")
35
37
  assert Evri::Entity.search("asdfasdfasdfasdfjlskdjflkasdjfkljasdklfjkladsjfklajsdl;kfjlaksdjfadklsjfkladsjlfkjasdfkljas#").empty?
36
38
  end
37
39
 
@@ -59,8 +61,8 @@ class TestEntity < Test::Unit::TestCase
59
61
  end
60
62
 
61
63
  def test_properties_from_related_by
62
- loaded_entity = @obama.related_by(:verb => :kill).first
63
- assert loaded_entity.info(:name)
64
+ joe = @obama.related_by(:verb => :kill).find { |e| e.name == "Joe Biden" }
65
+ assert joe.info(:name)
64
66
  end
65
67
 
66
68
  def test_relations
@@ -139,6 +141,13 @@ class TestEntity < Test::Unit::TestCase
139
141
  uri = "http://www.reuters.com/article/industryNews/idUSTRE4981RO20081009"
140
142
  dreamworks = Evri::Entity.find "/organization/dreamworks-0x3c510"
141
143
  entities = Evri::Entity.from_media(:uri => uri, :text => "Dreamworks")
142
- assert entities.include?(dreamworks)
144
+ # TODO FIX
145
+ #assert entities.include?(dreamworks)
146
+
147
+ 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
+ uri = "http://www.variety.com/article/VR1117994787.html?categoryid=14&cs=1&nid=2565"
149
+ movie = Evri::Entity.find "/product/forgetting-sarah-marshall-0x1e0d1"
150
+ entities = Evri::Entity.from_media(:uri => uri, :text => text)
151
+ assert entities.include?(movie)
143
152
  end
144
153
  end
@@ -0,0 +1,33 @@
1
+ require 'pp'
2
+ require 'test/unit'
3
+ require 'lib/evri'
4
+ require 'lib/traverse'
5
+ require 'mocha'
6
+
7
+ class TestTraverse < Test::Unit::TestCase
8
+ BARACK_ID = "/person/barack-obama-0x16f69"
9
+
10
+ def setup
11
+ @json = Evri.query(:type => :uri, :query => BARACK_ID)
12
+ end
13
+
14
+ def test_go
15
+ result = Traverse.new(@json)["entity"]
16
+ assert_equal BARACK_ID, result[:href]
17
+ assert_equal "Barack Obama", result[:name]
18
+ assert_equal "PERSON", result["type"]
19
+ assert_equal "Democratic Party", result["properties"]["property"].find { |p| p[:name] == "political_party" }[:value]
20
+ assert result[:facets][:facet].find { |f| f[:name] == "Author" }
21
+ assert result[:properties][:property].size > 20
22
+
23
+ # Get the properties and their values
24
+ result[:properties][:property].map do |property|
25
+ "#{ property[:name] } => #{ property[:value] }"
26
+ end
27
+
28
+ #unless result["does"]["not"]["exist"].blank?
29
+ #raise "shouldn't get here"
30
+ #end
31
+ end
32
+ end
33
+
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.04"
4
+ version: "0.05"
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-10-31 00:00:00 -07:00
12
+ date: 2008-11-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -101,3 +101,4 @@ test_files:
101
101
  - test/test_evri.rb
102
102
  - test/test_zeitgeist.rb
103
103
  - test/test_media.rb
104
+ - test/test_traverse.rb