osmn 0.1.0

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.
data/LICENSE ADDED
File without changes
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/lib/osmn/base.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module OSMN
6
+ class Base
7
+
8
+ private
9
+ def build_query(params)
10
+ query = params.map do |key, value|
11
+ "#{key.to_s}=#{URI.escape(value.to_s)}" if value
12
+ end.compact.join('&')
13
+ end
14
+
15
+ def build_results(response)
16
+ parsed = JSON.parse(response)
17
+
18
+ return case parsed
19
+ when Hash then build_place(parsed)
20
+ when Array then parsed.map{ |place| build_place(place) }
21
+ end
22
+ end
23
+
24
+ def build_place(place)
25
+ if place.is_a? Hash
26
+ result = place.to_struct('Result')
27
+ if result.respond_to?('address')
28
+ result.address = result.address.to_struct('Address')
29
+ end
30
+
31
+ result
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/osmn/hash.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def to_struct(name)
3
+ Struct.new(name, *keys).new(*values)
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module OSMN
2
+ class Reverse < OSMN::Base
3
+
4
+ def initialize(params)
5
+ @params = {}
6
+ self.params = params
7
+ end
8
+
9
+ def reverse_geocode
10
+ if @params[:lat] && @params[:lon]
11
+ query = build_query(@params)
12
+
13
+ uri = URI("http://nominatim.openstreetmap.org/reverse?#{query}")
14
+ request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
15
+ response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
16
+
17
+ results = build_results(response.body) if response.code == '200'
18
+ end
19
+ end
20
+
21
+ def params=(value)
22
+ if value.is_a? Hash
23
+ value.each do |k,v|
24
+ @params[k.to_sym] = v unless v.nil?
25
+ end
26
+ end
27
+
28
+ # We override the format because we expect a JSON response
29
+ @params[:format] = :json
30
+ end
31
+
32
+ def params
33
+ @params
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ module OSMN
2
+ class Search < OSMN::Base
3
+
4
+ def initialize(params)
5
+ @params = {}
6
+ self.params = params
7
+ end
8
+
9
+ def search
10
+ if @params[:q]
11
+ query = build_query(@params)
12
+
13
+ uri = URI("http://nominatim.openstreetmap.org/search?#{query}")
14
+ request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
15
+ response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
16
+
17
+ results = build_results(response.body) if response.code == '200'
18
+ end
19
+ end
20
+
21
+ def params=(value)
22
+ if value.is_a? Hash
23
+ value.each do |k,v|
24
+ @params[k.to_sym] = v unless v.nil?
25
+ end
26
+ end
27
+
28
+ # We override the format because we expect a JSON response
29
+ @params[:format] = :json
30
+ end
31
+
32
+ def params
33
+ @params
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module OSMN
2
+ VERSION = '0.1.0'
3
+ end
data/lib/osmn.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'osmn/base'
2
+ require 'osmn/search'
3
+ require 'osmn/reverse'
4
+ require 'osmn/version'
5
+ require 'osmn/hash'
6
+
7
+ module OSMN
8
+
9
+ def self.search(q = nil, details = 0)
10
+ search = Search.new(:q => q, :addressdetails => details)
11
+ search.search
12
+ end
13
+
14
+ def self.reverse_geocode(lat = nil, lon = nil, details = 0)
15
+ search = Reverse.new(:lat => lat, :lon => lon, :addressdetails => details)
16
+ search.reverse_geocode
17
+ end
18
+
19
+ end
data/test/test_osmn.rb ADDED
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+ require 'osmn'
3
+
4
+ class Tests < Test::Unit::TestCase
5
+
6
+ def test_nil_search
7
+ response = OSMN::search
8
+
9
+ assert_equal(nil, response)
10
+ end
11
+
12
+ def test_nil_reverse
13
+ response = OSMN::reverse_geocode
14
+
15
+ assert_equal(nil, response)
16
+ end
17
+
18
+ def test_search
19
+ response = OSMN::search('135 pilkington avenue, birmingham')[0]
20
+
21
+ assert_equal('62311100', response.place_id)
22
+ end
23
+
24
+ def test_reverse
25
+ response = OSMN::reverse_geocode(52.5487429714954, -1.81602098644987)
26
+
27
+ assert_equal('62762024', response.place_id)
28
+ end
29
+
30
+ def test_search
31
+ response = OSMN::search('135 pilkington avenue, birmingham', 0)[0]
32
+
33
+ assert_respond_to(response, :place_id)
34
+ assert_respond_to(response, :licence)
35
+ assert_respond_to(response, :osm_type)
36
+ assert_respond_to(response, :osm_id)
37
+ assert_respond_to(response, :boundingbox)
38
+ assert_respond_to(response, :lat)
39
+ assert_respond_to(response, :lon)
40
+ assert_respond_to(response, :display_name)
41
+ assert_respond_to(response, :class)
42
+ assert_respond_to(response, :type)
43
+
44
+ assert_raise NoMethodError do
45
+ response.address
46
+ end
47
+ end
48
+
49
+
50
+ def test_search_with_details
51
+ response = OSMN::search('135 pilkington avenue, birmingham', 1)[0]
52
+
53
+ assert_respond_to(response, :place_id)
54
+ assert_respond_to(response, :licence)
55
+ assert_respond_to(response, :osm_type)
56
+ assert_respond_to(response, :osm_id)
57
+ assert_respond_to(response, :boundingbox)
58
+ assert_respond_to(response, :lat)
59
+ assert_respond_to(response, :lon)
60
+ assert_respond_to(response, :display_name)
61
+ assert_respond_to(response, :class)
62
+ assert_respond_to(response, :type)
63
+ assert_respond_to(response, :address)
64
+
65
+ assert_respond_to(response.address, :house_number)
66
+ assert_respond_to(response.address, :road)
67
+ assert_respond_to(response.address, :suburb)
68
+ assert_respond_to(response.address, :city)
69
+ assert_respond_to(response.address, :county)
70
+ assert_respond_to(response.address, :state_district)
71
+ assert_respond_to(response.address, :state)
72
+ assert_respond_to(response.address, :postcode)
73
+ assert_respond_to(response.address, :country)
74
+ assert_respond_to(response.address, :country_code)
75
+ end
76
+
77
+ def test_reverse_geocode
78
+ response = OSMN::reverse_geocode(52.5487429714954, -1.81602098644987, 0)
79
+
80
+ assert_respond_to(response, :place_id)
81
+ assert_respond_to(response, :licence)
82
+ assert_respond_to(response, :osm_type)
83
+ assert_respond_to(response, :osm_id)
84
+ assert_respond_to(response, :lat)
85
+ assert_respond_to(response, :lon)
86
+ assert_respond_to(response, :display_name)
87
+
88
+ assert_raise NoMethodError do
89
+ response.address
90
+ end
91
+ end
92
+
93
+ def test_reverse_geocode_with_address_details
94
+ response = OSMN::reverse_geocode(52.5487429714954, -1.81602098644987, 1)
95
+
96
+ assert_respond_to(response, :place_id)
97
+ assert_respond_to(response, :licence)
98
+ assert_respond_to(response, :osm_type)
99
+ assert_respond_to(response, :osm_id)
100
+ assert_respond_to(response, :lat)
101
+ assert_respond_to(response, :lon)
102
+ assert_respond_to(response, :display_name)
103
+ assert_respond_to(response, :address)
104
+
105
+ assert_respond_to(response.address, :house_number)
106
+ assert_respond_to(response.address, :road)
107
+ assert_respond_to(response.address, :suburb)
108
+ assert_respond_to(response.address, :city)
109
+ assert_respond_to(response.address, :county)
110
+ assert_respond_to(response.address, :state_district)
111
+ assert_respond_to(response.address, :state)
112
+ assert_respond_to(response.address, :postcode)
113
+ assert_respond_to(response.address, :country)
114
+ assert_respond_to(response.address, :country_code)
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: osmn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jorge Kalmbach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A Ruby wrapper for the OpenStreetMap Nominatim API.
47
+ email: kalmbach@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - README.md
53
+ - LICENSE
54
+ - Rakefile
55
+ - lib/osmn.rb
56
+ - lib/osmn/base.rb
57
+ - lib/osmn/search.rb
58
+ - lib/osmn/reverse.rb
59
+ - lib/osmn/hash.rb
60
+ - lib/osmn/version.rb
61
+ - test/test_osmn.rb
62
+ homepage: http://github.com/kalmbach/osmn
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A Ruby wrapper for the OpenStreetMap Nominatim API.
86
+ test_files:
87
+ - test/test_osmn.rb