geonames_api 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b04085ba81b7f7d468df252648f0b343e510a422
4
- data.tar.gz: c9d634bfbdf444b769f43abd67bc2a5b2238ef56
3
+ metadata.gz: d605bbb8e74963f68bc1ceac9e757fa93e57803f
4
+ data.tar.gz: 3e1d3d10ff21f96b9e1403f57a0b75695379ace8
5
5
  SHA512:
6
- metadata.gz: d083c2f06f3b1a5f4d2d624206f586e652df601caea5ec3d78b3be993bac3252fc3b7ff93782dbbc1896573f1f00112bb73843ff5e11550adc274bb1490cc11c
7
- data.tar.gz: 1cdc8e7ee3166aec78f9e4c41b3023410c3e0a173e8900a5fb6e8210a2a1f7d63d9dfe1d44449660d4c8d3a7fc8d11b284199fb822da7c5d4777318aa3e2c7a0
6
+ metadata.gz: 70e98ad435c5e8045279998f9e098f33bfec6a02f8c31b7721d51603cd362353ae63771b773f2e29ddeff610688dfef9a0c1ed85c1d4b20186d83957779f5bd2
7
+ data.tar.gz: 70e213137e89e16725313b0f427ff9062da72c6828a24895027e023b1a0d601f22de94b083daf1e334a08c1811840ba7ea24334a0c1ffdaba5e7343489140267
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.2
3
4
  - 2.0.0
4
5
  - 1.9.3
5
6
 
@@ -1,4 +1,4 @@
1
- require 'cgi'
1
+ require 'uri'
2
2
 
3
3
  module GeoNamesAPI
4
4
  class Base < Entity
@@ -19,9 +19,9 @@ module GeoNamesAPI
19
19
 
20
20
  def self.where(params={})
21
21
  retries_remaining = GeoNamesAPI.retries
22
- url = url(params)
22
+ uri = uri(params)
23
23
  begin
24
- response = make_request(url)
24
+ response = make_request(uri)
25
25
  unless response.empty?
26
26
  parse_response(response, params)
27
27
  end
@@ -38,14 +38,12 @@ module GeoNamesAPI
38
38
  end
39
39
  end
40
40
 
41
- def self.make_request(url)
42
- JSON.load(open(url).read)
41
+ def self.make_request(uri)
42
+ JSON.load(open(uri, "User-Agent" => "#{GeoNamesAPI.name}/#{GeoNamesAPI::VERSION}").read)
43
43
  end
44
44
 
45
45
  private_class_method :make_request
46
46
 
47
- KEYS = %w(streetSegment geonames)
48
-
49
47
  def self.parse_response(response, request_params)
50
48
  GeoNamesAPI.logger.info "GEONAMES RESPONSE (#{Time.now}): #{response}" if GeoNamesAPI.logger
51
49
  if (status = response['status'])
@@ -56,13 +54,15 @@ module GeoNamesAPI
56
54
 
57
55
  private_class_method :parse_response
58
56
 
59
- def self.url(params={})
60
- endpoint = GeoNamesAPI.url + self::METHOD + params_to_url(GeoNamesAPI.params.merge(params))
57
+ def self.uri(params={})
58
+ endpoint = URI(GeoNamesAPI.url)
59
+ endpoint.path = "/%s" % self::METHOD # URI.path requires a leading /
60
+ endpoint.query = URI.encode_www_form(GeoNamesAPI.params.merge(params))
61
61
  GeoNamesAPI.logger.info "GEONAMES REQUEST (#{Time.now}): #{endpoint}" if GeoNamesAPI.logger
62
62
  endpoint
63
63
  end
64
64
 
65
- private_class_method :url
65
+ private_class_method :uri
66
66
 
67
67
  def self.name_params(names)
68
68
  return names.first if names.first.is_a? Hash
@@ -75,19 +75,5 @@ module GeoNamesAPI
75
75
 
76
76
  private_class_method :name_params
77
77
 
78
- def self.params_to_url(params={})
79
- esc_params = params.map do |key, value|
80
- "#{esc(key)}=#{esc(value)}"
81
- end
82
- "?#{esc_params.join('&')}"
83
- end
84
-
85
- private_class_method :params_to_url
86
-
87
- def self.esc(str)
88
- CGI::escape(str.to_s)
89
- end
90
-
91
- private_class_method :esc
92
78
  end
93
79
  end
@@ -2,7 +2,7 @@ module GeoNamesAPI
2
2
  class Children < ListEndpoint
3
3
 
4
4
  METHOD = "childrenJSON"
5
- FIND_PARAMS = %w(geonameId)
5
+ FIND_PARAMS = %w(geonameId maxRows)
6
6
 
7
7
  end
8
8
  end
@@ -9,11 +9,18 @@ module GeoNamesAPI
9
9
  attr_reader :request_params
10
10
 
11
11
  def initialize(response, request_params = nil)
12
- @response = response
13
- @request_params = request_params
12
+ marshal_load([response, request_params])
13
+ end
14
+
15
+ def marshal_load(x)
16
+ @response, @request_params = x
14
17
  parse_response
15
18
  end
16
19
 
20
+ def marshal_dump
21
+ [@response, @request_params]
22
+ end
23
+
17
24
  def parse_response
18
25
  @response.keys.each { |ea| parse_attr(ea) }
19
26
  end
@@ -1,3 +1,3 @@
1
1
  module GeoNamesAPI
2
- VERSION = Gem::Version.new('0.1.4')
2
+ VERSION = Gem::Version.new('0.1.5')
3
3
  end
@@ -2,26 +2,26 @@ require 'spec_helper'
2
2
 
3
3
 
4
4
  describe GeoNamesAPI::CountrySubdivision do
5
- def should_be_sf(result)
6
- result.should be_present
7
- result.admin_code1.should == 'CA'
8
- result.admin_name1.should == 'California'
9
- result.country_code.should == 'US'
10
- result.country_name.should == 'United States'
5
+ def expected_sf(result)
6
+ expect(result).to be_present
7
+ expect(result.admin_code1).to eq('CA')
8
+ expect(result.admin_name1).to eq('California')
9
+ expect(result.country_code).to eq('US')
10
+ expect(result.country_name).to eq('United States')
11
11
  end
12
12
 
13
13
  describe '::find' do
14
- it 'should find one subdivision' do
14
+ it 'expect to find one subdivision' do
15
15
  result = GeoNamesAPI::CountrySubdivision.find(37.8, -122.4)
16
- should_be_sf(result)
16
+ expected_sf(result)
17
17
  end
18
18
  end
19
19
 
20
20
  describe '::all' do
21
- it 'should find multiple subdivisions' do
21
+ it 'expect to find multiple subdivisions' do
22
22
  result = GeoNamesAPI::CountrySubdivision.all(37.8, -122.4)
23
- result.size.should > 0
24
- should_be_sf(result.first)
23
+ expect(result.size).to be > 0
24
+ expected_sf(result.first)
25
25
  end
26
26
  end
27
27
  end
@@ -1,28 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::Hierarchy do
4
- def should_be_sb(h)
5
- h.first.name.should == 'Earth'
6
- h.map { |ea| ea.name }.should ==
7
- ['Earth', 'North America', 'United States', 'California', 'Santa Barbara County', 'Santa Barbara']
4
+ def expect_sb(h)
5
+ expect(h.first.name).to eq('Earth')
6
+ expect(h.map(&:name)).to match_array(
7
+ ['Earth', 'North America', 'United States', 'California', 'Santa Barbara County', 'Santa Barbara'])
8
8
  end
9
9
 
10
- def should_be_roma(h)
11
- h.map { |ea| ea.name }.should ==
12
- ['Terra', 'Europa', 'Italia', 'Lazio', 'Roma', 'Roma', 'Roma']
10
+ def expect_roma(h)
11
+ expect(h.map(&:name)).to match_array(
12
+ ['Terra', 'Europa', 'Italia', 'Lazio', 'Roma', 'Roma', 'Roma'])
13
13
  end
14
14
 
15
15
  describe '::find' do
16
16
  it 'works for Santa Barbara' do
17
17
  h = GeoNamesAPI::Hierarchy.find(5392952)
18
- should_be_sb(h)
18
+ expect_sb(h)
19
19
  end
20
20
 
21
21
  it 'works for Roma ' do
22
22
  begin
23
23
  GeoNamesAPI.lang = :it
24
24
  h = GeoNamesAPI::Hierarchy.find(3169070)
25
- should_be_roma(h)
25
+ expect_roma(h)
26
26
  ensure
27
27
  GeoNamesAPI.lang = :en
28
28
  end
@@ -32,7 +32,7 @@ describe GeoNamesAPI::Hierarchy do
32
32
  describe '::where' do
33
33
  it 'works for Santa Barbara' do
34
34
  h = GeoNamesAPI::Hierarchy.where(geonameId: 5392952)
35
- should_be_sb(h)
35
+ expect_sb(h)
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'pathname'
3
+
4
+ describe GeoNamesAPI::Entity do
5
+ let(:filename) do
6
+ (Pathname.new(__FILE__).parent + "place.ser").realdirpath
7
+ end
8
+
9
+ class TestPlace < GeoNamesAPI::ListEndpoint
10
+ METHOD = "findNearbyJSON"
11
+ FIND_PARAMS = %w(lat lng radius maxRows)
12
+ end
13
+
14
+ def ensure_place_exists
15
+ unless filename.exist?
16
+ puts "Sorry, #{filename} didn't exist yet. Run this test again."
17
+ place = TestPlace.find(lat: 37.8018, lng: -122.3971, radius: 0.25)
18
+ filename.open('wb') { |io| Marshal.dump(place, io) }
19
+ end
20
+ end
21
+
22
+ # NOTE: if these fail, try deleting place.ser and re-running the spec.
23
+ it 'round-trips when an Entity has not been loaded yet' do
24
+ ensure_place_exists
25
+ obj = Marshal.load(filename.open('rb'))
26
+ expect(obj).to respond_to(:geoname_id)
27
+ expect(obj).to respond_to(:country_code)
28
+ expect(obj).to respond_to(:lat)
29
+ expect(obj).to respond_to(:lng)
30
+ expect(obj).to be_a(TestPlace)
31
+
32
+ expect(obj.geoname_id).to eq(5382567)
33
+ expect(obj.country_code).to eq('US')
34
+ expect(obj.lat).to be_within(0.001).of(37.8018)
35
+ expect(obj.lng).to be_within(0.001).of(-122.3971)
36
+ end
37
+ end
@@ -13,8 +13,8 @@ describe GeoNamesAPI::NearestIntersection do
13
13
  it 'returns the two street names of the nearest intersection' do
14
14
  response = described_class.find(latitude,longitude)
15
15
 
16
- response.intersection["street1"].should eq('Bertrand St')
17
- response.intersection["street2"].should eq('N 11th St')
16
+ expect(response.intersection["street1"]).to eq('Bertrand St')
17
+ expect(response.intersection["street2"]).to eq('N 11th St')
18
18
  end
19
19
  end
20
20
  end
@@ -2,13 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::Neighbourhood do
4
4
  describe "::find" do
5
- it "should find NYC" do
5
+ it "expect to find NYC" do
6
6
  result = GeoNamesAPI::Neighbourhood.find(lat: 40.78343, lng: -73.96625)
7
- result.hierarchy.should == ["United States", "New York", "New York County", "New York City-Manhattan", "Central Park"]
7
+ expect(result.hierarchy).to match_array(
8
+ ["United States", "New York", "New York County", "New York City-Manhattan", "Central Park"])
8
9
  end
9
10
 
10
- it "should not find streets outside of the US" do
11
- proc { GeoNamesAPI::Neighbourhood.find(0, 0) }.should raise_error GeoNamesAPI::Error
11
+ it "expect to not find streets outside of the US" do
12
+ expect { GeoNamesAPI::Neighbourhood.find(0, 0) }.to raise_error GeoNamesAPI::Error
12
13
  end
13
14
  end
14
15
  end
@@ -2,21 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::PlaceName do
4
4
  describe "::find" do
5
- it "should find one place" do
5
+ it "expect to find one place" do
6
6
  result = GeoNamesAPI::PlaceName.find("50.01","10.2")
7
- result.should be_present
7
+ expect(result).to be_present
8
8
  end
9
9
 
10
- it 'should find one place with a hash' do
10
+ it 'expect to find one place with a hash' do
11
11
  result = GeoNamesAPI::PlaceName.find(lat: 50.01, lng: 10.2)
12
- result.should be_present
12
+ expect(result).to be_present
13
13
  end
14
14
  end
15
15
 
16
16
  describe "::all" do
17
- it "should find maxRow places in 100km radius" do
17
+ it "expect to find maxRow places in 100km radius" do
18
18
  result = GeoNamesAPI::PlaceName.all("50.01","10.2","100", "3")
19
- result.count.should == 3
19
+ expect(result.size).to eq(3)
20
20
  end
21
21
  end
22
22
  end
@@ -2,61 +2,61 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::PlaceSearch do
4
4
  describe "::where" do
5
- it "should search for places dealing with ohio" do
5
+ it "expect to search for places dealing with ohio" do
6
6
  search = GeoNamesAPI::PlaceSearch.where(name: 'idaho', maxRows: 10)
7
- search.should be_present
8
- search.size.should == 10
9
- search.results.size.should == 10
7
+ expect(search).to be_present
8
+ expect(search.size).to eq(10)
9
+ expect(search.results.size).to eq(10)
10
10
  end
11
11
  end
12
12
 
13
13
  describe "::find_by_place_name" do
14
- it "should find the place by name" do
14
+ it "expect to find the place by name" do
15
15
  search = GeoNamesAPI::PlaceSearch.find_by_place_name("idaho", 10)
16
- search.total_results_count.should > 0
17
- search.results.each{|place| place.name.should =~ /idaho/i }
16
+ expect(search.total_results_count).to be > 10
17
+ search.results.each{|place| expect(place.name).to match(/idaho/i) }
18
18
  end
19
19
  end
20
20
 
21
21
  describe "::find_by_exact_place_name" do
22
- it "should find the place by the exact name" do
22
+ it "expect to find the place by the exact name" do
23
23
  search = GeoNamesAPI::PlaceSearch.find_by_exact_place_name('columbus', 10)
24
- search.total_results_count.should > 0
25
- search.results.each{|place| place.name.downcase.should == 'columbus' }
24
+ expect(search.total_results_count).to be > 0
25
+ search.results.each{|place| expect(place.name.downcase).to eq('columbus') }
26
26
  end
27
27
  end
28
28
 
29
29
  describe "#next_page" do
30
- it "should grab the next page of results from the same search" do
30
+ it "expect to grab the next page of results from the same search" do
31
31
  page_size = 3
32
32
  big_search = GeoNamesAPI::PlaceSearch.where(name: 'goleta', maxRows: page_size * 4)
33
33
  search_pg1 = GeoNamesAPI::PlaceSearch.where(name: 'goleta', maxRows: page_size)
34
- search_pg1.size.should == page_size
34
+ expect(search_pg1.size).to eq(page_size)
35
35
 
36
36
  search_pg2 = search_pg1.next_page
37
- search_pg2.size.should == page_size
38
- search_pg2.request_params[:startRow].should == page_size
37
+ expect(search_pg2.size).to eq(page_size)
38
+ expect(search_pg2.request_params[:startRow]).to eq(page_size)
39
39
 
40
40
  search_pg3 = search_pg2.next_page
41
- search_pg3.request_params[:startRow].should == page_size * 2
42
- search_pg3.size.should == page_size
41
+ expect(search_pg3.request_params[:startRow]).to eq(page_size * 2)
42
+ expect(search_pg3.size).to eq(page_size)
43
43
 
44
44
  # Ordering isn't always deterministic, so we're just looking for an overlap bigger than 1 page size:
45
45
  expected_ids = big_search.results.map { |ea| ea.geoname_id }
46
46
  paged_ids = (search_pg1.results + search_pg2.results + search_pg3.results).map { |ea| ea.geoname_id }
47
47
  matching_ids = paged_ids & expected_ids
48
48
  if matching_ids.size <= page_size
49
- # use .should just to render a nice error message:
50
- paged_ids.should =~ matching_ids
49
+ # use expect.to just to render a nice error message:
50
+ expect(paged_ids).to match(matching_ids)
51
51
  end
52
52
  end
53
53
  end
54
54
 
55
55
  describe "#to_page" do
56
- it "should set startRow appropriately" do
56
+ it "expect to set startRow appropriately" do
57
57
  search2 = GeoNamesAPI::PlaceSearch.all("columbus", 2)
58
58
  page4 = search2.to_page(4)
59
- page4.request_params[:startRow].should == 8
59
+ expect(page4.request_params[:startRow]).to eq(8)
60
60
  end
61
61
  end
62
62
  end
@@ -2,16 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::Place do
4
4
  describe "::find" do
5
- it "should find one place" do
5
+ it "expect to find one place" do
6
6
  result = GeoNamesAPI::Place.find("50.01","10.2")
7
- result.should be_present
7
+ expect(result).to be_present
8
8
  end
9
9
  end
10
10
 
11
11
  describe "::all" do
12
- it "should find multiple places in 100km radius" do
12
+ it "expect to find multiple places in 100km radius" do
13
13
  result = GeoNamesAPI::Place.all("50.01","10.2","100")
14
- result.size.should > 0
14
+ expect(result.size).to be > 0
15
15
  end
16
16
  end
17
17
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe GeoNamesAPI::Base do
4
4
  describe "retries" do
5
5
  before :each do
6
- GeoNamesAPI.stub(:max_sleep_time_between_retries).and_return(0)
6
+ allow(GeoNamesAPI).to receive(:max_sleep_time_between_retries).and_return(0)
7
7
  @timeout = JSON.load <<-JSON
8
8
  {
9
9
  "status": {
@@ -19,17 +19,17 @@ describe GeoNamesAPI::Base do
19
19
  end
20
20
 
21
21
  it "retries when geonames returns timeout errors" do
22
- GeoNamesAPI::Hierarchy.stub(:make_request).and_return(@timeout, @response)
22
+ allow(GeoNamesAPI::Hierarchy).to receive(:make_request).and_return(@timeout, @response)
23
23
  hierarchy = GeoNamesAPI::Hierarchy.find(6295630)
24
24
  earth = hierarchy.first
25
- earth.should be_present
26
- earth.name.should == "Earth"
25
+ expect(earth).to be_present
26
+ expect(earth.name).to eq("Earth")
27
27
  end
28
28
 
29
29
  it "fails when geonames returns timeout errors too many times" do
30
- GeoNamesAPI::Hierarchy.stub(:make_request).and_return(@timeout, @timeout, @response)
31
- GeoNamesAPI.stub(:retries).and_return(1)
32
- proc { GeoNamesAPI::Hierarchy.find(6295630) }.should raise_error GeoNamesAPI::Timeout
30
+ allow(GeoNamesAPI::Hierarchy).to receive(:make_request).and_return(@timeout, @timeout, @response)
31
+ allow(GeoNamesAPI).to receive(:retries).and_return(1)
32
+ expect { GeoNamesAPI::Hierarchy.find(6295630) }.to raise_error GeoNamesAPI::Timeout
33
33
  end
34
34
  end
35
35
  end
@@ -2,21 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::Street do
4
4
  describe "::find" do
5
- it "should find one Street" do
5
+ it "expect to find one Street" do
6
6
  result = GeoNamesAPI::Street.find(37.451, -122.18)
7
- result.should be_present
7
+ expect(result).to be_present
8
8
  end
9
9
 
10
- it "should not find streets outside of the US" do
10
+ it "expect to not find streets outside of the US" do
11
11
  result = GeoNamesAPI::Street.find(50.01, 10.2)
12
- result.should == nil
12
+ expect(result).to be_nil
13
13
  end
14
14
  end
15
15
 
16
16
  describe "::all" do
17
- it "should find multiple Streets in 100km radius" do
17
+ it "expect to find multiple Streets in 100km radius" do
18
18
  result = GeoNamesAPI::Street.all(37.8, -122.4, 1, 3)
19
- result.size.should == 3
19
+ expect(result.size).to eq(3)
20
20
  end
21
21
  end
22
22
  end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoNamesAPI::WeatherICAO do
4
4
  describe "::find" do
5
- it "should find one station" do
5
+ it "expect to find one station" do
6
6
  result = GeoNamesAPI::WeatherICAO.find('KHAF')
7
- result.should be_present
7
+ expect(result).to be_present
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geonames_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Devine
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-14 00:00:00.000000000 Z
12
+ date: 2014-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -130,8 +130,10 @@ files:
130
130
  - spec/geonames_api/country_spec.rb
131
131
  - spec/geonames_api/country_subdivision_spec.rb
132
132
  - spec/geonames_api/hierarchy_spec.rb
133
+ - spec/geonames_api/marshalling_spec.rb
133
134
  - spec/geonames_api/nearest_intersection_spec.rb
134
135
  - spec/geonames_api/neighbourhood_spec.rb
136
+ - spec/geonames_api/place.ser
135
137
  - spec/geonames_api/place_name_spec.rb
136
138
  - spec/geonames_api/place_search_spec.rb
137
139
  - spec/geonames_api/place_spec.rb
@@ -170,8 +172,10 @@ test_files:
170
172
  - spec/geonames_api/country_spec.rb
171
173
  - spec/geonames_api/country_subdivision_spec.rb
172
174
  - spec/geonames_api/hierarchy_spec.rb
175
+ - spec/geonames_api/marshalling_spec.rb
173
176
  - spec/geonames_api/nearest_intersection_spec.rb
174
177
  - spec/geonames_api/neighbourhood_spec.rb
178
+ - spec/geonames_api/place.ser
175
179
  - spec/geonames_api/place_name_spec.rb
176
180
  - spec/geonames_api/place_search_spec.rb
177
181
  - spec/geonames_api/place_spec.rb
@@ -179,3 +183,4 @@ test_files:
179
183
  - spec/geonames_api/street_spec.rb
180
184
  - spec/geonames_api/weather_icao_spec.rb
181
185
  - spec/spec_helper.rb
186
+ has_rdoc: