leaseweb-rest-api 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: adc4d1eef0cc5d6c853af1e1bfd5283241140d15
4
- data.tar.gz: 906b653fd5dbeeeaab1e91e0c29ce2b5b6afcf86
3
+ metadata.gz: 0d2aac5e36fa1ddbeab7ca822846a079aaea17d9
4
+ data.tar.gz: c2112b2e361ab38d9748b32492a40b2394f3a559
5
5
  SHA512:
6
- metadata.gz: 4269770ca1487cd9c82315b7a1cf5df14e36496bfe7289e722f11f3fae5d79f1a87ec6949adf5a6a257ceae21b2f4ab0e85f2063af5239ed5c617a095aae3343
7
- data.tar.gz: 1b0efc5c9ed2eaad0685eb670f10019fdc41e78334ae6b49d9d86c4f3e5f5c5aacf887b8f97e39e031bb896fe6d532fe493aead9e4c73cfa775d3f688b04c118
6
+ metadata.gz: 8890b2ab77228b22a50143188f8734a373dab38f52f2292ac81496abd7574474feea457d73560b107d05401af4b2c415eae837ac67d8c7e9244c6d9d056df23c
7
+ data.tar.gz: 1ae5426e7ff41dce43d73d4a5bcde4d1c12ed65f4b9876e29413fe719d3ac39ddb881a0a5fc159744ea0dfe32ed2b2024e401015bc1fe7dd48d1f2348e813ebd
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Style/FileName:
2
+ Enabled: false
3
+
4
+ Style/MethodName:
5
+ Enabled: no
6
+
7
+ Style/VariableName:
8
+ Enabled: no
9
+
10
+ Metrics/LineLength:
11
+ Max: 300
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'leaseweb-rest-api'
3
- s.version = '1.0.2'
3
+ s.version = '1.1.0'
4
4
  s.authors = 'Arnoud Vermeer'
5
5
  s.email = 'a.vermeer@tech.leaseweb.com'
6
6
  s.license = 'Apache'
@@ -13,29 +13,39 @@ class LeasewebAPI
13
13
 
14
14
  base_uri 'https://api.leaseweb.com/v1'
15
15
 
16
- def initialize(apikey = nil, privateKey = nil, password = nil, clientId = nil, clientSecret = nil)
17
- @auth_token_url = 'https://auth.leaseweb.com/token'
18
- if !apikey.nil?
19
- @options = { headers: { 'X-Lsw-Auth' => apikey } }
20
- elsif !clientId.nil? && !clientSecret.nil?
21
- access_token = getOauthToken(clientId, clientSecret)['access_token']
22
- @options = { headers: { 'Authorization' => "Bearer #{access_token}" } }
23
- else
24
- puts 'Your API credentials are required.'
25
- exit
26
- end
27
- @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password) unless privateKey.nil? || password.nil?
16
+ def apiKeyAuth(apikey)
17
+ @options = { headers: { 'X-Lsw-Auth' => apikey } }
28
18
  end
29
19
 
30
20
  def getOauthToken(clientId, clientSecret)
31
- auth = { username: clientId, password: clientSecret }
32
- self.class.post(@auth_token_url, basic_auth: auth, body: { grant_type: 'client_credentials' })
21
+ self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: clientId, password: clientSecret }, body: { grant_type: 'client_credentials' })
22
+ @options = { headers: { 'Authorization' => "Bearer #{access_token}" } }
23
+ end
24
+
25
+ def readPrivateKey(privateKey, password)
26
+ @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password)
33
27
  end
34
28
 
35
29
  def get(url)
36
30
  self.class.get(url, @options)
37
31
  end
38
32
 
33
+ def get_paginated(url, key, offset = 0, limit = 50)
34
+ data = []
35
+
36
+ loop do
37
+ response = self.class.get(url + "&offset=#{offset}&limit=#{limit}", @options)
38
+ total = response.parsed_response['_metadata']['totalCount']
39
+
40
+ data += response.parsed_response[key]
41
+
42
+ offset += limit
43
+ break unless offset < total
44
+ end
45
+
46
+ data
47
+ end
48
+
39
49
  def post(url, body)
40
50
  opt = @options.merge!(body: body)
41
51
  self.class.post(url, opt)
data/readme.md CHANGED
@@ -43,12 +43,28 @@ Start by creating a new instance of the `LeasewebAPI` class, and passing your ap
43
43
  api_key = 'e12b534e-3bf6-4208-89e6-e43798b3c30f'
44
44
  privateKeyFile = './id_rsa'
45
45
  password = 'my_super_strong_s3cr3t_passw0rd'
46
- api = LeasewebAPI.new(api_key, privateKeyFile, password)
46
+
47
+ api = LeasewebAPI.new
48
+ api.apiKeyAuth(api_key)
49
+ api.readPrivateKey(privateKeyFile, password)
50
+ ```
51
+
52
+ or via oAuth:
53
+
54
+ ```ruby
55
+ client_id = 'sadasdasd.asdasdasd.com'
56
+ client_secret = 'a844f7c131a7b63e4129cbcf88352034ef11c86481e0cb2ed3653c07345a113b'
57
+ privateKeyFile = './id_rsa'
58
+ password = 'my_super_strong_s3cr3t_passw0rd'
59
+
60
+ api = LeasewebAPI.new
61
+ getOauthToken(client_id, client_secret)
62
+ api.readPrivateKey(privateKeyFile, password)
47
63
  ```
48
64
 
49
65
  All return values are the direct JSON responses from Leaseweb converted into a Hash.
50
66
 
51
- See: [documentation](http://developer.leaseweb.com/document/api-documentation/)
67
+ See: [documentation](http://developer.leaseweb.com/docs/)
52
68
 
53
69
  ### Managing servers
54
70
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaseweb-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnoud Vermeer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-19 00:00:00.000000000 Z
11
+ date: 2017-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -60,6 +60,7 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - ".gitignore"
62
62
  - ".rspec"
63
+ - ".rubocop.yml"
63
64
  - Gemfile
64
65
  - LICENSE.txt
65
66
  - leaseweb-rest-api.gemspec