diff --git a/.gitignore b/.gitignore index 3c7cfff..90ef2f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ *~ nbproject +.bundle +vendor/bundle +coverage diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..0eb654b --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format nested diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c80ee36 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "http://rubygems.org" + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..33316f1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,31 @@ +PATH + remote: . + specs: + ruby-geonames (0.2.7) + +GEM + remote: http://rubygems.org/ + specs: + diff-lcs (1.1.2) + fakeweb (1.3.0) + rake (0.8.7) + rcov (0.9.9) + rspec (2.0.0.beta.20) + rspec-core (= 2.0.0.beta.20) + rspec-expectations (= 2.0.0.beta.20) + rspec-mocks (= 2.0.0.beta.20) + rspec-core (2.0.0.beta.20) + rspec-expectations (2.0.0.beta.20) + diff-lcs (>= 1.1.2) + rspec-mocks (2.0.0.beta.20) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.0.0) + fakeweb (~> 1.3.0) + rake + rcov (~> 0.9.9) + rspec (= 2.0.0.beta.20) + ruby-geonames! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..31f6558 --- /dev/null +++ b/Rakefile @@ -0,0 +1,14 @@ +require 'bundler/setup' +Bundler.require(:default, :development) +require 'rspec/core/rake_task' + +task :default => :spec + +RSpec::Core::RakeTask.new + +namespace :spec do + RSpec::Core::RakeTask.new :rcov do |task| + task.rcov = true + task.rcov_opts = "--exclude spec/*,gems/*" + end +end diff --git a/lib/Rakefile.rb b/lib/Rakefile.rb deleted file mode 100644 index 1f32ca7..0000000 --- a/lib/Rakefile.rb +++ /dev/null @@ -1,21 +0,0 @@ -#============================================================================= -# -# Copyright 2007 Adam Wisniewski -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may not -# use this file except in compliance with the License. You may obtain a copy of -# the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations under -# the License. -# -#============================================================================= - -require 'rake' -require 'rake/testtask' -require 'rake/rdoctask' diff --git a/lib/postal_code_search_criteria.rb b/lib/postal_code_search_criteria.rb index 8e4bc6e..1424fb7 100644 --- a/lib/postal_code_search_criteria.rb +++ b/lib/postal_code_search_criteria.rb @@ -49,15 +49,15 @@ def to_query_params_string end if !@latitude.nil? - url = url + "&lat" + CGI::escape( @latitude.to_s ) + url = url + "&lat=" + CGI::escape( @latitude.to_s ) end if !@longitude.nil? - url = url + "&lng" + CGI::escape( @longitude.to_s ) + url = url + "&lng=" + CGI::escape( @longitude.to_s ) end if !@style.nil? - url = url + "&style" + CGI::escape( @style ) + url = url + "&style=" + CGI::escape( @style ) end if !@country_code.nil? @@ -65,7 +65,7 @@ def to_query_params_string end if !@max_rows.nil? - url = url + "&maxRows=" + CGI::escape( @max_rows ) + url = url + "&maxRows=" + CGI::escape( @max_rows.to_s ) end if !@radius.nil? diff --git a/ruby-geonames.gemspec b/ruby-geonames.gemspec index 7d3e8c1..3e4ded1 100644 --- a/ruby-geonames.gemspec +++ b/ruby-geonames.gemspec @@ -1,15 +1,20 @@ -SPEC = Gem::Specification.new do |s| - s.name = "ruby-geonames" +Gem::Specification.new do |s| + s.name = "ruby-geonames" s.version = "0.2.7" - s.author = "Adam Wisniewski" - s.email = "adamw@tbcn.ca" + s.author = "Adam Wisniewski" + s.email = "adamw@tbcn.ca" s.date = "2009-07-07" s.homepage = "http://github.com/elecnix/ruby-geonames" - s.platform = Gem::Platform::RUBY - s.summary = "Ruby library for Geonames Web Services (http://www.geonames.org/export/)" - s.files = ["README.markdown", "lib/timezone.rb", "lib/wikipedia_article.rb", "lib/geonames.rb", "lib/postal_code_search_criteria.rb", "lib/intersection.rb", "lib/country_subdivision.rb", "lib/toponym.rb", "lib/toponym_search_result.rb", "lib/bounding_box.rb", "lib/web_service.rb", "lib/postal_code.rb", "lib/toponym_search_criteria.rb", "lib/tc_country_info.rb", "lib/main.rb", "lib/Rakefile.rb", "lib/address.rb", "lib/country_info.rb"] + s.platform = Gem::Platform::RUBY + s.summary = "Ruby library for Geonames Web Services (http://www.geonames.org/export/)" + s.files = ["README.markdown", "lib/timezone.rb", "lib/wikipedia_article.rb", "lib/geonames.rb", "lib/postal_code_search_criteria.rb", "lib/intersection.rb", "lib/country_subdivision.rb", "lib/toponym.rb", "lib/toponym_search_result.rb", "lib/bounding_box.rb", "lib/web_service.rb", "lib/postal_code.rb", "lib/toponym_search_criteria.rb", "lib/tc_country_info.rb", "lib/main.rb", "lib/address.rb", "lib/country_info.rb"] s.require_path = "lib" - s.has_rdoc = true + s.has_rdoc = true s.extra_rdoc_files = ["README.markdown"] -end - + s.required_rubygems_version = ">= 1.3.6" + s.add_development_dependency "bundler", "~> 1.0.0" + s.add_development_dependency "fakeweb", "~> 1.3.0" + s.add_development_dependency "rake" + s.add_development_dependency "rcov", "~> 0.9.9" + s.add_development_dependency "rspec", "2.0.0.beta.20" +end diff --git a/spec/fixtures/find_nearby_postal_codes/lat_lng.xml.http b/spec/fixtures/find_nearby_postal_codes/lat_lng.xml.http new file mode 100644 index 0000000..07b29ba --- /dev/null +++ b/spec/fixtures/find_nearby_postal_codes/lat_lng.xml.http @@ -0,0 +1,81 @@ +HTTP/1.1 200 OK +Date: Wed, 22 Sep 2010 01:12:12 GMT +Server: Apache/2.2.13 (Linux/SUSE) +Cache-Control: no-cache +Access-Control-Allow-Origin: * +Transfer-Encoding: chunked +Content-Type: text/xml;charset=UTF-8 + + + + +8750 +Klöntal +CH +47.021 +8.97896 +GL +Glarus +800 +Glarus +1609 +Glarus +2.827649162427546 + + +8750 +Glarus Zustellung +CH +47.021 +8.97896 +GL +Glarus +800 +Glarus +1609 +Glarus +2.827649162427546 + + +8772 +Nidfurn +CH +46.98333 +9.05 +GL +Glarus +800 +Glarus +1621 +Nidfurn +4.220733421769597 + + +8775 +Luchsingen +CH +46.96667 +9.03333 +GL +Glarus +800 +Glarus +1614 +Luchsingen +4.486650531422443 + + +8773 +Haslen GL +CH +46.96333 +9.03667 +GL +Glarus +800 +Glarus +1610 +Haslen +4.93536846606444 + + diff --git a/spec/geonames/postal_code_search_criteria_spec.rb b/spec/geonames/postal_code_search_criteria_spec.rb new file mode 100644 index 0000000..9bd0edc --- /dev/null +++ b/spec/geonames/postal_code_search_criteria_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +module Geonames + describe PostalCodeSearchCriteria do + describe "#to_query_params_string" do + context "with a numeric latitude" do + let(:latitude) { 25.123 } + before(:each) { subject.latitude = latitude } + it("contains the lat parameter") { subject.to_query_params_string.should =~ /lat=#{latitude}/ } + end + + context "with a numeric longitude" do + let(:longitude) { -52.123 } + before(:each) { subject.longitude = longitude } + it("contains the lng parameter") { subject.to_query_params_string.should =~ /lng=#{longitude}/ } + end + + context "with a numeric max_rows" do + let(:max_rows) { 1 } + before(:each) { subject.max_rows = max_rows } + it("contains the maxRows parameter") { subject.to_query_params_string.should =~ /maxRows=#{max_rows}/ } + end + + context "with a style" do + let(:style) { "SHORT" } + before(:each) { subject.style = style } + it("contains the style parameter") { subject.to_query_params_string.should =~ /style=#{style}/ } + end + end + end +end diff --git a/spec/geonames/web_service/postal_code_search_spec.rb b/spec/geonames/web_service/postal_code_search_spec.rb new file mode 100644 index 0000000..7c31b97 --- /dev/null +++ b/spec/geonames/web_service/postal_code_search_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +module Geonames + describe WebService do + describe ".postal_code_search" do + subject { Geonames::WebService.postal_code_search(criteria) } + + context "lookup by latitude and longitude" do + let(:criteria) do + criteria = Geonames::PostalCodeSearchCriteria.new + criteria.latitude = 47 + criteria.longitude = 9 + criteria + end + + before(:each) do + FakeWeb.register_uri(:get, /\/postalCodeSearch\?.*&lat=47.*&lng=9/, :response => File.read( + File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'find_nearby_postal_codes', 'lat_lng.xml.http') + )) + end + + it { should be_a_kind_of(Array) } + + it "returns PostalCode instances" do + subject.each do |postal_code| + postal_code.should be_a_kind_of PostalCode + end + end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..fb2de35 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,25 @@ +require 'rubygems' + +# Set up gems listed in the Gemfile. +gemfile = File.expand_path('../Gemfile', File.dirname(__FILE__)) +begin + ENV['BUNDLE_GEMFILE'] = gemfile + require 'bundler' + Bundler.setup +rescue Bundler::GemNotFound => e + STDERR.puts e.message + STDERR.puts "Try running `bundle install`." + exit! +end + +Bundler.require(:default, :development) + +Rspec.configure do |config| + config.mock_with :rspec +end + +# Requires supporting files with custom matchers and macros, etc, +# in ./support/ and its subdirectories. +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f } + +require 'geonames' diff --git a/spec/support/fakeweb.rb b/spec/support/fakeweb.rb new file mode 100644 index 0000000..331dd44 --- /dev/null +++ b/spec/support/fakeweb.rb @@ -0,0 +1,7 @@ +FakeWeb.allow_net_connect = false + +Rspec.configure do |config| + config.before(:each) do + FakeWeb.clean_registry + end +end