geonames-with-proxy 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ Git repo of http://code.google.com/p/ruby-geonames project with proxy settings.
2
+
3
+ See http://www.tbcn.ca/ruby_geonames for more info.
4
+
5
+ == Proxy settings
6
+
7
+ Geonames.proxy_host = 'proxy.example.org'
8
+ Geonames.proxy_port = 3128
@@ -0,0 +1,24 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'rake/rdoctask'
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
8
+ t.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new(:rcov) do |t|
12
+ t.pattern = 'spec/**/*_spec.rb'
13
+ t.rcov = true
14
+ end
15
+
16
+ Rake::RDocTask.new do |rdoc|
17
+ require 'geonames'
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = "geonames #{Geonames::VERSION}"
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Address
21
+
22
+ end
23
+ end
24
+
25
+
@@ -0,0 +1,32 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class CountrySubdivision
21
+ attr :country_code
22
+ attr :country_name
23
+ attr :admin_code_1
24
+ attr :admin_name_1
25
+
26
+ attr_writer :country_code, :country_name
27
+ attr_writer :admin_name_1, :admin_code_1
28
+
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,55 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ require 'cgi'
20
+ require 'net/http'
21
+ require 'rexml/document'
22
+
23
+ module Geonames
24
+ VERSION = "0.2.3"
25
+ GEONAMES_SERVER = "http://ws.geonames.org"
26
+ USER_AGENT = "geonames ruby webservice client #{VERSION}"
27
+
28
+ def self.proxy_host
29
+ @@proxy_host
30
+ end
31
+ def self.proxy_host=(phost)
32
+ @@proxy_host = phost
33
+ end
34
+ self.proxy_host = nil
35
+
36
+ def self.proxy_port
37
+ @@proxy_port
38
+ end
39
+ def self.proxy_port=(pport)
40
+ @@proxy_port = pport
41
+ end
42
+ proxy_port = nil
43
+ end
44
+
45
+ require 'web_service'
46
+ require 'toponym'
47
+ require 'toponym_search_result'
48
+ require 'toponym_search_criteria'
49
+ require 'postal_code'
50
+ require 'postal_code_search_criteria'
51
+ require 'timezone'
52
+ require 'country_subdivision'
53
+ require 'wikipedia_article'
54
+ require 'intersection'
55
+
@@ -0,0 +1,42 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Intersection
21
+ attr :street_1
22
+ attr :street_2
23
+ attr :latitude
24
+ attr :longitude
25
+ attr :distance
26
+ attr :postal_code
27
+ attr :place_name
28
+ attr :country_code
29
+ attr :admin_code_1
30
+ attr :admin_name_2
31
+ attr :admin_code_2
32
+ attr :admin_name_1
33
+
34
+ attr_writer :street_1, :street_2
35
+ attr_writer :postal_code, :place_name, :country_code
36
+ attr_writer :latitude, :longitude, :admin_name_1
37
+ attr_writer :admin_code_1, :admin_name_2, :admin_code_2
38
+ attr_writer :distance
39
+ end
40
+ end
41
+
42
+
@@ -0,0 +1,40 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class PostalCode
21
+ attr :postal_code
22
+ attr :place_name
23
+ attr :country_code
24
+ attr :latitude
25
+ attr :longitude
26
+ attr :admin_name_1
27
+ attr :admin_code_1
28
+ attr :admin_name_2
29
+ attr :admin_code_2
30
+ attr :distance
31
+
32
+ attr_writer :postal_code, :place_name, :country_code
33
+ attr_writer :latitude, :longitude, :admin_name_1
34
+ attr_writer :admin_code_1, :admin_name_2, :admin_code_2
35
+ attr_writer :distance
36
+
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,84 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class PostalCodeSearchCriteria
21
+
22
+ attr :postal_code
23
+ attr :place_name
24
+ attr :country_code
25
+ attr :latitude
26
+ attr :longitude
27
+ attr :style
28
+ attr :max_rows
29
+ attr :is_or_operator
30
+ attr :radius
31
+
32
+ attr_writer :postal_code, :place_name, :country_code
33
+ attr_writer :latitude, :longitude, :style
34
+ attr_writer :max_rows, :is_or_operator, :radius
35
+
36
+ def initialize
37
+ @is_or_operator = false
38
+ end
39
+
40
+ def to_query_params_string
41
+ url = ''
42
+
43
+ if !@postal_code.nil?
44
+ url = url + "&postalcode=" + CGI::escape( @postal_code )
45
+ end
46
+
47
+ if !@place_name.nil?
48
+ url = url + "&placename=" + CGI::escape( @place_name )
49
+ end
50
+
51
+ if !@latitude.nil?
52
+ url = url + "&lat" + CGI::escape( @latitude )
53
+ end
54
+
55
+ if !@longitude.nil?
56
+ url = url + "&lng" + CGI::escape( @longitude )
57
+ end
58
+
59
+ if !@style.nil?
60
+ url = url + "&style" + CGI::escape( @style )
61
+ end
62
+
63
+ if !@country_code.nil?
64
+ url = url + "&country=" + CGI::escape( @country_code )
65
+ end
66
+
67
+ if !@max_rows.nil?
68
+ url = url + "&maxRows=" + CGI::escape( @max_rows )
69
+ end
70
+
71
+ if !@radius.nil?
72
+ url = url + "&radius=" + CGI::escape( @radius.to_s )
73
+ end
74
+
75
+ if @is_or_operator
76
+ url = url + "&operator=OR"
77
+ end
78
+
79
+ url
80
+ end
81
+ end
82
+ end
83
+
84
+
@@ -0,0 +1,29 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Timezone
21
+ attr :timezone_id
22
+ attr :gmt_offset
23
+ attr :dst_offset
24
+
25
+ attr_writer :timezone_id, :gmt_offset, :dst_offset
26
+ end
27
+ end
28
+
29
+
@@ -0,0 +1,45 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class Toponym
21
+
22
+ attr :geoname_id
23
+ attr :name
24
+ attr :alternate_names
25
+ attr :country_code
26
+ attr :country_name
27
+ attr :population
28
+ attr :elevation
29
+ attr :feature_class
30
+ attr :feature_class_name
31
+ attr :feature_code
32
+ attr :feature_code_name
33
+ attr :latitude
34
+ attr :longitude
35
+ attr :distance
36
+
37
+ attr_writer :geoname_id, :name, :alternate_names, :country_code
38
+ attr_writer :country_name, :population, :elevation, :feature_class
39
+ attr_writer :feature_class_name, :feature_code,:feature_code_name
40
+ attr_writer :latitude, :longitude, :distance
41
+
42
+ end
43
+ end
44
+
45
+
@@ -0,0 +1,44 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class ToponymSearchCriteria
21
+
22
+ attr :q
23
+ attr :country_code
24
+ attr :name
25
+ attr :name_equals
26
+ attr :name_starts_with
27
+ attr :tag
28
+ attr :language
29
+ attr :style
30
+ attr :feature_class
31
+ attr :feature_codes
32
+ attr :admin_code_1
33
+ attr :max_rows
34
+ attr :start_row
35
+
36
+ attr_writer :q, :country_code, :name, :name_equals
37
+ attr_writer :name_starts_with, :tag, :language, :style
38
+ attr_writer :feature_class, :feature_codes, :admin_code_1
39
+ attr_writer :max_rows, :start_row
40
+
41
+ end
42
+ end
43
+
44
+
@@ -0,0 +1,33 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class ToponymSearchResult
21
+
22
+ attr :total_results_count
23
+ attr :toponyms
24
+
25
+ attr_writer :total_results_count, :toponyms
26
+
27
+ def initialize
28
+ @toponyms = Array.new
29
+ end
30
+ end
31
+ end
32
+
33
+
@@ -0,0 +1,350 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Andrew Turner, High Earth Orbit
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
+ # use this file except in compliance with the License. You may obtain a copy of
8
+ # the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+ # License for the specific language governing permissions and limitations under
16
+ # the License.
17
+ #
18
+ #=============================================================================
19
+
20
+ module Geonames
21
+ class WebService
22
+ def self.get_element_child_text(element, child)
23
+ unless element.elements[child].nil?
24
+ element.elements[child][0].to_s
25
+ end
26
+ end
27
+
28
+ def self.get_element_child_float(element, child)
29
+ if !element.elements[child].nil?
30
+ element.elements[child][0].to_s.to_f
31
+ end
32
+ end
33
+
34
+ def self.get_element_child_int(element, child)
35
+ if !element.elements[child].nil?
36
+ element.elements[child][0].to_s.to_i
37
+ end
38
+ end
39
+
40
+ def self.element_to_postal_code(element)
41
+ postal_code = PostalCode.new
42
+
43
+ postal_code.admin_code_1 = get_element_child_text element, 'adminCode1'
44
+ postal_code.admin_code_2 = get_element_child_text element, 'adminCode2'
45
+ postal_code.admin_name_1 = get_element_child_text element, 'adminName1'
46
+ postal_code.admin_name_2 = get_element_child_text element, 'adminName2'
47
+ postal_code.country_code = get_element_child_text element, 'countryCode'
48
+ postal_code.distance = get_element_child_float element, 'distance'
49
+ postal_code.longitude = get_element_child_float element, 'lat'
50
+ postal_code.latitude = get_element_child_float element, 'lng'
51
+ postal_code.place_name = get_element_child_text element, 'name'
52
+ postal_code.postal_code = get_element_child_text element, 'postalcode'
53
+
54
+ postal_code
55
+ end
56
+
57
+ def self.element_to_wikipedia_article(element)
58
+ article = WikipediaArticle.new
59
+
60
+ article.language = get_element_child_text element, 'lang'
61
+ article.title = get_element_child_text element, 'title'
62
+ article.summary = get_element_child_text element, 'summary'
63
+ article.wikipedia_url = get_element_child_text element, 'wikipediaUrl'
64
+ article.feature = get_element_child_text element, 'feature'
65
+ article.population = get_element_child_text element, 'population'
66
+ article.elevation = get_element_child_text element, 'elevation'
67
+ article.latitude = get_element_child_float element, 'lat'
68
+ article.longitude = get_element_child_float element, 'lng'
69
+ article.thumbnail_img = get_element_child_text element, 'thumbnailImg'
70
+ article.distance = get_element_child_float element, 'distance'
71
+
72
+ article
73
+ end
74
+
75
+ def self.element_to_toponym(element)
76
+ toponym = Toponym.new
77
+
78
+ toponym.name = get_element_child_text element, 'name'
79
+ toponym.alternate_names = get_element_child_text element, 'alternateNames'
80
+ toponym.latitude = get_element_child_float element, 'lat'
81
+ toponym.longitude = get_element_child_float element, 'lng'
82
+ toponym.geoname_id = get_element_child_text element, 'geonameId'
83
+ toponym.country_code = get_element_child_text element, 'countryCode'
84
+ toponym.country_name = get_element_child_text element, 'countryName'
85
+ toponym.feature_class = get_element_child_text element, 'fcl'
86
+ toponym.feature_code = get_element_child_text element, 'fcode'
87
+ toponym.feature_class_name = get_element_child_text element, 'fclName'
88
+ toponym.feature_code_name = get_element_child_text element, 'fCodeName'
89
+ toponym.population = get_element_child_int element, 'population'
90
+ toponym.elevation = get_element_child_text element, 'elevation'
91
+ toponym.distance = get_element_child_float element, 'distance'
92
+
93
+ toponym
94
+ end
95
+
96
+ def self.element_to_intersection(element)
97
+ intersection = Intersection.new
98
+
99
+ intersection.street_1 = get_element_child_text element, 'street1'
100
+ intersection.street_2 = get_element_child_text element, 'street2'
101
+ intersection.admin_code_1 = get_element_child_text element, 'adminCode1'
102
+ intersection.admin_code_1 = get_element_child_text element, 'adminCode1'
103
+ intersection.admin_code_2 = get_element_child_text element, 'adminCode2'
104
+ intersection.admin_name_1 = get_element_child_text element, 'adminName1'
105
+ intersection.admin_name_2 = get_element_child_text element, 'adminName2'
106
+ intersection.country_code = get_element_child_text element, 'countryCode'
107
+ intersection.distance = get_element_child_float element, 'distance'
108
+ intersection.longitude = get_element_child_float element, 'lat'
109
+ intersection.latitude = get_element_child_float element, 'lng'
110
+ intersection.place_name = get_element_child_text element, 'name'
111
+ intersection.postal_code = get_element_child_text element, 'postalcode'
112
+
113
+ intersection
114
+ end
115
+
116
+ def self.postal_code_search(postal_code, place_name, country_code)
117
+ postal_code_sc = PostalCodeSearchCriteria.new
118
+ postal_code_sc.postal_code = postal_code
119
+ postal_code_sc.place_name = place_name
120
+ postal_code_sc.coutry_code = country_code
121
+
122
+ postal_code_search postal_code_sc
123
+ end
124
+
125
+ def self.postal_code_search(search_criteria)
126
+ # postal codes to reutrn
127
+ postal_codes = Array.new
128
+
129
+ url = Geonames::GEONAMES_SERVER + "/postalCodeSearch?a=a"
130
+ url += search_criteria.to_query_params_string
131
+
132
+ res = fetch_results(url)
133
+
134
+ REXML::Document.new(res.body).elements.each("geonames/code") do |element|
135
+ postal_codes << element_to_postal_code( element )
136
+ end
137
+
138
+ postal_codes
139
+ end
140
+
141
+ def self.find_nearby_postal_codes( search_criteria )
142
+ # postal codes to reutrn
143
+ postal_codes = Array.new
144
+
145
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPostalCodes?a=a"
146
+ url += search_criteria.to_query_params_string
147
+ uri = URI.parse(url)
148
+
149
+ res = fetch_results(url)
150
+
151
+ REXML::Document.new(res.body).elements.each("geonames/code") do |element|
152
+ postal_codes << element_to_postal_code( element )
153
+ end
154
+
155
+ postal_codes
156
+ end
157
+
158
+ def self.find_nearby_place_name(lat, long)
159
+ places = Array.new
160
+
161
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPlaceName?a=a"
162
+ url += "&lat=" + lat.to_s
163
+ url += "&lng=" + long.to_s
164
+
165
+ res = fetch_results(url)
166
+
167
+ REXML::Document.new(res.body).elements.each("geonames/geoname") do |element|
168
+ places << element_to_toponym( element )
169
+ end
170
+
171
+ places
172
+ end
173
+
174
+ def self.find_nearest_intersection(lat, long)
175
+ url = Geonames::GEONAMES_SERVER + "/findNearestIntersection?a=a"
176
+ url += "&lat=" + lat.to_s
177
+ url += "&lng=" + long.to_s
178
+
179
+ res = fetch_results(url)
180
+
181
+ intersection = []
182
+ REXML::Document.new(res.body).elements.each("geonames/intersection") do |element|
183
+ intersection = element_to_intersection( element )
184
+ end
185
+
186
+ intersection
187
+ end
188
+
189
+ def self.timezone(lat, long)
190
+ timezone = Timezone.new
191
+
192
+ url = Geonames::GEONAMES_SERVER + "/timezone?a=a"
193
+ url += "&lat=" + lat.to_s
194
+ url += "&lng=" + long.to_s
195
+
196
+ res = fetch_results(url)
197
+
198
+ REXML::Document.new(res.body).elements.each("geonames/timezone") do |element|
199
+ timezone.timezone_id = get_element_child_text( element, 'timezoneId' )
200
+ timezone.gmt_offset = get_element_child_float( element, 'gmtOffset' )
201
+ timezone.dst_offset = get_element_child_float( element, 'dstOffset' )
202
+ end
203
+
204
+ timezone
205
+ end
206
+
207
+ def self.findNearbyWikipedia(hashes)
208
+ # here for backwards compatibility
209
+ find_nearby_wikipedia( hashes )
210
+ end
211
+
212
+ def self.find_nearby_wikipedia(hashes)
213
+ articles = Array.new
214
+
215
+ lat = hashes[:lat]
216
+ long = hashes[:long]
217
+ lang = hashes[:lang]
218
+ radius = hashes[:radius]
219
+ max_rows = hashes[:max_rows]
220
+ country = hashes[:country]
221
+ postalcode = hashes[:postalcode]
222
+ q = hashes[:q]
223
+
224
+ url = Geonames::GEONAMES_SERVER + "/findNearbyWikipedia?a=a"
225
+
226
+ if !lat.nil? && !long.nil?
227
+ url += "&lat=" + lat.to_s
228
+ url += "&lng=" + long.to_s
229
+ url += "&radius=" + radius.to_s unless radius.nil?
230
+ url += "&max_rows=" + max_rows.to_s unless max_rows.nil?
231
+
232
+ elsif !q.nil?
233
+ url += "&q=" + q
234
+ url += "&radius=" + radius.to_s unless radius.nil?
235
+ url += "&max_rows=" + max_rows.to_s unless max_rows.nil?
236
+ end
237
+
238
+ res = fetch_results(url)
239
+
240
+ REXML::Document.new(res.body).elements.each("geonames/entry") do |element|
241
+ articles << element_to_wikipedia_article( element )
242
+ end
243
+
244
+ articles
245
+ end
246
+
247
+ def self.findBoundingBoxWikipedia(hashes)
248
+ # here for backwards compatibility
249
+ find_bounding_box_wikipedia( hashes )
250
+ end
251
+
252
+ def self.find_bounding_box_wikipedia(hashes)
253
+ articles = Array.new
254
+
255
+ north = hashes[:north]
256
+ east = hashes[:east]
257
+ south = hashes[:south]
258
+ west = hashes[:west]
259
+ lang = hashes[:lang]
260
+ radius = hashes[:radius]
261
+ max_rows = hashes[:max_rows]
262
+ country = hashes[:country]
263
+ postalcode = hashes[:postalcode]
264
+ q = hashes[:q]
265
+
266
+ url = Geonames::GEONAMES_SERVER + "/wikipediaBoundingBox?a=a"
267
+
268
+ url += "&north=" + north.to_s
269
+ url += "&east=" + east.to_s
270
+ url += "&south=" + south.to_s
271
+ url += "&west=" + west.to_s
272
+ url += "&radius=" + radius.to_s unless radius.nil?
273
+ url += "&max_rows=" + max_rows.to_s unless max_rows.nil?
274
+
275
+ res = fetch_results(url)
276
+
277
+ REXML::Document.new(res.body).elements.each("geonames/entry") do |element|
278
+ articles << element_to_wikipedia_article( element )
279
+ end
280
+
281
+ articles
282
+ end
283
+
284
+ def self.country_subdivision(lat, long)
285
+ country_subdivision = CountrySubdivision.new
286
+
287
+ url = Geonames::GEONAMES_SERVER + "/countrySubdivision?a=a"
288
+ url += "&lat=" + lat.to_s
289
+ url += "&lng=" + long.to_s
290
+
291
+ res = fetch_results(url)
292
+
293
+ REXML::Document.new(res.body).elements.each("geonames/countrySubdivision") do |element|
294
+ country_subdivision.country_code = get_element_child_text(element, 'countryCode')
295
+ country_subdivision.country_name = get_element_child_text(element, 'countryName')
296
+ country_subdivision.admin_code_1 = get_element_child_text(element, 'adminCode1')
297
+ country_subdivision.admin_name_1 = get_element_child_text(element, 'adminName1')
298
+ end
299
+
300
+ country_subdivision
301
+ end
302
+
303
+ def self.country_code ( lat, long )
304
+ url = Geonames::GEONAMES_SERVER + "/countrycode?a=a"
305
+ url += "&lat=" + lat.to_s
306
+ url += "&lng=" + long.to_s
307
+
308
+ fetch_results(url).body.strip
309
+ end
310
+
311
+ def self.search(search_criteria)
312
+ toponym_sr = ToponymSearchResult.new
313
+
314
+ url = Geonames::GEONAMES_SERVER + "/search?a=a"
315
+ url += "&q=" + CGI::escape(search_criteria.q) unless search_criteria.q.nil?
316
+ url += "&name_equals=" + CGI::escape(search_criteria.name_equals) unless search_criteria.name_equals.nil?
317
+ url += "&name_startsWith=" + CGI::escape(search_criteria.name_starts_with) unless search_criteria.name_starts_with.nil?
318
+ url += "&name=" + CGI::escape(search_criteria.name) unless search_criteria.name.nil?
319
+ url += "&tag=" + CGI::escape(search_criteria.tag) unless search_criteria.tag.nil?
320
+ url += "&country=" + CGI::escape(search_criteria.country_code) unless search_criteria.country_code.nil?
321
+ url += "&adminCode1=" + CGI::escape(search_criteria.admin_code_1) unless search_criteria.admin_code_1.nil?
322
+ url += "&lang=" + CGI::escape(search_criteria.language) unless search_criteria.language.nil?
323
+ url += "&featureClass=" + CGI::escape(search_criteria.feature_class) unless search_criteria.feature_class.nil?
324
+ url += "&maxRows=" + CGI::escape(search_criteria.max_rows) unless search_criteria.max_rows.nil?
325
+ url += "&startRow=" + CGI::escape(search_criteria.start_row) unless search_criteria.start_row.nil?
326
+ url += "&style=" + CGI::escape( search_criteria.style ) unless search_criteria.style.nil?
327
+ for feature_code in search_criteria.feature_codes
328
+ url += "&fcode=" + CGI::escape(feature_code)
329
+ end unless search_criteria.feature_codes.nil?
330
+
331
+ doc = REXML::Document.new fetch_results(url).body
332
+
333
+ toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
334
+ doc.elements.each("geonames/geoname") do |element|
335
+ toponym_sr.toponyms << element_to_toponym(element)
336
+ end
337
+
338
+ toponym_sr
339
+ end
340
+
341
+ def self.fetch_results(url)
342
+ uri = URI.parse(url)
343
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
344
+
345
+ Net::HTTP::Proxy(Geonames.proxy_host, Geonames.proxy_port).start(uri.host, uri.port) { |http|
346
+ http.request(req)
347
+ }
348
+ end
349
+ end
350
+ end
@@ -0,0 +1,42 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
+ # use this file except in compliance with the License. You may obtain a copy of
7
+ # the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+ #
17
+ #=============================================================================
18
+
19
+ module Geonames
20
+ class WikipediaArticle
21
+
22
+ attr :language
23
+ attr :title
24
+ attr :summary
25
+ attr :wikipedia_url
26
+ attr :feature
27
+ attr :population
28
+ attr :elevation
29
+ attr :latitude
30
+ attr :longitude
31
+ attr :thumbnail_img
32
+ attr :distance
33
+
34
+ attr_writer :language, :title, :summary
35
+ attr_writer :wikipedia_url, :feature, :population
36
+ attr_writer :elevation, :latitude, :longitude
37
+ attr_writer :thumbnail_img, :distance
38
+
39
+ end
40
+ end
41
+
42
+
File without changes
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geonames-with-proxy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 3
10
+ version: 0.2.3
11
+ platform: ruby
12
+ authors:
13
+ - Adam Wisniewski
14
+ - alex
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-20 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rake
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description:
51
+ email:
52
+ - adamw@tbcn.ca
53
+ - alex@cloudware.it
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - lib/address.rb
62
+ - lib/country_subdivision.rb
63
+ - lib/geonames.rb
64
+ - lib/intersection.rb
65
+ - lib/postal_code.rb
66
+ - lib/postal_code_search_criteria.rb
67
+ - lib/timezone.rb
68
+ - lib/toponym.rb
69
+ - lib/toponym_search_criteria.rb
70
+ - lib/toponym_search_result.rb
71
+ - lib/web_service.rb
72
+ - lib/wikipedia_article.rb
73
+ - spec/spec_helper.rb
74
+ - Rakefile
75
+ - README.rdoc
76
+ has_rdoc: true
77
+ homepage: http://www.tbcn.ca/ruby_geonames
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 31
91
+ segments:
92
+ - 1
93
+ - 8
94
+ version: "1.8"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 9
101
+ segments:
102
+ - 1
103
+ - 3
104
+ version: "1.3"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.4.1
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Ruby library for Geonames Web Services (http://www.geonames.org/export/)
112
+ test_files: []
113
+