geonames 0.2.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +7 -0
  4. data/README.markdown +82 -0
  5. data/Rakefile +13 -0
  6. data/init.rb +1 -0
  7. data/lib/geonames.rb +38 -22
  8. data/lib/geonames/bounding_box.rb +12 -0
  9. data/lib/{country_subdivision.rb → geonames/config.rb} +26 -13
  10. data/lib/geonames/country_info.rb +18 -0
  11. data/lib/{Rakefile.rb → geonames/country_subdivision.rb} +11 -6
  12. data/lib/geonames/intersection.rb +28 -0
  13. data/lib/{toponym_search_result.rb → geonames/postal_code.rb} +10 -16
  14. data/lib/geonames/postal_code_search_criteria.rb +50 -0
  15. data/lib/{timezone.rb → geonames/timezone.rb} +10 -11
  16. data/lib/geonames/toponym.rb +29 -0
  17. data/lib/geonames/toponym_search_criteria.rb +34 -0
  18. data/lib/{address.rb → geonames/toponym_search_result.rb} +10 -7
  19. data/lib/geonames/version.rb +3 -0
  20. data/lib/geonames/web_service.rb +467 -0
  21. data/lib/{wikipedia_article.rb → geonames/wikipedia_article.rb} +5 -20
  22. data/ruby-geonames.gemspec +24 -0
  23. data/spec/fixtures/country_info/thailand.xml.http +29 -0
  24. data/spec/fixtures/country_subdivision/ontario.xml.http +20 -0
  25. data/spec/fixtures/countrycode/canada.xml.http +16 -0
  26. data/spec/fixtures/find_nearby/marchtrenk.xml.http +37 -0
  27. data/spec/fixtures/find_nearby/wilkinson.xml.http +23 -0
  28. data/spec/fixtures/find_nearby_place_name/oshawa.xml.http +23 -0
  29. data/spec/fixtures/find_nearby_postal_codes/oshawa.xml.http +81 -0
  30. data/spec/fixtures/find_nearby_wikipedia/general_motors_centre.xml.http +81 -0
  31. data/spec/fixtures/find_nearest_intersection/park_ave_and_e_51st_st.xml.http +24 -0
  32. data/spec/fixtures/hierarchy/zurich.xml.http +87 -0
  33. data/spec/fixtures/postal_code_search/lat_lng.xml.http +81 -0
  34. data/spec/fixtures/postal_code_search/oshawa.xml.http +77 -0
  35. data/spec/fixtures/search/austria.xml.http +146 -0
  36. data/spec/fixtures/search/marchtrenk.xml.http +93 -0
  37. data/spec/fixtures/search/upper_austria.xml.http +48 -0
  38. data/spec/fixtures/timezone/america_toronto.xml.http +24 -0
  39. data/spec/fixtures/wikipedia_bounding_box/wyoming.xml.http +154 -0
  40. data/spec/geonames/postal_code_search_criteria_spec.rb +31 -0
  41. data/spec/geonames/web_service/country_code_spec.rb +24 -0
  42. data/spec/geonames/web_service/country_info_spec.rb +42 -0
  43. data/spec/geonames/web_service/country_subdivision_spec.rb +24 -0
  44. data/spec/geonames/web_service/find_bounding_box_wikipedia_spec.rb +28 -0
  45. data/spec/geonames/web_service/find_nearby_place_name_spec.rb +24 -0
  46. data/spec/geonames/web_service/find_nearby_postal_codes_spec.rb +29 -0
  47. data/spec/geonames/web_service/find_nearby_spec.rb +41 -0
  48. data/spec/geonames/web_service/find_nearby_wikipedia_spec.rb +26 -0
  49. data/spec/geonames/web_service/find_nearest_intersection_spec.rb +18 -0
  50. data/spec/geonames/web_service/hierarchy_spec.rb +23 -0
  51. data/spec/geonames/web_service/postal_code_search_spec.rb +49 -0
  52. data/spec/geonames/web_service/search_spec.rb +54 -0
  53. data/spec/geonames/web_service/timezone_spec.rb +22 -0
  54. data/spec/geonames/web_service_spec.rb +13 -0
  55. data/spec/spec_helper.rb +13 -0
  56. data/spec/support/fakeweb.rb +9 -0
  57. metadata +152 -61
  58. data/README +0 -0
  59. data/geonames-0.2.2.gem +0 -0
  60. data/geonames.gemspec +0 -18
  61. data/lib/intersection.rb +0 -42
  62. data/lib/main.rb +0 -63
  63. data/lib/postal_code.rb +0 -40
  64. data/lib/postal_code_search_criteria.rb +0 -84
  65. data/lib/toponym.rb +0 -45
  66. data/lib/toponym_search_criteria.rb +0 -44
  67. data/lib/web_service.rb +0 -493
  68. data/nbproject/private/private.properties +0 -0
  69. data/nbproject/private/private.xml +0 -4
  70. data/nbproject/project.properties +0 -3
  71. data/nbproject/project.xml +0 -15
@@ -0,0 +1,6 @@
1
+ .bundle
2
+ .rvmrc
3
+ coverage
4
+ Gemfile.lock
5
+ vendor/bundle
6
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "rcov", "~> 0.9.9", :platforms => :ruby_18
7
+ end
@@ -0,0 +1,82 @@
1
+ # Geonames Ruby API [![Build Status](https://api.travis-ci.org/mceachen/ruby-geonames.png?branch=master)](https://travis-ci.org/mceachen/ruby-geonames)
2
+
3
+ Ruby library for [Geonames Web Services](http://www.geonames.org/export/)
4
+
5
+ Created by [TouchBase Counsulting](http://www.tbcn.ca/geonames) to support GIS processes for [Carpool Connect](http://www.carpoolconnect.com/). Inspired by the Geonames [Java Client API library](http://www.geonames.org/source-code/).
6
+
7
+ ## Installing ruby-geonames
8
+
9
+ Install from the command line:
10
+
11
+ sudo gem install ruby-geonames
12
+
13
+ ## Examples
14
+
15
+ The following exercises several of the Geonames web services, [full list of services](http://www.geonames.org/export/).
16
+
17
+ **Load the the geonames API**
18
+
19
+ require 'geonames'
20
+
21
+ **get list of places near by longitude/longitude location**
22
+
23
+ places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834
24
+ p places_nearby
25
+
26
+ outputs:
27
+
28
+ `[#<Geonames::Toponym:0x6c8c98 @population=nil, @geoname_id='6094578', @longitude=-78.849568878, @feature_class_name=nil, @country_name='Canada', @latitude=43.900120387, @feature_class='P', @country_code='CA', @name='Oshawa', @feature_code_name=nil, @elevation=nil, @distance=2.6679930307932, @alternate_names=nil, @feature_code='PPL'>]`
29
+
30
+ **get timezone for longitude/longitude location**
31
+
32
+ timezone = Geonames::WebService.timezone 43.900120387, -78.882869834
33
+ p timezone
34
+
35
+ **get country code for longitude/longitude location**
36
+
37
+ country_code = Geonames::WebService.country_code 43.900120387, -78.882869834
38
+ p country_code
39
+
40
+ **get country sub-division info for longitude/longitude location**
41
+
42
+ country_subdivision = Geonames::WebService.country_subdivision 43.900120387, -78.882869834
43
+ p country_subdivision
44
+
45
+ **get postal codes for a given location**
46
+
47
+ postal_code_sc = Geonames::PostalCodeSearchCriteria.new
48
+ postal_code_sc.place_name = "Oshawa"
49
+ postal_codes = Geonames::WebService.postal_code_search postal_code_sc
50
+ p postal_codes
51
+
52
+ **get nearby postal codes for a place name**
53
+
54
+ postal_code_sc = Geonames::PostalCodeSearchCriteria.new
55
+ postal_code_sc.place_name = "Oshawa"
56
+ postal_codes_nearby = Geonames::WebService.find_nearby_postal_codes postal_code_sc
57
+ p postal_codes_nearby
58
+
59
+ ## Language Support
60
+
61
+ Geonames uses 'en' as the default locale. You can change the requested language like this:
62
+
63
+ Geonames.lang = 'de'
64
+
65
+ In a Rails application, you could set the language from the I18n.locale:
66
+
67
+ Geonames.lang = I18n.locale
68
+
69
+ ## Commercial Service Support
70
+
71
+ If you use the commercial service, you should put something like this in your configuration:
72
+
73
+ Geonames.username = 'username'
74
+ Geonames.base_url = 'http://ws.geonames.net'
75
+
76
+ In a Rails application, this could go into `config/initializers/geonames.rb`.
77
+
78
+ # Contributors
79
+
80
+ 1. Adam Wisniewski
81
+ 2. Nicolas Marchildon (elecnix)
82
+ 3. Chris Griego
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ namespace :spec do
9
+ RSpec::Core::RakeTask.new :rcov do |task|
10
+ task.rcov = true
11
+ task.rcov_opts = "--exclude spec/*,gems/*"
12
+ end if RUBY_VERSION < "1.9"
13
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "geonames")
@@ -1,12 +1,13 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
7
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
8
+ # the License at
8
9
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
10
11
  #
11
12
  # Unless required by applicable law or agreed to in writing, software
12
13
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -15,26 +16,41 @@
15
16
  # the License.
16
17
  #
17
18
  #=============================================================================
19
+ require 'geonames/version'
18
20
 
19
- require 'cgi'
20
- require 'net/http'
21
- require 'rexml/document'
21
+ module Geonames
22
+ autoload :BoundingBox, 'geonames/bounding_box'
23
+ autoload :Config, 'geonames/config'
24
+ autoload :CountryInfo, 'geonames/country_info'
25
+ autoload :CountrySubdivision, 'geonames/country_subdivision'
26
+ autoload :Intersection, 'geonames/intersection'
27
+ autoload :PostalCode, 'geonames/postal_code'
28
+ autoload :PostalCodeSearchCriteria, 'geonames/postal_code_search_criteria'
29
+ autoload :Timezone, 'geonames/timezone'
30
+ autoload :Toponym, 'geonames/toponym'
31
+ autoload :ToponymSearchCriteria, 'geonames/toponym_search_criteria'
32
+ autoload :ToponymSearchResult, 'geonames/toponym_search_result'
33
+ autoload :WebService, 'geonames/web_service'
34
+ autoload :WikipediaArticle, 'geonames/wikipedia_article'
22
35
 
23
- require 'web_service'
24
- require 'toponym'
25
- require 'toponym_search_result'
26
- require 'toponym_search_criteria'
27
- require 'postal_code'
28
- require 'postal_code_search_criteria'
29
- require 'timezone'
30
- require 'country_subdivision'
31
- require 'wikipedia_article'
32
- require 'intersection'
36
+ GEONAMES_SERVER = "http://ws.geonames.net"
37
+ USER_AGENT = "geonames ruby webservice client #{VERSION}"
33
38
 
34
- module Geonames
35
-
36
- GEONAMES_SERVER = "http://ws.geonames.org"
37
- USER_AGENT = "geonames ruby webservice client 0.1"
38
-
39
- end
39
+ class << self
40
+ def config
41
+ Thread.current[:geonames_config] ||= Geonames::Config.new
42
+ end
40
43
 
44
+ %w(base_url lang username token).each do |method|
45
+ module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
46
+ def #{method}
47
+ config.#{method}
48
+ end
49
+
50
+ def #{method}=(value)
51
+ config.#{method} = (value)
52
+ end
53
+ DELEGATORS
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ module Geonames
2
+ class BoundingBox
3
+ attr_accessor :north_point, :south_point, :east_point, :west_point
4
+
5
+ def initialize(north=0.0, south=0.0, east=0.0, west=0.0)
6
+ self.north_point = north
7
+ self.south_point = south
8
+ self.east_point = east
9
+ self.west_point = west
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,13 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2010 Jan Schwenzien <jan@schwenzien.info>
4
+ # Contributions by Chris Griego
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
7
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
8
+ # the License at
8
9
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
10
11
  #
11
12
  # Unless required by applicable law or agreed to in writing, software
12
13
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -17,16 +18,28 @@
17
18
  #=============================================================================
18
19
 
19
20
  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
-
21
+ class Config
22
+ @@default_base_url = "http://api.geonames.org"
23
+ @@default_lang = "en"
24
+ @@default_username = nil
25
+ @@default_token = nil
26
+
27
+ attr_writer :base_url, :lang, :username, :token
28
+
29
+ def base_url
30
+ @base_url || @@default_base_url
31
+ end
32
+
33
+ def lang
34
+ @lang || @@default_lang
29
35
  end
30
- end
31
36
 
37
+ def username
38
+ @username || @@default_username
39
+ end
32
40
 
41
+ def token
42
+ @token || @@default_token
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ require 'geonames/bounding_box'
2
+
3
+ module Geonames
4
+ class CountryInfo
5
+ attr_accessor :country_code, :country_name, :iso_numeric, :iso_alpha_3,
6
+ :fips_code, :continent, :capital, :area_sq_km, :population,
7
+ :currency_code, :geoname_id, :bounding_box, :languages
8
+
9
+ def initialize
10
+ @bounding_box = BoundingBox.new
11
+ @languages = []
12
+ end
13
+
14
+ def set_bounding_box(north, south, east, west)
15
+ @bounding_box = BoundingBox.new(north, south, east, west)
16
+ end
17
+ end
18
+ end
@@ -1,12 +1,13 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
7
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
8
+ # the License at
8
9
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
10
11
  #
11
12
  # Unless required by applicable law or agreed to in writing, software
12
13
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -16,6 +17,10 @@
16
17
  #
17
18
  #=============================================================================
18
19
 
19
- require 'rake'
20
- require 'rake/testtask'
21
- require 'rake/rdoctask'
20
+ module Geonames
21
+ class CountrySubdivision
22
+ attr_accessor :country_code, :country_name,
23
+ :admin_name_1, :admin_code_1,
24
+ :code_fips, :code_iso
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
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 Intersection
22
+ attr_accessor :street_1, :street_2,
23
+ :postal_code, :place_name, :country_code,
24
+ :latitude, :longitude, :admin_name_1,
25
+ :admin_code_1, :admin_name_2, :admin_code_2,
26
+ :distance
27
+ end
28
+ end
@@ -1,12 +1,13 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
7
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
8
+ # the License at
8
9
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
10
11
  #
11
12
  # Unless required by applicable law or agreed to in writing, software
12
13
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -17,17 +18,10 @@
17
18
  #=============================================================================
18
19
 
19
20
  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
21
+ class PostalCode
22
+ attr_accessor :postal_code, :place_name, :country_code,
23
+ :latitude, :longitude, :admin_name_1,
24
+ :admin_code_1, :admin_name_2, :admin_code_2,
25
+ :distance
26
+ end
31
27
  end
32
-
33
-
@@ -0,0 +1,50 @@
1
+ #=============================================================================
2
+ #
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
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
+ require 'cgi'
21
+
22
+ module Geonames
23
+ class PostalCodeSearchCriteria
24
+ attr_accessor :postal_code, :place_name, :country_code,
25
+ :latitude, :longitude, :style,
26
+ :max_rows, :is_or_operator, :radius
27
+
28
+ def initialize(params={})
29
+ params.each do |attr, value|
30
+ self.public_send("#{attr}=", value)
31
+ end if params
32
+
33
+ @is_or_operator = false
34
+ end
35
+
36
+ def to_query_params_string
37
+ url = ''
38
+ url << "&postalcode=" + CGI.escape(@postal_code) unless @postal_code.nil?
39
+ url << "&placename=" + CGI.escape(@place_name) unless @place_name.nil?
40
+ url << "&lat=" + CGI.escape(@latitude.to_s) unless @latitude.nil?
41
+ url << "&lng=" + CGI.escape(@longitude.to_s) unless @longitude.nil?
42
+ url << "&style=" + CGI.escape(@style) unless @style.nil?
43
+ url << "&country=" + CGI.escape(@country_code) unless @country_code.nil?
44
+ url << "&maxRows=" + CGI.escape(@max_rows.to_s) unless @max_rows.nil?
45
+ url << "&radius=" + CGI.escape(@radius.to_s) unless @radius.nil?
46
+ url << "&operator=OR" if @is_or_operator
47
+ url
48
+ end
49
+ end
50
+ end
@@ -1,12 +1,13 @@
1
1
  #=============================================================================
2
2
  #
3
- # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
3
+ # Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
4
+ # Contributions by Chris Griego
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
7
  # use this file except in compliance with the License. You may obtain a copy of
7
- # the License at
8
+ # the License at
8
9
  #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
10
11
  #
11
12
  # Unless required by applicable law or agreed to in writing, software
12
13
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@@ -17,13 +18,11 @@
17
18
  #=============================================================================
18
19
 
19
20
  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
21
+ class Timezone
22
+ attr_accessor :timezone_id, :gmt_offset, :dst_offset, :raw_offset
23
+
24
+ def tzinfo
25
+ TZInfo::Timezone.get(timezone_id)
26
+ end
26
27
  end
27
28
  end
28
-
29
-