globodns_client 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/globodns_client.rb +1 -0
 - data/lib/globodns_client/connection.rb +27 -27
 - data/lib/globodns_client/exceptions.rb +33 -0
 - data/lib/globodns_client/version.rb +1 -1
 - metadata +2 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: ef189c0e0e8641ad46bce929c8e9b65ef9f7a8a8
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 679c80481af03e2fe7c957b874070d87c279e06e
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 1a21db3fe2c80ce26a58ead6da080e104836ce5b3b8776f5f219395cdff6cc1a704e866ab4e32c30e39a478b2e67d74eba8add2b405c32c2db08d58ed47b8ebe
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 427778107dcc3dbb602f16b55a10fca3c31ff264495b09e5473d874ed09df367321da07ccf45e9184363e22f84a892fc582fac3d1334b85010dd9c9e325ddda6
         
     | 
    
        data/lib/globodns_client.rb
    CHANGED
    
    
| 
         @@ -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( 
     | 
| 
      
 13 
     | 
    
         
            +
                def get_zone(key, kind = 'A')
         
     | 
| 
       14 
14 
     | 
    
         
             
                  if kind.eql?('A')
         
     | 
| 
       15 
     | 
    
         
            -
                    domain =  
     | 
| 
      
 15 
     | 
    
         
            +
                    domain = key.split('.', 2).last
         
     | 
| 
       16 
16 
     | 
    
         
             
                  elsif kind.eql?('PTR')
         
     | 
| 
       17 
     | 
    
         
            -
                    if  
     | 
| 
       18 
     | 
    
         
            -
                      domain =  
     | 
| 
      
 17 
     | 
    
         
            +
                    if key.include?('in-addr.arpa')
         
     | 
| 
      
 18 
     | 
    
         
            +
                      domain = key.split('.', 2).last
         
     | 
| 
       19 
19 
     | 
    
         
             
                    else
         
     | 
| 
       20 
     | 
    
         
            -
                      match =  
     | 
| 
      
 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 '#{ 
     | 
| 
      
 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( 
     | 
| 
       38 
     | 
    
         
            -
                  zone = get_zone( 
     | 
| 
       39 
     | 
    
         
            -
                  host = get_host( 
     | 
| 
      
 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( 
     | 
| 
       48 
     | 
    
         
            -
                  zone = get_zone( 
     | 
| 
       49 
     | 
    
         
            -
                  if record = get_record( 
     | 
| 
       50 
     | 
    
         
            -
                    raise " 
     | 
| 
      
 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( 
     | 
| 
      
 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( 
     | 
| 
       59 
     | 
    
         
            -
                  zone = get_zone( 
     | 
| 
       60 
     | 
    
         
            -
                  unless record = get_record( 
     | 
| 
       61 
     | 
    
         
            -
                    raise "Record not found for (#{ 
     | 
| 
      
 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( 
     | 
| 
      
 68 
     | 
    
         
            +
                def get_host(key, zone, kind)
         
     | 
| 
       69 
69 
     | 
    
         
             
                  if kind.eql?('A')
         
     | 
| 
       70 
     | 
    
         
            -
                    host =  
     | 
| 
      
 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 =  
     | 
| 
      
 74 
     | 
    
         
            +
                      host = key.split('.').last
         
     | 
| 
       75 
75 
     | 
    
         
             
                    when 3
         
     | 
| 
       76 
     | 
    
         
            -
                      host =  
     | 
| 
      
 76 
     | 
    
         
            +
                      host = key.split('.')[2..3].reverse.join('.')
         
     | 
| 
       77 
77 
     | 
    
         
             
                    when 2
         
     | 
| 
       78 
     | 
    
         
            -
                      host =  
     | 
| 
      
 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 
     | 
| 
      
 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
         
     | 
    
        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. 
     | 
| 
      
 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:
         
     |