Class: Dnsync::Dnsimple

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsync/dnsimple.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, token, domain) ⇒ Dnsimple

Returns a new instance of Dnsimple.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dnsync/dnsimple.rb', line 11

def initialize(email, token, domain)
  unless email.present?
    raise ArgumentError, "email must be specified"
  end

  unless token.present?
    raise ArgumentError, "token must be specified"
  end

  unless domain.present?
    raise ArgumentError, "domain must be specified"
  end

  @email  = email
  @token  = token
  @domain = domain
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



9
10
11
# File 'lib/dnsync/dnsimple.rb', line 9

def domain
  @domain
end

Instance Method Details

#connectionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dnsync/dnsimple.rb', line 29

def connection
  @connection ||= Faraday.new('https://api.dnsimple.com/v1/') do |conn|
    conn.request :url_encoded # form-encode POST params

    # conn.response :logger
    conn.response :raise_error
    conn.response :json, :content_type => /\bjson$/

    conn.adapter Faraday.default_adapter

    conn.headers['X-DNSimple-Token'] = "#{@email}:#{@token}"

    conn.options.timeout      = 5
    conn.options.open_timeout = 5
  end
end

#zoneObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dnsync/dnsimple.rb', line 46

def zone
  records_by_record = connection.get("domains/#{@domain}/records").body.group_by do |dnsimple_record|
    [ dnsimple_record['record']['name'], dnsimple_record['record']['record_type'] ]
  end

  records = records_by_record.map do |(name, record_type), records|
    fqdn = name.present? ? "#{name}.#{@domain}" : @domain
    ttl = records.first['record']['ttl']

    answers = records.map do |record|
      Answer.new(record['record']['content'], record['record']['prio'])
    end

    Record.new(fqdn, record_type, ttl, answers)
  end

  Zone.new(@domain, records)
end