Skip to content

Repository files navigation

🚛 Onfleet Ruby SDK

Ruby SDK for Onfleet.com's API. This is NOT an official SDK. Official Documentation. The entirety of the response payload is accessible by instance methods. The raw response can is accessible through the params instance method on all objects.

Usage

Install the gem.

gem'onfleet-ruby'

Set the API Key directly:

Onfleet.api_key= <API_KEY>

Or set the API key by environment variable (see below).

Environment variables

Optional

  • ONFLEET_API_KEY If this is not set then you must set Onfleet#api_key directly (see above).

  • ONFLEET_BASE_URL The base URL to send API requests to. Defaults to https://onfleet.com/api/v2/.

Objects

Onfleet::OrganizationOnfleet::AdminOnfleet::WorkerOnfleet::TeamOnfleet::DestinationOnfleet::RecipientOnfleet::TaskOnfleet::Hub

Organizations

GET

org=Onfleet::Organization.getorg.id# => 1234567890org.name# => OrgNameorg.email# => org@email.comorg.country# => United Statesorg.delegatees# => ['delegatees_id']org.timezone# => "America/Los_Angeles"org.time_created# => 1438713844000org.time_last_modified# => 1438713844000

GET delegatee details

delegatee=Onfleet::Organization.get_delegatee_details(id)delegatee.id# => "4eKRvRGA7JW6C8TaGyuJeSrK"delegatee.name# => "North Beach Runners"delegatee.email# => "hello@nbr.co"delegatee.timezone# => "America/Los_Angeles"delegatee.country# => "US"

Administrators

NameTypeDescription
namestringThe administrator’s complete name.
emailstringThe administrator’s email address.
phonestring(Optional) The administrator's E.164-formatted phone number.
metadataarray(Optional) Any associated metadata

Create

admin=Onfleet::Admin.create({name: 'John Doe',email: 'john@company.com',phone: '41555546782'})

Update

admin=Onfleet::Admin.update('ADMIN_ID',{name: 'New Name'})# oradmin.name="New Name"admin.save

Delete

Onfleet::Admin.delete('ADMIN_ID')# => true

List

list=Onfleet::Admin.list# => [<Onfleet::Admin>]list.first# => Onfleet::Admin

Workers

Worker

NameTypeDescription
namestringThe workers complete name.
phonestringThe worker's phone number.
teamsstring ArrayOne or more team IDs of which the worker is a member.
vehicleobject(Optional) The worker’s vehicle, providing no vehicle details is interpreted as the worker being on foot.
metadataarray(Optional) Any associated metadata

Vehicle

NameTypeDescription
typestringThe vehicle’s type, must be one of CAR, MOTORCYCLE, BICYCLE or TRUCK.
descriptionstring(Optional) The vehicle’s make, model, year, or any other relevant identifying details.
license_platestring(Optional) The vehicle’s license plate number.
colorstring(Optional) The worker’s vehicle, providing no vehicle details is interpreted as the worker being on foot.

Create

worker=Onfleet::Worker.create({name: 'John Doe',email: 'john@company.com',teams: ["TEAM_ID"],vehicle: {type: 'CAR'}})worker.name# => "John Doe"worker.vehicle.type# => "CAR"worker.vehicle.color="Blue"worker.saveworker.vehicle.color# => "Blue"

Update

Onfleet::Worker.update({name: "New Name"}# orworker.name="New Name"work.saveworker.name# => "New Name"

Delete

Onfleet::Worker.delete('WORKER_ID')# => true

List

Onfleet::Worker.list

Get

worker=Onfleet::Worker.get('WORKER_ID')

Teams

List

Onfleet::Team.list

Get

Onfleet::Team.get('TEAM_ID')

Destinations

Destination

NameTypeDescription
addressobjectThe destination’s street address details.
locationarray(Optional) The [ longitude, latitude ] geographic coordinates. If missing, the API will geocode based on the address details provided. Note that geocoding may slightly modify the format of the address properties.
notesstring(Optional) Notes for the destination
metadataarray(Optional) Any associated metadata

Address

NameTypeDescription
numberstringThe number component of this address, it may also contain letters.
streetstringThe street name
citystringThe city name
countrystringName Of Country
apartmentstring(Optional) The apartment or suite number
namestring(Optional) A name associated with this address
statestring(Optional) State name
postal_codestring(Optional) The postal code
unparsedstring(Optional) A complete comma seperated address for ex. 148 townsend, 94102, USA. Including this field, all other address details will be ignored. The address will be automatically geocoded.

Create

destination=Onfleet::Destination.create({address: {unparsed: '200 12th st, 94103, ca'}})destination.street# => '12th street'destination.number# => '200'destination.postal_code# => '94103'

Get

Onfleet::Destination('DEST_ID')

Recipients

NameTypeDescription
namestringThe recipient's full name.
phonestringA unique valid phone number.
notesstring(Optional) Notes for the recipient.
skip_sms_notificationsboolean(Optional) To disable sms notification. Defaults to false
skip_phone_number_verificatonboolean(Optional) Whether to skip validation of the phone number.
metadataarray(Optional) Any associated metadata

Create

recipient=Onfleet::Recipient.create({name: 'John Doe',phone: '4155556789'})recipient.id# => ChdA82dA~Dn232recpient.name# => 'John Doe'

Update

Onfleet::Recipient.update('REC_ID',{name: 'New Name'})# orrecipient.name="New Name"recipient.save

Get

Onfleet::Recipient.get('REC_ID')

Find

######Note: Throws InvalidRequestError if cannot find resource

# by name (case sensitive)rec=Onfleet::Recipient.find('name','John Doe')rec.name="John Doe"#by phonerec=Onfleet::Recipient.find('phone','4155556789')

Tasks

NameTypeDescription
destinationstring or hashID of the destination, or the Destination data itself
recipientsarray of string or hashAn array containing zero or one IDs of the task's recipients; alternately, an array containing Recipient data as entries
merchantstring(Optional) ID of merchant organization.
executorstring(Optional) ID of the executor organization.
complete_afternumber(Optional) A timestamp for the earliest time the task should be completed.
complete_beforenumber(Optional) A timestamp for the latest time the task should be completed.
pickup_taskboolean(Optional) Whether the task is a pickup task.
dependenciesstring array(Optional) One or more IDs of tasks which must be completed prior to this task.
notesnotes(Optional) Notes for the task.
auto_assignobject(Optional) The automatic assignment options for the newly created task. See above for exact object structure and allowed values.
metadataarray(Optional) Any associated metadata

Create

# First Create a destination and Recipient# Then create the tasktask=Onfleet::Task.create({recipients: ['REC_ID'],destination: 'DEC_ID'})# Alternatively, create the Destination and Recipient in a single call to Onfleet# If a recipient exists for the phone number, it will be updated with the new informationanother_task=Onfleet::Task.create(destination: {address: {unparsed: "123 Smith St"},notes: "Some destination notes"},recipients: [{name: "Foo Bar",phone: "987-654-3210",notes: "Some recipient notes"}])

Update

Onfleet::Task.update('TASK_ID',{notes: 'Adding some notes'})# ortask.notes="Adding some notes"task.save

Get

task=Onfleet::Task.get('TASK_ID')

Delete

Onfleet::Task.delete('TASK_ID')

List

Onfleet::Task.list# You can also list tasks of certain statesOnfleet::Task.list({state: 0})# => returns all tasks with state 0, see official docs for valid states

Complete Currently not supported

Hubs

NameTypeDescription
namestringThe hub’s name.
locationarrayThe [longitude, latitude] geographic coordinates.
addressobjectThe hub’s street address details.

List

list=Onfleet::Hub.list# => [<Onfleet::Hub>]list.first# => Onfleet::Hub

Metadata

NameTypeDescription
namestringthe name of the property
typestringThe type of the property. Must be one of [ ‘boolean’, ‘number’, ‘string’, ‘object’, ‘array’ ]
subtypestring(Optional) Required only for entries of type array, used for future visualization purposes. Must be one of [ ‘boolean’, ‘number’, ‘string’, ‘object’ ].
valuestringThe value of the property. The JSON type must match the type (and subtype) provided for the entry.
# Returns an array with entities matching the metadata query# Any entity supporting metadata can be queried (eg: Admins, Workers, Tasks, Destinations, Recipients)tasks=Onfleet::Task.query_by_metadata([{name: "property",type: "string",value: "abc"}])

Error Handling

begin# perform onfleet api requestsrescueAuthenticationError=>e# API authentication issuesrescueConnectionError=>e# API connection ProblemsrescueInvalidRequestError=>e# Bad request/invalid request params# also if resource is not foundrescueOnfleetError=>e# general errorend

TODO

  1. Tests
  2. Better error handling

About

Onfleet Ruby Gem

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages