Introduction

This is the documentation for the Ruby client for Objectia® API. You can find the source code for the client on GitHub. We have API clients in several other programming languages hosted on GitHub.

Requirements

The Ruby client for Objectia API requires Ruby version 2.2 or later. If you’re using a Mac or Linux machine, you probably already have Ruby installed. You can check this by opening up a terminal and running the following command:

ruby --version

You should see something like:

$ ruby --version
ruby 2.5.4

Windows users can use RubyInstaller to install Ruby.

Installation

The easiest way to instal objectia-ruby is from RubyGems.

gem install objectia

Or, you can clone the source code for objectia-ruby, and install the library from there.

IMPORTANT

If the command line gives you a long error message that says Permission Denied in the middle of it, try running the above commands with sudo: sudo gem install objectia.

Using

Now that we have Ruby and objectia-ruby installed, we can start using the API. In the example below we will look up the geolocation of the IP address 8.8.8.8.

require "objectia"

api_key = ENV["OBJECTIA_APIKEY"]

client = Objectia::Client.new({
    api_key: api_key,
})
location = client.geoip.get("8.8.8.8")
print "Country code: ", location["country_code"]

TIP

If you do not have an API Key, you can easily generate one by heading over to the API settings page.

Error handling

Handle errors with a rescue block:

# If you enter an invalid IP address, we get an error
begin
    location = client.geoip.get("x")
rescue Objectia::ResponseError => e
    if e.code == "err-invalid-ip"
		# Handle error...
	else
		# Other error...
end

You will find all the error codes in the REST API documentation.