ruby_ovh 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ruby_ovh.rb +197 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7feaa7e75da71c99ea03047f28da5146c87df3fe893bfca7761cdc4d0c41cb4c
4
+ data.tar.gz: 5c7a297a967e7ac0eccc1f57a5d7ef51a550c80a317911b7f1849b5724042dae
5
+ SHA512:
6
+ metadata.gz: 406e4562798e3c95b24e390beede985bb8cffb51247d1ec4efa4d6d7b33ba8c7259d0b6843969b1e8c0b9b39cfed9c9ff5f0244785f0bc161a0162fa595a0f59
7
+ data.tar.gz: ef2ef77f13d34d577361c35bc71e2a5646aafe69c7bab02d7c2ee6dd7abe981d2582a9fd9efad477a8ffaa43234abc3a718f4144a152455930b0f9277f4ed0c5
data/lib/ruby_ovh.rb ADDED
@@ -0,0 +1,197 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'digest'
4
+
5
+ module RubyOvh
6
+ class Client
7
+
8
+ OVH_API = 'https://eu.api.ovh.com'
9
+ VERSION = '1.0'
10
+
11
+ attr_reader :ak, :ck, :as
12
+
13
+ ##
14
+ # == Usage
15
+ #
16
+ # # First Time :
17
+ # client = RubyOvh::Client.new({application_key: 'XXXX', application_secret: 'YYYY' })
18
+ # response = client.generate_consumer_key
19
+ # puts "You need to memorize your consumer_key : #{response[:consumer_key]}"
20
+ # puts "You need visit this address in your browser in order to activate your consumer key #{response[:validation_url]}"
21
+ #
22
+ # # Other times
23
+ # client = RubyOvh::Client.new({application_key: 'XXXX', application_secret: 'YYYY', consumer_key: 'ZZZZZ' })
24
+ #
25
+ # client.query({ method: 'GET', url: "/me", query: {} })
26
+ #
27
+ def initialize(options = {})
28
+ @ak = options[:application_key]
29
+ @as = options[:application_secret]
30
+ @ck = options[:consumer_key]
31
+ end
32
+
33
+ ##
34
+ # Method to call one time to generate a consumer_key (https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/#requesting-an-authentication-token-from-ovh)
35
+ #
36
+ # == Parameters
37
+ #
38
+ # params : hash with sereval keys :
39
+ # 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
40
+ # 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
41
+ # debug : see response to the API.
42
+ #
43
+ # == Return
44
+ #
45
+ # Hash with validation_url and consumer_key keys
46
+ # (Visit the validation url in your favorite browser and put your consumer_key (ck) in your scripts)
47
+ #
48
+ def generate_consumer_key(params = {})
49
+ access_rules = params[:access_rules]
50
+ url_to_redirect = params[:redirection]
51
+
52
+ conn = Faraday.new(:url => OVH_API)
53
+ response = conn.post do |req|
54
+ req.url "/#{VERSION}/auth/credential"
55
+ req.headers['Content-Type'] = 'application/json'
56
+ req.headers['X-Ovh-Application'] = @ak
57
+ req.body = {
58
+ "accessRules": (access_rules || [
59
+ {
60
+ "method": "GET",
61
+ "path": "/*"
62
+ },{
63
+ "method": "POST",
64
+ "path": "/*"
65
+ },{
66
+ "method": "PUT",
67
+ "path": "/*"
68
+ }
69
+ ]),
70
+ "redirection": url_to_redirect
71
+ }.to_json
72
+ end
73
+
74
+ if params[:debug]
75
+ puts "*" * 200
76
+ puts response.body
77
+ puts "*" * 200
78
+ end
79
+
80
+ response = JSON.parse(response.body)
81
+
82
+ ck = response['consumerKey']
83
+ url = response['validationUrl']
84
+
85
+ { validation_url: url, consumer_key: ck }
86
+ end
87
+
88
+ ##
89
+ # This method allow you to call Ovh API.
90
+ #
91
+ # == Parameters
92
+ #
93
+ # params : hash with these keys :
94
+ # method: GET or POST or PUT or DELETE
95
+ # url: url's part of Ovh API (see here : https://eu.api.ovh.com/console/)
96
+ # query: API POST parameters
97
+ # debug: to debug
98
+ #
99
+ # == Example
100
+ #
101
+ # # GET query
102
+ # client.signature_timestamp({ url: "/domain/zone/mydomain.org/record?fieldType=A" , method: "GET", query: {} })
103
+ #
104
+ # OR
105
+ #
106
+ # # POST query
107
+ # client.signature_timestamp({ url: "/domain/zone/mydomain.org/record" , method: "POST", query: {
108
+ # "subDomain": "blog",
109
+ # "target": "XX.X.X.XXX",
110
+ # "fieldType": "A"
111
+ # }})
112
+ #
113
+ # == Return
114
+ #
115
+ # Response REST API.
116
+ #
117
+ def query(params = {})
118
+ json_body = params[:query].to_json
119
+ url = params[:url]
120
+ url = RubyOvh::Client.normalize_url(url)
121
+ signature_et_ts = self.signature_timestamp({ url: url, query: json_body, method: params[:method].upcase })
122
+ timestamp = signature_et_ts[:timestamp]
123
+ signature = signature_et_ts[:signature]
124
+
125
+ headers = {
126
+ 'X-Ovh-Application' => @ak,
127
+ 'X-Ovh-Timestamp' => timestamp,
128
+ 'X-Ovh-Signature' => signature,
129
+ 'X-Ovh-Consumer' => @ck,
130
+ 'Content-Type' => 'application/json'
131
+ }
132
+
133
+ conn = Faraday.new(:url => OVH_API)
134
+ response = conn.run_request(params[:method].downcase.to_sym,"/#{VERSION}/#{url}",json_body,headers)
135
+
136
+ if params[:debug]
137
+ puts "*" * 200
138
+ puts response.inspect
139
+ puts "*" * 200
140
+ end
141
+
142
+ JSON.parse(response.body)
143
+ end
144
+
145
+ protected
146
+
147
+ ##
148
+ # Each Ovh API request needs a signature, this method generate the signature.
149
+ #
150
+ # == Parameters
151
+ #
152
+ # params : hash with these keys :
153
+ # method: GET or POST or PUT or DELETE
154
+ # url: url's part of Ovh API (see here : https://eu.api.ovh.com/console/)
155
+ # query: API POST parameters
156
+ # debug: to debug
157
+ #
158
+ # == Example
159
+ #
160
+ # client.signature_timestamp({ url: "/domain/zone/mydomain.org/record?fieldType=A" , method: "GET", query: {} })
161
+ #
162
+ # OR
163
+ #
164
+ # client.signature_timestamp({ url: "/domain/zone/mydomain.org/record" , method: "POST", query: {
165
+ # "subDomain": "blog",
166
+ # "target": "XX.X.X.XXX",
167
+ # "fieldType": "A"
168
+ # }})
169
+ #
170
+ # == Return
171
+ #
172
+ # Hash with timestamp and signature keys
173
+ #
174
+ def signature_timestamp(params = {})
175
+ conn = Faraday.new(:url => OVH_API)
176
+ response = conn.get do |req|
177
+ req.url "/#{VERSION}/auth/time"
178
+ end
179
+
180
+ timestamp = response.body
181
+ #timestamp = Time.now.to_i
182
+ puts "Timestamp : #{timestamp}" if params[:debug]
183
+
184
+ pre_hash_signature = [@as, @ck, params[:method], "#{OVH_API}/#{VERSION}/#{params[:url]}", params[:query], timestamp].join("+")
185
+ puts "Pre Hash Signature : #{pre_hash_signature}" if params[:debug]
186
+
187
+ post_hash_signature = "$1$#{Digest::SHA1.hexdigest(pre_hash_signature)}"
188
+ puts "Post Hash Signature : #{post_hash_signature}" if params[:debug]
189
+
190
+ { timestamp: timestamp, signature: post_hash_signature }
191
+ end
192
+
193
+ def self.normalize_url(url)
194
+ (url.start_with?('/') ? url[1..-1] : url)
195
+ end
196
+ end
197
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_ovh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Sylvain Claudel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem allow you to communicate with OVH API
28
+ email: claudel.sylvain@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/ruby_ovh.rb
34
+ homepage: https://rivsc.space
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.0.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Ruby OVH client API
57
+ test_files: []