ovh-rest 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ovh.rb +3 -0
- data/lib/ovh/rest.rb +78 -0
- data/ovh-rest.gemspec +19 -0
- metadata +49 -0
data/lib/ovh.rb
ADDED
data/lib/ovh/rest.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require "uri"
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module OVH
|
8
|
+
|
9
|
+
class RESTError < StandardError; end
|
10
|
+
|
11
|
+
class REST
|
12
|
+
API_URL = "https://api.ovh.com/1.0"
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def generate_consumer_key(api_key, access_rules)
|
16
|
+
uri = URI.parse("#{API_URL}/auth/credential")
|
17
|
+
request = Net::HTTP::Post.new(uri.path, initheader = {"X-Ovh-Application" => api_key, "Content-type" => "application/json"})
|
18
|
+
request.body = access_rules.to_json
|
19
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
20
|
+
http.use_ssl = true
|
21
|
+
response = http.request(request)
|
22
|
+
JSON.parse(response.body)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(api_key, api_secret, consumer_key)
|
27
|
+
@api_url = API_URL
|
28
|
+
@api_key, @api_secret, @consumer_key = api_key, api_secret, consumer_key
|
29
|
+
end
|
30
|
+
|
31
|
+
[:get, :post, :put, :delete].each do |method|
|
32
|
+
define_method method do |endpoint, payload = nil|
|
33
|
+
raise RESTError, "Invalid endpoint #{endpoint}, should match '/<service>/.*'" unless %r{^/\w+/.*$}.match(endpoint)
|
34
|
+
|
35
|
+
url = @api_url + endpoint
|
36
|
+
uri = URI.parse(url)
|
37
|
+
body = payload.to_json unless payload.nil?
|
38
|
+
|
39
|
+
# create OVH authentication headers
|
40
|
+
headers = build_headers(method, url, body)
|
41
|
+
|
42
|
+
# instanciate Net::HTTP::Get, Post, Put or Delete class
|
43
|
+
request = Net::HTTP.const_get(method.capitalize).new(uri.path, initheader = headers)
|
44
|
+
request.body = body
|
45
|
+
|
46
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
47
|
+
http.use_ssl = true
|
48
|
+
response = http.request(request)
|
49
|
+
result = JSON.parse(response.body)
|
50
|
+
|
51
|
+
unless response.is_a?(Net::HTTPSuccess)
|
52
|
+
raise RESTError, "Error querying #{endpoint}: #{result["message"]}"
|
53
|
+
end
|
54
|
+
|
55
|
+
result
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def build_headers(method, url, body)
|
62
|
+
ts = Time.now.to_i.to_s
|
63
|
+
sig = compute_signature(method, url, body, ts)
|
64
|
+
|
65
|
+
headers = {
|
66
|
+
"X-Ovh-Application" => @api_key,
|
67
|
+
"X-Ovh-Consumer" => @consumer_key,
|
68
|
+
"X-Ovh-Timestamp" => ts,
|
69
|
+
"X-Ovh-Signature" => sig,
|
70
|
+
"Content-type" => "application/json"
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def compute_signature(method, url, body, ts)
|
75
|
+
"$1$" + Digest::SHA1.hexdigest("#{@api_secret}+#{@consumer_key}+#{method.upcase}+#{url}+#{body}+#{ts}")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/ovh-rest.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ovh'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ovh-rest"
|
8
|
+
spec.version = OVH::VERSION
|
9
|
+
spec.authors = ["Jonathan Amiez"]
|
10
|
+
spec.email = ["jonathan.amiez@fotolia.com"]
|
11
|
+
spec.description = "OVH REST API interface"
|
12
|
+
spec.summary = "Manage OVH services from Ruby"
|
13
|
+
spec.homepage = "https://github.com/Fotolia/ovh-rest"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ovh-rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Amiez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: OVH REST API interface
|
15
|
+
email:
|
16
|
+
- jonathan.amiez@fotolia.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/ovh.rb
|
22
|
+
- lib/ovh/rest.rb
|
23
|
+
- ovh-rest.gemspec
|
24
|
+
homepage: https://github.com/Fotolia/ovh-rest
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.25
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Manage OVH services from Ruby
|
49
|
+
test_files: []
|