Class: RubyOvh::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ovh.rb

Constant Summary collapse

OVH_API =
'https://eu.api.ovh.com'
VERSION =
'1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Usage

# First Time : client = RubyOvh::Client.new({application_key: ‘XXXX’, application_secret: ‘YYYY’ }) response = client.generate_consumer_key puts “You need to memorize your consumer_key : #:consumer_key” puts “You need visit this address in your browser in order to activate your consumer key #:validation_url”

# Other times client = RubyOvh::Client.new({application_key: ‘XXXX’, application_secret: ‘YYYY’, consumer_key: ‘ZZZZZ’ })

client.query({ method: ‘GET’, url: “/me”, query: {} })



27
28
29
30
31
# File 'lib/ruby_ovh.rb', line 27

def initialize(options = {})
  @ak = options[:application_key]
  @as = options[:application_secret]
  @ck = options[:consumer_key]
end

Instance Attribute Details

#akObject (readonly)

Returns the value of attribute ak.



11
12
13
# File 'lib/ruby_ovh.rb', line 11

def ak
  @ak
end

#asObject (readonly)

Returns the value of attribute as.



11
12
13
# File 'lib/ruby_ovh.rb', line 11

def as
  @as
end

#ckObject (readonly)

Returns the value of attribute ck.



11
12
13
# File 'lib/ruby_ovh.rb', line 11

def ck
  @ck
end

Instance Method Details

#generate_consumer_key(params = {}) ⇒ Object

Method to call one time to generate a consumer_key (docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/#requesting-an-authentication-token-from-ovh)

Parameters

params : hash with sereval keys :

access_rules : Array of rules see here : https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/#requesting-an-authentication-token-from-ovh
redirection : Url to redirect after clic on validation url see here : https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/#requesting-an-authentication-token-from-ovh
debug : see response to the API.

Return

Hash with validation_url and consumer_key keys (Visit the validation url in your favorite browser and put your consumer_key (ck) in your scripts)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_ovh.rb', line 48

def generate_consumer_key(params = {})
  access_rules = params[:access_rules]
  url_to_redirect = params[:redirection]

  conn = Faraday.new(:url => OVH_API)
  response = conn.post do |req|
    req.url "/#{VERSION}/auth/credential"
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-Ovh-Application'] = @ak
    req.body = {
        "accessRules": (access_rules || [
            {
                "method": "GET",
                "path": "/*"
            },{
                "method": "POST",
                "path": "/*"
            },{
                "method": "PUT",
                "path": "/*"
            }
        ]),
        "redirection": url_to_redirect
    }.to_json
  end

  if params[:debug]
    puts "*" * 200
    puts response.body
    puts "*" * 200
  end

  response = JSON.parse(response.body)

  ck = response['consumerKey']
  url = response['validationUrl']

  { validation_url: url, consumer_key: ck }
end

#query(params = {}) ⇒ Object

This method allow you to call Ovh API.

Parameters

params : hash with these keys :

method: GET or POST or PUT or DELETE
url: url's part of Ovh API (see here : https://eu.api.ovh.com/console/)
query: API POST parameters
debug: to debug

Example

# GET query client.signature_timestamp({ url: “/domain/zone/mydomain.org/record?fieldType=A” , method: “GET”, query: {} })

OR

# POST query client.signature_timestamp({ url: “/domain/zone/mydomain.org/record” , method: “POST”, query:

"subDomain": "blog",
"target": "XX.X.X.XXX",
"fieldType": "A"

})

Return

Response REST API.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby_ovh.rb', line 117

def query(params = {})
  json_body = params[:query].to_json
  url = params[:url]
  url = RubyOvh::Client.normalize_url(url)
  signature_et_ts = self.signature_timestamp({ url: url, query: json_body, method: params[:method].upcase })
  timestamp = signature_et_ts[:timestamp]
  signature = signature_et_ts[:signature]

  headers = {
    'X-Ovh-Application' => @ak,
    'X-Ovh-Timestamp'   => timestamp,
    'X-Ovh-Signature'   => signature,
    'X-Ovh-Consumer'    => @ck,
    'Content-Type'      => 'application/json'
  }

  conn = Faraday.new(:url => OVH_API)
  response = conn.run_request(params[:method].downcase.to_sym,"/#{VERSION}/#{url}",json_body,headers)

  if params[:debug]
    puts "*" * 200
    puts response.inspect
    puts "*" * 200
  end

  JSON.parse(response.body)
end