hacked0ff-reverse_geocode 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,8 @@ require 'uri'
3
3
  require 'json'
4
4
 
5
5
  class ReverseGeocode
6
+ GOOGLE_URI = "http://maps.google.com/maps/geo"
7
+
6
8
  class GeocodeError < StandardError
7
9
  ERRORS = {
8
10
  400 => "Bad Request",
@@ -16,7 +18,7 @@ class ReverseGeocode
16
18
  }
17
19
 
18
20
  def initialize(error_code)
19
- super("#{error_code}: #{ERRORS[error_code]}")
21
+ super("#{error_code}: #{ERRORS[error_code] || 'Unknown Error'}")
20
22
  end
21
23
  end
22
24
 
@@ -49,17 +51,17 @@ class ReverseGeocode
49
51
  end
50
52
 
51
53
  def city
52
- @city ||= placemark[1]/'AddressDetails'/'Country'/'AdministrativeArea'/'Locality'/'LocalityName'
54
+ @city ||= (placemark_by_accuracy(4)/'AddressDetails'/'Country'/'AdministrativeArea'/'AddressLine').first
53
55
  end
54
56
 
55
57
  def county
56
- @county ||= (placemark[3]/'AddressDetails'/'Country'/'AdministrativeArea'/'AddressLine').first
58
+ @county ||= (placemark_by_accuracy(3)/'AddressDetails'/'Country'/'AdministrativeArea'/'AddressLine').first
57
59
  end
58
60
 
59
61
  private
60
62
 
61
63
  def first_placemark
62
- placemark[0]
64
+ placemark_by_accuracy(8)
63
65
  end
64
66
 
65
67
  def first_administrative_area
@@ -70,8 +72,12 @@ class ReverseGeocode
70
72
  (response/"Placemark")
71
73
  end
72
74
 
75
+ def placemark_by_accuracy(accuracy)
76
+ placemark.detect { |x| (x/'AddressDetails'/'Accuracy').to_i == accuracy.to_i }
77
+ end
78
+
73
79
  def reverse_geocode_uri
74
- URI.parse("http://maps.google.com/maps/geo?ll=#{URI.escape(lat.to_s)},#{URI.escape(long.to_s)}&output=json")
80
+ URI.parse("#{GOOGLE_URI}?ll=#{URI.escape(lat.to_s)},#{URI.escape(long.to_s)}")
75
81
  end
76
82
 
77
83
  def parse_json
@@ -90,3 +96,9 @@ end
90
96
  class Hash
91
97
  alias_method :/, :[]
92
98
  end
99
+
100
+ class NilClass
101
+ def /(other)
102
+ raise ArgumentError, "Unknown method '/' called on nil with #{other.inspect}. Maybe you were looking for a Hash?"
103
+ end
104
+ end
@@ -124,6 +124,13 @@ describe "ReverseGeocode" do
124
124
  end
125
125
  end
126
126
 
127
+ describe '#placemark_by_accuracy' do
128
+ it 'should return the placemark with the given accuracy' do
129
+ (@reverse_geocode.send(:placemark_by_accuracy, 3)/'AddressDetails'/'Accuracy').to_i.should == 3
130
+
131
+ end
132
+ end
133
+
127
134
  describe "without a latitude or longitude" do
128
135
  it "should raise an ArgumentError" do
129
136
  lambda { ReverseGeocode.new(nil, 1) }.should raise_error(ArgumentError)
@@ -153,7 +160,7 @@ describe "ReverseGeocode" do
153
160
  @reverse_geocode.stub!(:parse_json).and_return(error_json(code))
154
161
  end
155
162
 
156
- it "should raise a GeocodeError" do
163
+ it "should raise a GeocodeError with #{message}" do
157
164
  lambda {
158
165
  @reverse_geocode.send(:handle_response)
159
166
  }.should raise_error(ReverseGeocode::GeocodeError, "#{code}: #{message}")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hacked0ff-reverse_geocode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Campbell & ReinH