├── .rspec ├── lib ├── nominatim │ ├── version.rb │ ├── polygon.rb │ ├── response │ │ └── parse_json.rb │ ├── point.rb │ ├── reverse.rb │ ├── client.rb │ ├── configuration.rb │ ├── address.rb │ ├── place.rb │ └── search.rb └── nominatim.rb ├── .travis.yml ├── Gemfile ├── Rakefile ├── CHANGELOG.md ├── .gitignore ├── Guardfile ├── spec ├── fixtures │ ├── reverse.json │ └── search.json ├── nominatim │ ├── client_spec.rb │ ├── polygon_spec.rb │ ├── point_spec.rb │ ├── reverse_spec.rb │ ├── search_spec.rb │ ├── place_spec.rb │ └── address_spec.rb ├── spec_helper.rb └── nominatim_spec.rb ├── nominatim.gemspec ├── LICENSE └── README.md /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format progress 3 | -------------------------------------------------------------------------------- /lib/nominatim/version.rb: -------------------------------------------------------------------------------- 1 | module Nominatim 2 | VERSION = "0.0.6" 3 | end 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.0.0 4 | - 1.9.3 5 | - 1.9.2 6 | - rbx-19mode 7 | script: "bundle exec rake" 8 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 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' 9 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v0.0.3 2 | 3 | * Added Polygon class. 4 | 5 | * Fixed few typos. 6 | 7 | * Added documentation to Place class. 8 | 9 | * Added Point class. 10 | 11 | * Returning ids as integers instead of strings. 12 | 13 | ## v0.0.1 14 | 15 | * Initial release. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 19 | *.swp 20 | *.swo 21 | *.rvmrc 22 | -------------------------------------------------------------------------------- /lib/nominatim/polygon.rb: -------------------------------------------------------------------------------- 1 | module Nominatim 2 | class Polygon 3 | attr_reader :coordinates 4 | 5 | # @param coordinates [Array>] 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 -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 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 14 | -------------------------------------------------------------------------------- /lib/nominatim/response/parse_json.rb: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /spec/fixtures/reverse.json: -------------------------------------------------------------------------------- 1 | {"place_id":"13686660","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1241690521","lat":"37.733976","lon":"-122.3912081","display_name":"4900, 3rd Street, San Francisco, California, 94124, United States of America","address":{"house_number":"4900","road":"3rd Street","city":"San Francisco","county":"San Francisco","state":"California","postcode":"94124","country":"United States of America","country_code":"us"}} 2 | -------------------------------------------------------------------------------- /spec/nominatim/client_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Client do 4 | after do 5 | WebMock.reset! 6 | end 7 | 8 | describe '#get' do 9 | before do 10 | stub_get('/search').to_return(body: "[]") 11 | end 12 | 13 | it 'requests the correct resource' do 14 | Nominatim::Client.new.get('/search') 15 | a_get('/search').should have_been_requested 16 | end 17 | 18 | it 'parses the body' do 19 | response = Nominatim::Client.new.get('/search') 20 | response.body.should eq [] 21 | end 22 | end 23 | end -------------------------------------------------------------------------------- /spec/nominatim/polygon_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Polygon do 4 | 5 | let(:polygon) { Nominatim::Polygon.new([[-1.816513, 52.5487566], [-1.8164913, 52.548824], [-1.8164685, 52.5488213]]) } 6 | 7 | it 'sets coordinates correctly' do 8 | polygon.coordinates.first.lat.should eq -1.816513 9 | polygon.coordinates.first.lon.should eq 52.5487566 10 | end 11 | 12 | describe '#coordinates' do 13 | it 'retruns an array of coordinates' do 14 | polygon.coordinates.each do |p| 15 | p.should be_a Nominatim::Point 16 | end 17 | end 18 | end 19 | end -------------------------------------------------------------------------------- /lib/nominatim/point.rb: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | unless ENV['CI'] 2 | require 'simplecov' 3 | 4 | SimpleCov.start do 5 | add_filter 'spec' 6 | end 7 | end 8 | 9 | require 'nominatim' 10 | require 'webmock/rspec' 11 | 12 | WebMock.disable_net_connect! 13 | 14 | RSpec.configure do |config| 15 | config.treat_symbols_as_metadata_keys_with_true_values = true 16 | config.run_all_when_everything_filtered = true 17 | config.filter_run :focus 18 | 19 | config.before do 20 | Nominatim.config.reset! 21 | end 22 | end 23 | 24 | def a_get(path) 25 | a_request(:get, "#{Nominatim.config.endpoint}#{path}").with(query: { format: 'json' }) 26 | end 27 | 28 | def stub_get(path) 29 | stub_request(:get, "#{Nominatim.config.endpoint}#{path}").with(query: { format: 'json' }) 30 | end 31 | 32 | def fixture_path 33 | File.expand_path('../fixtures', __FILE__) 34 | end 35 | 36 | def fixture(file) 37 | File.new(fixture_path + '/' + file) 38 | end 39 | -------------------------------------------------------------------------------- /lib/nominatim.rb: -------------------------------------------------------------------------------- 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 37 | -------------------------------------------------------------------------------- /nominatim.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/nominatim/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.authors = ["Jakub Svehla"] 6 | gem.email = ["jakub.svehla@gmail.com"] 7 | gem.description = %q{A Ruby wrapper for the Nominatim API.} 8 | gem.summary = %q{A Ruby wrapper for the Nominatim API.} 9 | gem.homepage = "https://github.com/jakubsvehla/nominatim" 10 | 11 | gem.files = `git ls-files`.split($\) 12 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 13 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 14 | gem.name = "nominatim" 15 | gem.require_paths = ["lib"] 16 | gem.version = Nominatim::VERSION 17 | 18 | gem.add_dependency 'faraday' 19 | gem.add_dependency 'multi_json' 20 | 21 | gem.add_development_dependency 'rake' 22 | gem.add_development_dependency 'rspec', '~> 2.10' 23 | gem.add_development_dependency 'webmock' 24 | gem.add_development_dependency 'simplecov' 25 | gem.add_development_dependency 'yard' 26 | end 27 | -------------------------------------------------------------------------------- /lib/nominatim/reverse.rb: -------------------------------------------------------------------------------- 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 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. -------------------------------------------------------------------------------- /lib/nominatim/client.rb: -------------------------------------------------------------------------------- 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 41 | -------------------------------------------------------------------------------- /lib/nominatim/configuration.rb: -------------------------------------------------------------------------------- 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 53 | -------------------------------------------------------------------------------- /spec/nominatim/point_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Point do 4 | let(:point) { Nominatim::Point.new(52.5487969264788, -1.81642935385411) } 5 | 6 | describe '#lat' do 7 | it 'returns the latitude' do 8 | point.lat.should eq 52.5487969264788 9 | end 10 | end 11 | 12 | describe '#latitude' do 13 | it 'returns the latitude' do 14 | point.latitude.should eq 52.5487969264788 15 | end 16 | end 17 | 18 | describe '#lon' do 19 | it 'returns the longitude' do 20 | point.lon.should eq -1.81642935385411 21 | end 22 | end 23 | 24 | describe '#longitude' do 25 | it 'returns the longitude' do 26 | point.longitude.should eq -1.81642935385411 27 | end 28 | end 29 | 30 | describe '#to_a' do 31 | it 'returns an array representing the point' do 32 | point.to_a.should eq [52.5487969264788, -1.81642935385411] 33 | end 34 | end 35 | 36 | describe '#to_s' do 37 | it 'returns a string representing the point' do 38 | point.to_s.should eq '[52.5487969264788, -1.81642935385411]' 39 | end 40 | end 41 | 42 | describe '#==' do 43 | it 'returns true when objects coordinates are the same' do 44 | other = Nominatim::Point.new(52.5487969264788, -1.81642935385411) 45 | (point == other).should be_true 46 | end 47 | 48 | it 'returns false when objects coordinates are different' do 49 | other = Nominatim::Point.new(-1.81642935385411, 52.5487969264788) 50 | (point == other).should be_false 51 | end 52 | end 53 | end -------------------------------------------------------------------------------- /lib/nominatim/address.rb: -------------------------------------------------------------------------------- 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 84 | -------------------------------------------------------------------------------- /spec/nominatim_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim do 4 | describe '.search' do 5 | it 'returns a Nominatim::Search' do 6 | Nominatim.search.should be_a Nominatim::Search 7 | end 8 | 9 | it 'adds a query criterion if given as a parameter' do 10 | search = Nominatim.search('San Francisco') 11 | search.criteria[:q].should eq 'San Francisco' 12 | end 13 | end 14 | 15 | describe '.reverse' do 16 | it 'returns a Nominatim::Reverse' do 17 | Nominatim.reverse.should be_a Nominatim::Reverse 18 | end 19 | 20 | it 'adds a reverse query criteria if given as a parameters' do 21 | reverse = Nominatim.reverse('37.733976', '-122.3912081') 22 | reverse.criteria[:lat].should eq '37.733976' 23 | reverse.criteria[:lon].should eq '-122.3912081' 24 | end 25 | end 26 | 27 | describe '.configure' do 28 | 29 | before do 30 | Nominatim.config.reset! 31 | end 32 | 33 | it 'has a default endpoint' do 34 | Nominatim.config.endpoint.should eq "http://nominatim.openstreetmap.org" 35 | end 36 | 37 | it 'sets the endpoint' do 38 | Nominatim.configure do |config| 39 | config.endpoint = "http://nominatim.org/" 40 | end 41 | Nominatim.config.endpoint.should eq "http://nominatim.org/" 42 | end 43 | 44 | it 'has a default user agent' do 45 | Nominatim.config.user_agent.should eq "Nominatim Ruby Gem #{Nominatim::VERSION}" 46 | end 47 | 48 | it 'sets the user agent' do 49 | Nominatim.configure do |config| 50 | config.user_agent = "MyApp" 51 | end 52 | Nominatim.config.user_agent.should eq "MyApp" 53 | end 54 | 55 | it 'has a default email' do 56 | Nominatim.config.email.should be_nil 57 | end 58 | 59 | it 'sets the email' do 60 | Nominatim.configure do |config| 61 | config.email = "foo@bar.com" 62 | end 63 | Nominatim.config.email.should eq "foo@bar.com" 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/nominatim/place.rb: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /spec/nominatim/reverse_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Reverse do 4 | 5 | let(:reverse) { Nominatim::Reverse.new } 6 | 7 | it 'has no criteria set' do 8 | reverse.criteria.should be_empty 9 | end 10 | 11 | it 'allows chaining of criterions' do 12 | reverse.lat('37.733976').lon('-122.3912081').address_details(1) 13 | reverse.criteria[:lat].should eq '37.733976' 14 | reverse.criteria[:lon].should eq '-122.3912081' 15 | reverse.criteria[:addressdetails].should eq 1 16 | end 17 | 18 | describe '#each' do 19 | let(:reverse) { Nominatim::Reverse.new.lat('37.733976').lon('-122.3912081').address_details(1) } 20 | 21 | before do 22 | stub_get('/reverse'). 23 | with(query: { lat: '37.733976', lon: '-122.3912081', addressdetails: 1 }). 24 | to_return(body: fixture('reverse.json')) 25 | end 26 | 27 | it 'iterates over the matching places' do 28 | reverse.fetch.should be_a Nominatim::Place 29 | end 30 | 31 | it 'returns correct places' do 32 | reverse.fetch.display_name.should eq '4900, 3rd Street, San Francisco, California, 94124, United States of America' 33 | reverse.fetch.address.city.should eq 'San Francisco' 34 | reverse.fetch.address.state.should eq 'California' 35 | end 36 | end 37 | 38 | describe '#lat' do 39 | it 'adds a latitude criterion' do 40 | reverse.lat('37.733976') 41 | reverse.criteria[:lat].should eq '37.733976' 42 | end 43 | end 44 | 45 | describe '#lon' do 46 | it 'adds a longitude criterion' do 47 | reverse.lon('-122.3912081') 48 | reverse.criteria[:lon].should eq '-122.3912081' 49 | end 50 | end 51 | 52 | describe '#address_details' do 53 | it 'adds an address details criterion' do 54 | reverse.address_details(true) 55 | reverse.criteria[:addressdetails].should eq 1 56 | end 57 | 58 | it 'sets 1 when set with true' do 59 | reverse.address_details(true) 60 | reverse.criteria[:addressdetails].should eq 1 61 | end 62 | 63 | it 'sets 0 when set with false' do 64 | reverse.address_details(false) 65 | reverse.criteria[:addressdetails].should eq 0 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/nominatim/search.rb: -------------------------------------------------------------------------------- 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 | @criteria.delete(:q) if (@criteria.keys & [:street, :city, :county, :state, :country, :postalcode]).count > 0 13 | @results ||= get(Nominatim.config.search_url, @criteria).body.map! { |attrs| Nominatim::Place.new(attrs) } 14 | @results.each(&block) 15 | end 16 | 17 | # Structured search requests 18 | # @see https://wiki.openstreetmap.org/wiki/Nominatim 19 | %w(city county state country postalcode).to_a.each do |criterion| 20 | define_method(criterion) do |param| 21 | @criteria[criterion.to_sym] = param 22 | self 23 | end 24 | end 25 | 26 | # Structured street search request 27 | # 28 | # @see https://wiki.openstreetmap.org/wiki/Nominatim 29 | def street housenumber, streetname 30 | @criteria[:street] = "#{housenumber} #{streetname}" 31 | self 32 | end 33 | 34 | # Query string to search for. 35 | # 36 | # @param q [String] Query string 37 | # @return [Nominatim::Search] 38 | def query(q) 39 | @criteria[:q] = q 40 | self 41 | end 42 | 43 | # Limit search results to a specific country (or a list of countries). 44 | # 45 | # @param codes [Array, String] 46 | # @see https://wiki.openstreetmap.org/wiki/Nominatim 47 | # @return [Nominatim::Search] 48 | def country_codes(codes) 49 | if codes.instance_of? Array 50 | @criteria[:countrycodes] = codes.join(',') 51 | else 52 | @criteria[:countrycodes] = codes 53 | end 54 | self 55 | end 56 | 57 | # The preferred area to find search results. 58 | # 59 | # @param viewbox [Array] 60 | # @return [Nominatim::Search] 61 | def viewbox(viewbox) 62 | @criteria[:viewbox] = viewbox.join(',') 63 | self 64 | end 65 | 66 | # Restrict the results to only items contained with the bounding box. 67 | # 68 | # @param bool [true, false] 69 | # @see https://wiki.openstreetmap.org/wiki/Nominatim 70 | # @return [Nominatim::Search] 71 | def bounded(bool) 72 | @criteria[:bounded] = bool ? 1 : 0 73 | self 74 | end 75 | 76 | # Output polygon outlines for items found. 77 | # 78 | # @param bool [true, false] 79 | # @return [Nominatim::Search] 80 | def polygon(bool) 81 | @criteria[:polygon] = bool ? 1 : 0 82 | self 83 | end 84 | 85 | # Include a breakdown of the address into elements. 86 | # 87 | # @param bool [true, false] 88 | # @return [Nominatim::Search] 89 | def address_details(bool) 90 | @criteria[:addressdetails] = bool ? 1 : 0 91 | self 92 | end 93 | 94 | # Exclude given place ids from the search result. 95 | # 96 | # @param ids [Array, String] Place ids 97 | # @return [Nominatim::Search] 98 | def exclude_place_ids(ids) 99 | if ids.instance_of? Array 100 | @criteria[:exclude_place_ids] = ids.join(',') 101 | else 102 | @criteria[:exclude_place_ids] = ids 103 | end 104 | self 105 | end 106 | 107 | # Limit the number of returned results. 108 | # 109 | # @param limit [Integer] 110 | # @return [Nominatim::Search] 111 | def limit(limit) 112 | @criteria[:limit] = limit 113 | self 114 | end 115 | 116 | end 117 | end 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nominatim 2 | 3 | A Ruby wrapper for the Nominatim API. [![Build Status](https://secure.travis-ci.org/jakubsvehla/nominatim.png?branch=master)](http://travis-ci.org/jakubsvehla/nominatim) 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 | ## Documentation 20 | 21 | [http://rdoc.info/gems/nominatim][documentation] 22 | 23 | [documentation]: http://rdoc.info/gems/nominatim 24 | 25 | ## Usage 26 | 27 | ```ruby 28 | places = Nominatim.search('San Francisco').limit(10).address_details(true) 29 | 30 | for place in places 31 | puts "#{place.display_name} (#{place.type})" 32 | end 33 | 34 | puts "Found #{places.count} places." 35 | ``` 36 | 37 | ### Structured requests 38 | 39 | ```ruby 40 | places = Nominatim.search.city('San Antonio').country('Mexico').limit(10).address_details(true) 41 | 42 | for place in places 43 | puts "#{place.display_name} (#{place.type})" 44 | end 45 | 46 | puts "Found #{places.count} places." 47 | ``` 48 | 49 | Nominatim::Search has the following methods to craft structures requests: 50 | 51 | - street: accepts house number and street name as parameters 52 | - city 53 | - county 54 | - state 55 | - country 56 | - postalcode 57 | 58 | See http://wiki.openstreetmap.org/wiki/Nominatim#Parameters 59 | 60 | ## Configuration 61 | 62 | ```ruby 63 | Nominatim.configure do |config| 64 | config.email = 'your-email-address@example.com' 65 | config.endpoint = 'http://open.mapquestapi.com/nominatim/v1' 66 | end 67 | ``` 68 | 69 | ## Contributing 70 | 71 | 1. [Fork the repository.][fork] 72 | 2. [Create a topic branch.][branch] 73 | 3. Add specs for your unimplemented feature or bug fix. 74 | 4. Run `bundle exec rake spec`. If your specs pass, return to step 3. 75 | 5. Implement your feature or bug fix. 76 | 6. Run `bundle exec rake spec`. If your specs fail, return to step 5. 77 | 7. Run `open coverage/index.html`. If your changes are not completely covered 78 | by your tests, return to step 3. 79 | 8. Add, commit, and push your changes. 80 | 9. [Submit a pull request.][pr] 81 | 82 | [fork]: http://help.github.com/fork-a-repo/ 83 | [branch]: http://learn.github.com/p/branching.html 84 | [pr]: http://help.github.com/send-pull-requests/ 85 | 86 | ## Supported Ruby Versions 87 | 88 | Nominatim is tested under 1.9.2 and 1.9.3. 89 | 90 | ## Copyright 91 | 92 | Copyright (c) 2012 Jakub Svehla 93 | 94 | MIT License 95 | 96 | Permission is hereby granted, free of charge, to any person obtaining 97 | a copy of this software and associated documentation files (the 98 | "Software"), to deal in the Software without restriction, including 99 | without limitation the rights to use, copy, modify, merge, publish, 100 | distribute, sublicense, and/or sell copies of the Software, and to 101 | permit persons to whom the Software is furnished to do so, subject to 102 | the following conditions: 103 | 104 | The above copyright notice and this permission notice shall be 105 | included in all copies or substantial portions of the Software. 106 | 107 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 108 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 109 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 110 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 111 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 112 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 113 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 114 | -------------------------------------------------------------------------------- /spec/nominatim/search_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Search do 4 | 5 | let(:search) { Nominatim::Search.new } 6 | 7 | it 'has no criteria set' do 8 | search.criteria.should be_empty 9 | end 10 | 11 | it 'allows chaining of criterions' do 12 | search.query('Los Angeles').limit(1) 13 | search.criteria[:q].should eq 'Los Angeles' 14 | search.criteria[:limit].should eq 1 15 | end 16 | 17 | describe '#each' do 18 | 19 | let(:search) { Nominatim::Search.new.query('Los Angeles').limit(1) } 20 | 21 | let(:structured_search){ Nominatim::Search.new.query('Text').city('Los Angeles').country('us').limit(1) } 22 | 23 | before do 24 | stub_get('/search'). 25 | with(query: { q: 'Los Angeles', limit: 1 }). 26 | to_return(body: fixture('search.json')) 27 | stub_get('/search'). 28 | with(query: { city: 'Los Angeles', country: 'us', limit: 1 }). 29 | to_return(body: fixture('search.json')) 30 | end 31 | 32 | it 'iterates over the matching places' do 33 | search.each do |place| 34 | place.should be_a Nominatim::Place 35 | end 36 | end 37 | 38 | it 'returns all matching places' do 39 | search.count.should eq 1 40 | end 41 | 42 | it 'returns correct places' do 43 | search.first.display_name.should eq 'Los Angeles, California, United States of America' 44 | search.first.lat.should eq 34.0966764 45 | search.first.lon.should eq -117.7196785 46 | end 47 | 48 | it 'omits q parameter from structured search' do 49 | structured_search.first.display_name.should eq 'Los Angeles, California, United States of America' 50 | structured_search.first.lat.should eq 34.0966764 51 | structured_search.first.lon.should eq -117.7196785 52 | end 53 | end 54 | 55 | describe '#query' do 56 | it 'adds a query criterion' do 57 | search.query('Los Angeles') 58 | search.criteria[:q].should eq 'Los Angeles' 59 | end 60 | end 61 | 62 | describe '#country_codes' do 63 | it 'adds a country codes criterion' do 64 | search.country_codes('us') 65 | search.criteria[:countrycodes].should eq 'us' 66 | end 67 | 68 | it 'adds all country codes when set with an array' do 69 | search.country_codes(['us', 'ca']) 70 | search.criteria[:countrycodes].should eq 'us,ca' 71 | end 72 | end 73 | 74 | describe '#viewbox' do 75 | it 'adds a viewbox criterion' do 76 | search.viewbox(["52.5487442016602", "-1.81651306152344", "52.5488510131836", "-1.81634628772736"]) 77 | search.criteria[:viewbox].should eq "52.5487442016602,-1.81651306152344,52.5488510131836,-1.81634628772736" 78 | end 79 | end 80 | 81 | describe '#street' do 82 | it 'adds a street criterion' do 83 | search.street('1000', 'street name') 84 | search.criteria[:street].should eq "1000 street name" 85 | end 86 | end 87 | describe '#city' do 88 | it 'adds a city criterion' do 89 | search.city('City name') 90 | search.criteria[:city].should eq "City name" 91 | end 92 | end 93 | describe '#county' do 94 | it 'adds a county criterion' do 95 | search.county('County name') 96 | search.criteria[:county].should eq "County name" 97 | end 98 | end 99 | 100 | describe '#state' do 101 | it 'adds a state criterion' do 102 | search.state('State name') 103 | search.criteria[:state].should eq "State name" 104 | end 105 | end 106 | 107 | describe '#country' do 108 | it 'adds a country criterion' do 109 | search.country('Country name') 110 | search.criteria[:country].should eq "Country name" 111 | end 112 | end 113 | 114 | 115 | describe '#bounded' do 116 | it 'adds a bounded criterion' do 117 | search.bounded(true) 118 | search.criteria[:bounded].should eq 1 119 | end 120 | 121 | it 'sets 1 when set with true' do 122 | search.bounded(true) 123 | search.criteria[:bounded].should eq 1 124 | end 125 | 126 | it 'sets 0 when set with false' do 127 | search.bounded(false) 128 | search.criteria[:bounded].should eq 0 129 | end 130 | end 131 | 132 | describe '#polygon' do 133 | it 'adds a polygon criterion' do 134 | search.polygon(true) 135 | search.criteria[:polygon].should eq 1 136 | end 137 | 138 | it 'sets 1 when set with true' do 139 | search.polygon(true) 140 | search.criteria[:polygon].should eq 1 141 | end 142 | 143 | it 'sets 0 when set with false' do 144 | search.polygon(false) 145 | search.criteria[:polygon].should eq 0 146 | end 147 | end 148 | 149 | describe '#address_details' do 150 | it 'adds an address details criterion' do 151 | search.address_details(true) 152 | search.criteria[:addressdetails].should eq 1 153 | end 154 | 155 | it 'sets 1 when set with true' do 156 | search.address_details(true) 157 | search.criteria[:addressdetails].should eq 1 158 | end 159 | 160 | it 'sets 0 when set with false' do 161 | search.address_details(false) 162 | search.criteria[:addressdetails].should eq 0 163 | end 164 | end 165 | 166 | describe '#exclude_place_ids' do 167 | it 'excludes given place ids' do 168 | search.exclude_place_ids('1') 169 | search.criteria[:exclude_place_ids].should eq '1' 170 | end 171 | 172 | it 'adds all place ids when set with an array' do 173 | search.exclude_place_ids(['1', '2', '3']) 174 | search.criteria[:exclude_place_ids].should eq '1,2,3' 175 | end 176 | end 177 | 178 | describe '#limit' do 179 | it 'adds a limit criterion' do 180 | search.limit(1) 181 | search.criteria[:limit].should eq 1 182 | end 183 | end 184 | 185 | end -------------------------------------------------------------------------------- /spec/nominatim/place_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Place do 4 | describe '#display_name' do 5 | it 'returns a full name when set with display_name' do 6 | place = Nominatim::Place.new(display_name: 'Los Angeles, California, United States of America') 7 | place.display_name.should eq 'Los Angeles, California, United States of America' 8 | end 9 | 10 | it 'returns nil when not set' do 11 | place = Nominatim::Place.new 12 | place.display_name.should be_nil 13 | end 14 | end 15 | 16 | describe '#class' do 17 | it 'returns a class when set with class' do 18 | place = Nominatim::Place.new(class: 'place') 19 | place.class.should eq 'place' 20 | end 21 | 22 | it 'returns nil when not set' do 23 | place = Nominatim::Place.new 24 | place.class.should be_nil 25 | end 26 | end 27 | 28 | describe '#type' do 29 | it 'returns a type when set with type' do 30 | place = Nominatim::Place.new(type: 'county') 31 | place.type.should eq 'county' 32 | end 33 | 34 | it 'returns nil when not set' do 35 | place = Nominatim::Place.new 36 | place.type.should be_nil 37 | end 38 | end 39 | 40 | describe '#address' do 41 | it 'returns a Nominatim::Address when set' do 42 | place = Nominatim::Place.new(address: {county: 'Los Angeles', state: 'California', country: 'United States of America'}) 43 | place.address.should be_a Nominatim::Address 44 | end 45 | 46 | it 'returns nil when not set' do 47 | place = Nominatim::Place.new 48 | place.address.should be_nil 49 | end 50 | end 51 | 52 | describe '#lat' do 53 | it 'returns a latitude when set with lat' do 54 | place = Nominatim::Place.new(lat: '52.5487969264788', lon: '-1.81642935385411') 55 | place.lat.should eq 52.5487969264788 56 | end 57 | 58 | it 'returns nil when not set' do 59 | place = Nominatim::Place.new 60 | place.lat.should be_nil 61 | end 62 | end 63 | 64 | describe '#latitude' do 65 | it 'returns a latitude when set with lat' do 66 | place = Nominatim::Place.new(lat: '52.5487969264788', lon: '-1.81642935385411') 67 | place.latitude.should eq 52.5487969264788 68 | end 69 | 70 | it 'returns nil when not set' do 71 | place = Nominatim::Place.new 72 | place.latitude.should be_nil 73 | end 74 | end 75 | 76 | describe '#lon' do 77 | it 'returns a longitude when set with lon' do 78 | place = Nominatim::Place.new(lat: '52.5487969264788', lon: '-1.81642935385411') 79 | place.lon.should eq -1.81642935385411 80 | end 81 | 82 | it 'returns nil when not set' do 83 | place = Nominatim::Place.new 84 | place.lon.should be_nil 85 | end 86 | end 87 | 88 | describe '#longitude' do 89 | it 'returns a longitude when set with lon' do 90 | place = Nominatim::Place.new(lat: '52.5487969264788', lon: '-1.81642935385411') 91 | place.longitude.should eq -1.81642935385411 92 | end 93 | 94 | it 'returns nil when not set' do 95 | place = Nominatim::Place.new 96 | place.longitude.should be_nil 97 | end 98 | end 99 | 100 | describe '#boundingbox' do 101 | it 'returns a boundingbox when set with boundingbox' do 102 | place = Nominatim::Place.new(boundingbox: ["52.5487442016602", "52.5488510131836", "-1.81651306152344", "-1.81634628772736"]) 103 | place.boundingbox.should eq ["52.5487442016602", "52.5488510131836", "-1.81651306152344", "-1.81634628772736"] 104 | end 105 | 106 | it 'returns nil when not set' do 107 | place = Nominatim::Place.new 108 | place.boundingbox.should be_nil 109 | end 110 | end 111 | 112 | describe '#bounding_box' do 113 | it 'returns a bounding box when set with boundingbox' do 114 | place = Nominatim::Place.new(boundingbox: ["52.5487442016602", "52.5488510131836", "-1.81651306152344", "-1.81634628772736"]) 115 | place.bounding_box.should eq ["52.5487442016602", "52.5488510131836", "-1.81651306152344", "-1.81634628772736"] 116 | end 117 | 118 | it 'returns nil when not set' do 119 | place = Nominatim::Place.new 120 | place.bounding_box.should be_nil 121 | end 122 | end 123 | 124 | describe '#polygonpoints' do 125 | it 'returns polygon points when set with polygonpoints' do 126 | place = Nominatim::Place.new(polygonpoints: [["-1.816513", "52.5487566"], ["-1.8164913", "52.548824"], ["-1.8164685", "52.5488213"]]) 127 | place.polygonpoints.should be_a Nominatim::Polygon 128 | end 129 | 130 | it 'returns nil when not set' do 131 | place = Nominatim::Place.new 132 | place.polygonpoints.should be_nil 133 | end 134 | end 135 | 136 | describe '#place_id' do 137 | it 'returns a place id when set with place_id' do 138 | place = Nominatim::Place.new(place_id: '84327444') 139 | place.place_id.should eq 84327444 140 | end 141 | 142 | it 'returns nil when not set' do 143 | place = Nominatim::Place.new 144 | place.place_id.should be_nil 145 | end 146 | end 147 | 148 | describe '#osm_id' do 149 | it 'returns an osm id when set with osm_id' do 150 | place = Nominatim::Place.new(osm_id: '90394480') 151 | place.osm_id.should eq 90394480 152 | end 153 | 154 | it 'returns nil when not set' do 155 | place = Nominatim::Place.new 156 | place.osm_id.should be_nil 157 | end 158 | end 159 | 160 | describe '#osm_type' do 161 | it 'returns an osm type when set with osm_type' do 162 | place = Nominatim::Place.new(osm_type: 'way') 163 | place.osm_type.should eq 'way' 164 | end 165 | 166 | it 'returns nil when not set' do 167 | place = Nominatim::Place.new 168 | place.osm_type.should be_nil 169 | end 170 | end 171 | 172 | end 173 | -------------------------------------------------------------------------------- /spec/nominatim/address_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Nominatim::Address do 4 | 5 | describe '#attraction' do 6 | it 'returns a attraction when set with attraction' do 7 | address = Nominatim::Address.new(attraction: 'Eiffel Tower') 8 | address.attraction.should eq 'Eiffel Tower' 9 | end 10 | 11 | it 'returns nil when not set' do 12 | address = Nominatim::Address.new 13 | address.attraction.should be_nil 14 | end 15 | end 16 | 17 | describe '#clothes' do 18 | it 'returns clothes when set with clothes' do 19 | address = Nominatim::Address.new(clothes: 'XXI') 20 | address.clothes.should eq 'XXI' 21 | end 22 | 23 | it 'returns nil when not set' do 24 | address = Nominatim::Address.new 25 | address.clothes.should be_nil 26 | end 27 | end 28 | 29 | describe '#house_number' do 30 | it 'returns a house number when set with house_number' do 31 | address = Nominatim::Address.new(house_number: 1) 32 | address.house_number.should eq 1 33 | end 34 | 35 | it 'returns nil when not set' do 36 | address = Nominatim::Address.new 37 | address.house_number.should be_nil 38 | end 39 | end 40 | 41 | describe '#road' do 42 | it 'returns a road when set with road' do 43 | address = Nominatim::Address.new(road: 'Infinite Loop') 44 | address.road.should eq 'Infinite Loop' 45 | end 46 | 47 | it 'returns nil when not set' do 48 | address = Nominatim::Address.new 49 | address.road.should be_nil 50 | end 51 | end 52 | 53 | describe '#commercial' do 54 | it 'returns a commercial when set with commercial' do 55 | address = Nominatim::Address.new(commercial: 'Apple, Inc.') 56 | address.commercial.should eq 'Apple, Inc.' 57 | end 58 | 59 | it 'returns nil when not set' do 60 | address = Nominatim::Address.new 61 | address.commercial.should be_nil 62 | end 63 | end 64 | 65 | describe '#pedestrian' do 66 | it 'returns a pedestrian when set with pedestrian' do 67 | address = Nominatim::Address.new(pedestrian: 'Avenue Pierre Loti') 68 | address.pedestrian.should eq 'Avenue Pierre Loti' 69 | end 70 | 71 | it 'returns nil when not set' do 72 | address = Nominatim::Address.new 73 | address.pedestrian.should be_nil 74 | end 75 | end 76 | 77 | describe '#suburb' do 78 | it 'returns a suburb when set with suburb' do 79 | address = Nominatim::Address.new(suburb: 'Quartier du Gros Caillou') 80 | address.suburb.should eq 'Quartier du Gros Caillou' 81 | end 82 | 83 | it 'returns nil when not set' do 84 | address = Nominatim::Address.new 85 | address.suburb.should be_nil 86 | end 87 | end 88 | 89 | describe '#city_district' do 90 | it 'returns a city district when set with city_district' do 91 | address = Nominatim::Address.new(city_district: '7th Arrondissement') 92 | address.city_district.should eq '7th Arrondissement' 93 | end 94 | 95 | it 'returns nil when not set' do 96 | address = Nominatim::Address.new 97 | address.city_district.should be_nil 98 | end 99 | end 100 | 101 | describe '#city' do 102 | it 'returns a city when set with city' do 103 | address = Nominatim::Address.new(city: 'Santa Clara') 104 | address.city.should eq 'Santa Clara' 105 | end 106 | 107 | it 'returns nil when not set' do 108 | address = Nominatim::Address.new 109 | address.city.should be_nil 110 | end 111 | end 112 | 113 | describe '#administrative' do 114 | it 'returns a administrative when set with administrative' do 115 | address = Nominatim::Address.new(administrative: 'Paris') 116 | address.administrative.should eq 'Paris' 117 | end 118 | 119 | it 'returns nil when not set' do 120 | address = Nominatim::Address.new 121 | address.administrative.should be_nil 122 | end 123 | end 124 | 125 | describe '#county' do 126 | it 'returns a county when set with county' do 127 | address = Nominatim::Address.new(county: 'Santa Clara County') 128 | address.county.should eq 'Santa Clara County' 129 | end 130 | 131 | it 'returns nil when not set' do 132 | address = Nominatim::Address.new 133 | address.county.should be_nil 134 | end 135 | end 136 | 137 | describe '#state_district' do 138 | it 'returns a state disctrict when set with state_district' do 139 | address = Nominatim::Address.new(state_district: 'West Midlands') 140 | address.state_district.should eq 'West Midlands' 141 | end 142 | 143 | it 'returns nil when not set' do 144 | address = Nominatim::Address.new 145 | address.state_district.should be_nil 146 | end 147 | end 148 | 149 | describe '#state' do 150 | it 'returns a state when set with state' do 151 | address = Nominatim::Address.new(state: 'California') 152 | address.state.should eq 'California' 153 | end 154 | 155 | it 'returns nil when not set' do 156 | address = Nominatim::Address.new 157 | address.state.should be_nil 158 | end 159 | end 160 | 161 | describe '#postcode' do 162 | it 'returns a postcode when set with postcode' do 163 | address = Nominatim::Address.new(postcode: '95014') 164 | address.postcode.should eq '95014' 165 | end 166 | 167 | it 'returns nil when not set' do 168 | address = Nominatim::Address.new 169 | address.postcode.should be_nil 170 | end 171 | end 172 | 173 | describe '#country' do 174 | it 'returns a country when set with country' do 175 | address = Nominatim::Address.new(country: 'United States of America') 176 | address.country.should eq 'United States of America' 177 | end 178 | 179 | it 'returns nil when not set' do 180 | address = Nominatim::Address.new 181 | address.country.should be_nil 182 | end 183 | end 184 | 185 | describe '#country_code' do 186 | it 'returns a country code when set with country_code' do 187 | address = Nominatim::Address.new(country_code: 'us') 188 | address.country_code.should eq 'us' 189 | end 190 | 191 | it 'returns nil when not set' do 192 | address = Nominatim::Address.new 193 | address.country_code.should be_nil 194 | end 195 | end 196 | 197 | describe '#place' do 198 | it 'returns a place when set with place' do 199 | address = Nominatim::Address.new(place: 'Europe') 200 | address.place.should eq 'Europe' 201 | end 202 | 203 | it 'returns nil when not set' do 204 | address = Nominatim::Address.new 205 | address.place.should be_nil 206 | end 207 | end 208 | 209 | end 210 | -------------------------------------------------------------------------------- /spec/fixtures/search.json: -------------------------------------------------------------------------------- 1 | [{"place_id":"1595230","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"316944811","boundingbox":["34.086675872803","34.1066796875","-117.72968078613","-117.70967315674"],"polygonpoints":[["-117.7196785","34.1066764"],["-117.71952220822","34.106675178569"],["-117.71936595462","34.106671514576"],["-117.71920977738","34.106665408915"],["-117.71905371463","34.106656863077"],["-117.71889780451","34.106645879152"],["-117.7187420851","34.10663245982"],["-117.71858659445","34.106616608362"],["-117.71843137054","34.106598328648"],["-117.71827645128","34.106577625145"],["-117.71812187452","34.106554502911"],["-117.71796767802","34.106528967592"],["-117.71781389946","34.106501025429"],["-117.71766057639","34.106470683245"],["-117.71750774627","34.106437948455"],["-117.71735544643","34.106402829053"],["-117.71720371409","34.10636533362"],["-117.7170525863","34.106325471315"],["-117.71690209999","34.106283251876"],["-117.71675229191","34.106238685616"],["-117.71660319866","34.106191783422"],["-117.71645485667","34.106142556753"],["-117.71630730217","34.106091017633"],["-117.7161605712","34.106037178652"],["-117.71601469962","34.105981052963"],["-117.71586972305","34.105922654277"],["-117.71572567691","34.105861996859"],["-117.7155825964","34.105799095527"],["-117.71544051645","34.105733965647"],["-117.71529947179","34.10566662313"],["-117.71515949686","34.105597084426"],["-117.71502062586","34.105525366522"],["-117.71488289272","34.105451486939"],["-117.71474633107","34.105375463724"],["-117.71461097429","34.105297315449"],["-117.71447685543","34.105217061203"],["-117.71434400726","34.105134720593"],["-117.71421246224","34.105050313732"],["-117.71408225249","34.10496386124"],["-117.71395340983","34.104875384237"],["-117.71382596572","34.104784904335"],["-117.71369995131","34.104692443639"],["-117.71357539738","34.104598024734"],["-117.71345233435","34.104501670686"],["-117.71333079229","34.104403405034"],["-117.71321080088","34.104303251781"],["-117.71309238944","34.104201235394"],["-117.7129755869","34.104097380795"],["-117.71286042178","34.103991713353"],["-117.71274692223","34.103884258881"],["-117.71263511597","34.103775043629"],["-117.7125250303","34.103664094278"],["-117.71241669213","34.103551437929"],["-117.71231012792","34.103437102105"],["-117.7122053637","34.103321114734"],["-117.71210242507","34.103203504152"],["-117.71200133716","34.10308429909"],["-117.71190212468","34.102963528666"],["-117.71180481186","34.102841222384"],["-117.71170942247","34.102717410122"],["-117.71161597981","34.102592122124"],["-117.71152450672","34.102465388998"],["-117.71143502554","34.102337241702"],["-117.71134755812","34.102207711541"],["-117.71126212583","34.102076830158"],["-117.71117874955","34.101944629524"],["-117.71109744964","34.101811141935"],["-117.71101824596","34.1016764"],["-117.71094115786","34.101540436634"],["-117.71086620418","34.101403285052"],["-117.71079340321","34.101264978757"],["-117.71072277275","34.101125551536"],["-117.71065433005","34.100985037449"],["-117.71058809183","34.100843470822"],["-117.71052407427","34.100700886238"],["-117.710462293","34.100557318527"],["-117.71040276313","34.100412802761"],["-117.7103454992","34.100267374245"],["-117.71029051518","34.100121068503"],["-117.71023782452","34.099973921276"],["-117.71018744009","34.099825968511"],["-117.71013937419","34.09967724635"],["-117.71009363857","34.099527791124"],["-117.71005024439","34.099377639342"],["-117.71000920227","34.099226827686"],["-117.70997052222","34.099075392995"],["-117.70993421369","34.098923372263"],["-117.70990028556","34.098770802628"],["-117.70986874611","34.098617721359"],["-117.70983960305","34.098464165852"],["-117.70981286349","34.098310173618"],["-117.70978853397","34.098155782277"],["-117.70976662043","34.098001029542"],["-117.70974712823","34.09784595322"],["-117.70973006212","34.097690591191"],["-117.70971542628","34.09753498141"],["-117.70970322428","34.097379161889"],["-117.7096934591","34.097223170694"],["-117.70968613313","34.09706704593"],["-117.70968124815","34.096910825736"],["-117.70967880536","34.096754548276"],["-117.70967880536","34.096598251724"],["-117.70968124815","34.096441974264"],["-117.70968613313","34.09628575407"],["-117.7096934591","34.096129629306"],["-117.70970322428","34.095973638111"],["-117.70971542628","34.09581781859"],["-117.70973006212","34.095662208809"],["-117.70974712823","34.09550684678"],["-117.70976662043","34.095351770458"],["-117.70978853397","34.095197017723"],["-117.70981286349","34.095042626382"],["-117.70983960305","34.094888634148"],["-117.70986874611","34.094735078641"],["-117.70990028556","34.094581997372"],["-117.70993421369","34.094429427737"],["-117.70997052222","34.094277407005"],["-117.71000920227","34.094125972314"],["-117.71005024439","34.093975160658"],["-117.71009363857","34.093825008876"],["-117.71013937419","34.09367555365"],["-117.71018744009","34.093526831489"],["-117.71023782452","34.093378878724"],["-117.71029051518","34.093231731497"],["-117.7103454992","34.093085425755"],["-117.71040276313","34.092939997239"],["-117.710462293","34.092795481473"],["-117.71052407427","34.092651913762"],["-117.71058809183","34.092509329178"],["-117.71065433005","34.092367762551"],["-117.71072277275","34.092227248464"],["-117.71079340321","34.092087821243"],["-117.71086620418","34.091949514948"],["-117.71094115786","34.091812363366"],["-117.71101824596","34.0916764"],["-117.71109744964","34.091541658065"],["-117.71117874955","34.091408170476"],["-117.71126212583","34.091275969842"],["-117.71134755812","34.091145088459"],["-117.71143502554","34.091015558298"],["-117.71152450672","34.090887411002"],["-117.71161597981","34.090760677876"],["-117.71170942247","34.090635389878"],["-117.71180481186","34.090511577616"],["-117.71190212468","34.090389271334"],["-117.71200133716","34.09026850091"],["-117.71210242507","34.090149295848"],["-117.7122053637","34.090031685266"],["-117.71231012792","34.089915697895"],["-117.71241669213","34.089801362071"],["-117.7125250303","34.089688705722"],["-117.71263511597","34.089577756371"],["-117.71274692223","34.089468541119"],["-117.71286042178","34.089361086647"],["-117.7129755869","34.089255419205"],["-117.71309238944","34.089151564606"],["-117.71321080088","34.089049548219"],["-117.71333079229","34.088949394966"],["-117.71345233435","34.088851129314"],["-117.71357539738","34.088754775266"],["-117.71369995131","34.088660356361"],["-117.71382596572","34.088567895665"],["-117.71395340983","34.088477415763"],["-117.71408225249","34.08838893876"],["-117.71421246224","34.088302486268"],["-117.71434400726","34.088218079407"],["-117.71447685543","34.088135738797"],["-117.71461097429","34.088055484551"],["-117.71474633107","34.087977336276"],["-117.71488289272","34.087901313061"],["-117.71502062586","34.087827433478"],["-117.71515949686","34.087755715574"],["-117.71529947179","34.08768617687"],["-117.71544051645","34.087618834353"],["-117.7155825964","34.087553704473"],["-117.71572567691","34.087490803141"],["-117.71586972305","34.087430145723"],["-117.71601469962","34.087371747037"],["-117.7161605712","34.087315621348"],["-117.71630730217","34.087261782367"],["-117.71645485667","34.087210243247"],["-117.71660319866","34.087161016578"],["-117.71675229191","34.087114114384"],["-117.71690209999","34.087069548124"],["-117.7170525863","34.087027328685"],["-117.71720371409","34.08698746638"],["-117.71735544643","34.086949970947"],["-117.71750774627","34.086914851545"],["-117.71766057639","34.086882116755"],["-117.71781389946","34.086851774571"],["-117.71796767802","34.086823832408"],["-117.71812187452","34.086798297089"],["-117.71827645128","34.086775174855"],["-117.71843137054","34.086754471352"],["-117.71858659445","34.086736191638"],["-117.7187420851","34.08672034018"],["-117.71889780451","34.086706920848"],["-117.71905371463","34.086695936923"],["-117.71920977738","34.086687391085"],["-117.71936595462","34.086681285424"],["-117.71952220822","34.086677621431"],["-117.7196785","34.0866764"],["-117.71983479178","34.086677621431"],["-117.71999104538","34.086681285424"],["-117.72014722262","34.086687391085"],["-117.72030328537","34.086695936923"],["-117.72045919549","34.086706920848"],["-117.7206149149","34.08672034018"],["-117.72077040555","34.086736191638"],["-117.72092562946","34.086754471352"],["-117.72108054872","34.086775174855"],["-117.72123512548","34.086798297089"],["-117.72138932198","34.086823832408"],["-117.72154310054","34.086851774571"],["-117.72169642361","34.086882116755"],["-117.72184925373","34.086914851545"],["-117.72200155357","34.086949970947"],["-117.72215328591","34.08698746638"],["-117.7223044137","34.087027328685"],["-117.72245490001","34.087069548124"],["-117.72260470809","34.087114114384"],["-117.72275380134","34.087161016578"],["-117.72290214333","34.087210243247"],["-117.72304969783","34.087261782367"],["-117.7231964288","34.087315621348"],["-117.72334230038","34.087371747037"],["-117.72348727695","34.087430145723"],["-117.72363132309","34.087490803141"],["-117.7237744036","34.087553704473"],["-117.72391648355","34.087618834353"],["-117.72405752821","34.08768617687"],["-117.72419750314","34.087755715574"],["-117.72433637414","34.087827433478"],["-117.72447410728","34.087901313061"],["-117.72461066893","34.087977336276"],["-117.72474602571","34.088055484551"],["-117.72488014457","34.088135738797"],["-117.72501299274","34.088218079407"],["-117.72514453776","34.088302486268"],["-117.72527474751","34.08838893876"],["-117.72540359017","34.088477415763"],["-117.72553103428","34.088567895665"],["-117.72565704869","34.088660356361"],["-117.72578160262","34.088754775266"],["-117.72590466565","34.088851129314"],["-117.72602620771","34.088949394966"],["-117.72614619912","34.089049548219"],["-117.72626461056","34.089151564606"],["-117.7263814131","34.089255419205"],["-117.72649657822","34.089361086647"],["-117.72661007777","34.089468541119"],["-117.72672188403","34.089577756371"],["-117.7268319697","34.089688705722"],["-117.72694030787","34.089801362071"],["-117.72704687208","34.089915697895"],["-117.7271516363","34.090031685266"],["-117.72725457493","34.090149295848"],["-117.72735566284","34.09026850091"],["-117.72745487532","34.090389271334"],["-117.72755218814","34.090511577616"],["-117.72764757753","34.090635389878"],["-117.72774102019","34.090760677876"],["-117.72783249328","34.090887411002"],["-117.72792197446","34.091015558298"],["-117.72800944188","34.091145088459"],["-117.72809487417","34.091275969842"],["-117.72817825045","34.091408170476"],["-117.72825955036","34.091541658065"],["-117.72833875404","34.0916764"],["-117.72841584214","34.091812363366"],["-117.72849079582","34.091949514948"],["-117.72856359679","34.092087821243"],["-117.72863422725","34.092227248464"],["-117.72870266995","34.092367762551"],["-117.72876890817","34.092509329178"],["-117.72883292573","34.092651913762"],["-117.728894707","34.092795481473"],["-117.72895423687","34.092939997239"],["-117.7290115008","34.093085425755"],["-117.72906648482","34.093231731497"],["-117.72911917548","34.093378878724"],["-117.72916955991","34.093526831489"],["-117.72921762581","34.09367555365"],["-117.72926336143","34.093825008876"],["-117.72930675561","34.093975160658"],["-117.72934779773","34.094125972314"],["-117.72938647778","34.094277407005"],["-117.72942278631","34.094429427737"],["-117.72945671444","34.094581997372"],["-117.72948825389","34.094735078641"],["-117.72951739695","34.094888634148"],["-117.72954413651","34.095042626382"],["-117.72956846603","34.095197017723"],["-117.72959037957","34.095351770458"],["-117.72960987177","34.09550684678"],["-117.72962693788","34.095662208809"],["-117.72964157372","34.09581781859"],["-117.72965377572","34.095973638111"],["-117.7296635409","34.096129629306"],["-117.72967086687","34.09628575407"],["-117.72967575185","34.096441974264"],["-117.72967819464","34.096598251724"],["-117.72967819464","34.096754548276"],["-117.72967575185","34.096910825736"],["-117.72967086687","34.09706704593"],["-117.7296635409","34.097223170694"],["-117.72965377572","34.097379161889"],["-117.72964157372","34.09753498141"],["-117.72962693788","34.097690591191"],["-117.72960987177","34.09784595322"],["-117.72959037957","34.098001029542"],["-117.72956846603","34.098155782277"],["-117.72954413651","34.098310173618"],["-117.72951739695","34.098464165852"],["-117.72948825389","34.098617721359"],["-117.72945671444","34.098770802628"],["-117.72942278631","34.098923372263"],["-117.72938647778","34.099075392995"],["-117.72934779773","34.099226827686"],["-117.72930675561","34.099377639342"],["-117.72926336143","34.099527791124"],["-117.72921762581","34.09967724635"],["-117.72916955991","34.099825968511"],["-117.72911917548","34.099973921276"],["-117.72906648482","34.100121068503"],["-117.7290115008","34.100267374245"],["-117.72895423687","34.100412802761"],["-117.728894707","34.100557318527"],["-117.72883292573","34.100700886238"],["-117.72876890817","34.100843470822"],["-117.72870266995","34.100985037449"],["-117.72863422725","34.101125551536"],["-117.72856359679","34.101264978757"],["-117.72849079582","34.101403285052"],["-117.72841584214","34.101540436634"],["-117.72833875404","34.1016764"],["-117.72825955036","34.101811141935"],["-117.72817825045","34.101944629524"],["-117.72809487417","34.102076830158"],["-117.72800944188","34.102207711541"],["-117.72792197446","34.102337241702"],["-117.72783249328","34.102465388998"],["-117.72774102019","34.102592122124"],["-117.72764757753","34.102717410122"],["-117.72755218814","34.102841222384"],["-117.72745487532","34.102963528666"],["-117.72735566284","34.10308429909"],["-117.72725457493","34.103203504152"],["-117.7271516363","34.103321114734"],["-117.72704687208","34.103437102105"],["-117.72694030787","34.103551437929"],["-117.7268319697","34.103664094278"],["-117.72672188403","34.103775043629"],["-117.72661007777","34.103884258881"],["-117.72649657822","34.103991713353"],["-117.7263814131","34.104097380795"],["-117.72626461056","34.104201235394"],["-117.72614619912","34.104303251781"],["-117.72602620771","34.104403405034"],["-117.72590466565","34.104501670686"],["-117.72578160262","34.104598024734"],["-117.72565704869","34.104692443639"],["-117.72553103428","34.104784904335"],["-117.72540359017","34.104875384237"],["-117.72527474751","34.10496386124"],["-117.72514453776","34.105050313732"],["-117.72501299274","34.105134720593"],["-117.72488014457","34.105217061203"],["-117.72474602571","34.105297315449"],["-117.72461066893","34.105375463724"],["-117.72447410728","34.105451486939"],["-117.72433637414","34.105525366522"],["-117.72419750314","34.105597084426"],["-117.72405752821","34.10566662313"],["-117.72391648355","34.105733965647"],["-117.7237744036","34.105799095527"],["-117.72363132309","34.105861996859"],["-117.72348727695","34.105922654277"],["-117.72334230038","34.105981052963"],["-117.7231964288","34.106037178652"],["-117.72304969783","34.106091017633"],["-117.72290214333","34.106142556753"],["-117.72275380134","34.106191783422"],["-117.72260470809","34.106238685616"],["-117.72245490001","34.106283251876"],["-117.7223044137","34.106325471315"],["-117.72215328591","34.10636533362"],["-117.72200155357","34.106402829053"],["-117.72184925373","34.106437948455"],["-117.72169642361","34.106470683245"],["-117.72154310054","34.106501025429"],["-117.72138932198","34.106528967592"],["-117.72123512548","34.106554502911"],["-117.72108054872","34.106577625145"],["-117.72092562946","34.106598328648"],["-117.72077040555","34.106616608362"],["-117.7206149149","34.10663245982"],["-117.72045919549","34.106645879152"],["-117.72030328537","34.106656863077"],["-117.72014722262","34.106665408915"],["-117.71999104538","34.106671514576"],["-117.71983479178","34.106675178569"],["-117.7196785","34.1066764"]],"lat":"34.0966764","lon":"-117.7196785","display_name":"Los Angeles, California, United States of America","class":"place","type":"county","icon":"http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png","address":{"county":"Los Angeles","state":"California","country":"United States of America","country_code":"us"}}] --------------------------------------------------------------------------------