ruby-nominatim 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed156fabbe98525ac066b5dd2eec91fa5afc8af4
4
+ data.tar.gz: 8ccba8b6597d95258c7e28cf3512f86ebd88b9e0
5
+ SHA512:
6
+ metadata.gz: 63d23fa498b1cf916124e1722108ca63d27015f9fe0ccf08f581a8994c217f333c4a8f52f85dae1ddd02ac27bf063335c392f886ba4b2dc5e81fb74cdf8837ef
7
+ data.tar.gz: 0503255b459f977cf01a4f19e77ab0685f9c58ce22dd43b8f938c30aa55315e35bc04da3e02427fd81f727e9404893ecb679d544563206c49c6c662cecf723b7
@@ -0,0 +1,36 @@
1
+ require "nominatim/version"
2
+ require "nominatim/configuration"
3
+ require "nominatim/point"
4
+ require "nominatim/polygon"
5
+ require "nominatim/address"
6
+ require "nominatim/place"
7
+ require "nominatim/response/parse_json"
8
+ require "nominatim/client"
9
+ require "nominatim/search"
10
+ require "nominatim/reverse"
11
+
12
+ module Nominatim
13
+
14
+ # @return [Nominatim::Search]
15
+ def self.search(q = nil)
16
+ search = Nominatim::Search.new
17
+ search.query(q) if q
18
+ search
19
+ end
20
+
21
+ # @return [Nominatim::Reverse]
22
+ def self.reverse(lat = nil, lon = nil)
23
+ search = Nominatim::Reverse.new
24
+ search.lat(lat).lon(lon) if lat && lon
25
+ search
26
+ end
27
+
28
+ # @return [Nominatim::Configuration]
29
+ def self.config
30
+ @config ||= Configuration.new
31
+ end
32
+
33
+ def self.configure(&block)
34
+ config.configure &block
35
+ end
36
+ end
@@ -0,0 +1,83 @@
1
+ module Nominatim
2
+ class Address
3
+ def initialize(attrs = {})
4
+ @attrs = attrs
5
+ end
6
+
7
+ def attraction
8
+ @attraction ||= @attrs[:attraction]
9
+ end
10
+
11
+ def clothes
12
+ @clothes ||= @attrs[:clothes]
13
+ end
14
+
15
+ def house_number
16
+ @house_number ||= @attrs[:house_number]
17
+ end
18
+
19
+ def road
20
+ @road ||= @attrs[:road]
21
+ end
22
+
23
+ def commercial
24
+ @commercial ||= @attrs[:commercial]
25
+ end
26
+
27
+ def pedestrian
28
+ @pedestrian ||= @attrs[:pedestrian]
29
+ end
30
+
31
+ def suburb
32
+ @suburb ||= @attrs[:suburb]
33
+ end
34
+
35
+ def city_district
36
+ @city_district ||= @attrs[:city_district]
37
+ end
38
+
39
+ def city
40
+ @city ||= @attrs[:city]
41
+ end
42
+
43
+ def administrative
44
+ @administrative ||= @attrs[:administrative]
45
+ end
46
+
47
+ def county
48
+ @county ||= @attrs[:county]
49
+ end
50
+
51
+ def state_district
52
+ @state_district ||= @attrs[:state_district]
53
+ end
54
+
55
+ def state
56
+ @state ||= @attrs[:state]
57
+ end
58
+
59
+ def postcode
60
+ @postcode ||= @attrs[:postcode]
61
+ end
62
+
63
+ def country
64
+ @country ||= @attrs[:country]
65
+ end
66
+
67
+ def country_code
68
+ @country_code ||= @attrs[:country_code]
69
+ end
70
+
71
+ def place
72
+ @place ||= @attrs[:place]
73
+ end
74
+
75
+ def town
76
+ @town ||= @attrs[:town]
77
+ end
78
+
79
+ def village
80
+ @village ||= @attrs[:village]
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,40 @@
1
+ require 'faraday'
2
+
3
+ module Nominatim
4
+ class Client
5
+
6
+ # Performs an HTTP GET request
7
+ def get(path, params = {})
8
+ connection.get path, params
9
+ end
10
+
11
+ private
12
+
13
+ # Returns a Faraday::Connection object
14
+ #
15
+ # @return [Faraday::Connection]
16
+ def connection
17
+ return @connection if defined? @connection
18
+
19
+ options = {
20
+ request: {
21
+ timeout: Nominatim.config.timeout
22
+ }
23
+ }
24
+
25
+ @connection = Faraday.new Nominatim.config.endpoint, options do |builder|
26
+ builder.use Nominatim::Response::ParseJson
27
+ builder.adapter Faraday.default_adapter
28
+ end
29
+
30
+ @connection.params[:format] = 'json'
31
+ @connection.params[:email] = Nominatim.config.email if Nominatim.config.email
32
+ @connection.params[:key] = Nominatim.config.key if Nominatim.config.key
33
+
34
+ @connection.headers[:user_agent] = Nominatim.config.user_agent
35
+ @connection.headers[:"accept-language"] = Nominatim.config.accept_language
36
+
37
+ @connection
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,52 @@
1
+ module Nominatim
2
+ class Configuration
3
+
4
+ DEFAULT_ENDPOINT = 'http://nominatim.openstreetmap.org'
5
+
6
+ DEFAULT_KEY = nil
7
+
8
+ DEFAULT_USER_AGENT = "Nominatim Ruby Gem #{Nominatim::VERSION}"
9
+
10
+ DEFAULT_EMAIL = nil
11
+
12
+ DEFAULT_LANGUAGE = 'en'
13
+
14
+ DEFAULT_TIMEOUT = nil
15
+
16
+ DEFAULT_SEARCH_URL = 'search'
17
+ DEFAULT_REVERSE_URL = 'reverse'
18
+
19
+ VALID_OPTIONS_KEYS = [
20
+ :endpoint,
21
+ :key,
22
+ :user_agent,
23
+ :email,
24
+ :accept_language,
25
+ :timeout,
26
+ :search_url,
27
+ :reverse_url
28
+ ]
29
+
30
+ attr_accessor *VALID_OPTIONS_KEYS
31
+
32
+ def initialize
33
+ reset!
34
+ end
35
+
36
+ def configure
37
+ yield self
38
+ self
39
+ end
40
+
41
+ def reset!
42
+ self.endpoint = DEFAULT_ENDPOINT
43
+ self.key = DEFAULT_KEY
44
+ self.user_agent = DEFAULT_USER_AGENT
45
+ self.email = DEFAULT_EMAIL
46
+ self.accept_language = DEFAULT_LANGUAGE
47
+ self.timeout = DEFAULT_TIMEOUT
48
+ self.search_url = DEFAULT_SEARCH_URL
49
+ self.reverse_url = DEFAULT_REVERSE_URL
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,93 @@
1
+ module Nominatim
2
+ class Place
3
+ # attr_reader :attrs
4
+ # alias to_hash attrs
5
+
6
+ def initialize(attrs = {})
7
+ @attrs = attrs
8
+ end
9
+
10
+ # Return display name
11
+ #
12
+ # @return [String]
13
+ def display_name
14
+ @display_name ||= @attrs[:display_name]
15
+ end
16
+
17
+ # Return a class
18
+ #
19
+ # @return [String]
20
+ def class
21
+ @class ||= @attrs[:class]
22
+ end
23
+
24
+ # Return a type
25
+ #
26
+ # @return [String]
27
+ def type
28
+ @type ||= @attrs[:type]
29
+ end
30
+
31
+ # Return an address
32
+ #
33
+ # @return [Nominatim::Address]
34
+ def address
35
+ @address ||= Nominatim::Address.new(@attrs[:address]) if @attrs[:address]
36
+ end
37
+
38
+ # Return a latitude
39
+ #
40
+ # @return [Float]
41
+ def lat
42
+ point.lat
43
+ end
44
+ alias latitude lat
45
+
46
+ # Return a longitude
47
+ #
48
+ # @return [Float]
49
+ def lon
50
+ point.lon
51
+ end
52
+ alias longitude lon
53
+
54
+ def boundingbox
55
+ @boundingbox ||= @attrs[:boundingbox]
56
+ end
57
+ alias bounding_box boundingbox
58
+
59
+ # Return a polygon
60
+ #
61
+ # @return [Nominatim::Polygon]
62
+ def polygonpoints
63
+ @polygonpoints ||= Nominatim::Polygon.new(@attrs[:polygonpoints]) if @attrs[:polygonpoints]
64
+ end
65
+
66
+ # Return a place id
67
+ #
68
+ # @return [Integer]
69
+ def place_id
70
+ @place_id ||= @attrs[:place_id].to_i if @attrs[:place_id]
71
+ end
72
+
73
+ # Return an OSM id
74
+ #
75
+ # @return [Integer]
76
+ def osm_id
77
+ @osm_id ||= @attrs[:osm_id].to_i if @attrs[:osm_id]
78
+ end
79
+
80
+ # Return an OSM type
81
+ #
82
+ # @return [String]
83
+ def osm_type
84
+ @osm_type ||= @attrs[:osm_type]
85
+ end
86
+
87
+ private
88
+
89
+ def point
90
+ @point ||= Nominatim::Point.new(@attrs[:lat], @attrs[:lon])
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,30 @@
1
+ module Nominatim
2
+ class Point
3
+ attr_reader :lat, :lon
4
+ alias latitude lat
5
+ alias longitude lon
6
+
7
+ # @param lat [Float]
8
+ # @param lon [Float]
9
+ def initialize(lat, lon)
10
+ @lat, @lon = lat.to_f, lon.to_f if lat && lon
11
+ end
12
+
13
+ # @return [Array]
14
+ def to_a
15
+ [lat, lon]
16
+ end
17
+
18
+ # Return a string representation of the point
19
+ #
20
+ # @return [String]
21
+ def to_s
22
+ to_a.to_s
23
+ end
24
+
25
+ # @return [true, false]
26
+ def ==(other)
27
+ self.to_a == other.to_a
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ module Nominatim
2
+ class Polygon
3
+ attr_reader :coordinates
4
+
5
+ # @param coordinates [Array<Array<Float, String>>]
6
+ def initialize(coordinates)
7
+ @coordinates = []
8
+ coordinates.each do |c|
9
+ @coordinates.push(Nominatim::Point.new(c[0], c[1]))
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ module Nominatim
5
+ module Response
6
+ class ParseJson < Faraday::Response::Middleware
7
+ def on_complete(env)
8
+ if env[:body].empty?
9
+ env[:body] = nil
10
+ else
11
+ env[:body] = MultiJson.load(env[:body], symbolize_keys: true)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ module Nominatim
2
+ class Reverse < Client
3
+ attr_reader :criteria
4
+
5
+ def initialize
6
+ @criteria = {}
7
+ end
8
+
9
+ # Returns search result or nil if no results received.
10
+ def fetch
11
+ body = get(Nominatim.config.reverse_url, @criteria).body
12
+ return nil if body.empty?
13
+ Nominatim::Place.new(body)
14
+ end
15
+
16
+ # Latitude string to search for.
17
+ #
18
+ # @param lat [String] Latitude
19
+ # @return [Nominatim::Reverse]
20
+ def lat(lat)
21
+ @criteria[:lat] = lat
22
+ self
23
+ end
24
+
25
+ # Longitude string to search for.
26
+ #
27
+ # @param lon [String] Longitude
28
+ # @return [Nominatim::Reverse]
29
+ def lon(lon)
30
+ @criteria[:lon] = lon
31
+ self
32
+ end
33
+
34
+ # Include a breakdown of the address into elements.
35
+ #
36
+ # @param bool [true, false]
37
+ # @return [Nominatim::Reverse]
38
+ def address_details(bool)
39
+ @criteria[:addressdetails] = bool ? 1 : 0
40
+ self
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,171 @@
1
+ module Nominatim
2
+ class Search < Client
3
+ include Enumerable
4
+ attr_reader :criteria
5
+
6
+ def initialize
7
+ @criteria = {}
8
+ end
9
+
10
+ # Iterates over the search results.
11
+ def each(&block)
12
+ @results ||= get(Nominatim.config.search_url, @criteria).body.map! { |attrs| Nominatim::Place.new(attrs) }
13
+ @results.each(&block)
14
+ end
15
+
16
+ # Query string to search for.
17
+ #
18
+ # @param q [String] Query string
19
+ # @return [Nominatim::Search]
20
+ def query(q)
21
+ @criteria[:q] = q
22
+ self
23
+ end
24
+
25
+ # Street string to search for.
26
+ #
27
+ # @param street [String] Street string
28
+ # @return [Nominatim::Search]
29
+ def street(street)
30
+ @criteria[:street] = street
31
+ self
32
+ end
33
+
34
+ # City string to search for.
35
+ #
36
+ # @param city [String] city string
37
+ # @return [Nominatim::Search]
38
+ def city(city)
39
+ @criteria[:city] = city
40
+ self
41
+ end
42
+
43
+ # County string to search for.
44
+ #
45
+ # @param county [String] county string
46
+ # @return [Nominatim::Search]
47
+ def county(county)
48
+ @criteria[:county] = county
49
+ self
50
+ end
51
+
52
+ # State string to search for.
53
+ #
54
+ # @param state [String] state string
55
+ # @return [Nominatim::Search]
56
+ def state(state)
57
+ @criteria[:state] = state
58
+ self
59
+ end
60
+
61
+ # Country string to search for.
62
+ #
63
+ # @param country [String] country string
64
+ # @return [Nominatim::Search]
65
+ def country(country)
66
+ @criteria[:country] = country
67
+ self
68
+ end
69
+
70
+ # Postal code string to search for.
71
+ #
72
+ # @param postalcode [String] postalcode string
73
+ # @return [Nominatim::Search]
74
+ def postalcode(postalcode)
75
+ @criteria[:postalcode] = postalcode
76
+ self
77
+ end
78
+
79
+ # Limit search results to a specific country (or a list of countries).
80
+ #
81
+ # @param codes [Array<String>, String]
82
+ # @see https://wiki.openstreetmap.org/wiki/Nominatim
83
+ # @return [Nominatim::Search]
84
+ def country_codes(codes)
85
+ if codes.instance_of? Array
86
+ @criteria[:countrycodes] = codes.join(',')
87
+ else
88
+ @criteria[:countrycodes] = codes
89
+ end
90
+ self
91
+ end
92
+
93
+ # The preferred area to find search results.
94
+ #
95
+ # @param viewbox [Array<String>]
96
+ # @return [Nominatim::Search]
97
+ def viewbox(viewbox)
98
+ @criteria[:viewbox] = viewbox.join(',')
99
+ self
100
+ end
101
+
102
+ # Restrict the results to only items contained with the bounding box.
103
+ #
104
+ # @param bool [true, false]
105
+ # @see https://wiki.openstreetmap.org/wiki/Nominatim
106
+ # @return [Nominatim::Search]
107
+ def bounded(bool)
108
+ @criteria[:bounded] = bool ? 1 : 0
109
+ self
110
+ end
111
+
112
+ # Output polygon outlines for items found.
113
+ #
114
+ # @param bool [true, false]
115
+ # @return [Nominatim::Search]
116
+ def polygon(bool)
117
+ @criteria[:polygon] = bool ? 1 : 0
118
+ self
119
+ end
120
+
121
+ # Include a breakdown of the address into elements.
122
+ #
123
+ # @param bool [true, false]
124
+ # @return [Nominatim::Search]
125
+ def address_details(bool)
126
+ @criteria[:addressdetails] = bool ? 1 : 0
127
+ self
128
+ end
129
+
130
+ # Exclude given place ids from the search result.
131
+ #
132
+ # @param ids [Array<String>, String] Place ids
133
+ # @return [Nominatim::Search]
134
+ def exclude_place_ids(ids)
135
+ if ids.instance_of? Array
136
+ @criteria[:exclude_place_ids] = ids.join(',')
137
+ else
138
+ @criteria[:exclude_place_ids] = ids
139
+ end
140
+ self
141
+ end
142
+
143
+ # Limit the number of returned results.
144
+ #
145
+ # @param limit [Integer]
146
+ # @return [Nominatim::Search]
147
+ def limit(limit)
148
+ @criteria[:limit] = limit
149
+ self
150
+ end
151
+
152
+ # Limit results to certain type, instead of trying to match
153
+ # all possible matches.
154
+ #
155
+ # Possible values:
156
+ # - settlement
157
+ # - country
158
+ # - city
159
+ # - state
160
+ #
161
+ # This feature is not in official Nominatim documentation.
162
+ #
163
+ # @param type Type to restrict to
164
+ # @return [Nominatim::Search]
165
+ def featuretype(type)
166
+ @criteria[:featuretype] = type
167
+ self
168
+ end
169
+
170
+ end
171
+ end
@@ -0,0 +1,3 @@
1
+ module Nominatim
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-nominatim
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samu Voutilainen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.22'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.22'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description: Ruby bindings for Nominatim, providing easy interface with customizable
112
+ backends.
113
+ email:
114
+ - smar@smar.fi
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - lib/nominatim.rb
120
+ - lib/nominatim/address.rb
121
+ - lib/nominatim/client.rb
122
+ - lib/nominatim/configuration.rb
123
+ - lib/nominatim/place.rb
124
+ - lib/nominatim/point.rb
125
+ - lib/nominatim/polygon.rb
126
+ - lib/nominatim/response/parse_json.rb
127
+ - lib/nominatim/reverse.rb
128
+ - lib/nominatim/search.rb
129
+ - lib/nominatim/version.rb
130
+ homepage: https://github.com/Smarre/ruby-nominatim
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.8
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A Ruby wrapper for the Nominatim API.
154
+ test_files: []
155
+ has_rdoc: