cloudgarage-api 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 2e00990df04d52b953dc998488cfea4be937655552e6f8681428c86c58b35dde
4
- data.tar.gz: 1ea09fc36a91ef2c284ae317622d062857f60330723433a3515d91b5a44a59ce
3
+ metadata.gz: ed9846103bf204429d93058dc290bfaf654ed47e41bffecb52fe81a56806de58
4
+ data.tar.gz: 0bf05055812f995cea5dc6af117ac4d990dbac38d34095b9a7da05ba625414d9
5
5
  SHA512:
6
- metadata.gz: 7c13723cce461ca9417328b4a0abc9570d3c320c180731adbe8f4bda0d74bc6375531c7c9e87aeb5ac48dcbd502ad684d1c65236f95ad8dc779f111964c71161
7
- data.tar.gz: 732e590bbffd3a9fdfa1e81762a43f5334653e723dee419cebbd28ac26e3faa41f07243b57ebd5c44b2eeef557ac0f2b972056d845ff23f9e8d09abb56b63efa
6
+ metadata.gz: 5411022bee23d99be2e61d88097ea14f16e19084524229691304b89117aaba76e6cd9cf2485588217fb9d1828ea0c079d2970754583a47367e4aa09cc1987944
7
+ data.tar.gz: 288146e09da86819d8816be459a43701f356fada580083fb411920ece08e56bd7e8b20e8caee056017892720e17c1d2595b05fdb3478e7891d578fd20141d12c
data/README.md CHANGED
@@ -29,8 +29,6 @@ require 'cloudgarage-api'
29
29
  client = CloudGarage.new(client_id, client_secret)
30
30
  ```
31
31
 
32
- If you receive some exceptions of `RestClient`, see error messages by `$!.response.body`.
33
-
34
32
  ### Contract APIs
35
33
 
36
34
  ```ruby
@@ -70,10 +68,10 @@ client.servers
70
68
  client.server_info(server_id)
71
69
  #=> information of a server instance specified by UUID
72
70
 
73
- client.server_auto_backup_info(server_id) # not works well
71
+ client.server_auto_backup_info(server_id)
74
72
  #=> information of a server auto backup specified by UUID
75
73
 
76
- client.server_security_info(server_id) # not works well
74
+ client.server_security_info(server_id)
77
75
  #=> information of a server security specified by UUID
78
76
 
79
77
  # create a server
@@ -1,7 +1,7 @@
1
1
 
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "cloudgarage-api"
4
+ require "cloudgarage-api/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "cloudgarage-api"
@@ -7,7 +7,6 @@
7
7
  require 'rest-client'
8
8
 
9
9
  class CloudGarage
10
- VERSION = "0.1.0".freeze
11
10
  BASE_URI = 'https://api.cloudgarage.jp'.freeze
12
11
 
13
12
  def initialize(client_id, client_secret)
@@ -17,49 +16,49 @@ class CloudGarage
17
16
 
18
17
  def token
19
18
  payload = {'client_id' => @client_id, 'client_secret' => @client_secret}
20
- @token = post('tokens', payload)['token']['id']
19
+ @token = post('tokens', payload)[:token][:id]
21
20
  end
22
21
 
23
22
  def contracts(contract_id = nil)
24
23
  if contract_id
25
- get("contracts/#{contract_id}")['contract']
24
+ get("contracts/#{contract_id}")[:contract]
26
25
  else
27
- get('contracts')['contracts']
26
+ get('contracts')[:contracts]
28
27
  end
29
28
  end
30
29
 
31
30
  # image_type: :os, :application, :private
32
31
  def images(image_type = nil)
33
- images = get("images")['images']
32
+ images = get("images")[:images]
34
33
  if image_type
35
34
  image_type = image_type.to_s.upcase
36
- images.select!{|i| i['image_type'] == image_type}
35
+ images.select!{|i| i[:image_type] == image_type}
37
36
  end
38
37
  return images
39
38
  end
40
39
 
41
40
  def keypairs(keypair_id = nil)
42
41
  if keypair_id
43
- get("keypairs/#{keypair_id}")['keypair']
42
+ get("keypairs/#{keypair_id}")[:keypair]
44
43
  else
45
- get('keypairs')['keypairs']
44
+ get('keypairs')[:keypairs]
46
45
  end
47
46
  end
48
47
 
49
48
  def servers()
50
- get('servers')['servers']
49
+ get('servers')[:servers]
51
50
  end
52
51
 
53
52
  def server_info(server_id)
54
- get("servers/#{server_id}")['server_detail']
53
+ get("servers/#{server_id}")[:server_detail]
55
54
  end
56
55
 
57
56
  def server_auto_backup_info(server_id)
58
- get("servers/#{server_id}/autoBackup")['autoBackup']
57
+ get("servers/#{server_id}/autoBackup")[:autoBackup]
59
58
  end
60
59
 
61
60
  def server_security_info(server_id)
62
- get("servers/#{server_id}/security")['securityRules']
61
+ get("servers/#{server_id}/security")[:securityRules]
63
62
  end
64
63
 
65
64
  def create_server(name, password, contract_id: nil, spec: {}, ports: [], image_id: nil, keyname: nil, comment: nil)
@@ -70,7 +69,7 @@ class CloudGarage
70
69
  payload['image_id'] = image_id if image_id
71
70
  payload['keyname'] = keyname if keyname
72
71
  payload['comment'] = comment if comment
73
- post('servers', payload)['resource_id']
72
+ post('servers', payload)[:resource_id]
74
73
  end
75
74
 
76
75
  # operation: :start, :restart, :restart_hard, :stop
@@ -89,7 +88,7 @@ class CloudGarage
89
88
  end
90
89
 
91
90
  def version
92
- get("version")['version']
91
+ get("version")[:version]
93
92
  end
94
93
 
95
94
  private
@@ -99,19 +98,40 @@ private
99
98
  return h
100
99
  end
101
100
 
101
+ def call_api
102
+ begin
103
+ yield
104
+ rescue RestClient::ExceptionWithResponse => ex
105
+ def ex.message
106
+ if @response
107
+ "#{default_message}: #{JSON.parse(response.body)['fault']['messages'].join(' ')}"
108
+ else
109
+ default_message
110
+ end
111
+ end
112
+ raise ex
113
+ end
114
+ end
115
+
102
116
  def get(api)
103
- parse_body(RestClient.get("#{BASE_URI}/#{api}", header))
117
+ call_api do
118
+ parse_body(RestClient.get("#{BASE_URI}/#{api}", header))
119
+ end
104
120
  end
105
121
 
106
122
  def post(api, payload)
107
- parse_body(RestClient.post("#{BASE_URI}/#{api}", payload.to_json, header))
123
+ call_api do
124
+ parse_body(RestClient.post("#{BASE_URI}/#{api}", payload.to_json, header))
125
+ end
108
126
  end
109
127
 
110
128
  def delete(api)
111
- RestClient.delete("#{BASE_URI}/#{api}", header)
129
+ call_api do
130
+ RestClient.delete("#{BASE_URI}/#{api}", header)
131
+ end
112
132
  end
113
133
 
114
134
  def parse_body(res)
115
- JSON.parse(res.body)
135
+ JSON.parse(res.body, symbolize_names: true)
116
136
  end
117
137
  end
@@ -0,0 +1,3 @@
1
+ class CloudGarage
2
+ VERSION = "0.1.2".freeze
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudgarage-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tada, Tadashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-11 00:00:00.000000000 Z
11
+ date: 2019-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -112,6 +112,7 @@ files:
112
112
  - bin/setup
113
113
  - cloudgarage-api.gemspec
114
114
  - lib/cloudgarage-api.rb
115
+ - lib/cloudgarage-api/version.rb
115
116
  homepage: https://github.com/tdtds/cloudgarage-api
116
117
  licenses: []
117
118
  metadata: {}