joevandyk-evri 0.03 → 0.07

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -4,12 +4,18 @@ README.txt
4
4
  Rakefile
5
5
  TODO
6
6
  evri-api.gemspec
7
+ index.html
7
8
  lib/evri.rb
8
9
  lib/evri/entity.rb
9
10
  lib/evri/media.rb
10
11
  lib/evri/relation.rb
12
+ lib/evri/urls.rb
11
13
  lib/evri/zeitgeist.rb
14
+ lib/traverse.rb
15
+ t.rb
16
+ test/barack.txt
12
17
  test/test_entity.rb
13
18
  test/test_evri.rb
14
19
  test/test_media.rb
20
+ test/test_traverse.rb
15
21
  test/test_zeitgeist.rb
data/README.txt CHANGED
@@ -6,17 +6,16 @@
6
6
 
7
7
  A beautiful API that wraps the RESTful services provided by evri.com.
8
8
 
9
- == FEATURES/PROBLEMS:
10
-
11
- None yet.
12
-
13
9
  == SYNOPSIS:
14
10
 
15
- None yet.
11
+ obama = Evri::Entity.search("Barack Obama").first
12
+ obama.relationships
13
+ obama.images
14
+ obama.articles
16
15
 
17
16
  == REQUIREMENTS:
18
17
 
19
- none yet.
18
+ json
20
19
 
21
20
  == INSTALL:
22
21
 
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ require 'hoe'
5
5
  require './lib/evri.rb'
6
6
 
7
7
  Hoe.new('evri', Evri::VERSION) do |p|
8
+ p.rubyforge_name = 'evri-api'
8
9
  p.developer('Joe Van Dyk', 'joe@@fixieconsulting.com')
9
10
  p.extra_deps = [:json]
10
11
  p.extra_dev_deps = [:mocha]
@@ -18,4 +19,10 @@ task :github do
18
19
  `rake debug_gem | sed 1d > evri-api.gemspec`
19
20
  end
20
21
 
22
+ task :make_index_html do
23
+ sh "scp index.html joevandyk@rubyforge.org:/var/www/gforge-projects/evri-api"
24
+ end
25
+
26
+ task :publish_docs => :make_index_html
27
+
21
28
  # vim: syntax=Ruby
data/evri-api.gemspec CHANGED
@@ -1,22 +1,22 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{evri}
3
- s.version = "0.03"
3
+ s.version = "0.07"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Joe Van Dyk"]
7
- s.date = %q{2008-10-27}
7
+ s.date = %q{2008-11-06}
8
8
  s.description = %q{A beautiful API that wraps the RESTful services provided by evri.com.}
9
9
  s.email = ["joe@@fixieconsulting.com"]
10
- s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
11
- s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "TODO", "evri-api.gemspec", "lib/evri.rb", "lib/evri/entity.rb", "lib/evri/media.rb", "lib/evri/relation.rb", "lib/evri/zeitgeist.rb", "test/test_entity.rb", "test/test_evri.rb", "test/test_media.rb", "test/test_zeitgeist.rb"]
10
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt", "test/barack.txt"]
11
+ s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "TODO", "evri-api.gemspec", "index.html", "lib/evri.rb", "lib/evri/entity.rb", "lib/evri/media.rb", "lib/evri/relation.rb", "lib/evri/urls.rb", "lib/evri/zeitgeist.rb", "lib/traverse.rb", "t.rb", "test/barack.txt", "test/test_entity.rb", "test/test_evri.rb", "test/test_media.rb", "test/test_traverse.rb", "test/test_zeitgeist.rb"]
12
12
  s.has_rdoc = true
13
13
  s.homepage = %q{http://github.com/joevandyk/evri-api}
14
14
  s.rdoc_options = ["--main", "README.txt"]
15
15
  s.require_paths = ["lib"]
16
- s.rubyforge_project = %q{evri}
16
+ s.rubyforge_project = %q{evri-api}
17
17
  s.rubygems_version = %q{1.2.0}
18
18
  s.summary = %q{A beautiful API that wraps the RESTful services provided by evri.com.}
19
- s.test_files = ["test/test_media.rb", "test/test_zeitgeist.rb", "test/test_entity.rb", "test/test_evri.rb"]
19
+ s.test_files = ["test/test_entity.rb", "test/test_evri.rb", "test/test_zeitgeist.rb", "test/test_media.rb", "test/test_traverse.rb"]
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -25,15 +25,15 @@ Gem::Specification.new do |s|
25
25
  if current_version >= 3 then
26
26
  s.add_runtime_dependency(%q<json>, [">= 0"])
27
27
  s.add_development_dependency(%q<mocha>, [">= 0"])
28
- s.add_development_dependency(%q<hoe>, [">= 1.7.0"])
28
+ s.add_development_dependency(%q<hoe>, [">= 1.8.2"])
29
29
  else
30
30
  s.add_dependency(%q<json>, [">= 0"])
31
31
  s.add_dependency(%q<mocha>, [">= 0"])
32
- s.add_dependency(%q<hoe>, [">= 1.7.0"])
32
+ s.add_dependency(%q<hoe>, [">= 1.8.2"])
33
33
  end
34
34
  else
35
35
  s.add_dependency(%q<json>, [">= 0"])
36
36
  s.add_dependency(%q<mocha>, [">= 0"])
37
- s.add_dependency(%q<hoe>, [">= 1.7.0"])
37
+ s.add_dependency(%q<hoe>, [">= 1.8.2"])
38
38
  end
39
39
  end
data/index.html ADDED
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1> Evri Page </h1>
4
+ <p> Welcome, Dude. <a href="/evri">Here is the API</a>.</p>
5
+ </body>
6
+ </html>
data/lib/evri.rb CHANGED
@@ -1,4 +1,3 @@
1
- $: << File.dirname(__FILE__)
2
1
 
3
2
  require 'uri'
4
3
  require 'net/http'
@@ -16,6 +15,8 @@ for file in Dir[File.join(File.dirname(__FILE__), 'evri', '*')]
16
15
  require file
17
16
  end
18
17
 
18
+ require File.join(File.dirname(__FILE__), 'traverse')
19
+
19
20
  module Evri
20
21
 
21
22
  # A general error.
@@ -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.03"
28
+ VERSION = "0.07"
28
29
  @@api_host = "api.evri.com"
29
30
  @@source_host = @@api_host
30
31
  @@source_url = nil
@@ -57,77 +58,28 @@ 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
- # puts "Error!"
63
- # puts e.message
64
- # puts json
65
63
  raise Error.new(e)
66
64
  end
67
65
  end
68
66
 
69
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.
70
70
  def self.query options={}
71
- query = ""
72
- case options[:type]
73
- when :uri
74
- path = options[:query]
75
- when :relations
76
- path = options[:query] + "/relations"
77
- if options[:from_domains]
78
- query = "includeDomain=#{options[:from_domains]}"
79
- end
80
- when :related_by
81
- if options[:uri]
82
- path = options[:query] + "/related/entities"
83
- query = "uri=#{escape(options[:uri])}"
84
- else
85
- path = options[:query] + "/relations"
86
- if options[:verb]
87
- path += "/#{options[:verb]}/#{escape(options[:value])}"
88
- if options[:entity]
89
- path += options[:entity].href
90
- end
91
- end
92
- if options[:media]
93
- query += "media=#{options[:media]}"
94
- end
95
- end
96
- when :zeitgeist
97
- path = "/zeitgeist/entities/" + options[:query]
98
- when :related_medias
99
- path = options[:query] + "/media/related"
100
- if options[:entities]
101
- query = options[:entities].map do |e|
102
- "entityURI=#{e}&"
103
- end
104
- query = query.join
105
- end
106
- query += "type=#{options[:media]}" if options[:media]
107
- when :from_media
108
- path = "/media/entities"
109
- query = "uri=#{escape(options[:uri])}&text=#{escape(options[:text])}"
110
- when :search
111
- path = "/entities/find"
112
- query = "name=#{escape(options[:query])}"
113
- when :prefix
114
- path = "/entities/find"
115
- query = "prefix=#{escape(options[:query])}"
116
- else
117
- raise ArgumentError, "unexpected type #{ options[:type] }"
118
- end
119
- query = nil if query.empty?
71
+ path, query = Evri::URLs.generate options
72
+
120
73
  uri = URI::HTTP.build :host => self.api_host, :path => path + '.json', :query => query
121
- # puts "getting #{ uri }"
122
74
  response = Net::HTTP.get_response(uri)
75
+
123
76
  raise Error.new("unexpected http response: #{ response.code }") unless response.code == "200"
77
+
78
+ # Remember the source URL for this request.
124
79
  @@source_url = @@source_host + uri.request_uri
125
- response.body
126
- end
127
80
 
128
- # Escapes CGI text
129
- def self.escape text
130
- text ? CGI.escape(text.to_s) : ''
81
+ # Return the parsed JSON
82
+ parse_json(response.body)
131
83
  end
132
84
 
133
85
  private
data/lib/evri/entity.rb CHANGED
@@ -1,52 +1,70 @@
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
- # Finds a specific entity, given an ID.
6
+ # Finds a specific Entity, given an ID. An Entity is a person, company, product, etc.
7
+ #
8
+ # IDs look like <tt>/person/barack-obama-0x16f69</tt> or <tt>product/forgetting-sarah-marshall-0x1e0d1</tt>
9
+ # Entity.find "/person/barack-obama-0x16f69"
7
10
  def self.find id
11
+ raise "Must provide an id" unless id
8
12
  @entended_properties = true
9
13
  @results ||= {}
10
- id = "/" + id unless id =~ /\A\//
14
+ id = "/" + id
11
15
  return @results[id] if @results[id]
12
- @results[id] = create_from_json do
16
+ @results[id] = create_one do
13
17
  Evri.query(:type => :uri, :query => id)
14
18
  end
15
19
  end
16
20
 
17
- # Searches for an exact match.
21
+ # Searches for an exact match. It will return an array of Entities.
22
+ # Entity.search "Barack Obama"
23
+ # Entity.search "Forgetting Sarah Marshall"
18
24
  def self.search name
19
- create_from_jsons do
20
- Evri.query(:type => :search, :query => name)
25
+ create_many do
26
+ Evri.query(:type => :search, :query => name)["entities/entity"]
21
27
  end
22
28
  end
23
29
 
24
30
  # Searches for a partial match. i.e. 'ob' will return 'Obama'.
31
+ #
32
+ # Entity.search_by_prefix("oba")
33
+ #
34
+ # will likely return an Entity representing Barack Obama.
25
35
  def self.search_by_prefix prefix
26
- create_from_jsons do
27
- Evri.query(:type => :prefix, :query => prefix)
36
+ create_many do
37
+ Evri.query(:type => :prefix, :query => prefix)["entities/entity"]
28
38
  end
29
39
  end
30
40
 
31
- # Given a :uri and :text options, return the related entities.
41
+ # Returns Entities that the given +:uri+ option mentions.
42
+ #
43
+ # Entity.from_media :uri => "http://www.evri.com/rd/servlet/RequestDispatcherServlet?req=cl&url=http%3A%2F%2Ffeeds.feedburner.com%2F%7Er%2Fvariety%2Fnews%2Ftv%2F%7E3%2F434927667%2FVR1117994787"
44
+ #
45
+ # If Evri doesn't know about the given +:uri+ option, you can pass in a
46
+ # +:text+ option.
47
+ #
48
+ # 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.")
49
+ # uri = "http://www.evri.com/rd/servlet/RequestDispatcherServlet?req=cl&url=http%3A%2F%2Ffeeds.feedburner.com%2F%7Er%2Fvariety%2Fnews%2Ftv%2F%7E3%2F434927667%2FVR1117994787"
50
+ # Evri.from_media :uri => uri, :text => text
32
51
  def self.from_media options={}
33
52
  uri, text = options[:uri], options[:text]
34
53
  raise ArgumentError, "Must specify URI via the :uri option" unless uri
35
54
  raise ArgumentError, "Must specify some text via the :text option" unless text
36
55
 
37
- json = Evri.parse_json(Evri.query(:type => :from_media, :uri => uri, :text => text))
38
- json["graph"]["entities"].map do |e, entity_json|
39
- Entity.new entity_json
56
+ create_many do
57
+ json = Evri.query(:type => :from_media, :uri => uri, :text => text)
58
+ json["graph/entities/entity"]
40
59
  end
41
60
  end
42
61
 
43
- # Returns where the information for this entity came from.
44
- def source_url
45
- @source_url
46
- end
47
62
 
48
63
  # Returns information about an entity.
49
- # i.e obama.info(:birth_date)
64
+ #
65
+ # obama.info(:birth_date)
66
+ #
67
+ # If +:options+ is nil, will return all the available information.
50
68
  def info option=nil
51
69
  @properties ||= {}
52
70
  if defined?(@extended_properties)
@@ -62,46 +80,48 @@ module Evri
62
80
  end
63
81
  end
64
82
 
65
- def to_s
83
+ def to_s # :nodoc:
66
84
  "#{name} (#{ id })"
67
85
  end
68
86
 
69
- def inspect
87
+ def inspect # :nodoc:
70
88
  "#<Evri::Entity:#{to_s}>"
71
89
  end
72
90
 
73
- # Returns the name of the entity
91
+ # Returns the name of the entity.
74
92
  def name
75
- @parsed_json["name"]["$"] || @parsed_json["name"]
93
+ @parsed_json["name"]
76
94
  end
77
95
 
78
- def initialize json
96
+ def initialize json # :nodoc:
79
97
  @parsed_json = json
80
98
  @relations = []
81
99
  @source_url = Evri.source_url
82
- if json["properties"]
100
+ if json and json["properties"]
83
101
  set_properties json["properties"]
84
102
  end
85
103
  end
86
104
 
87
- def == b
105
+ def == b # :nodoc:
88
106
  self.id == b.id
89
107
  end
90
108
 
91
109
  # Returns the Evri URI of the entity
92
110
  def href
93
- @parsed_json["@href"]
111
+ @parsed_json[:href] || target_href
94
112
  end
95
113
 
96
114
  alias id href
97
115
  alias uri href
98
116
 
99
- # TODO
117
+ # TODO document this
100
118
  def target_href
101
- @parsed_json["@targetHref"]
119
+ @parsed_json[:targetHref]
102
120
  end
103
121
 
104
122
  # Returns relationships for the entity
123
+ #
124
+ # obama.relations
105
125
  def relations options={}
106
126
  from_domains = nil
107
127
  if options[:from]
@@ -112,16 +132,19 @@ module Evri
112
132
  end
113
133
  end
114
134
  json = Evri.query(:type => :relations, :query => id, :from_domains => from_domains)
115
- Evri.parse_json(json)["relations"].each do |type, relation_json|
116
- next if type != 'relation'
117
- relation_json.each do |r|
118
- @relations << Relation.new(r)
119
- end
135
+ json["relations/relation"].each do |json|
136
+ @relations << Relation.new(json)
120
137
  end
121
138
  @relations
122
139
  end
123
140
 
124
- # 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)
125
148
  def related_by options={}
126
149
  media, verb, verb_value, entity, uri = nil, nil, nil, nil, nil
127
150
  options.each do |key, value|
@@ -136,21 +159,10 @@ module Evri
136
159
  verb_value = value
137
160
  end
138
161
  end
139
- @entities = []
140
162
  json = Evri.query(:type => :related_by, :media => media, :query => id, :uri => uri, :entity => entity, :verb => verb, :value => verb_value)
141
- Evri.parse_json(json)["relations"].each do |type, relation_json|
142
- next if type != 'relation'
143
- relation_json["targets"].each do |type, r|
144
- r.each do |entity|
145
- if entity.class == Hash
146
- if entity["@href"]
147
- @entities << Entity.new(entity)
148
- end
149
- end
150
- end
151
- end
163
+ Entity.create_many do
164
+ json["relations/relation/targets/entity"]
152
165
  end
153
- @entities
154
166
  end
155
167
 
156
168
  # Returns images relating to the current entity
@@ -166,12 +178,9 @@ module Evri
166
178
  end
167
179
  end
168
180
 
169
- json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image'))
170
- # TODO handle images here
171
- if json["mediaResult"]["imageList"]
172
- json["mediaResult"]["imageList"]["image"].each do |media_json|
173
- medias << Image.new(media_json)
174
- end
181
+ json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image')
182
+ json["mediaResult/imageList/image"].each do |media_json|
183
+ medias << Image.new(media_json)
175
184
  end
176
185
  medias
177
186
  end
@@ -190,11 +199,9 @@ module Evri
190
199
  end
191
200
 
192
201
  type = options[:type] ? options[:type] : nil
193
- json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type))
194
- if json["mediaResult"]["articleList"]
195
- json["mediaResult"]["articleList"]["article"].each do |media_json|
196
- medias << Article.new(media_json)
197
- end
202
+ json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type)
203
+ json["mediaResult/articleList/article"].each do |media_json|
204
+ medias << Article.new(media_json)
198
205
  end
199
206
  medias
200
207
  end
@@ -202,37 +209,31 @@ module Evri
202
209
 
203
210
  private
204
211
 
205
- def self.create_from_json json=nil, &block
212
+ def self.create_one json=nil, &block
206
213
  begin
207
214
  json = block.call if block
208
- Entity.new Evri.parse_json(json)["entity"]
215
+ Entity.new json["entity"]
209
216
  rescue Evri::Error => e
210
217
  raise Evri::EntityNotFound.new(e.message)
211
218
  end
212
219
  end
213
220
 
214
- def self.create_from_jsons jsons=nil, &block
221
+ def self.create_many jsons=nil, &block
215
222
  jsons = block.call if block
216
- result = []
217
- Evri.parse_json(jsons)["entities"].each do |type, entity|
218
- if entity.class == Array
219
- entity.each do |e|
220
- result << Entity.new(e)
221
- end
222
- else
223
- result << Entity.new(entity)
224
- end
223
+ return [] unless jsons
224
+ if jsons.multiple?
225
+ jsons.map { |e| Entity.new e }
226
+ else
227
+ [Entity.new(jsons)]
225
228
  end
226
- result
227
229
  end
228
230
 
229
231
  def set_properties json
230
232
  @properties = {}
231
- json["property"].each do |prop|
232
- if prop.class == Array
233
- @properties[prop.first.to_sym] = prop.last["$"]
234
- else
235
- @properties[prop["name"]["$"].to_sym] = prop["value"]["$"]
233
+ return unless json[:property]
234
+ json[:property].each do |prop|
235
+ unless prop[:name].empty?
236
+ @properties[prop[:name].to_sym] = prop[:value]
236
237
  end
237
238
  end
238
239
  end
data/lib/evri/media.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module Evri
2
4
  class Media
3
5
  end
@@ -5,42 +7,42 @@ module Evri
5
7
  # Represents an Article.
6
8
  class Article < Media
7
9
  attr_accessor :title, :href, :uri, :author, :published_at, :content
8
- def initialize json
9
- @title = json["title"]["$"]
10
- @author = json["author"]["$"]
11
- @content = json["content"] ? json["content"]["$"] : "No content provided"
12
- @published_at = json["published"]["$"]
13
- @href = Evri.api_host + json["link"]["@href"]
14
- @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]
15
17
  end
16
18
  end
17
19
 
18
20
  # Represents an Image.
19
21
  class Image < Media
20
22
  attr_accessor :size, :title, :article_href, :mime_type, :thumbnail, :date, :content, :width, :url, :click_url, :height
21
- def initialize json
22
- @title = json["title"]
23
- @width = json["@width"]
24
- @height = json["@height"]
25
- @url = json["@url"]
26
- @size = json["size"]
27
- @title = json["title"]
28
- @article_href = json["articleHref"]["$"]
29
- @mime_type = json["mimeType"]
30
- @date = Date.parse(json["date"]["$"])
31
- @content = json["content"]["$"]
32
- @click_url = json["clickUrl"]["$"]
33
- @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])
34
36
  end
35
37
 
36
38
  # Represents an Thumbnail for an Image.
37
39
  class Thumbnail
38
40
  attr_accessor :size, :width, :height, :url
39
- def initialize json
40
- @size = json["size"]
41
- @width = json["@width"]
42
- @height = json["@height"]
43
- @url = json["@url"]
41
+ def initialize json # :nodoc:
42
+ @size = json[:size]
43
+ @width = json[:width]
44
+ @height = json[:height]
45
+ @url = json[:url]
44
46
  end
45
47
  end
46
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_many do
18
+ json["relations/relation/targets/entity"]
19
+ end
19
20
  @entities
20
21
  end
21
22
  end
data/lib/evri/urls.rb ADDED
@@ -0,0 +1,67 @@
1
+ module Evri
2
+ module URLs
3
+ # FIXME This is ugly and should be changed.
4
+ # Given a set of options, builds a path and query string that we will be requesting.
5
+ # :type is the name of the type of query you are done.
6
+ # :query (should be renamed) is the thing you are looking for
7
+ # :entity is an entity that ..
8
+ def self.generate options = {} #:nodoc:
9
+ query = ""
10
+ case options[:type]
11
+ when :uri
12
+ path = options[:query]
13
+ when :relations
14
+ path = options[:query] + "/relations"
15
+ if options[:from_domains]
16
+ query = "includeDomain=#{options[:from_domains]}"
17
+ end
18
+ when :related_by
19
+ if options[:uri]
20
+ path = options[:query] + "/related/entities"
21
+ query = "uri=#{escape(options[:uri])}"
22
+ else
23
+ path = options[:query] + "/relations"
24
+ if options[:verb]
25
+ path += "/#{options[:verb]}/#{escape(options[:value])}"
26
+ if options[:entity]
27
+ path += options[:entity].href
28
+ end
29
+ end
30
+ if options[:media]
31
+ query += "media=#{options[:media]}"
32
+ end
33
+ end
34
+ when :zeitgeist
35
+ path = "/zeitgeist/entities/" + options[:query]
36
+ when :related_medias
37
+ path = options[:query] + "/media/related"
38
+ if options[:entities]
39
+ query = options[:entities].map do |e|
40
+ "entityURI=#{e}&"
41
+ end
42
+ query = query.join
43
+ end
44
+ query += "type=#{options[:media]}" if options[:media]
45
+ when :from_media
46
+ path = "/media/entities"
47
+ query = "uri=#{escape(options[:uri])}&text=#{escape(options[:text])}"
48
+ when :search
49
+ path = "/entities/find"
50
+ query = "name=#{escape(options[:query])}"
51
+ when :prefix
52
+ path = "/entities/find"
53
+ query = "prefix=#{escape(options[:query])}"
54
+ else
55
+ raise ArgumentError, "unexpected type #{ options[:type] }"
56
+ end
57
+
58
+ query = nil if query.empty?
59
+
60
+ return [path, query]
61
+ end
62
+
63
+ def self.escape text
64
+ text ? CGI.escape(text.to_s) : ''
65
+ end
66
+ end
67
+ 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_many do
21
+ json["zeitgeist/#{t}/entities/entity"]
25
22
  end
26
23
  end
27
24
  )
data/lib/traverse.rb ADDED
@@ -0,0 +1,65 @@
1
+
2
+ class Traverse
3
+ include Enumerable
4
+ attr_reader :json
5
+
6
+ def each
7
+ @json.each { |j| yield Traverse.new(j) }
8
+ end
9
+
10
+ def multiple?
11
+ @json.class == Array
12
+ end
13
+
14
+ def initialize json
15
+ @json = json
16
+ end
17
+
18
+ def size
19
+ @json.size
20
+ end
21
+
22
+ def blank?
23
+ size == 0
24
+ end
25
+
26
+ alias empty? blank?
27
+
28
+ # TODO clean this crap up
29
+ def [] value
30
+ value = value.to_s
31
+ if values = value.split("/") and values.size > 1
32
+ str = values.map { |v| "['#{v}']" }.join
33
+ code = "self#{str}"
34
+ return eval(code)
35
+ end
36
+ result =
37
+ if @json.class == Array
38
+ @json.find do |key|
39
+ if key.class == Array
40
+ if key.first.class == Array
41
+ if key.first.first == "$"
42
+ key.first.last == value
43
+ end
44
+ elsif key.first.class == String
45
+ key.first == value
46
+ end
47
+ end
48
+ end
49
+ else
50
+ @json[value] || @json["@#{value}"]
51
+ end
52
+ if result
53
+ if result.class == Hash
54
+ result = result["$"] || result
55
+ end
56
+ end
57
+ if result.class == Hash || result.class == Array
58
+ result = Traverse.new(result)
59
+ else
60
+ result
61
+ end
62
+ return result if result
63
+ return Traverse.new([])
64
+ end
65
+ end
data/test/barack.txt ADDED
@@ -0,0 +1,307 @@
1
+ {
2
+ "entity": {
3
+ "name": {
4
+ "$": "Barack Obama"
5
+ },
6
+ "@href": "\/person\/barack-obama-0x16f69",
7
+ "@inOntology": "true",
8
+ "@id": "94057",
9
+ "type": {
10
+ "$": "PERSON"
11
+ },
12
+ "links": {
13
+
14
+ },
15
+ "@score": "0.0",
16
+ "properties": {
17
+ "property": [
18
+ {
19
+ "name": {
20
+ "$": "birthplace"
21
+ },
22
+ "@provenance": "8",
23
+ "value": {
24
+ "$": "Honolulu, Hawaii"
25
+ }
26
+ },
27
+ {
28
+ "name": {
29
+ "$": "occupation"
30
+ },
31
+ "@provenance": "8",
32
+ "value": {
33
+ "$": "Attorney"
34
+ }
35
+ },
36
+ {
37
+ "name": {
38
+ "$": "source_url"
39
+ },
40
+ "@provenance": "8",
41
+ "value": {
42
+ "$": "http:\/\/en.wikipedia.org\/wiki\/Barack_Obama"
43
+ }
44
+ },
45
+ {
46
+ "name": {
47
+ "$": "residence"
48
+ },
49
+ "@provenance": "8",
50
+ "value": {
51
+ "$": "Kenwood, Chicago, Illinois"
52
+ }
53
+ },
54
+ {
55
+ "name": {
56
+ "$": "full_title"
57
+ },
58
+ "@provenance": "8",
59
+ "value": {
60
+ "$": "President-elect of the United States"
61
+ }
62
+ },
63
+ {
64
+ "name": {
65
+ "$": "name"
66
+ },
67
+ "@provenance": "8",
68
+ "value": {
69
+ "$": "Obama, Barack Hussein"
70
+ }
71
+ },
72
+ {
73
+ "name": {
74
+ "$": "birth_name"
75
+ },
76
+ "@provenance": "8",
77
+ "value": {
78
+ "$": "Barack Hussein Obama II"
79
+ }
80
+ },
81
+ {
82
+ "name": {
83
+ "$": "birth_date"
84
+ },
85
+ "@provenance": "8",
86
+ "value": {
87
+ "$": "1961-08-04 00:00:00.0"
88
+ }
89
+ },
90
+ {
91
+ "name": {
92
+ "$": "birthplace"
93
+ },
94
+ "@provenance": "8",
95
+ "value": {
96
+ "$": "Honolulu, Hawaii, U.S.A."
97
+ }
98
+ },
99
+ {
100
+ "name": {
101
+ "$": "name"
102
+ },
103
+ "@provenance": "8",
104
+ "value": {
105
+ "$": "Obama, Barack, Jr."
106
+ }
107
+ },
108
+ {
109
+ "name": {
110
+ "$": "university_attended"
111
+ },
112
+ "@provenance": "8",
113
+ "value": {
114
+ "$": "Columbia University"
115
+ }
116
+ },
117
+ {
118
+ "name": {
119
+ "$": "vice_president"
120
+ },
121
+ "@provenance": "8",
122
+ "@linkObjectName": "Joe Biden",
123
+ "value": {
124
+ "$": "Joe Biden (elect)"
125
+ },
126
+ "@linkHref": "\/person\/joe-biden-0x2d99a",
127
+ "@linkInOntology": "true"
128
+ },
129
+ {
130
+ "name": {
131
+ "$": "name"
132
+ },
133
+ "@provenance": "8",
134
+ "value": {
135
+ "$": "Barack Obama"
136
+ }
137
+ },
138
+ {
139
+ "name": {
140
+ "$": "wikipedia_paragraph"
141
+ },
142
+ "@provenance": "8",
143
+ "value": {
144
+ "$": "Barack Hussein Obama II (pronounced: \/b\u0259\u02c8r\u0251\u02d0k h\u028a\u02c8se\u026an o\u028a\u02c8b\u0251\u02d0m\u0259\/; born August 4, 1961) is the President-elect of the United States of America and the junior United States Senator from Illinois. His inauguration into office as the forty-fourth President of the United States is scheduled for January 20, 2009. Obama is the first African American to be elected President of the United States, and was the first to be nominated for President by a major political party. Obama is also the first candidate born in Hawaii to have been nominated and subsequently elected president.\n\nA graduate of Columbia University and Harvard Law School, he became the first African American to serve as president of the Harvard Law Review. Obama worked as a community organizer and practiced as a civil rights attorney before serving three terms in the Illinois Senate from 1997 to 2004. He taught constitutional law at the University of Chicago Law School from 1992 to 2004. Following an unsuccessful bid for a seat in the U.S. House of Representatives in 2000, he announced his campaign for the U.S. Senate in January 2003. After a primary victory in March 2004, Obama delivered the keynote address at the Democratic National Convention in July 2004. He was elected to the Senate in November 2004 with 70 percent of the vote."
145
+ }
146
+ },
147
+ {
148
+ "name": {
149
+ "$": "children"
150
+ },
151
+ "@provenance": "8",
152
+ "value": {
153
+ "$": "Malia Ann, Sasha"
154
+ }
155
+ },
156
+ {
157
+ "name": {
158
+ "$": "university_attended"
159
+ },
160
+ "@provenance": "8",
161
+ "@linkObjectName": "Occidental College",
162
+ "value": {
163
+ "$": "Occidental College"
164
+ },
165
+ "@linkHref": "\/organization\/occidental-college-0xbd22f",
166
+ "@linkInOntology": "true"
167
+ },
168
+ {
169
+ "name": {
170
+ "$": "university_attended"
171
+ },
172
+ "@provenance": "8",
173
+ "@linkObjectName": "Harvard Law School",
174
+ "value": {
175
+ "$": "Harvard Law School"
176
+ },
177
+ "@linkHref": "\/organization\/harvard-law-school-0xd826a",
178
+ "@linkInOntology": "true"
179
+ },
180
+ {
181
+ "name": {
182
+ "$": "religion"
183
+ },
184
+ "@provenance": "8",
185
+ "@linkObjectName": "United Church of Christ",
186
+ "value": {
187
+ "$": "United Church of Christ"
188
+ },
189
+ "@linkHref": "\/organization\/united-church-of-christ-0x12af2d",
190
+ "@linkInOntology": "true"
191
+ },
192
+ {
193
+ "name": {
194
+ "$": "political_party"
195
+ },
196
+ "@provenance": "8",
197
+ "@linkObjectName": "Democratic Party",
198
+ "value": {
199
+ "$": "Democratic Party"
200
+ },
201
+ "@linkHref": "\/organization\/democratic-party-0x3e58b",
202
+ "@linkInOntology": "true"
203
+ },
204
+ {
205
+ "name": {
206
+ "$": "occupation"
207
+ },
208
+ "@provenance": "8",
209
+ "value": {
210
+ "$": "Politician"
211
+ }
212
+ },
213
+ {
214
+ "name": {
215
+ "$": "spouse"
216
+ },
217
+ "@provenance": "8",
218
+ "@linkObjectName": "Michelle Obama",
219
+ "value": {
220
+ "$": "Michelle Obama"
221
+ },
222
+ "@linkHref": "\/person\/michelle-obama-0x4c6e8",
223
+ "@linkInOntology": "true"
224
+ },
225
+ {
226
+ "name": {
227
+ "$": "represent_political"
228
+ },
229
+ "@groupNum": "2",
230
+ "@provenance": "8",
231
+ "@linkObjectName": "Illinois",
232
+ "value": {
233
+ "$": "Illinois"
234
+ },
235
+ "@linkHref": "\/location\/illinois-0x319c2",
236
+ "@linkInOntology": "true"
237
+ },
238
+ {
239
+ "name": {
240
+ "$": "official_website"
241
+ },
242
+ "@provenance": "8",
243
+ "value": {
244
+ "$": "Office of the President-Elect"
245
+ }
246
+ },
247
+ {
248
+ "name": {
249
+ "$": "birth_date"
250
+ },
251
+ "@provenance": "8",
252
+ "value": {
253
+ "$": "1961-08-04 00:00:00.0"
254
+ }
255
+ }
256
+ ]
257
+ },
258
+ "facets": {
259
+ "facet": [
260
+ {
261
+ "name": {
262
+ "$": "Author"
263
+ },
264
+ "relations": {
265
+
266
+ },
267
+ "properties": {
268
+
269
+ }
270
+ },
271
+ {
272
+ "name": {
273
+ "$": "Lawyer"
274
+ },
275
+ "relations": {
276
+
277
+ },
278
+ "properties": {
279
+
280
+ }
281
+ },
282
+ {
283
+ "name": {
284
+ "$": "U.S. Politician"
285
+ },
286
+ "relations": {
287
+
288
+ },
289
+ "properties": {
290
+
291
+ }
292
+ },
293
+ {
294
+ "name": {
295
+ "$": "Academic"
296
+ },
297
+ "relations": {
298
+
299
+ },
300
+ "properties": {
301
+
302
+ }
303
+ }
304
+ ]
305
+ }
306
+ }
307
+ }
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
@@ -140,5 +142,11 @@ class TestEntity < Test::Unit::TestCase
140
142
  dreamworks = Evri::Entity.find "/organization/dreamworks-0x3c510"
141
143
  entities = Evri::Entity.from_media(:uri => uri, :text => "Dreamworks")
142
144
  assert entities.include?(dreamworks)
145
+
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.)
147
+ uri = "http://www.variety.com/article/VR1117994787.html?categoryid=14&cs=1&nid=2565"
148
+ movie = Evri::Entity.find "/product/forgetting-sarah-marshall-0x1e0d1"
149
+ entities = Evri::Entity.from_media(:uri => uri, :text => text)
150
+ assert entities.include?(movie)
143
151
  end
144
152
  end
@@ -0,0 +1,30 @@
1
+ require 'pp'
2
+ require 'test/unit'
3
+ require 'lib/evri'
4
+ require 'mocha'
5
+
6
+ class TestTraverse < Test::Unit::TestCase
7
+ def setup
8
+ @json = JSON.parse(File.read("test/barack.txt"))
9
+ end
10
+
11
+ def test_go
12
+ result = Traverse.new(@json)["entity"]
13
+ assert_equal "/person/barack-obama-0x16f69", result[:href]
14
+ assert_equal "Barack Obama", result[:name]
15
+ assert_equal "PERSON", result["type"]
16
+ assert_equal "Democratic Party", result["properties"]["property"].find { |p| p[:name] == "political_party" }[:value]
17
+ assert result[:facets][:facet].find { |f| f[:name] == "Author" }
18
+ assert result[:properties][:property].size > 20
19
+
20
+ # Get the properties and their values
21
+ result[:properties][:property].map do |property|
22
+ "#{ property[:name] } => #{ property[:value] }"
23
+ end
24
+
25
+ assert_equal result["properties/property"].size, result[:properties][:property].size
26
+
27
+ assert result["does/not/exist"].blank?
28
+ end
29
+ end
30
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joevandyk-evri
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.03"
4
+ version: "0.07"
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-27 00:00:00 -07:00
12
+ date: 2008-11-06 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,7 +37,7 @@ dependencies:
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.7.0
40
+ version: 1.8.2
41
41
  version:
42
42
  description: A beautiful API that wraps the RESTful services provided by evri.com.
43
43
  email:
@@ -50,6 +50,7 @@ extra_rdoc_files:
50
50
  - History.txt
51
51
  - Manifest.txt
52
52
  - README.txt
53
+ - test/barack.txt
53
54
  files:
54
55
  - History.txt
55
56
  - Manifest.txt
@@ -57,14 +58,20 @@ files:
57
58
  - Rakefile
58
59
  - TODO
59
60
  - evri-api.gemspec
61
+ - index.html
60
62
  - lib/evri.rb
61
63
  - lib/evri/entity.rb
62
64
  - lib/evri/media.rb
63
65
  - lib/evri/relation.rb
66
+ - lib/evri/urls.rb
64
67
  - lib/evri/zeitgeist.rb
68
+ - lib/traverse.rb
69
+ - t.rb
70
+ - test/barack.txt
65
71
  - test/test_entity.rb
66
72
  - test/test_evri.rb
67
73
  - test/test_media.rb
74
+ - test/test_traverse.rb
68
75
  - test/test_zeitgeist.rb
69
76
  has_rdoc: true
70
77
  homepage: http://github.com/joevandyk/evri-api
@@ -88,13 +95,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
95
  version:
89
96
  requirements: []
90
97
 
91
- rubyforge_project: evri
98
+ rubyforge_project: evri-api
92
99
  rubygems_version: 1.2.0
93
100
  signing_key:
94
101
  specification_version: 2
95
102
  summary: A beautiful API that wraps the RESTful services provided by evri.com.
96
103
  test_files:
97
- - test/test_media.rb
98
- - test/test_zeitgeist.rb
99
104
  - test/test_entity.rb
100
105
  - test/test_evri.rb
106
+ - test/test_zeitgeist.rb
107
+ - test/test_media.rb
108
+ - test/test_traverse.rb