Skip to content

Commit

Permalink
Add more document coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsl committed May 21, 2009
1 parent d219a70 commit 9b56fcc
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 85 deletions.
2 changes: 2 additions & 0 deletions lib/placemaker.rb
Expand Up @@ -3,6 +3,8 @@
require 'nokogiri'
require 'cgi'

require File.join(File.dirname(__FILE__), %w[placemaker xml_helper])

lib_dirs = [ 'core_ext', 'placemaker' ].map do |d|
File.join(File.dirname(__FILE__), d)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/placemaker/coordinates.rb
@@ -0,0 +1,13 @@
module Placemaker
class Coordinates
include Placemaker::XmlHelper

def lat
@nodeset.search('.//xmlns:latitude', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text.to_f
end

def lng
@nodeset.search('.//xmlns:longitude', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text.to_f
end
end
end
27 changes: 22 additions & 5 deletions lib/placemaker/document.rb
@@ -1,14 +1,31 @@
module Placemaker

class Document
def initialize(nodeset)
@nodeset = nodeset
end
include Placemaker::XmlHelper

def places
# Returns a Placemaker::Location object that is a container for one named place mentioned in the document
def place_details
@nodeset.search('.//xmlns:placeDetails', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').map do |p|
Placemaker::Place.new(p)
Placemaker::PlaceDetails.new(p)
end
end

# Returns a Placemaker::Location object that is a container for the smallest administrative place that best describes the document
def administrative_scope
as = @nodeset.search('.//xmlns:administrativeScope', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').first
Placemaker::Location.new(as) unless as.nil?
end

# Returns a Placemaker::Location object that is a container for the smallest place that best describes the document
def geographic_scope
gs = @nodeset.search('.//xmlns:geographicScope', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').first
Placemaker::Location.new(gs) unless gs.nil?
end

def extents
extents = @nodeset.search('.//xmlns:extents', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').first
Placemaker::Extents.new(extents) unless extents.nil?
end
end

end
23 changes: 23 additions & 0 deletions lib/placemaker/extents.rb
@@ -0,0 +1,23 @@
module Placemaker

# container for the the map extents covering the places mentioned in the document
class Extents
include Placemaker::XmlHelper

# coordinates of center (WGS84)
def center
Placemaker::Coordinates.new(@nodeset.search('.//xmlns:center', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema'))
end

# coordinates of southwest corner of bounding box (WGS84)
def south_west
Placemaker::Coordinates.new(@nodeset.search('.//xmlns:southWest', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema'))
end

# coordinates of northeast corner of bounding box (WGS84)
def north_east
Placemaker::Coordinates.new(@nodeset.search('.//xmlns:northEast', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema'))
end
end

end
33 changes: 33 additions & 0 deletions lib/placemaker/location.rb
@@ -0,0 +1,33 @@
module Placemaker

# Parses and contains data for +place+, +administrative scope+ and +geographic scope+ elements.
class Location
include Placemaker::XmlHelper

# permanent identifier for the place
def woe_id
nested_node('woeId').to_i
end

# fully qualified name for the place
def name
nested_node('name')
end

# place type name for the place
def location_type
nested_node('type')
end

# centroid for the place
def centroid
Placemaker::Coordinates.new(@nodeset.search('.//xmlns:centroid', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema'))
end

private

def nested_node(name)
@nodeset.search(".//xmlns:#{name}", 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text
end
end
end
47 changes: 0 additions & 47 deletions lib/placemaker/place.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/placemaker/place_details.rb
@@ -0,0 +1,28 @@
module Placemaker

# container for one named place mentioned in the document
class PlaceDetails
include Placemaker::XmlHelper

# Returns a Placemaker::Location object as a container for place information.
def place
Placemaker::Location.new(@nodeset.search('.//xmlns:place', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema'))
end

# type of match (0=text or text and coordinates, 1=coordinates only)
def match_type
nested_node('matchType').to_i
end

# relative weight of the place within the document (range 1-100)
def weight
nested_node('weight').to_i
end

# confidence that the document mentions the place (range 1-10)
def confidence
nested_node('confidence').to_i
end

end
end
14 changes: 14 additions & 0 deletions lib/placemaker/xml_helper.rb
@@ -0,0 +1,14 @@
module Placemaker
module XmlHelper
def initialize(nodeset)
@nodeset = nodeset
end

# Returns the inner text of a node with the given name under the current @nodeset
def nested_node(name)
@nodeset.search(".//xmlns:#{name}", 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text
end

private :nested_node
end
end
18 changes: 16 additions & 2 deletions lib/placemaker/xml_parser.rb
Expand Up @@ -3,10 +3,24 @@ class XmlParser
def initialize(xml_body)
@body = xml_body
@xml = Nokogiri::XML(xml_body)
end

# time in seconds to process the document
def processing_time
@xml.xpath('.//xmlns:processingTime', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text.to_f
end

# version of the software used to process the document
def version
@xml.xpath('.//xmlns:version', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text.strip
end

# length in bytes of the document
def document_length
@xml.xpath('.//xmlns:documentLength', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').inner_text.to_i
end

# Returns a set of documents corresponding to the RSS entries in input data.
# Returns a collection of Placemaker::Document objects.
# Returns a set of Placemaker::Document objects as containers for content location information
def documents
@xml.xpath('//xmlns:document', 'xmlns' => 'http://wherein.yahooapis.com/v1/schema').map do |d|
Placemaker::Document.new(d)
Expand Down
2 changes: 1 addition & 1 deletion placemaker.gemspec
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{placemaker}
s.version = "0.0.1.2"
s.version = "0.0.1.3"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Justin Leitgeb"]
Expand Down
14 changes: 14 additions & 0 deletions spec/placemaker/coordinates_spec.rb
@@ -0,0 +1,14 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Placemaker::Coordinates do
before do
@xml_str = File.read(File.join(File.dirname(__FILE__), %w[.. fixtures xml_rss_feed_result.xml]))
@xmlp = Placemaker::XmlParser.new(@xml_str)
@doc = @xmlp.documents[5]
@coords = @doc.place_details.first.place.centroid
end

it "should be a Placemaker::Coordinates object" do
@coords.should be_a(Placemaker::Coordinates)
end
end
28 changes: 24 additions & 4 deletions spec/placemaker/document_spec.rb
Expand Up @@ -6,14 +6,34 @@
@xmlp = Placemaker::XmlParser.new(@xml_str)
@doc = @xmlp.documents[5]
end

describe "#places" do
it "should have 4 places for the given node" do
@doc.places.size.should == 4
@doc.place_details.size.should == 4
end

it "should return a Placemaker::PlaceDetails object for the first node" do
@doc.place_details.first.should be_a(Placemaker::PlaceDetails)
end
end

describe "#administrative_scope" do
it "should have an administrative scope" do
@doc.administrative_scope.should_not be_nil
end

it "should return a Placemaker::Location object" do
@doc.administrative_scope.should be_a(Placemaker::Location)
end
end

describe "#geographic_scope" do
it "should have an geographic scope" do
@doc.geographic_scope.should_not be_nil
end

it "should return a Placemaker::Place object for the first node" do
@doc.places.first.should be_a(Placemaker::Place)
it "should return a Placemaker::Location object" do
@doc.geographic_scope.should be_a(Placemaker::Location)
end
end
end
56 changes: 56 additions & 0 deletions spec/placemaker/extents_spec.rb
@@ -0,0 +1,56 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Placemaker::Extents do
before do
@xml_str = File.read(File.join(File.dirname(__FILE__), %w[.. fixtures xml_rss_feed_result.xml]))
@xmlp = Placemaker::XmlParser.new(@xml_str)
@doc = @xmlp.documents[5]
@extents = @doc.extents
end

it "should be a Placemaker::Extents object" do
@extents.should be_a(Placemaker::Extents)
end

describe "#center" do
it "should return a Placemaker::Point object" do
@extents.center.should be_a(Placemaker::Coordinates)
end

it "should have a lat of 38.8913" do
@extents.center.lat.should == 38.8913
end

it "should have a lng of -77.0337" do
@extents.center.lng.should == -77.0337
end
end

describe "#south_west" do
it "should return a Placemaker::Point object" do
@extents.south_west.should be_a(Placemaker::Coordinates)
end

it "should have a lat of 18.9108" do
@extents.south_west.lat.should == 18.9108
end

it "should have a lng of -167.276" do
@extents.south_west.lng.should == -167.276
end
end

describe "#north_east" do
it "should return a Placemaker::Point object" do
@extents.north_east.should be_a(Placemaker::Coordinates)
end

it "should have a lat of 72.8961" do
@extents.north_east.lat.should == 72.8961
end

it "should have a lng of -66.6879" do
@extents.north_east.lng.should == -66.6879
end
end
end

0 comments on commit 9b56fcc

Please sign in to comment.