Class: Evri::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/evri/entity.rb

Overview

Represents an Evri Entity.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Entity

:nodoc:



96
97
98
99
100
101
102
103
# File 'lib/evri/entity.rb', line 96

def initialize json # :nodoc:
  @parsed_json  = json
  @relations    = []
  @source_url = Evri.source_url
  if json and json["properties"]
    set_properties json["properties"]
  end
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/evri/entity.rb', line 4

def properties
  @properties
end

#source_urlObject (readonly)

Returns the value of attribute source_url.



4
5
6
# File 'lib/evri/entity.rb', line 4

def source_url
  @source_url
end

Class Method Details

.find(id) ⇒ Object

Finds a specific Entity, given an ID. An Entity is a person, company, product, etc.

IDs look like /person/barack-obama-0x16f69 or product/forgetting-sarah-marshall-0x1e0d1

Entity.find "/person/barack-obama-0x16f69"


10
11
12
13
14
15
16
17
18
19
# File 'lib/evri/entity.rb', line 10

def self.find id
  raise "Must provide an id" unless id
  @entended_properties = true
  @results ||= {}
  id = "/" + id 
  return @results[id] if @results[id]
  @results[id] = create_one do 
    Evri.query(:type => :uri, :query => id) 
  end
end

.from_media(options = {}) ⇒ Object

Returns Entities that the given :uri option mentions.

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"

If Evri doesn’t know about the given :uri option, you can pass in a :text option.

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.")
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" 
Evri.from_media :uri => uri, :text => text

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
# File 'lib/evri/entity.rb', line 51

def self.from_media options={}
  uri, text = options[:uri], options[:text]
  raise ArgumentError, "Must specify URI via the :uri option" unless uri
  raise ArgumentError, "Must specify some text via the :text option" unless text

  create_many do
    json = Evri.query(:type => :from_media, :uri => uri, :text => text)
    json["graph/entities/entity"]
  end
end

.search(name) ⇒ Object

Searches for an exact match. It will return an array of Entities.

Entity.search "Barack Obama"
Entity.search "Forgetting Sarah Marshall"


24
25
26
27
28
# File 'lib/evri/entity.rb', line 24

def self.search name
  create_many do
    Evri.query(:type => :search, :query => name)["entities/entity"]
  end
end

.search_by_prefix(prefix) ⇒ Object

Searches for a partial match. i.e. ‘ob’ will return ‘Obama’.

Entity.search_by_prefix("oba")

will likely return an Entity representing Barack Obama.



35
36
37
38
39
# File 'lib/evri/entity.rb', line 35

def self.search_by_prefix prefix
  create_many do
    Evri.query(:type => :prefix, :query => prefix)["entities/entity"]
  end
end

Instance Method Details

#==(b) ⇒ Object

:nodoc:



105
106
107
# File 'lib/evri/entity.rb', line 105

def == b # :nodoc:
  self.id == b.id
end

#articles(options = {}) ⇒ Object

Returns articles relating to the current entity



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/evri/entity.rb', line 189

def articles options={}
  medias = []
  if options[:entities]
    entity_uris = options[:entities].map do |e|
      if e.class == Evri::Entity
        e.uri
      else
        e
      end
    end
  end

  type = options[:type] ? options[:type] : nil
  json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type)
  json["mediaResult/articleList/article"].each do |media_json|
    medias << Article.new(media_json)
  end
  medias
end

#hrefObject Also known as: id, uri

Returns the Evri URI of the entity



110
111
112
# File 'lib/evri/entity.rb', line 110

def href
  @parsed_json[:href] || target_href
end

#images(options = {}) ⇒ Object

Returns images relating to the current entity



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/evri/entity.rb', line 169

def images options={}
  medias = []
  if options[:entities]
    entity_uris = options[:entities].map do |e|
      if e.class == Evri::Entity
        e.uri
      else
        e
      end
    end
  end

  json = Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image')
  json["mediaResult/imageList/image"].each do |media_json|
    medias << Image.new(media_json)
  end
  medias
end

#info(option = nil) ⇒ Object

Returns information about an entity.

obama.info(:birth_date)

If :options is nil, will return all the available information.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/evri/entity.rb', line 68

def info option=nil
  @properties ||= {}
  if defined?(@extended_properties) 
    if option
      @properties[option]
    else
      @properties
    end
  else
    @properties = Entity.find(uri).properties
    @extended_properties = true
    info(option)
  end
end

#inspectObject

:nodoc:



87
88
89
# File 'lib/evri/entity.rb', line 87

def inspect # :nodoc:
  "#<Evri::Entity:#{to_s}>"
end

#nameObject

Returns the name of the entity.



92
93
94
# File 'lib/evri/entity.rb', line 92

def name
  @parsed_json["name"]
end

Returns which entities are related to the current entity.

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).

@obama.related_by(:verb => :kill)      # Returns the entities that are related to Obama by the verb 'kill'
@obama.related_by(:entity => @mccain)  # Returns the entities that are related to Obama by McCain
@obama.related_by(:uri => "http://cnn.com/article/here") # Returns the entities that are related to Obama by the given URI
@obama.related_by(:facet => "politician", :entity => @mccain)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/evri/entity.rb', line 148

def related_by  options={}
  media, verb, verb_value, entity, uri = nil, nil, nil, nil, nil
  options.each do |key, value|
    if key == :entity
      entity = value 
    elsif key == :uri
      uri = value
    elsif key == :type
      media = value
    else
      verb = key
      verb_value = value
    end
  end
  json = Evri.query(:type => :related_by, :media => media, :query => id, :uri => uri, :entity => entity, :verb => verb, :value => verb_value)
  Entity.create_many do
    json["relations/relation/targets/entity"]
  end
end

#relations(options = {}) ⇒ Object

Returns relationships for the entity

obama.relations


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/evri/entity.rb', line 125

def relations options={}
  from_domains = nil
  if options[:from]
    if options[:from].class == String
      from_domains = options[:from]
    else
      from_domains = options[:from].join(',')
    end
  end
  json = Evri.query(:type => :relations, :query => id, :from_domains => from_domains)
  json["relations/relation"].each do |json|
    @relations << Relation.new(json)
  end
  @relations
end

#target_hrefObject

TODO document this



118
119
120
# File 'lib/evri/entity.rb', line 118

def target_href
  @parsed_json[:targetHref]
end

#to_sObject

:nodoc:



83
84
85
# File 'lib/evri/entity.rb', line 83

def to_s # :nodoc:
  "#{name} (#{ id })"
end