hcloud 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- autoload :Typhoeus, "typhoeus"
2
- autoload :Oj, "oj"
1
+ autoload :Typhoeus, 'typhoeus'
2
+ autoload :Oj, 'oj'
3
3
 
4
4
  module Hcloud
5
5
  class Client
@@ -14,7 +14,7 @@ module Hcloud
14
14
  end
15
15
 
16
16
  def authorized?
17
- request("server_types").run
17
+ request('server_types').run
18
18
  true
19
19
  rescue Error::Unauthorized
20
20
  false
@@ -68,14 +68,14 @@ module Hcloud
68
68
  q << x.to_param
69
69
  end
70
70
  path = path.dup
71
- path << "?"+q.join("&")
71
+ path << '?' + q.join('&')
72
72
  r = Typhoeus::Request.new(
73
73
  "https://api.hetzner.cloud/v1/#{path}",
74
74
  {
75
- headers: {
76
- "Authorization" => "Bearer #{token}",
77
- "Content-Type" => "application/json",
78
- },
75
+ headers: {
76
+ 'Authorization' => "Bearer #{token}",
77
+ 'Content-Type' => 'application/json'
78
+ }
79
79
  }.merge(options)
80
80
  )
81
81
  r.on_complete do |response|
@@ -88,20 +88,20 @@ module Hcloud
88
88
  raise Error::ServerError, "Connection error: #{response.return_code}"
89
89
  when 400...600
90
90
  j = Oj.load(response.body)
91
- code = j.dig("error", "code")
91
+ code = j.dig('error', 'code')
92
92
  error_class = case code
93
- when "invalid_input" then Error::InvalidInput
94
- when "forbidden" then Error::Forbidden
95
- when "locked" then Error::Locked
96
- when "not_found" then Error::NotFound
97
- when "rate_limit_exceeded" then Error::RateLimitExceeded
98
- when "resource_unavailable" then Error::ResourceUnavilable
99
- when "service_error" then Error::ServiceError
100
- when "uniqueness_error" then Error::UniquenessError
93
+ when 'invalid_input' then Error::InvalidInput
94
+ when 'forbidden' then Error::Forbidden
95
+ when 'locked' then Error::Locked
96
+ when 'not_found' then Error::NotFound
97
+ when 'rate_limit_exceeded' then Error::RateLimitExceeded
98
+ when 'resource_unavailable' then Error::ResourceUnavilable
99
+ when 'service_error' then Error::ServiceError
100
+ when 'uniqueness_error' then Error::UniquenessError
101
101
  else
102
102
  Error::ServerError
103
103
  end
104
- raise error_class, j.dig("error", "message")
104
+ raise error_class, j.dig('error', 'message')
105
105
  end
106
106
  end
107
107
  r
@@ -5,10 +5,9 @@ module Hcloud
5
5
  name: nil,
6
6
  description: nil,
7
7
  location: Location,
8
- server_types: nil,
9
- }
8
+ server_types: nil
9
+ }.freeze
10
10
 
11
11
  include EntryLoader
12
-
13
12
  end
14
13
  end
@@ -3,10 +3,10 @@ module Hcloud
3
3
  include Enumerable
4
4
 
5
5
  def all
6
- j = Oj.load(request("datacenters").run.body)
7
- dc = j["datacenters"].map{|x| Datacenter.new(x, self, client) }
8
- dc.reject{|x| x.id == j["recommendation"]}.unshift(
9
- dc.find{|x| x.id == j["recommendation"]}
6
+ j = Oj.load(request('datacenters').run.body)
7
+ dc = j['datacenters'].map { |x| Datacenter.new(x, self, client) }
8
+ dc.reject { |x| x.id == j['recommendation'] }.unshift(
9
+ dc.find { |x| x.id == j['recommendation'] }
10
10
  )
11
11
  end
12
12
 
@@ -16,32 +16,30 @@ module Hcloud
16
16
 
17
17
  def find(id)
18
18
  Datacenter.new(
19
- Oj.load(request("datacenters/#{id}").run.body)["datacenter"],
19
+ Oj.load(request("datacenters/#{id}").run.body)['datacenter'],
20
20
  self,
21
21
  client
22
22
  )
23
23
  end
24
-
24
+
25
25
  def find_by(name:)
26
- x = Oj.load(request("datacenters", q: {name: name}).run.body)["datacenters"]
26
+ x = Oj.load(request('datacenters', q: { name: name }).run.body)['datacenters']
27
27
  return nil if x.none?
28
28
  x.each do |s|
29
29
  return Datacenter.new(s, self, client)
30
30
  end
31
31
  end
32
-
32
+
33
33
  def [](arg)
34
34
  case arg
35
35
  when Integer
36
- begin
37
- find(arg)
38
- rescue Error::NotFound
39
- end
36
+ begin
37
+ find(arg)
38
+ rescue Error::NotFound
39
+ end
40
40
  when String
41
41
  find_by(name: arg)
42
42
  end
43
43
  end
44
-
45
44
  end
46
45
  end
47
-
@@ -5,7 +5,7 @@ module Hcloud
5
5
 
6
6
  included do |klass|
7
7
  klass.send(:attr_reader, :parent, :client)
8
- klass.const_get(:Attributes).each do |attribute, value|
8
+ klass.const_get(:Attributes).each do |attribute, _value|
9
9
  klass.send(:attr_accessor, attribute)
10
10
  end
11
11
  end
@@ -16,14 +16,14 @@ module Hcloud
16
16
  self.class.const_get(:Attributes).each do |attribute, value|
17
17
  case value
18
18
  when nil
19
- self.send("#{attribute}=", resource[attribute.to_s])
19
+ send("#{attribute}=", resource[attribute.to_s])
20
20
  when :time
21
21
  unless resource[attribute.to_s].nil?
22
- self.send("#{attribute}=", Time.parse(resource[attribute.to_s]))
22
+ send("#{attribute}=", Time.parse(resource[attribute.to_s]))
23
23
  end
24
- else
25
- if value.is_a?(Class) and value.include?(EntryLoader)
26
- self.send("#{attribute}=", value.new(resource[attribute.to_s], self, client))
24
+ else
25
+ if value.is_a?(Class) && value.include?(EntryLoader)
26
+ send("#{attribute}=", value.new(resource[attribute.to_s], self, client))
27
27
  end
28
28
  end
29
29
  end
@@ -8,29 +8,29 @@ module Hcloud
8
8
  dns_ptr: nil,
9
9
  server: nil,
10
10
  home_location: Location,
11
- blocked: nil,
12
- }
11
+ blocked: nil
12
+ }.freeze
13
13
  include EntryLoader
14
14
 
15
15
  def update(description:)
16
- j = Oj.load(request("floating_ips/#{id.to_i}",
17
- j: {description: description},
16
+ j = Oj.load(request("floating_ips/#{id.to_i}",
17
+ j: { description: description },
18
18
  method: :put).run.body)
19
- FloatingIP.new(j["floating_ip"], self, client)
19
+ FloatingIP.new(j['floating_ip'], self, client)
20
20
  end
21
-
21
+
22
22
  def assign(server:)
23
- j = Oj.load(request("floating_ips/#{id.to_i}/actions/assign",
24
- j: {server: server}).run.body)
25
- Action.new(j["action"], self, client)
23
+ j = Oj.load(request("floating_ips/#{id.to_i}/actions/assign",
24
+ j: { server: server }).run.body)
25
+ Action.new(j['action'], self, client)
26
26
  end
27
-
27
+
28
28
  def unassign
29
- j = Oj.load(request("floating_ips/#{id.to_i}/actions/unassign",
29
+ j = Oj.load(request("floating_ips/#{id.to_i}/actions/unassign",
30
30
  method: :post).run.body)
31
- Action.new(j["action"], self, client)
31
+ Action.new(j['action'], self, client)
32
32
  end
33
-
33
+
34
34
  def actions
35
35
  ActionResource.new(client: client, parent: self, base_path: "floating_ips/#{id.to_i}")
36
36
  end
@@ -39,6 +39,5 @@ module Hcloud
39
39
  request("floating_ips/#{id}", method: :delete).run.body
40
40
  true
41
41
  end
42
-
43
42
  end
44
43
  end
@@ -3,25 +3,26 @@ module Hcloud
3
3
  include Enumerable
4
4
 
5
5
  def all
6
- j = Oj.load(request("floating_ips").run.body)
7
- j["floating_ips"].map{|x| FloatingIP.new(x, self, client) }
6
+ mj('floating_ips') do |j|
7
+ j.flat_map { |x| x['floating_ips'].map { |x| FloatingIP.new(x, self, client) } }
8
+ end
8
9
  end
9
10
 
10
11
  def create(type:, server: nil, home_location: nil, description: nil)
11
12
  query = {}
12
- method(:create).parameters.inject(query) do |r,x|
13
+ method(:create).parameters.inject(query) do |r, x|
13
14
  (var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
14
15
  end
15
- j = Oj.load(request("floating_ips", j: query, code: 200).run.body)
16
+ j = Oj.load(request('floating_ips', j: query, code: 200).run.body)
16
17
  [
17
- j.key?("action") ? Action.new(j["action"], self, client) : nil,
18
- FloatingIP.new(j["floating_ip"], self, client),
18
+ j.key?('action') ? Action.new(j['action'], self, client) : nil,
19
+ FloatingIP.new(j['floating_ip'], self, client)
19
20
  ]
20
21
  end
21
22
 
22
23
  def find(id)
23
24
  FloatingIP.new(
24
- Oj.load(request("floating_ips/#{id}").run.body)["floating_ip"],
25
+ Oj.load(request("floating_ips/#{id}").run.body)['floating_ip'],
25
26
  self,
26
27
  client
27
28
  )
@@ -30,12 +31,11 @@ module Hcloud
30
31
  def [](arg)
31
32
  case arg
32
33
  when Integer
33
- begin
34
- find(arg)
35
- rescue Error::NotFound
36
- end
34
+ begin
35
+ find(arg)
36
+ rescue Error::NotFound
37
+ end
37
38
  end
38
39
  end
39
-
40
40
  end
41
41
  end
@@ -13,22 +13,22 @@ module Hcloud
13
13
  bound_to: nil,
14
14
  os_flavor: nil,
15
15
  os_version: nil,
16
- rapid_deploy: nil,
17
- }
16
+ rapid_deploy: nil
17
+ }.freeze
18
18
 
19
19
  include EntryLoader
20
20
 
21
21
  def to_snapshot
22
- update(type: "snapshot")
22
+ update(type: 'snapshot')
23
23
  end
24
24
 
25
25
  def update(description: nil, type: nil)
26
26
  query = {}
27
- method(:update).parameters.inject(query) do |r,x|
27
+ method(:update).parameters.inject(query) do |r, x|
28
28
  (var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
29
29
  end
30
30
  Image.new(
31
- Oj.load(request("images/#{id.to_i}", j: query, method: :put).run.body)["image"],
31
+ Oj.load(request("images/#{id.to_i}", j: query, method: :put).run.body)['image'],
32
32
  parent,
33
33
  client
34
34
  )
@@ -1,11 +1,11 @@
1
1
  module Hcloud
2
2
  class ImageResource < AbstractResource
3
3
  def all
4
- mj("images") do |j|
5
- j.flat_map{|x| x["images"].map{ |x| Image.new(x, self, client) } }
4
+ mj('images') do |j|
5
+ j.flat_map { |x| x['images'].map { |x| Image.new(x, self, client) } }
6
6
  end
7
7
  end
8
-
8
+
9
9
  def [](arg)
10
10
  case arg
11
11
  when Integer
@@ -20,29 +20,28 @@ module Hcloud
20
20
 
21
21
  def find(id)
22
22
  Image.new(
23
- Oj.load(request("images/#{id.to_i}").run.body)["image"],
23
+ Oj.load(request("images/#{id.to_i}").run.body)['image'],
24
24
  self,
25
25
  client
26
26
  )
27
27
  end
28
-
28
+
29
29
  def where(sort: nil, type: nil, bound_to: nil, name: nil)
30
30
  query = {}
31
- method(:where).parameters.inject(query) do |r,x|
31
+ method(:where).parameters.inject(query) do |r, x|
32
32
  (var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
33
33
  end
34
- mj("images", q: query) do |j|
35
- j.flat_map{|x| x["images"].map{ |x| Image.new(x, self, client) } }
34
+ mj('images', q: query) do |j|
35
+ j.flat_map { |x| x['images'].map { |x| Image.new(x, self, client) } }
36
36
  end
37
37
  end
38
38
 
39
39
  def find_by(name:)
40
- j = Oj.load(request("images", q: {name: name}).run.body)["images"]
40
+ j = Oj.load(request('images', q: { name: name }).run.body)['images']
41
41
  return if j.none?
42
42
  j.each do |x|
43
43
  return Image.new(x, self, client)
44
44
  end
45
45
  end
46
-
47
46
  end
48
47
  end
@@ -5,8 +5,8 @@ module Hcloud
5
5
  name: nil,
6
6
  description: nil,
7
7
  type: nil
8
- }
9
-
8
+ }.freeze
9
+
10
10
  include EntryLoader
11
11
  end
12
12
  end
@@ -1,8 +1,8 @@
1
1
  module Hcloud
2
2
  class IsoResource < AbstractResource
3
3
  def all
4
- mj("isos") do |j|
5
- j.flat_map{|x| x["isos"].map{ |x| Iso.new(x, self, client) } }
4
+ mj('isos') do |j|
5
+ j.flat_map { |x| x['isos'].map { |x| Iso.new(x, self, client) } }
6
6
  end
7
7
  end
8
8
 
@@ -20,19 +20,18 @@ module Hcloud
20
20
 
21
21
  def find(id)
22
22
  Iso.new(
23
- Oj.load(request("isos/#{id.to_i}").run.body)["iso"],
23
+ Oj.load(request("isos/#{id.to_i}").run.body)['iso'],
24
24
  self,
25
25
  client
26
26
  )
27
27
  end
28
28
 
29
29
  def find_by(name:)
30
- j = Oj.load(request("isos", q: {name: name}).run.body)["isos"]
30
+ j = Oj.load(request('isos', q: { name: name }).run.body)['isos']
31
31
  return if j.none?
32
32
  j.each do |x|
33
33
  return Iso.new(x, self, client)
34
34
  end
35
35
  end
36
-
37
36
  end
38
37
  end
@@ -7,10 +7,9 @@ module Hcloud
7
7
  country: nil,
8
8
  city: nil,
9
9
  longitude: nil,
10
- latitude: nil,
11
- }
10
+ latitude: nil
11
+ }.freeze
12
12
 
13
13
  include EntryLoader
14
-
15
14
  end
16
15
  end
@@ -1,38 +1,37 @@
1
1
  module Hcloud
2
2
  class LocationResource < AbstractResource
3
3
  def all
4
- mj("locations") do |j|
5
- j.flat_map{|x| x["locations"].map{ |x| Location.new(x, self, client) } }
4
+ mj('locations') do |j|
5
+ j.flat_map { |x| x['locations'].map { |x| Location.new(x, self, client) } }
6
6
  end
7
7
  end
8
8
 
9
9
  def find(id)
10
10
  Location.new(
11
- Oj.load(request("locations/#{id}").run.body)["location"],
11
+ Oj.load(request("locations/#{id}").run.body)['location'],
12
12
  self,
13
13
  client
14
14
  )
15
15
  end
16
-
16
+
17
17
  def find_by(name:)
18
- x = Oj.load(request("locations", q: {name: name}).run.body)["locations"]
18
+ x = Oj.load(request('locations', q: { name: name }).run.body)['locations']
19
19
  return nil if x.none?
20
20
  x.each do |s|
21
21
  return Location.new(s, self, client)
22
22
  end
23
23
  end
24
-
24
+
25
25
  def [](arg)
26
26
  case arg
27
27
  when Integer
28
- begin
29
- find(arg)
30
- rescue Error::NotFound
31
- end
28
+ begin
29
+ find(arg)
30
+ rescue Error::NotFound
31
+ end
32
32
  when String
33
33
  find_by(name: arg)
34
34
  end
35
35
  end
36
-
37
36
  end
38
37
  end