DEV Community

loading...
Cover image for Use OVH API in Ruby

Use OVH API in Ruby

Bento
Hi, I'm Bento ✌️ I'm a 🇫🇷 developer. I started my journey with PHP-Symfony before falling in ❤️ with Ruby and Rails. I now work as a fullstack dev with Ruby&React as a freelancer 💪
・1 min read

If you're searching to quickly copy&paste some code to use OVH's API in Ruby... welcome my friend.
This post is for you. If not, you may not be interested.

Introduction

Lately I struggled to use OVH's API on a Ruby because there isn't any official wrapper made for Ruby.

So I followed OVH's tutorial to make a call to their API, and translated it in a simple Ruby script.

Step 1: Create your script keys

Fill the informations on this page to create your script credentials.

Step 2: Write the Ruby script

Create a file called app.rb for example, and put the code below :

require 'httparty'
require 'digest/sha1'

# CREDENTIALS
APPLICATION_KEY = "hDSISKsDSKdSOHIs" # Fill it with your own key
APPLICATION_SECRET = "hHkNkGHkGuyFliuHGIUGiohmoiHHIUGh" # Fill it with your own key
CONSUMER_KEY = "hIOHoiNkhIgfOYUmpioGPIuhihujIMUG" # Fill it with your own key

BASE_URL = "https://eu.api.ovh.com/1.0"
QUERY = BASE_URL + "/telephony/aliases"

######## STEP 1: CREATE SIGNATURE ########

METHOD = "GET"
QUERY = "https://eu.api.ovh.com/1.0/telephony/aliases"
BODY = ""
TSTAMP = HTTParty.get("https://eu.api.ovh.com/1.0/auth/time").to_s
SIGNATURE = "$1$" + Digest::SHA1.hexdigest(APPLICATION_SECRET + "+" + CONSUMER_KEY + "+" + METHOD + "+" + QUERY + "+" + BODY + "+" + TSTAMP)

######## STEP 2: GET TELEPHONY ALIASES (e.g) ########

options = {
  "headers": {
    "X-Ovh-Application": APPLICATION_KEY,
    "X-Ovh-Consumer": CONSUMER_KEY,
    "X-Ovh-Signature": SIGNATURE,
    "X-Ovh-Timestamp": TSTAMP,
  }
}

aliases = HTTParty.get(BASE_URL + QUERY, options)
puts aliases

Enter fullscreen mode Exit fullscreen mode

Then run :

> ruby app.rb
Enter fullscreen mode Exit fullscreen mode

And you should see something like :

[
    "xxxxxxxxxx",
    "xxxxxxxxxx",
    "xxxxxxxxxx",
    ...
  ]
Enter fullscreen mode Exit fullscreen mode

Discussion (0)