globodns_client 0.1.0 → 0.1.1

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: ec954bb4426dd8729340fdd7932850bdaa9a0b48
4
- data.tar.gz: 0a70efb439f1a9ef2fb9b11f6b4e86c1d28564f7
3
+ metadata.gz: ef189c0e0e8641ad46bce929c8e9b65ef9f7a8a8
4
+ data.tar.gz: 679c80481af03e2fe7c957b874070d87c279e06e
5
5
  SHA512:
6
- metadata.gz: cda40cdb67fa27ab1a176522633e6134e7d0d2e94b92429178c9c7f9ea0da283936a2b2c90024dee7ddb8e585a9ce6e84511ecc4b335f6a537cab0e7f5f9edbb
7
- data.tar.gz: ab981b559774be47b2fab5152d23ce2473643a43564657894197f2b0b606f8e363007e6b149a90959b84d95932f86eead9a486c603b2dd8b61ce0a45ad5da20f
6
+ metadata.gz: 1a21db3fe2c80ce26a58ead6da080e104836ce5b3b8776f5f219395cdff6cc1a704e866ab4e32c30e39a478b2e67d74eba8add2b405c32c2db08d58ed47b8ebe
7
+ data.tar.gz: 427778107dcc3dbb602f16b55a10fca3c31ff264495b09e5473d874ed09df367321da07ccf45e9184363e22f84a892fc582fac3d1334b85010dd9c9e325ddda6
@@ -1,4 +1,5 @@
1
1
  require "globodns_client/version"
2
2
  require 'globodns_client/connection'
3
+ require 'globodns_client/exceptions'
3
4
 
4
5
  module GlobodnsClient; end
@@ -7,17 +7,17 @@ module GlobodnsClient
7
7
  @auth_token = options[:auth_token]
8
8
  @host = options[:host]
9
9
  @timeout = options[:timeout] || 30
10
- raise "You must inform the auth_token and host for GloboDNS" unless @auth_token && @host
10
+ raise ArgumentError, "You must inform the auth_token and host for GloboDNS" unless @auth_token && @host
11
11
  end
12
12
 
13
- def get_zone(fqdn, kind = 'A')
13
+ def get_zone(key, kind = 'A')
14
14
  if kind.eql?('A')
15
- domain = fqdn.split('.', 2).last
15
+ domain = key.split('.', 2).last
16
16
  elsif kind.eql?('PTR')
17
- if fqdn.include?('in-addr.arpa')
18
- domain = fqdn.split('.', 2).last
17
+ if key.include?('in-addr.arpa')
18
+ domain = key.split('.', 2).last
19
19
  else
20
- match = fqdn.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)
20
+ match = key.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)
21
21
  domain = (match[1..3]+["0"]).reverse.join('.')+'.in-addr.arpa'
22
22
  end
23
23
  else
@@ -28,15 +28,15 @@ module GlobodnsClient
28
28
  if domain.count('.') > 1 && kind == 'A' || domain.count('.') > 2 && kind == 'PTR'
29
29
  res = get_zone(domain, kind)
30
30
  else
31
- raise "Couldn't find a proper zone for '#{@fqdn}'"
31
+ raise GlobodnsClient::NotFound, "Couldn't find a proper zone for '#{key}'"
32
32
  end
33
33
  end
34
34
  res.is_a?(Array) ? res[0]['domain'] : res
35
35
  end
36
36
 
37
- def get_record(fqdn, kind, zone = nil)
38
- zone = get_zone(fqdn, kind) if zone.nil?
39
- host = get_host(fqdn, zone, kind)
37
+ def get_record(key, kind, zone = nil)
38
+ zone = get_zone(key, kind) if zone.nil?
39
+ host = get_host(key, zone, kind)
40
40
  response = request('get', 'record', host, zone['id'], kind)
41
41
  response.each do |r|
42
42
  return r[kind.downcase] unless r[kind.downcase].nil?
@@ -44,49 +44,49 @@ module GlobodnsClient
44
44
  false
45
45
  end
46
46
 
47
- def new_record(fqdn, kind, value)
48
- zone = get_zone(fqdn, kind)
49
- if record = get_record(fqdn, kind, zone)
50
- raise "Address already (#{fqdn}) exists with ip (#{record['content']})"
47
+ def new_record(key, kind, value)
48
+ zone = get_zone(key, kind)
49
+ if record = get_record(key, kind, zone)
50
+ raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists with reference (#{record['content']})"
51
51
  else
52
- host = get_host(fqdn, zone, kind)
52
+ host = get_host(key, zone, kind)
53
53
  response = request('post', 'record', host, zone['id'], kind, value)
54
54
  end
55
55
  response['record']
56
56
  end
57
57
 
58
- def delete_record(fqdn, kind)
59
- zone = get_zone(fqdn, kind)
60
- unless record = get_record(fqdn, kind, zone)
61
- raise "Record not found for (#{fqdn})"
58
+ def delete_record(key, kind)
59
+ zone = get_zone(key, kind)
60
+ unless record = get_record(key, kind, zone)
61
+ raise GlobodnsClient::NotFound, "Record not found for (#{key})"
62
62
  end
63
63
  response = request('delete', 'record', nil, record['id'])
64
64
  end
65
65
 
66
66
  private
67
67
 
68
- def get_host(fqdn, zone, kind)
68
+ def get_host(key, zone, kind)
69
69
  if kind.eql?('A')
70
- host = fqdn.split('.'+zone['name'])[0]
70
+ host = key.split('.'+zone['name'])[0]
71
71
  elsif kind.eql?('PTR')
72
72
  case zone['name'].count('.')
73
73
  when 4, 5
74
- host = fqdn.split('.').last
74
+ host = key.split('.').last
75
75
  when 3
76
- host = fqdn.split('.')[2..3].reverse.join('.')
76
+ host = key.split('.')[2..3].reverse.join('.')
77
77
  when 2
78
- host = fqdn.split('.')[1..3].reverse.join('.')
78
+ host = key.split('.')[1..3].reverse.join('.')
79
79
  else
80
80
  raise "Error"
81
81
  end
82
82
  else
83
- raise "Not implemented 1"
83
+ raise "Not implemented"
84
84
  end
85
85
  end
86
86
 
87
87
  def request(method,kind,value,id = nil, type = nil, addr = nil)
88
88
 
89
- raise "Invalid request. id shouldn't be nil" if kind.eql?('record') && id.nil?
89
+ raise ArgumentError, "Invalid request. id shouldn't be nil" if kind.eql?('record') && id.nil?
90
90
  headers = {'X-Auth-Token' => @auth_token, 'Content-type' => 'application/json'}
91
91
  if kind.eql?('domain')
92
92
  uri = 'domains.json'
@@ -119,7 +119,7 @@ module GlobodnsClient
119
119
  )
120
120
 
121
121
  if response.code < 200 || response.code > 399
122
- raise "Couldn't get a response - code (#{response.code} / message #{response.body})"
122
+ raise "Couldn't get a response from GloboDNS - code (#{response.code} / message #{response.body})"
123
123
  end
124
124
  method.eql?('delete') ? "" : JSON.parse(response.body)
125
125
  end
@@ -0,0 +1,33 @@
1
+ module GlobodnsClient
2
+ class AlreadyExists < StandardError
3
+ attr_reader :message
4
+
5
+ def initialize(message)
6
+ @message = message
7
+ end
8
+
9
+ def to_s #:nodoc:
10
+ "#{@message}"
11
+ end
12
+
13
+ def inspect #:nodoc:
14
+ "#<#{self.class}: message: #{@message.inspect}>"
15
+ end
16
+ end
17
+
18
+ class NotFound < StandardError
19
+ attr_reader :message
20
+
21
+ def initialize(message)
22
+ @message = message
23
+ end
24
+
25
+ def to_s #:nodoc:
26
+ "#{@message}"
27
+ end
28
+
29
+ def inspect #:nodoc:
30
+ "#<#{self.class}: message: #{@message.inspect}>"
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module GlobodnsClient
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globodns_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernesto Thorp
@@ -99,6 +99,7 @@ files:
99
99
  - globodns_client.gemspec
100
100
  - lib/globodns_client.rb
101
101
  - lib/globodns_client/connection.rb
102
+ - lib/globodns_client/exceptions.rb
102
103
  - lib/globodns_client/version.rb
103
104
  homepage: https://github.com/globocom/globodns-client-ruby
104
105
  licenses: