nominatim 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby
6
+ - rbx
7
+ script: "bundle exec rake"
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nominatim.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'bundler' do
5
+ watch('Gemfile')
6
+ watch(/^.+\.gemspec/)
7
+ end
8
+
9
+ guard 'rspec', :version => 2 do
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch('spec/spec_helper.rb') { "spec" }
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jakub Svehla
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Nominatim
2
+
3
+ A Ruby wrapper for the Nominatim API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nominatim'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nominatim
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ places = Nominatim.search('San Francisco').limit(10).address_details(true)
23
+
24
+ for place in places
25
+ puts "#{place.display_name} (#{place.type})"
26
+ end
27
+ puts "Found #{places.count} places."
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ ```ruby
33
+ Nominatim.configure do |config|
34
+ config.email = 'your-email-address@gmail.com'
35
+ config.endpoint = 'http://nominatim.openstreetmap.org'
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :test => :spec
8
+ task :default => :spec
9
+
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new
data/lib/nominatim.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "nominatim/version"
2
+ require "nominatim/configuration"
3
+ require "nominatim/address"
4
+ require "nominatim/place"
5
+ require "nominatim/response/parse_json"
6
+ require "nominatim/client"
7
+ require "nominatim/search"
8
+
9
+ module Nominatim
10
+
11
+ # @return [Nominatim::Search]
12
+ def self.search(q = nil)
13
+ search = Nominatim::Search.new
14
+ search.query(q) if q
15
+ search
16
+ end
17
+
18
+ # @return [Nominatim::Configuration]
19
+ def self.config
20
+ @config ||= Configuration.new
21
+ end
22
+
23
+ def self.configure(&block)
24
+ config.configure &block
25
+ end
26
+ end
@@ -0,0 +1,75 @@
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
+ end
75
+ end
@@ -0,0 +1,32 @@
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
+ @connection = Faraday.new Nominatim.config.endpoint do |builder|
20
+ builder.use Nominatim::Response::ParseJson
21
+ builder.adapter Faraday.default_adapter
22
+ end
23
+
24
+ @connection.params[:format] = 'json'
25
+ @connection.params[:email] = Nominatim.config.email if Nominatim.config.email
26
+
27
+ @connection.headers[:user_agent] = Nominatim.config.user_agent
28
+
29
+ @connection
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module Nominatim
2
+ class Configuration
3
+
4
+ DEFAULT_ENDPOINT = 'http://nominatim.openstreetmap.org'
5
+
6
+ DEFAULT_USER_AGENT = "Nominatim Ruby Gem #{Nominatim::VERSION}"
7
+
8
+ DEFAULT_EMAIL = nil
9
+
10
+ VALID_OPTIONS_KEYS = [
11
+ :endpoint,
12
+ :user_agent,
13
+ :email
14
+ ]
15
+
16
+ attr_accessor *VALID_OPTIONS_KEYS
17
+
18
+ def initialize
19
+ reset!
20
+ end
21
+
22
+ def configure
23
+ yield self
24
+ self
25
+ end
26
+
27
+ def reset!
28
+ self.endpoint = DEFAULT_ENDPOINT
29
+ self.user_agent = DEFAULT_USER_AGENT
30
+ self.email = DEFAULT_EMAIL
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
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
+ def display_name
11
+ @display_name ||= @attrs[:display_name]
12
+ end
13
+
14
+ def class
15
+ @class ||= @attrs[:class]
16
+ end
17
+
18
+ def type
19
+ @type ||= @attrs[:type]
20
+ end
21
+
22
+ def address
23
+ @address ||= Nominatim::Address.new(@attrs[:address]) unless @attrs[:address].nil?
24
+ end
25
+
26
+ def lat
27
+ @lat ||= @attrs[:lat] || @attrs[:latitude]
28
+ end
29
+ alias latitude lat
30
+
31
+ def lon
32
+ @lon ||= @attrs[:lon] || @attrs[:longitude]
33
+ end
34
+ alias longitude lon
35
+
36
+ def boundingbox
37
+ @boundingbox ||= @attrs[:boundingbox]
38
+ end
39
+ alias bounding_box boundingbox
40
+
41
+ def polygonpoints
42
+ @polygonpoints ||= @attrs[:polygonpoints]
43
+ end
44
+
45
+ def place_id
46
+ @place_id ||= @attrs[:place_id]
47
+ end
48
+
49
+ def osm_id
50
+ @osm_id ||= @attrs[:osm_id]
51
+ end
52
+
53
+ def osm_type
54
+ @osm_type ||= @attrs[:osm_type]
55
+ end
56
+ end
57
+ 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,98 @@
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
+ get('/search', @criteria).body.map! { |attrs| Nominatim::Place.new(attrs) }.each(&block)
13
+ end
14
+
15
+ # Query string to search from.
16
+ #
17
+ # @param q [String] Query string
18
+ # @return [Nominatim::Search]
19
+ def query(q)
20
+ @criteria[:q] = q
21
+ self
22
+ end
23
+
24
+ # Limit search results to a specific country (or a list of countries).
25
+ #
26
+ # @param codes [Array<String>, String]
27
+ # @see https://wiki.openstreetmap.org/wiki/Nominatim
28
+ # @return [Nominatim::Search]
29
+ def country_codes(codes)
30
+ if codes.instance_of? Array
31
+ @criteria[:countrycodes] = codes.join(',')
32
+ else
33
+ @criteria[:countrycodes] = codes
34
+ end
35
+ self
36
+ end
37
+
38
+ # The preferred area to find search results.
39
+ #
40
+ # @param viewbox [Array<String>]
41
+ # @return [Nominatim::Search]
42
+ def viewbox(viewbox)
43
+ @criteria[:viewbox] = viewbox.join(',')
44
+ self
45
+ end
46
+
47
+ # Restrict the results to only items contained with the bounding box.
48
+ #
49
+ # @param bool [true, false]
50
+ # @see https://wiki.openstreetmap.org/wiki/Nominatim
51
+ # @return [Nominatim::Search]
52
+ def bounded(bool)
53
+ @criteria[:bounded] = bool ? 1 : 0
54
+ self
55
+ end
56
+
57
+ # Output polygon outlines for items found.
58
+ #
59
+ # @param bool [true, false]
60
+ # @return [Nominatim::Search]
61
+ def polygon(bool)
62
+ @criteria[:polygon] = bool ? 1 : 0
63
+ self
64
+ end
65
+
66
+ # Include a breakdown of the address into elements.
67
+ #
68
+ # @param bool [true, false]
69
+ # @return [Nominatim::Search]
70
+ def address_details(bool)
71
+ @criteria[:addressdetails] = bool ? 1 : 0
72
+ self
73
+ end
74
+
75
+ # Exclude given place ids from the search result.
76
+ #
77
+ # @param ids [Array<String>, String] Place ids
78
+ # @return [Nominatim::Search]
79
+ def exclude_place_ids(ids)
80
+ if ids.instance_of? Array
81
+ @criteria[:exclude_place_ids] = ids.join(',')
82
+ else
83
+ @criteria[:exclude_place_ids] = ids
84
+ end
85
+ self
86
+ end
87
+
88
+ # Limit the number of returned results.
89
+ #
90
+ # @param limit [Integer]
91
+ # @return [Nominatim::Search]
92
+ def limit(limit)
93
+ @criteria[:limit] = limit
94
+ self
95
+ end
96
+
97
+ end
98
+ end