ovh-rest 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bc0866dbac656dfdf22eef5798d5a766d094092
4
+ data.tar.gz: 392e592fcff25c5c6466465581c2911b2a2b170b
5
+ SHA512:
6
+ metadata.gz: 797fd218c4791c34ade0995487b001e90bb8ceca720bae003097f4c077a3a6ea155efdaf0c3e49d0aa08ef2a4fb7083d9c7c254b3cdadb9019b56dac195bd3cf
7
+ data.tar.gz: 3f18825c1bbc9366249ac03e25575b39c5bbb822f47e1db7e0003f36c4b4386de12a21e3249a08849272f927cc8c3e5a6b8a8254e698be38bcf349e56b6e5bc2
@@ -0,0 +1,107 @@
1
+ # OVH-REST
2
+
3
+ OVH-REST is a (really) tiny, unofficial helper library OVH management [API](https://api.ovh.com/console/), wrapping the authentication parts and simplifying interaction in Ruby programs.
4
+
5
+ You need an `appKey` and an `appSecret` to use it, which can be obtained [there](https://www.ovh.com/fr/cgi-bin/api/createApplication.cgi).
6
+
7
+ ## Usage
8
+
9
+ First, you need to generate a `consumerKey` which is grants access to specific methods and parts of the API.
10
+
11
+ ```ruby
12
+ require 'ovh/rest'
13
+
14
+ access = {
15
+ "accessRules" => [
16
+ { "method" => "GET", "path" => "/sms/*" },
17
+ { "method" => "POST", "path" => "/sms/*" },
18
+ { "method" => "PUT", "path" => "/sms/*" },
19
+ { "method" => "DELETE", "path" => "/sms/*" },
20
+ ]
21
+ }
22
+
23
+ OVH::REST.generate_consumer_key("your_appKey", access)
24
+ => {
25
+ "validationUrl" => "https://www.ovh.com/fr/cgi-bin/api/requestCredential.cgi?credentialToken=XXXXXXXXXXXXXXXXXXXXXXX",
26
+ "consumerKey" => "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
27
+ "state" => "pendingValidation"
28
+ }
29
+
30
+ ```
31
+
32
+ You now have your `consumerKey`, which needs validation before use. Validation is done by following the `validationUrl` and entering your credentials.
33
+
34
+ Then you can use API features:
35
+
36
+ ```ruby
37
+ require 'ovh/rest'
38
+
39
+ ovh = OVH::REST.new(apiKey, appSecret, consumerKey)
40
+
41
+ # Get sms account status
42
+ result = ovh.get("/sms/sms-xx12345-1")
43
+
44
+ puts JSON.pretty_generate(result)
45
+ =>
46
+ {
47
+ "status": "enable",
48
+ "creditsLeft": 42,
49
+ "name": "sms-xx12345-1",
50
+ "userQuantityWithQuota": 0,
51
+ "description": "",
52
+ [...]
53
+ }
54
+
55
+ # Send sms
56
+ result = ovh.post("/sms/sms-xx12345-1/jobs", {
57
+ "charset" => "UTF-8",
58
+ "class" => "phoneDisplay",
59
+ "coding" => "7bit",
60
+ "priority" => "high",
61
+ "validityPeriod" => 2880
62
+ "message" => "Dude! Disk is CRITICAL!",
63
+ "receivers" => ["+12345678900", "+12009876543"],
64
+ "sender" => "+12424242424",
65
+ })
66
+
67
+ puts JSON.pretty_generate(result)
68
+ =>
69
+ {
70
+ "totalCreditsRemoved": 2,
71
+ "ids": [
72
+ 12345,
73
+ 12346
74
+ ]
75
+ }
76
+ ```
77
+
78
+ All methods and parameters are described on the API documentation
79
+
80
+ ## Change API URL
81
+
82
+ It's possible to change the API url according to where your OVH account is located. To change the API url for generate_consumer_key use something like this:
83
+
84
+ ```ruby
85
+ OVH::REST.generate_consumer_key("your_appKey", access, 'https://ca.api.ovh.com/1.0')
86
+ ```
87
+
88
+ Or, for calling other methods:
89
+
90
+ ```ruby
91
+ ovh = OVH::REST.new(apiKey, appSecret, consumerKey, 'https://ca.api.ovh.com/1.0')
92
+ ```
93
+
94
+ ## Setup
95
+
96
+ Only tested with MRI >= 1.9.3
97
+
98
+ Dependencies: none
99
+
100
+ Install:
101
+ * git clone https://github.com/Fotolia/ovh-rest
102
+ OR
103
+ * gem install ovh-rest
104
+
105
+ ## Links
106
+
107
+ Introduction, in french: http://www.ovh.com/fr/g934.premiers-pas-avec-l-api
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/lib/ovh.rb CHANGED
@@ -1,3 +1,2 @@
1
1
  module OVH
2
- VERSION = "0.0.1"
3
2
  end
@@ -9,22 +9,33 @@ module OVH
9
9
  class RESTError < StandardError; end
10
10
 
11
11
  class REST
12
- API_URL = "https://api.ovh.com/1.0"
12
+ DEFAULT_API_URL = "https://api.ovh.com/1.0"
13
+
14
+ attr_accessor :api_url
13
15
 
14
16
  class << self
15
- def generate_consumer_key(api_key, access_rules)
16
- uri = URI.parse("#{API_URL}/auth/credential")
17
+ def generate_consumer_key(api_key, access_rules, api_url = nil)
18
+ uri = URI.parse("#{api_url || DEFAULT_API_URL}/auth/credential")
17
19
  request = Net::HTTP::Post.new(uri.path, initheader = {"X-Ovh-Application" => api_key, "Content-type" => "application/json"})
18
20
  request.body = access_rules.to_json
19
- http = Net::HTTP.new(uri.host, uri.port)
21
+ http = build_http_object(uri.host, uri.port)
20
22
  http.use_ssl = true
21
23
  response = http.request(request)
22
24
  JSON.parse(response.body)
23
25
  end
26
+
27
+ def build_http_object(host, port)
28
+ if ENV['https_proxy']
29
+ proxy = URI.parse(ENV['https_proxy'])
30
+ Net::HTTP::Proxy(proxy.host, proxy.port).new(host, port)
31
+ else
32
+ Net::HTTP.new(host, port)
33
+ end
34
+ end
24
35
  end
25
36
 
26
- def initialize(api_key, api_secret, consumer_key)
27
- @api_url = API_URL
37
+ def initialize(api_key, api_secret, consumer_key, api_url = nil)
38
+ @api_url = api_url || DEFAULT_API_URL
28
39
  @api_key, @api_secret, @consumer_key = api_key, api_secret, consumer_key
29
40
  end
30
41
 
@@ -43,7 +54,7 @@ module OVH
43
54
  request = Net::HTTP.const_get(method.capitalize).new(uri.path, initheader = headers)
44
55
  request.body = body
45
56
 
46
- http = Net::HTTP.new(uri.host, uri.port)
57
+ http = REST.build_http_object(uri.host, uri.port)
47
58
  http.use_ssl = true
48
59
  response = http.request(request)
49
60
  result = JSON.parse(response.body)
@@ -1,11 +1,7 @@
1
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
2
  Gem::Specification.new do |spec|
7
3
  spec.name = "ovh-rest"
8
- spec.version = OVH::VERSION
4
+ spec.version = "0.0.3"
9
5
  spec.authors = ["Jonathan Amiez"]
10
6
  spec.email = ["jonathan.amiez@fotolia.com"]
11
7
  spec.description = "OVH REST API interface"
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class TestApiUrl < Test::Unit::TestCase
4
+ def test_default_api_url
5
+ ovh = OVH::REST.new('api_key', 'api_secret', 'consumer_key')
6
+ assert_equal ovh.api_url, 'https://api.ovh.com/1.0'
7
+ end
8
+
9
+ def test_api_url_change
10
+ ca_api = 'https://ca.api.ovh.com/1.0'
11
+
12
+ ovh = OVH::REST.new('api_key', 'api_secret', 'consumer_key', ca_api)
13
+ assert_equal ovh.api_url, ca_api
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ovh/rest'
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ovh-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jonathan Amiez
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2014-06-09 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: OVH REST API interface
15
14
  email:
@@ -18,32 +17,37 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
20
+ - README.md
21
+ - Rakefile
21
22
  - lib/ovh.rb
22
23
  - lib/ovh/rest.rb
23
24
  - ovh-rest.gemspec
25
+ - test/test_api_url.rb
26
+ - test/test_helper.rb
24
27
  homepage: https://github.com/Fotolia/ovh-rest
25
28
  licenses:
26
29
  - MIT
30
+ metadata: {}
27
31
  post_install_message:
28
32
  rdoc_options: []
29
33
  require_paths:
30
34
  - lib
31
35
  required_ruby_version: !ruby/object:Gem::Requirement
32
- none: false
33
36
  requirements:
34
- - - ! '>='
37
+ - - ">="
35
38
  - !ruby/object:Gem::Version
36
39
  version: '0'
37
40
  required_rubygems_version: !ruby/object:Gem::Requirement
38
- none: false
39
41
  requirements:
40
- - - ! '>='
42
+ - - ">="
41
43
  - !ruby/object:Gem::Version
42
44
  version: '0'
43
45
  requirements: []
44
46
  rubyforge_project:
45
- rubygems_version: 1.8.25
47
+ rubygems_version: 2.2.0
46
48
  signing_key:
47
- specification_version: 3
49
+ specification_version: 4
48
50
  summary: Manage OVH services from Ruby
49
- test_files: []
51
+ test_files:
52
+ - test/test_api_url.rb
53
+ - test/test_helper.rb