joevandyk-evri 0.01
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -0
 - data/Manifest.txt +14 -0
 - data/README.txt +48 -0
 - data/Rakefile +11 -0
 - data/TODO +13 -0
 - data/lib/evri/entity.rb +222 -0
 - data/lib/evri/media.rb +44 -0
 - data/lib/evri/relation.rb +20 -0
 - data/lib/evri/zeitgeist.rb +19 -0
 - data/lib/evri.rb +137 -0
 - data/test/test_entity.rb +144 -0
 - data/test/test_evri.rb +35 -0
 - data/test/test_media.rb +10 -0
 - data/test/test_zeitgeist.rb +19 -0
 - metadata +81 -0
 
    
        data/History.txt
    ADDED
    
    
    
        data/Manifest.txt
    ADDED
    
    
    
        data/README.txt
    ADDED
    
    | 
         @@ -0,0 +1,48 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            = evri-api
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            * http://github.com/joevandyk/evri-api
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            == DESCRIPTION:
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            A beautiful API that wraps the RESTful services provided by evri.com.
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            == FEATURES/PROBLEMS:
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            None yet.
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            == SYNOPSIS:
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            None yet.
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            == REQUIREMENTS:
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            none yet.
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            == INSTALL:
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            gem install evri-api
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            == LICENSE:
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            (The MIT License)
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            Copyright (c) 2008 Joe Van Dyk
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 32 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 33 
     | 
    
         
            +
            'Software'), to deal in the Software without restriction, including
         
     | 
| 
      
 34 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 35 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 36 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 37 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 40 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 43 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 44 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
         
     | 
| 
      
 45 
     | 
    
         
            +
            IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
         
     | 
| 
      
 46 
     | 
    
         
            +
            CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
         
     | 
| 
      
 47 
     | 
    
         
            +
            TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
         
     | 
| 
      
 48 
     | 
    
         
            +
            SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/Rakefile
    ADDED
    
    
    
        data/TODO
    ADDED
    
    | 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            STUFF LEFT TO DO
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            * Handle Query Tokens
         
     | 
| 
      
 4 
     | 
    
         
            +
              - QTs are initially generated here:
         
     | 
| 
      
 5 
     | 
    
         
            +
                - query token for whole medias/entity document
         
     | 
| 
      
 6 
     | 
    
         
            +
                - query token for each entity in the returned results of ^
         
     | 
| 
      
 7 
     | 
    
         
            +
                - query token for each pair of entities in above ^
         
     | 
| 
      
 8 
     | 
    
         
            +
              - Then pass them into the calls that can take them (and handle the returned QTs)
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            * Refactor code to make Resource and JSON handling flexible (after API is done)
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            * Ability to batch requests (via threading)
         
     | 
| 
      
 13 
     | 
    
         
            +
              - see how facebooker does it
         
     | 
    
        data/lib/evri/entity.rb
    ADDED
    
    | 
         @@ -0,0 +1,222 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Evri
         
     | 
| 
      
 2 
     | 
    
         
            +
              # Represents an Evri Entity.
         
     | 
| 
      
 3 
     | 
    
         
            +
              class Entity
         
     | 
| 
      
 4 
     | 
    
         
            +
                attr_reader :properties
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                def self.find id
         
     | 
| 
      
 7 
     | 
    
         
            +
                  @entended_properties = true
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @results ||= {}
         
     | 
| 
      
 9 
     | 
    
         
            +
                  id = "/" + id unless id =~ /\A\//
         
     | 
| 
      
 10 
     | 
    
         
            +
                  return @results[id] if @results[id]
         
     | 
| 
      
 11 
     | 
    
         
            +
                  @results[id] = create_from_json do 
         
     | 
| 
      
 12 
     | 
    
         
            +
                    Evri.query(:type => :uri, :query => id) 
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                def self.search name
         
     | 
| 
      
 17 
     | 
    
         
            +
                  create_from_jsons do
         
     | 
| 
      
 18 
     | 
    
         
            +
                    Evri.query(:type => :search, :query => name)
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                def self.search_by_prefix prefix
         
     | 
| 
      
 23 
     | 
    
         
            +
                  create_from_jsons do
         
     | 
| 
      
 24 
     | 
    
         
            +
                    Evri.query(:type => :prefix, :query => prefix)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
                end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                def self.from_media options={}
         
     | 
| 
      
 29 
     | 
    
         
            +
                  uri, text = options[:uri], options[:text]
         
     | 
| 
      
 30 
     | 
    
         
            +
                  raise ArgumentError, "Must specify URI via the :uri option" unless uri
         
     | 
| 
      
 31 
     | 
    
         
            +
                  raise ArgumentError, "Must specify some text via the :text option" unless text
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  json = Evri.parse_json(Evri.query(:type => :from_media, :uri => uri, :text => text))
         
     | 
| 
      
 34 
     | 
    
         
            +
                  json["graph"]["entities"].map do |e, entity_json|
         
     | 
| 
      
 35 
     | 
    
         
            +
                    Entity.new entity_json
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def source_url
         
     | 
| 
      
 40 
     | 
    
         
            +
                  @source_url
         
     | 
| 
      
 41 
     | 
    
         
            +
                end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                def info option=nil
         
     | 
| 
      
 44 
     | 
    
         
            +
                  @properties ||= {}
         
     | 
| 
      
 45 
     | 
    
         
            +
                  if defined?(@extended_properties) 
         
     | 
| 
      
 46 
     | 
    
         
            +
                    if option
         
     | 
| 
      
 47 
     | 
    
         
            +
                      @properties[option]
         
     | 
| 
      
 48 
     | 
    
         
            +
                    else
         
     | 
| 
      
 49 
     | 
    
         
            +
                      @properties
         
     | 
| 
      
 50 
     | 
    
         
            +
                    end
         
     | 
| 
      
 51 
     | 
    
         
            +
                  else
         
     | 
| 
      
 52 
     | 
    
         
            +
                    @properties = Entity.find(uri).properties
         
     | 
| 
      
 53 
     | 
    
         
            +
                    @extended_properties = true
         
     | 
| 
      
 54 
     | 
    
         
            +
                    info(option)
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
                end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                def to_s
         
     | 
| 
      
 59 
     | 
    
         
            +
                  "#{name} <#{ id }>"
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                def name
         
     | 
| 
      
 63 
     | 
    
         
            +
                  @parsed_json["name"]["$"] || @parsed_json["name"]
         
     | 
| 
      
 64 
     | 
    
         
            +
                end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                def initialize json
         
     | 
| 
      
 67 
     | 
    
         
            +
                  @parsed_json  = json
         
     | 
| 
      
 68 
     | 
    
         
            +
                  @relations    = []
         
     | 
| 
      
 69 
     | 
    
         
            +
                  @source_url = Evri.source_url
         
     | 
| 
      
 70 
     | 
    
         
            +
                  if json["properties"]
         
     | 
| 
      
 71 
     | 
    
         
            +
                    set_properties json["properties"]
         
     | 
| 
      
 72 
     | 
    
         
            +
                  end
         
     | 
| 
      
 73 
     | 
    
         
            +
                end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                def == b
         
     | 
| 
      
 76 
     | 
    
         
            +
                  self.id == b.id
         
     | 
| 
      
 77 
     | 
    
         
            +
                end
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                def href
         
     | 
| 
      
 80 
     | 
    
         
            +
                  @parsed_json["@href"]
         
     | 
| 
      
 81 
     | 
    
         
            +
                end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                alias id href
         
     | 
| 
      
 84 
     | 
    
         
            +
                alias uri href
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                def target_href
         
     | 
| 
      
 87 
     | 
    
         
            +
                  @parsed_json["@targetHref"]
         
     | 
| 
      
 88 
     | 
    
         
            +
                end
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
                def relations options={}
         
     | 
| 
      
 91 
     | 
    
         
            +
                  from_domains = nil
         
     | 
| 
      
 92 
     | 
    
         
            +
                  if options[:from]
         
     | 
| 
      
 93 
     | 
    
         
            +
                    if options[:from].class == String
         
     | 
| 
      
 94 
     | 
    
         
            +
                      from_domains = options[:from]
         
     | 
| 
      
 95 
     | 
    
         
            +
                    else
         
     | 
| 
      
 96 
     | 
    
         
            +
                      from_domains = options[:from].join(',')
         
     | 
| 
      
 97 
     | 
    
         
            +
                    end
         
     | 
| 
      
 98 
     | 
    
         
            +
                  end
         
     | 
| 
      
 99 
     | 
    
         
            +
                  json = Evri.query(:type => :relations, :query => id, :from_domains => from_domains)
         
     | 
| 
      
 100 
     | 
    
         
            +
                  Evri.parse_json(json)["relations"].each do |type, relation_json|
         
     | 
| 
      
 101 
     | 
    
         
            +
                    next if type != 'relation'
         
     | 
| 
      
 102 
     | 
    
         
            +
                    relation_json.each do |r|
         
     | 
| 
      
 103 
     | 
    
         
            +
                      @relations << Relation.new(r)
         
     | 
| 
      
 104 
     | 
    
         
            +
                    end
         
     | 
| 
      
 105 
     | 
    
         
            +
                  end
         
     | 
| 
      
 106 
     | 
    
         
            +
                  @relations
         
     | 
| 
      
 107 
     | 
    
         
            +
                end
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                def related_by  options={}
         
     | 
| 
      
 110 
     | 
    
         
            +
                  media, verb, verb_value, entity, uri = nil, nil, nil, nil, nil
         
     | 
| 
      
 111 
     | 
    
         
            +
                  options.each do |key, value|
         
     | 
| 
      
 112 
     | 
    
         
            +
                    if key == :entity
         
     | 
| 
      
 113 
     | 
    
         
            +
                      entity = value 
         
     | 
| 
      
 114 
     | 
    
         
            +
                    elsif key == :uri
         
     | 
| 
      
 115 
     | 
    
         
            +
                      uri = value
         
     | 
| 
      
 116 
     | 
    
         
            +
                    elsif key == :type
         
     | 
| 
      
 117 
     | 
    
         
            +
                      media = value
         
     | 
| 
      
 118 
     | 
    
         
            +
                    else
         
     | 
| 
      
 119 
     | 
    
         
            +
                      verb = key
         
     | 
| 
      
 120 
     | 
    
         
            +
                      verb_value = value
         
     | 
| 
      
 121 
     | 
    
         
            +
                    end
         
     | 
| 
      
 122 
     | 
    
         
            +
                  end
         
     | 
| 
      
 123 
     | 
    
         
            +
                  @entities = []
         
     | 
| 
      
 124 
     | 
    
         
            +
                  json = Evri.query(:type => :related_by, :media => media, :query => id, :uri => uri, :entity => entity, :verb => verb, :value => verb_value)
         
     | 
| 
      
 125 
     | 
    
         
            +
                  Evri.parse_json(json)["relations"].each do |type, relation_json|
         
     | 
| 
      
 126 
     | 
    
         
            +
                    next if type != 'relation'
         
     | 
| 
      
 127 
     | 
    
         
            +
                    relation_json["targets"].each do |type, r|
         
     | 
| 
      
 128 
     | 
    
         
            +
                      r.each do |entity|
         
     | 
| 
      
 129 
     | 
    
         
            +
                        if entity.class == Hash
         
     | 
| 
      
 130 
     | 
    
         
            +
                          if entity["@href"]
         
     | 
| 
      
 131 
     | 
    
         
            +
                            @entities << Entity.new(entity)
         
     | 
| 
      
 132 
     | 
    
         
            +
                          end
         
     | 
| 
      
 133 
     | 
    
         
            +
                        end
         
     | 
| 
      
 134 
     | 
    
         
            +
                      end
         
     | 
| 
      
 135 
     | 
    
         
            +
                    end
         
     | 
| 
      
 136 
     | 
    
         
            +
                  end
         
     | 
| 
      
 137 
     | 
    
         
            +
                  @entities
         
     | 
| 
      
 138 
     | 
    
         
            +
                end
         
     | 
| 
      
 139 
     | 
    
         
            +
             
     | 
| 
      
 140 
     | 
    
         
            +
                def images options={}
         
     | 
| 
      
 141 
     | 
    
         
            +
                  medias = []
         
     | 
| 
      
 142 
     | 
    
         
            +
                  if options[:entities]
         
     | 
| 
      
 143 
     | 
    
         
            +
                    entity_uris = options[:entities].map do |e|
         
     | 
| 
      
 144 
     | 
    
         
            +
                      if e.class == Evri::Entity
         
     | 
| 
      
 145 
     | 
    
         
            +
                        e.uri
         
     | 
| 
      
 146 
     | 
    
         
            +
                      else
         
     | 
| 
      
 147 
     | 
    
         
            +
                        e
         
     | 
| 
      
 148 
     | 
    
         
            +
                      end
         
     | 
| 
      
 149 
     | 
    
         
            +
                    end
         
     | 
| 
      
 150 
     | 
    
         
            +
                  end
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                  json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => 'image'))
         
     | 
| 
      
 153 
     | 
    
         
            +
                  # TODO handle images here
         
     | 
| 
      
 154 
     | 
    
         
            +
                  if json["mediaResult"]["imageList"]
         
     | 
| 
      
 155 
     | 
    
         
            +
                    json["mediaResult"]["imageList"]["image"].each do |media_json|
         
     | 
| 
      
 156 
     | 
    
         
            +
                      medias << Image.new(media_json)
         
     | 
| 
      
 157 
     | 
    
         
            +
                    end
         
     | 
| 
      
 158 
     | 
    
         
            +
                  end
         
     | 
| 
      
 159 
     | 
    
         
            +
                  medias
         
     | 
| 
      
 160 
     | 
    
         
            +
                end
         
     | 
| 
      
 161 
     | 
    
         
            +
             
     | 
| 
      
 162 
     | 
    
         
            +
                def articles options={}
         
     | 
| 
      
 163 
     | 
    
         
            +
                  medias = []
         
     | 
| 
      
 164 
     | 
    
         
            +
                  if options[:entities]
         
     | 
| 
      
 165 
     | 
    
         
            +
                    entity_uris = options[:entities].map do |e|
         
     | 
| 
      
 166 
     | 
    
         
            +
                      if e.class == Evri::Entity
         
     | 
| 
      
 167 
     | 
    
         
            +
                        e.uri
         
     | 
| 
      
 168 
     | 
    
         
            +
                      else
         
     | 
| 
      
 169 
     | 
    
         
            +
                        e
         
     | 
| 
      
 170 
     | 
    
         
            +
                      end
         
     | 
| 
      
 171 
     | 
    
         
            +
                    end
         
     | 
| 
      
 172 
     | 
    
         
            +
                  end
         
     | 
| 
      
 173 
     | 
    
         
            +
             
     | 
| 
      
 174 
     | 
    
         
            +
                  type = options[:type] ? options[:type] : nil
         
     | 
| 
      
 175 
     | 
    
         
            +
                  json = Evri.parse_json(Evri.query(:type => :related_medias, :query => href, :entities => entity_uris, :media => type))
         
     | 
| 
      
 176 
     | 
    
         
            +
                  if json["mediaResult"]["articleList"]
         
     | 
| 
      
 177 
     | 
    
         
            +
                    json["mediaResult"]["articleList"]["article"].each do |media_json|
         
     | 
| 
      
 178 
     | 
    
         
            +
                      medias << Article.new(media_json)
         
     | 
| 
      
 179 
     | 
    
         
            +
                    end
         
     | 
| 
      
 180 
     | 
    
         
            +
                  end
         
     | 
| 
      
 181 
     | 
    
         
            +
                  medias
         
     | 
| 
      
 182 
     | 
    
         
            +
                end
         
     | 
| 
      
 183 
     | 
    
         
            +
             
     | 
| 
      
 184 
     | 
    
         
            +
             
     | 
| 
      
 185 
     | 
    
         
            +
                private
         
     | 
| 
      
 186 
     | 
    
         
            +
             
     | 
| 
      
 187 
     | 
    
         
            +
                def self.create_from_json json=nil, &block
         
     | 
| 
      
 188 
     | 
    
         
            +
                  begin
         
     | 
| 
      
 189 
     | 
    
         
            +
                    json = block.call if block
         
     | 
| 
      
 190 
     | 
    
         
            +
                    Entity.new Evri.parse_json(json)["entity"]
         
     | 
| 
      
 191 
     | 
    
         
            +
                  rescue Evri::Error => e
         
     | 
| 
      
 192 
     | 
    
         
            +
                    raise Evri::EntityNotFound.new(e.message)
         
     | 
| 
      
 193 
     | 
    
         
            +
                  end
         
     | 
| 
      
 194 
     | 
    
         
            +
                end
         
     | 
| 
      
 195 
     | 
    
         
            +
                
         
     | 
| 
      
 196 
     | 
    
         
            +
                def self.create_from_jsons jsons=nil, &block
         
     | 
| 
      
 197 
     | 
    
         
            +
                  jsons = block.call if block
         
     | 
| 
      
 198 
     | 
    
         
            +
                  result = []
         
     | 
| 
      
 199 
     | 
    
         
            +
                  Evri.parse_json(jsons)["entities"].each  do |type, entity|
         
     | 
| 
      
 200 
     | 
    
         
            +
                    if entity.class == Array
         
     | 
| 
      
 201 
     | 
    
         
            +
                      entity.each do |e|
         
     | 
| 
      
 202 
     | 
    
         
            +
                        result << Entity.new(e)
         
     | 
| 
      
 203 
     | 
    
         
            +
                      end
         
     | 
| 
      
 204 
     | 
    
         
            +
                    else
         
     | 
| 
      
 205 
     | 
    
         
            +
                      result << Entity.new(entity)
         
     | 
| 
      
 206 
     | 
    
         
            +
                    end
         
     | 
| 
      
 207 
     | 
    
         
            +
                  end
         
     | 
| 
      
 208 
     | 
    
         
            +
                  result
         
     | 
| 
      
 209 
     | 
    
         
            +
                end
         
     | 
| 
      
 210 
     | 
    
         
            +
             
     | 
| 
      
 211 
     | 
    
         
            +
                def set_properties json
         
     | 
| 
      
 212 
     | 
    
         
            +
                  @properties = {}
         
     | 
| 
      
 213 
     | 
    
         
            +
                  json["property"].each do |prop|
         
     | 
| 
      
 214 
     | 
    
         
            +
                    if prop.class == Array
         
     | 
| 
      
 215 
     | 
    
         
            +
                      @properties[prop.first.to_sym] = prop.last["$"]
         
     | 
| 
      
 216 
     | 
    
         
            +
                    else
         
     | 
| 
      
 217 
     | 
    
         
            +
                      @properties[prop["name"]["$"].to_sym] = prop["value"]["$"]
         
     | 
| 
      
 218 
     | 
    
         
            +
                    end
         
     | 
| 
      
 219 
     | 
    
         
            +
                  end
         
     | 
| 
      
 220 
     | 
    
         
            +
                end
         
     | 
| 
      
 221 
     | 
    
         
            +
              end
         
     | 
| 
      
 222 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/evri/media.rb
    ADDED
    
    | 
         @@ -0,0 +1,44 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Evri
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Media
         
     | 
| 
      
 3 
     | 
    
         
            +
              end
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              class Article < Media
         
     | 
| 
      
 6 
     | 
    
         
            +
                attr_accessor :title, :href, :uri, :author, :published_at, :content
         
     | 
| 
      
 7 
     | 
    
         
            +
                def initialize json
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @title        = json["title"]["$"]
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @author       = json["author"]["$"]
         
     | 
| 
      
 10 
     | 
    
         
            +
                  @content      = json["content"] ? json["content"]["$"] : "No content provided" 
         
     | 
| 
      
 11 
     | 
    
         
            +
                  @published_at = json["published"]["$"]
         
     | 
| 
      
 12 
     | 
    
         
            +
                  @href         = Evri.api_host + json["link"]["@href"]
         
     | 
| 
      
 13 
     | 
    
         
            +
                  @uri          = json["link"]["@hostName"] + json["link"]["@path"]
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
              class Image < Media
         
     | 
| 
      
 18 
     | 
    
         
            +
                attr_accessor :size, :title, :article_href, :mime_type, :thumbnail, :date, :content, :width, :url, :click_url, :height
         
     | 
| 
      
 19 
     | 
    
         
            +
                def initialize json
         
     | 
| 
      
 20 
     | 
    
         
            +
                  @title    = json["title"]
         
     | 
| 
      
 21 
     | 
    
         
            +
                  @width    = json["@width"]
         
     | 
| 
      
 22 
     | 
    
         
            +
                  @height   = json["@height"]
         
     | 
| 
      
 23 
     | 
    
         
            +
                  @url      = json["@url"]
         
     | 
| 
      
 24 
     | 
    
         
            +
                  @size      = json["size"]
         
     | 
| 
      
 25 
     | 
    
         
            +
                  @title      = json["title"]
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @article_href      = json["articleHref"]["$"]
         
     | 
| 
      
 27 
     | 
    
         
            +
                  @mime_type      = json["mimeType"]
         
     | 
| 
      
 28 
     | 
    
         
            +
                  @date      = Date.parse(json["date"]["$"])
         
     | 
| 
      
 29 
     | 
    
         
            +
                  @content  = json["content"]["$"]
         
     | 
| 
      
 30 
     | 
    
         
            +
                  @click_url  = json["clickUrl"]["$"]
         
     | 
| 
      
 31 
     | 
    
         
            +
                  @thumbnail = Thumbnail.new(json["thumbnail"])
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                class Thumbnail
         
     | 
| 
      
 35 
     | 
    
         
            +
                  attr_accessor :size, :width, :height, :url
         
     | 
| 
      
 36 
     | 
    
         
            +
                  def initialize json
         
     | 
| 
      
 37 
     | 
    
         
            +
                    @size    = json["size"]
         
     | 
| 
      
 38 
     | 
    
         
            +
                    @width    = json["@width"]
         
     | 
| 
      
 39 
     | 
    
         
            +
                    @height   = json["@height"]
         
     | 
| 
      
 40 
     | 
    
         
            +
                    @url      = json["@url"]
         
     | 
| 
      
 41 
     | 
    
         
            +
                  end
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
              end
         
     | 
| 
      
 44 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Evri
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Relation
         
     | 
| 
      
 3 
     | 
    
         
            +
                attr_accessor :name, :href, :type
         
     | 
| 
      
 4 
     | 
    
         
            +
                def initialize json
         
     | 
| 
      
 5 
     | 
    
         
            +
                  @name = json["name"]["$"]
         
     | 
| 
      
 6 
     | 
    
         
            +
                  @href = json["@href"]
         
     | 
| 
      
 7 
     | 
    
         
            +
                  @type = json["type"]["$"]
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @entities = []
         
     | 
| 
      
 9 
     | 
    
         
            +
                end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                def entities
         
     | 
| 
      
 12 
     | 
    
         
            +
                  return @entities unless @entities.empty?
         
     | 
| 
      
 13 
     | 
    
         
            +
                  json = JSON.parse(Evri.query(:type => :uri, :query => @href))
         
     | 
| 
      
 14 
     | 
    
         
            +
                  json["relations"]["relation"]["targets"]["entity"].each do |entity_json|
         
     | 
| 
      
 15 
     | 
    
         
            +
                    @entities << Entity.new(entity_json)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @entities
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Evri
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Zeitgeist
         
     | 
| 
      
 3 
     | 
    
         
            +
                ENTITY_TYPES = %w( animal backterium checmical concept disorder event location organization person plant product virus )
         
     | 
| 
      
 4 
     | 
    
         
            +
                TYPES        = %w( all popular rising falling )
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                TYPES.each do |t|
         
     | 
| 
      
 7 
     | 
    
         
            +
                  ENTITY_TYPES.each do |et|
         
     | 
| 
      
 8 
     | 
    
         
            +
                    eval %(
         
     | 
| 
      
 9 
     | 
    
         
            +
                      def self.#{t}_#{et}
         
     | 
| 
      
 10 
     | 
    
         
            +
                        json =  Evri.parse_json(Evri.query :type => :zeitgeist, :query => "#{et}/#{t}")
         
     | 
| 
      
 11 
     | 
    
         
            +
                        json["zeitgeist"]["#{t}"]["entities"]["entity"].map do |entity_json|
         
     | 
| 
      
 12 
     | 
    
         
            +
                          Entity.new(entity_json)
         
     | 
| 
      
 13 
     | 
    
         
            +
                        end
         
     | 
| 
      
 14 
     | 
    
         
            +
                      end
         
     | 
| 
      
 15 
     | 
    
         
            +
                    )
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/evri.rb
    ADDED
    
    | 
         @@ -0,0 +1,137 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            $: << File.dirname(__FILE__) 
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'uri'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'net/http'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'cgi'
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            begin
         
     | 
| 
      
 8 
     | 
    
         
            +
              require 'rubygems' 
         
     | 
| 
      
 9 
     | 
    
         
            +
            rescue LoadError 
         
     | 
| 
      
 10 
     | 
    
         
            +
              nil
         
     | 
| 
      
 11 
     | 
    
         
            +
            end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            require 'json'
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            for file in Dir[File.join(File.dirname(__FILE__), 'evri', '*')]
         
     | 
| 
      
 16 
     | 
    
         
            +
              require file
         
     | 
| 
      
 17 
     | 
    
         
            +
            end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            module Evri
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              # A general error.
         
     | 
| 
      
 22 
     | 
    
         
            +
              class Error < Exception; end
         
     | 
| 
      
 23 
     | 
    
         
            +
              
         
     | 
| 
      
 24 
     | 
    
         
            +
              # Raised whenever the entity you are searching for cannot be found.
         
     | 
| 
      
 25 
     | 
    
         
            +
              class EntityNotFound < Error; end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
              VERSION     = "0.01"
         
     | 
| 
      
 28 
     | 
    
         
            +
              @@api_host    = "api.evri.com"
         
     | 
| 
      
 29 
     | 
    
         
            +
              @@source_host  = @@api_host
         
     | 
| 
      
 30 
     | 
    
         
            +
              @@source_url   = nil
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
              def self.api_host= host
         
     | 
| 
      
 33 
     | 
    
         
            +
                @@api_host = validate_host(host)
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              def self.api_host
         
     | 
| 
      
 37 
     | 
    
         
            +
                @@api_host
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              def self.source_host
         
     | 
| 
      
 41 
     | 
    
         
            +
                @@source_host
         
     | 
| 
      
 42 
     | 
    
         
            +
              end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
              def self.source_host= host
         
     | 
| 
      
 45 
     | 
    
         
            +
                @@source_host = validate_host(host)
         
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
              def self.source_url
         
     | 
| 
      
 49 
     | 
    
         
            +
                @@source_url
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
              def self.parse_json json
         
     | 
| 
      
 53 
     | 
    
         
            +
                begin
         
     | 
| 
      
 54 
     | 
    
         
            +
                  JSON.parse(json)
         
     | 
| 
      
 55 
     | 
    
         
            +
                rescue JSON::ParserError => e
         
     | 
| 
      
 56 
     | 
    
         
            +
                  # puts "Error!"
         
     | 
| 
      
 57 
     | 
    
         
            +
                  # puts e.message
         
     | 
| 
      
 58 
     | 
    
         
            +
                  # puts json
         
     | 
| 
      
 59 
     | 
    
         
            +
                  raise Error.new(e)
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
              # TODO Rewrite
         
     | 
| 
      
 64 
     | 
    
         
            +
              def self.query options={}
         
     | 
| 
      
 65 
     | 
    
         
            +
                query = ""
         
     | 
| 
      
 66 
     | 
    
         
            +
                case options[:type]
         
     | 
| 
      
 67 
     | 
    
         
            +
                when :uri
         
     | 
| 
      
 68 
     | 
    
         
            +
                  path = options[:query]
         
     | 
| 
      
 69 
     | 
    
         
            +
                when :relations
         
     | 
| 
      
 70 
     | 
    
         
            +
                  path = options[:query] + "/relations"
         
     | 
| 
      
 71 
     | 
    
         
            +
                  if options[:from_domains]
         
     | 
| 
      
 72 
     | 
    
         
            +
                    query = "includeDomain=#{options[:from_domains]}"
         
     | 
| 
      
 73 
     | 
    
         
            +
                  end
         
     | 
| 
      
 74 
     | 
    
         
            +
                when :related_by
         
     | 
| 
      
 75 
     | 
    
         
            +
                  if options[:uri]
         
     | 
| 
      
 76 
     | 
    
         
            +
                    path = options[:query] + "/related/entities"
         
     | 
| 
      
 77 
     | 
    
         
            +
                    query = "uri=#{escape(options[:uri])}"
         
     | 
| 
      
 78 
     | 
    
         
            +
                  else
         
     | 
| 
      
 79 
     | 
    
         
            +
                    path = options[:query] + "/relations"
         
     | 
| 
      
 80 
     | 
    
         
            +
                    if options[:verb]
         
     | 
| 
      
 81 
     | 
    
         
            +
                      path += "/#{options[:verb]}/#{escape(options[:value])}"
         
     | 
| 
      
 82 
     | 
    
         
            +
                      if options[:entity]
         
     | 
| 
      
 83 
     | 
    
         
            +
                        path += options[:entity].href
         
     | 
| 
      
 84 
     | 
    
         
            +
                      end
         
     | 
| 
      
 85 
     | 
    
         
            +
                    end
         
     | 
| 
      
 86 
     | 
    
         
            +
                    if options[:media]
         
     | 
| 
      
 87 
     | 
    
         
            +
                      query += "media=#{options[:media]}"
         
     | 
| 
      
 88 
     | 
    
         
            +
                    end
         
     | 
| 
      
 89 
     | 
    
         
            +
                  end
         
     | 
| 
      
 90 
     | 
    
         
            +
                when :zeitgeist
         
     | 
| 
      
 91 
     | 
    
         
            +
                  path = "/zeitgeist/entities/" + options[:query]
         
     | 
| 
      
 92 
     | 
    
         
            +
                when :related_medias
         
     | 
| 
      
 93 
     | 
    
         
            +
                  path = options[:query] + "/media/related"
         
     | 
| 
      
 94 
     | 
    
         
            +
                  if options[:entities]
         
     | 
| 
      
 95 
     | 
    
         
            +
                    query = options[:entities].map do |e|
         
     | 
| 
      
 96 
     | 
    
         
            +
                      "entityURI=#{e}&"
         
     | 
| 
      
 97 
     | 
    
         
            +
                    end
         
     | 
| 
      
 98 
     | 
    
         
            +
                    query = query.join
         
     | 
| 
      
 99 
     | 
    
         
            +
                  end
         
     | 
| 
      
 100 
     | 
    
         
            +
                  query += "type=#{options[:media]}" if options[:media]
         
     | 
| 
      
 101 
     | 
    
         
            +
                when :from_media
         
     | 
| 
      
 102 
     | 
    
         
            +
                  path = "/media/entities"
         
     | 
| 
      
 103 
     | 
    
         
            +
                  query = "uri=#{escape(options[:uri])}&text=#{escape(options[:text])}"
         
     | 
| 
      
 104 
     | 
    
         
            +
                when :search
         
     | 
| 
      
 105 
     | 
    
         
            +
                  path  = "/entities/find"
         
     | 
| 
      
 106 
     | 
    
         
            +
                  query = "name=#{escape(options[:query])}"
         
     | 
| 
      
 107 
     | 
    
         
            +
                when :prefix
         
     | 
| 
      
 108 
     | 
    
         
            +
                  path  = "/entities/find"
         
     | 
| 
      
 109 
     | 
    
         
            +
                  query = "prefix=#{escape(options[:query])}"
         
     | 
| 
      
 110 
     | 
    
         
            +
                else
         
     | 
| 
      
 111 
     | 
    
         
            +
                  raise ArgumentError, "unexpected type #{ options[:type] }"
         
     | 
| 
      
 112 
     | 
    
         
            +
                end
         
     | 
| 
      
 113 
     | 
    
         
            +
                query = nil if query.empty?
         
     | 
| 
      
 114 
     | 
    
         
            +
                uri   = URI::HTTP.build :host => self.api_host, :path => path + '.json', :query => query
         
     | 
| 
      
 115 
     | 
    
         
            +
                # puts "getting #{ uri }"
         
     | 
| 
      
 116 
     | 
    
         
            +
                response = Net::HTTP.get_response(uri)
         
     | 
| 
      
 117 
     | 
    
         
            +
                raise Error.new("unexpected http response: #{ response.code }") unless response.code == "200"
         
     | 
| 
      
 118 
     | 
    
         
            +
                @@source_url = @@source_host + uri.request_uri
         
     | 
| 
      
 119 
     | 
    
         
            +
                response.body
         
     | 
| 
      
 120 
     | 
    
         
            +
              end
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
              def self.escape text
         
     | 
| 
      
 123 
     | 
    
         
            +
                text ? CGI.escape(text.to_s) : ''
         
     | 
| 
      
 124 
     | 
    
         
            +
              end
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
              private
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
      
 128 
     | 
    
         
            +
              def self.validate_host host
         
     | 
| 
      
 129 
     | 
    
         
            +
                host = host.to_s
         
     | 
| 
      
 130 
     | 
    
         
            +
                begin
         
     | 
| 
      
 131 
     | 
    
         
            +
                  URI::HTTP.build(:host => host)
         
     | 
| 
      
 132 
     | 
    
         
            +
                  host
         
     | 
| 
      
 133 
     | 
    
         
            +
                rescue URI::InvalidComponentError
         
     | 
| 
      
 134 
     | 
    
         
            +
                  raise ArgumentError.new("Invalid host name specified (#{host}).  Use something like 'api.evri.com'")
         
     | 
| 
      
 135 
     | 
    
         
            +
                end
         
     | 
| 
      
 136 
     | 
    
         
            +
              end
         
     | 
| 
      
 137 
     | 
    
         
            +
            end
         
     | 
    
        data/test/test_entity.rb
    ADDED
    
    | 
         @@ -0,0 +1,144 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'test/unit'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'lib/evri'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'mocha'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            class TestEntity < Test::Unit::TestCase
         
     | 
| 
      
 6 
     | 
    
         
            +
              BARACK_ID   = "/person/barack-obama-0x16f69"
         
     | 
| 
      
 7 
     | 
    
         
            +
              MICHELLE_ID = "person/michelle-obama-0x4c6e8" # Works without leading slash
         
     | 
| 
      
 8 
     | 
    
         
            +
              MCCAIN_ID   = "/person/john-mccain-0x2a2a7"
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 11 
     | 
    
         
            +
                @obama    = Evri::Entity.find BARACK_ID
         
     | 
| 
      
 12 
     | 
    
         
            +
                @michelle = Evri::Entity.find MICHELLE_ID
         
     | 
| 
      
 13 
     | 
    
         
            +
                @mccain   = Evri::Entity.find MCCAIN_ID
         
     | 
| 
      
 14 
     | 
    
         
            +
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              def test_failed_http_get_raises_good_error
         
     | 
| 
      
 17 
     | 
    
         
            +
                bad_response = stub(:code => "500")
         
     | 
| 
      
 18 
     | 
    
         
            +
                Net::HTTP.expects(:get_response).returns(bad_response)
         
     | 
| 
      
 19 
     | 
    
         
            +
                assert_raises(Evri::Error) { @obama.relations }
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
              # Ensure that when we search for Barack, we get the @obama and @michelle objects returned
         
     | 
| 
      
 23 
     | 
    
         
            +
              def test_class_find
         
     | 
| 
      
 24 
     | 
    
         
            +
                results = Evri::Entity.search "obama"
         
     | 
| 
      
 25 
     | 
    
         
            +
                [@obama, @michelle].each do |person|
         
     | 
| 
      
 26 
     | 
    
         
            +
                  assert results.include?(person), "Expected search to contain #{ person }"
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
              def test_find_raises_for_missing_ids
         
     | 
| 
      
 31 
     | 
    
         
            +
                assert_raises(Evri::EntityNotFound) { Evri::Entity.find "crap" }
         
     | 
| 
      
 32 
     | 
    
         
            +
              end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
              def test_failed_search_returns_empty_array
         
     | 
| 
      
 35 
     | 
    
         
            +
                assert Evri::Entity.search("asdfasdfasdfasdfjlskdjflkasdjfkljasdklfjkladsjfklajsdl;kfjlaksdjfadklsjfkladsjlfkjasdfkljas#").empty?
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
              def test_class_find_with_prefix
         
     | 
| 
      
 39 
     | 
    
         
            +
                results = Evri::Entity.search_by_prefix "ob"
         
     | 
| 
      
 40 
     | 
    
         
            +
                assert results.include?(@obama), "Expected search to contain #{ @obama }"
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
              def test_name
         
     | 
| 
      
 44 
     | 
    
         
            +
                assert_equal @obama.name, "Barack Obama"
         
     | 
| 
      
 45 
     | 
    
         
            +
              end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
              def test_id
         
     | 
| 
      
 48 
     | 
    
         
            +
                assert_equal @obama.id, BARACK_ID
         
     | 
| 
      
 49 
     | 
    
         
            +
              end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
              def test_properties
         
     | 
| 
      
 52 
     | 
    
         
            +
                vandyk_parks = Evri::Entity.find "/person/van-dyke-parks-0x16b10"
         
     | 
| 
      
 53 
     | 
    
         
            +
                assert vandyk_parks.info(:occupation).include?("Composer")
         
     | 
| 
      
 54 
     | 
    
         
            +
              end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
              def test_properties_from_search
         
     | 
| 
      
 57 
     | 
    
         
            +
                vandyk_parks = Evri::Entity.search_by_prefix("van dyke parks").first
         
     | 
| 
      
 58 
     | 
    
         
            +
                assert vandyk_parks.info(:occupation).include?("Composer")
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
              def test_properties_from_related_by
         
     | 
| 
      
 62 
     | 
    
         
            +
                loaded_entity = @obama.related_by(:verb => :kill).first
         
     | 
| 
      
 63 
     | 
    
         
            +
                assert loaded_entity.info(:name) 
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
              def test_relations
         
     | 
| 
      
 67 
     | 
    
         
            +
                assert @obama.relations.find { |r| r.name == "US Politician" }
         
     | 
| 
      
 68 
     | 
    
         
            +
                assert @obama.relations.find { |r| r.href == "#{@obama.href}/relations/facet/politician" }
         
     | 
| 
      
 69 
     | 
    
         
            +
              end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
              def test_related_by_from_uri
         
     | 
| 
      
 72 
     | 
    
         
            +
                # Sends right data, but bug in API prevents it from knowing about the URI
         
     | 
| 
      
 73 
     | 
    
         
            +
                # assert !@obama.related_by(:uri => "www.boston.com/news/nation/articles/2008/10/23/obama_takes_campaign_break_to_visit_ill_grandmother/?rss_id=Boston.com+--+Latest+news")
         
     | 
| 
      
 74 
     | 
    
         
            +
              end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
              def test_relations_from_domains
         
     | 
| 
      
 77 
     | 
    
         
            +
                assert !@obama.relations(:from => "time.com").empty?
         
     | 
| 
      
 78 
     | 
    
         
            +
                assert !@obama.relations(:from => ["time.com", "nytimes.com"]).empty?
         
     | 
| 
      
 79 
     | 
    
         
            +
              end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
              def test_relations_with_type
         
     | 
| 
      
 82 
     | 
    
         
            +
                # TODO tests
         
     | 
| 
      
 83 
     | 
    
         
            +
                @obama.related_by(:verb => :kill, :type => :image)
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
              def test_relations_related_to
         
     | 
| 
      
 87 
     | 
    
         
            +
                # TODO tests
         
     | 
| 
      
 88 
     | 
    
         
            +
                @obama.related_by(:facet => :musical_artist)
         
     | 
| 
      
 89 
     | 
    
         
            +
                @obama.related_by(:qt => "joined-by-0x78")
         
     | 
| 
      
 90 
     | 
    
         
            +
                @mccain.related_by(:verb => :kill, :entity => @obama)
         
     | 
| 
      
 91 
     | 
    
         
            +
                @obama.related_by(:facet => :politician, :entity => @mccain)
         
     | 
| 
      
 92 
     | 
    
         
            +
              end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
              def test_source_url
         
     | 
| 
      
 95 
     | 
    
         
            +
                dude = @obama.related_by(:facet => :politician).first
         
     | 
| 
      
 96 
     | 
    
         
            +
                assert_equal "#{Evri.source_host}#{@obama.uri}/relations/facet/politician.json", dude.source_url
         
     | 
| 
      
 97 
     | 
    
         
            +
              end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
              def test_media_related_to_entities_about_an_entity
         
     | 
| 
      
 100 
     | 
    
         
            +
                # articles takes either entities or entity URIs
         
     | 
| 
      
 101 
     | 
    
         
            +
                # NOTE: the plural of 'media' is 'media'
         
     | 
| 
      
 102 
     | 
    
         
            +
                medias = @obama.articles(:entities => [@mccain, "/person/bill-ayers-0x27a65"])
         
     | 
| 
      
 103 
     | 
    
         
            +
                assert medias.size > 1
         
     | 
| 
      
 104 
     | 
    
         
            +
              end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
              def test_medias_with_type_of_image
         
     | 
| 
      
 107 
     | 
    
         
            +
                image = @obama.images.first
         
     | 
| 
      
 108 
     | 
    
         
            +
                %w( size title article_href mime_type thumbnail date content width url click_url height).each do |field|
         
     | 
| 
      
 109 
     | 
    
         
            +
                  assert image.send(field), "Expected #{ image.inspect }'s #{ field } field to be larger than zero"
         
     | 
| 
      
 110 
     | 
    
         
            +
                end
         
     | 
| 
      
 111 
     | 
    
         
            +
                %w( size width height url).each do |field|
         
     | 
| 
      
 112 
     | 
    
         
            +
                  assert image.thumbnail.send(field), "Expected #{ image.thumbnail.inspect }'s #{ field } field to be larger than zero"
         
     | 
| 
      
 113 
     | 
    
         
            +
                end
         
     | 
| 
      
 114 
     | 
    
         
            +
              end
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
              def test_relations_loading
         
     | 
| 
      
 117 
     | 
    
         
            +
                politician_relation = @obama.relations.find { |r| r.name == "US Politician" }
         
     | 
| 
      
 118 
     | 
    
         
            +
                assert politician_relation.entities.include?(@mccain)
         
     | 
| 
      
 119 
     | 
    
         
            +
              end
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
              def test_relations_with_verb
         
     | 
| 
      
 122 
     | 
    
         
            +
                entities = @obama.related_by(:verb => :attack)
         
     | 
| 
      
 123 
     | 
    
         
            +
                assert entities.first.target_href.include?("attack")
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                assert @obama.related_by(:qt => "joined-by-0x78").find { |e| e.name == "Joe Biden" }
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                assert  @obama.related_by(:facet => :politician).include?(@mccain)
         
     | 
| 
      
 128 
     | 
    
         
            +
              end
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
              def test_media
         
     | 
| 
      
 131 
     | 
    
         
            +
                media = @obama.articles.first
         
     | 
| 
      
 132 
     | 
    
         
            +
                # check for media.title, media.uri, etc
         
     | 
| 
      
 133 
     | 
    
         
            +
                %w( title author published_at content href uri).each do |field|
         
     | 
| 
      
 134 
     | 
    
         
            +
                  assert media.send(field).size > 0, "Expected #{ media.inspect }'s #{ field } field to be larger than zero"
         
     | 
| 
      
 135 
     | 
    
         
            +
                end
         
     | 
| 
      
 136 
     | 
    
         
            +
              end
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
              def test_find_by_media
         
     | 
| 
      
 139 
     | 
    
         
            +
                uri = "http://www.reuters.com/article/industryNews/idUSTRE4981RO20081009"
         
     | 
| 
      
 140 
     | 
    
         
            +
                dreamworks = Evri::Entity.find "/organization/dreamworks-0x3c510"
         
     | 
| 
      
 141 
     | 
    
         
            +
                entities = Evri::Entity.from_media(:uri => uri, :text => "Dreamworks")
         
     | 
| 
      
 142 
     | 
    
         
            +
                assert entities.include?(dreamworks)
         
     | 
| 
      
 143 
     | 
    
         
            +
              end
         
     | 
| 
      
 144 
     | 
    
         
            +
            end
         
     | 
    
        data/test/test_evri.rb
    ADDED
    
    | 
         @@ -0,0 +1,35 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'test/unit'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'lib/evri'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'mocha'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            class TestModuleEvri < Test::Unit::TestCase
         
     | 
| 
      
 6 
     | 
    
         
            +
              def test_setting_invalid_new_api_host
         
     | 
| 
      
 7 
     | 
    
         
            +
                remember_original_settings(:api_host) do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  assert_raise(ArgumentError) { Evri.api_host = "http://slashdot.org" }
         
     | 
| 
      
 9 
     | 
    
         
            +
                  assert_raise(ArgumentError) { Evri.api_host = "Joe's Cat" }
         
     | 
| 
      
 10 
     | 
    
         
            +
                  assert_raise(ArgumentError) { Evri.api_host = 3 }
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
              def test_setting_new_api_host
         
     | 
| 
      
 15 
     | 
    
         
            +
                remember_original_settings(:api_host) do
         
     | 
| 
      
 16 
     | 
    
         
            +
                  Evri.api_host = "new.api.evri.com"
         
     | 
| 
      
 17 
     | 
    
         
            +
                  assert_equal Evri.api_host, "new.api.evri.com"
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              def test_source_host
         
     | 
| 
      
 22 
     | 
    
         
            +
                remember_original_settings(:source_host) do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  Evri.source_host = "new.api.evri.com"
         
     | 
| 
      
 24 
     | 
    
         
            +
                  assert_equal Evri.source_host, "new.api.evri.com"
         
     | 
| 
      
 25 
     | 
    
         
            +
                end
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              private
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
              def remember_original_settings method
         
     | 
| 
      
 31 
     | 
    
         
            +
                original = Evri.send method
         
     | 
| 
      
 32 
     | 
    
         
            +
                yield ensure Evri.send"#{method}=", original
         
     | 
| 
      
 33 
     | 
    
         
            +
              end
         
     | 
| 
      
 34 
     | 
    
         
            +
            end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
    
        data/test/test_media.rb
    ADDED
    
    
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'test/unit'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'lib/evri'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            class TestZeitgeist < Test::Unit::TestCase
         
     | 
| 
      
 5 
     | 
    
         
            +
              BARACK_ID   = "person/barack-obama-0x16f69"
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 8 
     | 
    
         
            +
                @obama    = Evri::Entity.find BARACK_ID
         
     | 
| 
      
 9 
     | 
    
         
            +
              end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              def test_popular_person
         
     | 
| 
      
 12 
     | 
    
         
            +
                assert Evri::Zeitgeist.popular_person.include?(@obama)
         
     | 
| 
      
 13 
     | 
    
         
            +
              end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              def test_falling_product
         
     | 
| 
      
 16 
     | 
    
         
            +
                # just check to see that the first falling product's name is not blank.
         
     | 
| 
      
 17 
     | 
    
         
            +
                assert Evri::Zeitgeist.falling_product.first.name.size > 0
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,81 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: joevandyk-evri
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: "0.01"
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 7 
     | 
    
         
            +
            - Joe Van Dyk
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2008-10-27 00:00:00 -07:00
         
     | 
| 
      
 13 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 14 
     | 
    
         
            +
            dependencies: 
         
     | 
| 
      
 15 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 16 
     | 
    
         
            +
              name: hoe
         
     | 
| 
      
 17 
     | 
    
         
            +
              version_requirement: 
         
     | 
| 
      
 18 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 19 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 20 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 21 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 22 
     | 
    
         
            +
                    version: 1.7.0
         
     | 
| 
      
 23 
     | 
    
         
            +
                version: 
         
     | 
| 
      
 24 
     | 
    
         
            +
            description: A beautiful API that wraps the RESTful services provided by evri.com.
         
     | 
| 
      
 25 
     | 
    
         
            +
            email: 
         
     | 
| 
      
 26 
     | 
    
         
            +
            - joe@@fixieconsulting.com
         
     | 
| 
      
 27 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            extra_rdoc_files: 
         
     | 
| 
      
 32 
     | 
    
         
            +
            - History.txt
         
     | 
| 
      
 33 
     | 
    
         
            +
            - Manifest.txt
         
     | 
| 
      
 34 
     | 
    
         
            +
            - README.txt
         
     | 
| 
      
 35 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 36 
     | 
    
         
            +
            - History.txt
         
     | 
| 
      
 37 
     | 
    
         
            +
            - Manifest.txt
         
     | 
| 
      
 38 
     | 
    
         
            +
            - README.txt
         
     | 
| 
      
 39 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 40 
     | 
    
         
            +
            - TODO
         
     | 
| 
      
 41 
     | 
    
         
            +
            - lib/evri.rb
         
     | 
| 
      
 42 
     | 
    
         
            +
            - lib/evri/entity.rb
         
     | 
| 
      
 43 
     | 
    
         
            +
            - lib/evri/media.rb
         
     | 
| 
      
 44 
     | 
    
         
            +
            - lib/evri/relation.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            - lib/evri/zeitgeist.rb
         
     | 
| 
      
 46 
     | 
    
         
            +
            - test/test_entity.rb
         
     | 
| 
      
 47 
     | 
    
         
            +
            - test/test_evri.rb
         
     | 
| 
      
 48 
     | 
    
         
            +
            - test/test_media.rb
         
     | 
| 
      
 49 
     | 
    
         
            +
            - test/test_zeitgeist.rb
         
     | 
| 
      
 50 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 51 
     | 
    
         
            +
            homepage: http://github.com/joevandyk/evri-api
         
     | 
| 
      
 52 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 53 
     | 
    
         
            +
            rdoc_options: 
         
     | 
| 
      
 54 
     | 
    
         
            +
            - --main
         
     | 
| 
      
 55 
     | 
    
         
            +
            - README.txt
         
     | 
| 
      
 56 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 57 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 58 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 59 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 60 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 61 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 62 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 63 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 64 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 65 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 66 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 67 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 68 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 69 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 70 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            rubyforge_project: evri
         
     | 
| 
      
 73 
     | 
    
         
            +
            rubygems_version: 1.2.0
         
     | 
| 
      
 74 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 75 
     | 
    
         
            +
            specification_version: 2
         
     | 
| 
      
 76 
     | 
    
         
            +
            summary: A beautiful API that wraps the RESTful services provided by evri.com.
         
     | 
| 
      
 77 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 78 
     | 
    
         
            +
            - test/test_media.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
            - test/test_zeitgeist.rb
         
     | 
| 
      
 80 
     | 
    
         
            +
            - test/test_entity.rb
         
     | 
| 
      
 81 
     | 
    
         
            +
            - test/test_evri.rb
         
     |