Class: DNSimple::Record

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dnsimple/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Record

:nodoc:



20
21
22
23
24
25
# File 'lib/dnsimple/record.rb', line 20

def initialize(attributes)
  attributes.each do |key, value|
    m = "#{key}=".to_sym
    self.send(m, value) if self.respond_to?(m)
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/dnsimple/record.rb', line 11

def content
  @content
end

#domainObject

Returns the value of attribute domain.



7
8
9
# File 'lib/dnsimple/record.rb', line 7

def domain
  @domain
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/dnsimple/record.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#prioObject

Returns the value of attribute prio.



17
18
19
# File 'lib/dnsimple/record.rb', line 17

def prio
  @prio
end

#record_typeObject

Returns the value of attribute record_type.



13
14
15
# File 'lib/dnsimple/record.rb', line 13

def record_type
  @record_type
end

#ttlObject

Returns the value of attribute ttl.



15
16
17
# File 'lib/dnsimple/record.rb', line 15

def ttl
  @ttl
end

Class Method Details

.all(domain, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dnsimple/record.rb', line 111

def self.all(domain, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{DNSimple::Client.base_uri}/domains/#{domain.name}/records", options)

  pp response if DNSimple::Client.debug?

  case response.code
  when 200
    response.map { |r| DNSimple::Record.new({:domain => domain}.merge(r["record"])) }
  when 401
    raise DNSimple::AuthenticationFailed, "Authentication failed"
  else
    raise DNSimple::Error, "Error listing domains: #{response.code}"
  end
end

.create(domain, name, record_type, content, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dnsimple/record.rb', line 68

def self.create(domain, name, record_type, content, options={})
  record_hash = {:name => name, :record_type => record_type, :content => content}
  record_hash[:ttl] = options.delete(:ttl) || 3600
  record_hash[:prio] = options.delete(:priority)
  record_hash[:prio] = options.delete(:prio) || ''
  
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  options.merge!({:body => {:record => record_hash}})

  response = self.post("#{DNSimple::Client.base_uri}/domains/#{domain.name}/records", options) 

  pp response if DNSimple::Client.debug?

  case response.code
  when 201
    return DNSimple::Record.new({:domain => domain}.merge(response["record"]))
  when 401
    raise DNSimple::AuthenticationFailed
  when 406
    raise DNSimple::RecordExists.new("#{name}.#{domain.name}", response["errors"])
  else
    raise DNSimple::Error, "Failed to create #{name}.#{domain.name}: #{response["errors"]}"
  end
end

.find(domain, id, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dnsimple/record.rb', line 93

def self.find(domain, id, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{DNSimple::Client.base_uri}/domains/#{domain.name}/records/#{id}", options)

  pp response if DNSimple::Client.debug?

  case response.code
  when 200
    return DNSimple::Record.new({:domain => domain}.merge(response["record"]))
  when 401
    raise DNSimple::AuthenticationFailed
  when 404
    raise DNSimple::RecordNotFound, "Could not find record #{id} for domain #{domain.name}"
  else
    raise DNSimple::Error, "Failed to find domain #{domain.name}/#{id}: #{response["errors"]}"
  end
end

.resolve(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dnsimple/record.rb', line 60

def self.resolve(name)
  aliases = {
    'priority' => 'prio',
    'time-to-live' => 'ttl'
  }
  aliases[name] || name
end

Instance Method Details

#delete(options = {}) ⇒ Object Also known as: destroy



54
55
56
57
# File 'lib/dnsimple/record.rb', line 54

def delete(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  self.class.delete("#{DNSimple::Client.base_uri}/domains/#{domain.id}/records/#{id}", options)
end

#fqdnObject



27
28
29
# File 'lib/dnsimple/record.rb', line 27

def fqdn
  [name, domain.name].delete_if { |v| v !~ DNSimple::BLANK_REGEX }.join(".")
end

#save(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dnsimple/record.rb', line 31

def save(options={})
  record_hash = {}
  %w(name content ttl prio).each do |attribute|
    record_hash[DNSimple::Record.resolve(attribute)] = self.send(attribute)
  end

  options.merge!(DNSimple::Client.standard_options_with_credentials)
  options.merge!(:body => {:record => record_hash})

  response = self.class.put("#{DNSimple::Client.base_uri}/domains/#{domain.id}/records/#{id}.json", options)

  pp response if DNSimple::Client.debug?

  case response.code
  when 200
    return self
  when 401
    raise DNSimple::AuthenticationFailed
  else
    raise DNSimple::Error, "Failed to update record: #{response.inspect}" 
  end
end