Permalink
Browse files

more niceness :)

  • Loading branch information...
1 parent cfb215b commit 29c16b592ead8291bf4e2c502f6e2d81d24d77ff @kristianmandrup committed Feb 7, 2011
View
8 lib/geo_magic/geocode/base_adapter.rb
@@ -9,6 +9,10 @@ def initialize services = :google, env = :default
@environment = env
end
+ def configured?
+ configured
+ end
+
def get_service services
case services
when String, Symbol
@@ -35,6 +39,10 @@ def setup env
self.class.send(:include, ServiceAdapter)
end
end
+
+ protected
+
+ attr_accessor :configured
end
end
end
View
1 lib/geo_magic/geocode/config.rb
@@ -12,6 +12,7 @@ def reverse_geocode latitude, longitude
def configure path, env = :development
@config ||= ::YAML.load_file(path)[env.to_s]
+ send :configured, true
self
end
View
2 lib/geo_magic/geocoder.rb
@@ -9,7 +9,7 @@
module GeoMagic
class GeoCodeError < StandardError; end;
- class << self
+ class << self
def geo_coder options = {:type => :geocoder, :service => :google}
service_name = options[:service_name] || :google
type = options[:type] || :geocoder
View
14 lib/geo_magic/point.rb
@@ -1,10 +1,12 @@
require 'geo_magic/point/class_methods'
require 'geo_magic/point/conversion'
+require 'geo_magic/point/random'
module GeoMagic
class Point
extend ClassMethods
extend Conversion
+ extend Random
attr_accessor :latitude, :longitude
@@ -14,15 +16,16 @@ def initialize latitude, longitude
end
# factory method
- def create_from *args
- @latitude, @longitude = case args.size
+ def self.create_from *args
+ latitude, longitude = case args.size
when 1
- args.first.to_point
+ args.first.extend(GeoMagic::Point::Conversion).to_point
when 2
- args.to_points
+ args.extend(GeoMagic::Point::Conversion).to_points
else
raise "Bad argument to create a point from: #{args}"
end
+ new latitude, longitude
end
def to_point_hash mode= :long
@@ -35,7 +38,8 @@ def to_point_hash mode= :long
end
def to_location
- GeoMagic.geocoder.instance.reverse_geocode latitude, longitude
+ raise "You need to configure the GeoMagic geocoder, see README for instructions" if !GeoMagic.geo_coder.configured?
+ GeoMagic.geo_coder.instance.reverse_geocode latitude, longitude
end
def to_s
View
2 lib/geo_magic/point/random.rb
@@ -1,5 +1,5 @@
module GeoMagic
- class MapPoint
+ class Point
module Random
def move_random distance
conversion = distance.unit.radians_ratio
View
10 spec/geo_magic/distance_spec.rb
@@ -17,6 +17,14 @@
it "calculates distance using array args - using algorithm haversine" do
dist = GeoMagic::Distance.calculate @long1, @lat1, @long2, @lat2
km_dist = dist[:km]
- puts dist
+ puts dist
+ puts "km: #{km_dist}"
+ end
+
+ it "calculates distance using array args - using algorithm haversine" do
+ dist = GeoMagic::Distance.calculate {:long => @long1, :lat => @lat1}, {:long => @long2, :lat => @lat2}
+ km_dist = dist[:km]
+ puts dist
+ puts "km: #{km_dist}"
end
end
View
86 spec/geo_magic/point/conversion_spec.rb
@@ -7,32 +7,74 @@ def to_points
end
describe GeoMagic::Point::Conversion do
- before do
- @long1 = -104.88544
- @lat1 = 39.06546
+ describe '#to_point' do
+ before do
+ @long = -104.88544
+ @lat = 39.06546
+ end
+
+ it "should convert array to a point"
+ point = [@lat, @long].to_point
+ puts points.inspect
+ end
- @long2 = -104.80
- @lat2 = @lat1
+ it "should convert Hash to a point"
+ point = {:lat => @lat, :long => @long}.to_point
+ puts points.inspect
+ end
+
+ it "should convert String to a point"
+ point = "#{@lat}, #{@long}".to_point
+ puts points.inspect
+ end
+
+ it "should convert String array to a point"
+ point = ["#{@lat}", "#{@long}"].to_point
+ puts points.inspect
+ end
+
+ it "should convert Integer array to a point"
+ point = [1, 2].to_point
+ puts points.inspect
+ end
end
+
+ describe '#to_points' do
+ before do
+ @long1 = -104.88544
+ @lat1 = 39.06546
+
+ @long2 = -104.80
+ @lat2 = @lat1
+ end
- it "convert array into points" do
- points = [@lat1, @long1, @lat2, @long2].to_points
- puts points.inspect
- dist = GeoMagic::Distance.calculate points
- puts dist
- end
+ it "convert array into points" do
+ points = [@lat1, @long1, @lat2, @long2].to_points
+ puts points.inspect
+ dist = GeoMagic::Distance.calculate points
+ puts dist
+ end
- it "convert array with inner point arrays into points" do
- points = [@lat1, @long1, @lat2, @long2].to_points
- puts points.inspect
- dist = GeoMagic::Distance.calculate points
- puts dist
- end
+ it "convert array with inner point arrays into points" do
+ points = [@lat1, @long1, @lat2, @long2].to_points
+ puts points.inspect
+ dist = GeoMagic::Distance.calculate points
+ puts dist
+ end
- it "convert array with inner point arrays into points" do
- points = [{:long => @long1, :lat => @lat1}, [@lat2, @long2]].to_points
- puts points.inspect
- dist = GeoMagic::Distance.calculate points
- puts dist
+ it "convert array with inner point arrays into points" do
+ points = [{:long => @long1, :lat => @lat1}, [@lat2, @long2]].to_points
+ puts points.inspect
+ dist = GeoMagic::Distance.calculate points
+ puts dist
+ end
+
+ it "convert array with inner point arrays into points" do
+ points = [[@lat2, @long2], {:long => @long1, :lat => @lat1}].to_points
+ puts points.inspect
+ dist = GeoMagic::Distance.calculate points
+ puts dist
+ end
end
+
end
View
48 spec/geo_magic/point_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe GeoMagic::Point do
+ describe '#new' do
+ it "should create a new point" do
+ point = GeoMagic::Point.new -10, 20
+ puts point.inspect
+ end
+ end
+
+ describe '#create_from' do
+ it "should create a new point" do
+ point = GeoMagic::Point.create_from -10, 20
+ puts point.inspect
+ end
+
+ it "should create a new point" do
+ point = GeoMagic::Point.create_from :lat => -10, :long => 20
+ puts point.inspect
+ end
+ end
+
+ describe '#to_point_hash' do
+ it "should create a point hash" do
+ point = GeoMagic::Point.create_from -10, 20
+ hash = point.to_point_hash
+ puts point.inspect
+ puts hash.inspect
+ end
+ end
+
+ describe '#to_location' do
+ it "should create a location" do
+ point = GeoMagic::Point.create_from -10, 20
+ lambda {point.to_location}.should raise_error
+ puts point.inspect
+ end
+ end
+
+ describe '#to_s' do
+ it "should create a string rep" do
+ point = GeoMagic::Point.create_from -10, 20
+ str = point.to_s
+ puts point.inspect
+ puts str
+ end
+ end
+end

0 comments on commit 29c16b5

Please sign in to comment.