forked from carlosparamio/geoplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
place.rb
110 lines (99 loc) · 3.99 KB
/
place.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module GeoPlanet
class Place < Base
attr_reader :version # short or long
# short
attr_reader :woeid, :placetype, :placetype_code, :name, :uri, :lang
# long
attr_reader :country, :country_code, :postal
attr_reader :latitude, :longitude, :bounding_box
attr_reader :admin1, :admin1_code, :admin1_placetype
attr_reader :admin2, :admin2_code, :admin2_placetype
attr_reader :admin3, :admin3_code, :admin3_placetype
attr_reader :locality1, :locality1_placetype
attr_reader :locality2, :locality2_placetype
alias_method :lat, :latitude
alias_method :lon, :longitude
def initialize(woe_or_attrs)
case woe_or_attrs
when Integer then initialize_with_woe(woe_or_attrs)
when Hash then initialize_with_attrs(woe_or_attrs)
else
raise ArgumentError
end
end
def initialize_with_woe(woe)
url = self.class.build_url("place/#{woe}", :format => "json")
puts "Yahoo GeoPlanet: GET #{url}"
initialize_with_attrs JSON.parse(Place.get(url))['place']
end
def initialize_with_attrs(attrs)
@version = attrs['centroid'] ? 'long' : 'short'
# short
@woeid = attrs['woeid']
@placetype = attrs['placeTypeName']
@placetype_code = attrs['placeTypeName attrs']['code']
@name = attrs['name']
@uri = attrs['uri']
@lang = attrs['lang']
if version == 'long'
# long
@latitude = attrs['centroid']['latitude']
@longitude = attrs['centroid']['longitude']
@bounding_box = [ [ attrs['boundingBox']['southWest']['latitude'],
attrs['boundingBox']['southWest']['longitude'] ],
[ attrs['boundingBox']['northEast']['latitude'],
attrs['boundingBox']['northEast']['longitude'] ],
]
@country = attrs['country']
@country_code = attrs['country attrs']['code'] rescue nil
@postal = attrs['postal']
@admin1 = attrs['admin1']
@admin1_code = attrs['admin1 attrs']['code'] rescue nil
@admin1_placetype = attrs['admin1 attrs']['type'] rescue nil
@admin2 = attrs['admin2']
@admin2_code = attrs['admin2 attrs']['code'] rescue nil
@admin2_placetype = attrs['admin2 attrs']['type'] rescue nil
@admin3 = attrs['admin3']
@admin3_code = attrs['admin3 attrs']['code'] rescue nil
@admin3_placetype = attrs['admin3 attrs']['type'] rescue nil
@locality1 = attrs['locality1']
@locality1_placetype = attrs['locality1 attrs']['type'] rescue nil
@locality2 = attrs['locality2']
@locality2_placetype = attrs['locality2 attrs']['type'] rescue nil
end
self
end
# Association Collections
%w(parent ancestors belongtos neighbors siblings children).each do |association|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{association}(options = {})
url = Place.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
puts "Yahoo GeoPlanet: GET \#{url}"
Place.get_then_parse(url)
end
RUBY
end
def to_s
self.name
end
def to_i
self.woeid.to_i
end
class << self
def search(text, options = {})
text = URI.encode(text)
url = build_url('places', options.merge(:q => text, :format => 'json'))
puts "Yahoo GeoPlanet: GET #{url}"
get_then_parse(url)
end
def get_then_parse(url)
results = JSON.parse get(url)
return results['places']['place'].map{|attrs| Place.new attrs} if results['places']
return Place.new(results['place']) if results['place']
nil
rescue
nil
end
end
end
end