All tutorials

Ruby

intermediate

The Ruby wrapper is useful for Ruby developers who wish to seamlessly integrate the what3words Public API into their Ruby applications, without the hassle of having to manage the low level API calls themselves.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as -0.203586, 51.521251 and vice versa. It features a powerful autosuggest function, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, autosuggest can be used to post-process voice output.

All coordinates are latitude, longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

Resources: Full example of how to integrate the what3words Ruby wrapper is available in our GitHub repository.

1
2

Installation

The library is available through RubyGems.

You can simply add this line to your application’s Gemfile:

gem 'what3words', '~> 3.0'
Copied

And then execute:

bundle
Copied

Or install it yourself as:

gem install what3words
Copied
3

Setup

If not using Bundler, require it:

require 'what3words'
Copied

Initialise

The wrapper will point to the public version of the what3words API, to initialise it simply:

what3words = What3Words::API.new(:key => "YOURAPIKEY")
Copied
4

Usage

Convert to 3 word address

This function converts coordinates (expressed as latitude and longitude) to a 3 word address.

More information about convert_to_3wa, including returned results is available in the what3words REST API documentation.

Find the words for (29.567041, 106.587875):

what3words.convert_to_3wa [29.567041, 106.587875]

# => {:country=>"CN", :square=>{:southwest=>{:lng=>106.58786, :lat=>29.567028}, :northeast=>{:lng=>106.587891, :lat=>29.567055}}, :nearestPlace=>"Chongqing", :coordinates=>{:lng=>106.587875, :lat=>29.567041}, :words=>"disclose.strain.redefined", :language=>"en", :map=>"https://w3w.co/disclose.strain.redefined"}
Copied

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

It takes the words parameter as a string of 3 words, e.g.'table.book.chair'

More information about convert_to_coordinates, including returned results, is available in the what3words REST API documentation.

Find the words for prom.cape.pump:

what3words.convert_to_coordinates "prom.cape.pump"

# => {:country=>"GB", :square=>{:southwest=>{:lng=>-0.195426, :lat=>51.484449}, :northeast=>{:lng=>-0.195383, :lat=>51.484476}}, :nearestPlace=>"Kensington, London", :coordinates=>{:lng=>-0.195405, :lat=>51.484463}, :words=>"prom.cape.pump", :language=>"en", :map=>"https://w3w.co/prom.cape.pump"}
Copied

AutoSuggest

When presented with a 3 words address which may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • One or more countries
  • A geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More information about autosuggest, including returned results, is available in the what3words REST API documentation.

Code example Simple basic call:

what3words.autosuggest 'trop.caler.perdre', language: 'fr'

# => {:suggestions=>[{:country=>"FR", :nearestPlace=>"Saint-Lary-Soulan, Hautes-Pyrénées", :words=>"trier.caler.perdre", :rank=>1, :language=>"fr"}, {:country=>"ET", :nearestPlace=>"Asbe Teferi, Oromiya", :words=>"trôler.caler.perdre", :rank=>2, :language=>"fr"}, {:country=>"CN", :nearestPlace=>"Ulanhot, Inner Mongolia", :words=>"froc.caler.perdre", :rank=>3, :language=>"fr"}]}
Copied

Code example AutoSuggest, clipping the results returned to the United Kingdom and Belgium:

what3words.autosuggest 'disclose.strain.redefin', 'clip-to-country': 'GB,BE'

# => {:suggestions=>[{:country=>"GB", :nearestPlace=>"Nether Stowey, Somerset", :words=>"disclose.retrain.redefined", :rank=>1, :language=>"en"}, {:country=>"BE", :nearestPlace=>"Zemst, Flanders", :words=>"disclose.strain.reckon", :rank=>2, :language=>"en"}, {:country=>"GB", :nearestPlace=>"Waddington, Lincolnshire", :words=>"discloses.trains.redefined", :rank=>3, :language=>"en"}]}
Copied

Code example AutoSuggest, Focus on (51.4243877,-0.34745).

what3words.autosuggest 'filled.count.soap', focus: [51.4243877,-0.34745]

# => {:suggestions=>[{:country=>"GB", :nearestPlace=>"Bayswater, London", :words=>"filled.count.soap", :rank=>1, :distanceToFocusKm=>15, :language=>"en"}, {:country=>"GB", :nearestPlace=>"Orsett, Essex", :words=>"fills.count.slap", :rank=>2, :distanceToFocusKm=>48, :language=>"en"}, {:country=>"GB", :nearestPlace=>"Cobham, Surrey", :words=>"field.count.slap", :rank=>3, :distanceToFocusKm=>14, :language=>"en"}]}
Copied

Code example AutoSuggest, with Generic Voice input type.

what3words.autosuggest 'fun with code', 'input-type': 'generic-voice', language: 'en'

# => {:suggestions=>[{:country=>"US", :nearestPlace=>"Homer, Alaska", :words=>"fund.with.code", :rank=>1, :language=>"en"}, {:country=>"AU", :nearestPlace=>"Kumpupintil, Western Australia", :words=>"funk.with.code", :rank=>2, :language=>"en"}, {:country=>"US", :nearestPlace=>"Charleston, West Virginia", :words=>"fund.with.cove", :rank=>3, :language=>"en"}]}
Copied

Grid section

Grid section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More information about grid_section, including returned results, is available in the what3words REST API documentation.

Get a grid for 52.207988,0.116126 in the south west, and 52.208867,0.117540 in the north east:

what3words.grid_section '52.208867,0.117540,52.207988,0.116126'

# => {:lines=>[{:start=>{:lng=>0.116126, :lat=>52.20801}, :end=>{:lng=>0.11754, :lat=>52.20801}}, {:start=>{:lng=>0.116126, :lat=>52.208037}, :end=>{:lng=>0.11754, :lat=>52.208037}}, {:start=>{:lng=>0.116126, :lat=>52.208064}, :end=>{:lng=>0.11754, :lat=>52.208064}}, ___...___ ]}
Copied

Available languages

This function returns the currently supported languages. It will return the two-letter code, and the name of the language, both in that language and in English.

More information about available_languages, including returned results, is available in the what3words REST API documentation.

what3words.available_languages

# => {:languages=>[{:nativeName=>"Deutsch", :code=>"de", :name=>"German"}, {:nativeName=>"हिन्दी", :code=>"hi", :name=>"Hindi"}, {:nativeName=>"Português", :code=>"pt", :name=>"Portuguese"}, {:nativeName=>"Magyar", :code=>"hu", :name=>"Hungarian"}, {:nativeName=>"Українська", :code=>"uk", :name=>"Ukrainian"}, {:nativeName=>"Bahasa Indonesia", :code=>"id", :name=>"Bahasa Indonesia"}, {:nativeName=>"اردو", :code=>"ur", :name=>"Urdu"}, ___...___]}
Copied

Handling errors

Errors returned from the API can be caught with the wrapper through the use of a raise function.

Within the raise function, code and message values which represent the error, are accessible from the error object parameter

if response['error'].to_s.strip != ''
  raise ResponseError, "#{response['code']}: #{response['message']}"
end
Copied

Related tutorials