Skip to content

Commit

Permalink
Move Point and Polygon classes under Geo namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 18, 2012
1 parent 3365f8a commit 059cc55
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 35 deletions.
26 changes: 14 additions & 12 deletions lib/twitter/geo/point.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
require 'twitter/geo'

module Twitter
class Point < Twitter::Geo
class Geo
class Point < Twitter::Geo

# @return [Integer]
def latitude
self.coordinates[0]
end
alias lat latitude
# @return [Integer]
def latitude
self.coordinates[0]
end
alias lat latitude

# @return [Integer]
def longitude
self.coordinates[1]
end
alias long longitude
alias lng longitude
# @return [Integer]
def longitude
self.coordinates[1]
end
alias long longitude
alias lng longitude

end
end
end
4 changes: 3 additions & 1 deletion lib/twitter/geo/polygon.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'twitter/geo'

module Twitter
class Polygon < Twitter::Geo
class Geo
class Polygon < Twitter::Geo
end
end
end
4 changes: 2 additions & 2 deletions lib/twitter/geo_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class GeoFactory
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
# @return [Twitter::Point, Twitter::Polygon]
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def self.fetch_or_new(attrs={})
if type = attrs.delete(:type)
Twitter.const_get(type.camelize.to_sym).fetch_or_new(attrs)
Twitter::Geo.const_get(type.camelize.to_sym).fetch_or_new(attrs)
else
raise ArgumentError, "argument must have a :type key"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Place < Twitter::Identity
attr_reader :attributes, :country, :full_name, :name, :url, :woeid
alias woe_id woeid

# @return [Twitter::Point, Twitter::Polygon]
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def bounding_box
@bounding_box ||= Twitter::GeoFactory.fetch_or_new(@attrs[:bounding_box]) unless @attrs[:bounding_box].nil?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def full_text
self.retweeted_status && self.retweeted_status.user ? "RT @#{self.retweeted_status.user.screen_name}: #{self.retweeted_status.text}" : self.text
end

# @return [Twitter::Point, Twitter::Polygon]
# @return [Twitter::Geo::Point, Twitter::Geo::Polygon]
def geo
@geo ||= Twitter::GeoFactory.fetch_or_new(@attrs[:geo]) unless @attrs[:geo].nil?
end
Expand Down
12 changes: 6 additions & 6 deletions spec/twitter/geo/point_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'helper'

describe Twitter::Point do
describe Twitter::Geo::Point do

before do
@point = Twitter::Point.new(:coordinates => [-122.399983, 37.788299])
@point = Twitter::Geo::Point.new(:coordinates => [-122.399983, 37.788299])
end

describe "#==" do
it "returns false for empty objects" do
point = Twitter::Point.new
other = Twitter::Point.new
point = Twitter::Geo::Point.new
other = Twitter::Geo::Point.new
(point == other).should be_false
end
it "returns true when objects coordinates are the same" do
other = Twitter::Point.new(:coordinates => [-122.399983, 37.788299])
other = Twitter::Geo::Point.new(:coordinates => [-122.399983, 37.788299])
(@point == other).should be_true
end
it "returns false when objects coordinates are different" do
other = Twitter::Point.new(:coordinates => [37.788299, -122.399983])
other = Twitter::Geo::Point.new(:coordinates => [37.788299, -122.399983])
(@point == other).should be_false
end
it "returns false when classes are different" do
Expand Down
12 changes: 6 additions & 6 deletions spec/twitter/geo/polygon_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'helper'

describe Twitter::Polygon do
describe Twitter::Geo::Polygon do

before do
@polygon = Twitter::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
@polygon = Twitter::Geo::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
end

describe "#==" do
it "returns false for empty objects" do
polygon = Twitter::Polygon.new
other = Twitter::Polygon.new
polygon = Twitter::Geo::Polygon.new
other = Twitter::Geo::Polygon.new
(polygon == other).should be_false
end
it "returns true when objects coordinates are the same" do
other = Twitter::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
other = Twitter::Geo::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
(@polygon == other).should be_true
end
it "returns false when objects coordinates are different" do
other = Twitter::Polygon.new(:coordinates => [[[37.77752898, -122.40348192], [37.77752898, -122.387436], [37.79448597, -122.387436], [37.79448597, -122.40348192]]])
other = Twitter::Geo::Polygon.new(:coordinates => [[[37.77752898, -122.40348192], [37.77752898, -122.387436], [37.79448597, -122.387436], [37.79448597, -122.40348192]]])
(@polygon == other).should be_false
end
it "returns false when classes are different" do
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/geo_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
describe ".new" do
it "generates a Point" do
geo = Twitter::GeoFactory.fetch_or_new(:type => 'Point')
geo.should be_a Twitter::Point
geo.should be_a Twitter::Geo::Point
end
it "generates a Polygon" do
geo = Twitter::GeoFactory.fetch_or_new(:type => 'Polygon')
geo.should be_a Twitter::Polygon
geo.should be_a Twitter::Geo::Polygon
end
it "raises an ArgumentError when type is not specified" do
lambda do
Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/geo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(@geo == other).should be_false
end
it "returns false when classes are different" do
other = Twitter::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
other = Twitter::Geo::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
(@geo == other).should be_false
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
describe "#bounding_box" do
it "returns a Twitter::Place when set" do
place = Twitter::Place.new(:id => "247f43d441defc03", :bounding_box => {:type => 'Polygon', :coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]]})
place.bounding_box.should be_a Twitter::Polygon
place.bounding_box.should be_a Twitter::Geo::Polygon
end
it "returns nil when not set" do
place = Twitter::Place.new(:id => "247f43d441defc03")
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
end

describe "#geo" do
it "returns a Twitter::Point when set" do
it "returns a Twitter::Geo::Point when set" do
status = Twitter::Status.new(:id => 28669546014, :geo => {:id => 1, :type => 'Point'})
status.geo.should be_a Twitter::Point
status.geo.should be_a Twitter::Geo::Point
end
it "returns nil when not set" do
status = Twitter::Status.new(:id => 28669546014)
Expand Down

0 comments on commit 059cc55

Please sign in to comment.