Class: Porkbun::DNS

Inherits:
Abstract show all
Defined in:
lib/porkbun.rb

Instance Attribute Summary collapse

Attributes inherited from Abstract

#message, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#parse_response, #success?

Constructor Details

#initialize(options) ⇒ DNS

Returns a new instance of DNS.



44
45
46
47
48
49
50
51
52
# File 'lib/porkbun.rb', line 44

def initialize(options)
  @name = options[:name]
  @content = options[:content]
  @type = options[:type]
  @ttl = options[:ttl] || 600
  @prio = options[:prio]
  @domain = options[:domain]
  @id = options[:id]
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



42
43
44
# File 'lib/porkbun.rb', line 42

def content
  @content
end

#domainObject

Returns the value of attribute domain.



42
43
44
# File 'lib/porkbun.rb', line 42

def domain
  @domain
end

#idObject

Returns the value of attribute id.



42
43
44
# File 'lib/porkbun.rb', line 42

def id
  @id
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/porkbun.rb', line 42

def name
  @name
end

#notesObject

Returns the value of attribute notes.



42
43
44
# File 'lib/porkbun.rb', line 42

def notes
  @notes
end

#prioObject

Returns the value of attribute prio.



42
43
44
# File 'lib/porkbun.rb', line 42

def prio
  @prio
end

#ttlObject

Returns the value of attribute ttl.



42
43
44
# File 'lib/porkbun.rb', line 42

def ttl
  @ttl
end

#typeObject

Returns the value of attribute type.



42
43
44
# File 'lib/porkbun.rb', line 42

def type
  @type
end

Class Method Details

.create(options) ⇒ Object



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

def self.create(options)
  record = DNS.new options
  record.create
end

.retrieve(domain, id = nil) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
71
72
# File 'lib/porkbun.rb', line 63

def self.retrieve(domain, id = nil)
  raise Error, 'need domain' unless domain

  res = Porkbun.porkbun File.join('dns/retrieve', domain, id || '').chomp('/')
  return Error.new(res[:message]) if res[:status] == 'ERROR'

  res[:records].map do |record|
    DNS.new record.merge(domain:)
  end
end

Instance Method Details

#createObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/porkbun.rb', line 97

def create
  options = {
    name:,
    content:,
    type:,
    ttl:
  }
  options.merge!(prio:) if prio
  res = Porkbun.porkbun File.join('dns/create', domain), options
  parse_response res
  @id = res[:id]
  self
end

#deleteObject

Raises:



74
75
76
77
78
79
80
# File 'lib/porkbun.rb', line 74

def delete
  raise Error, 'Need ID to delete record' unless id

  res = Porkbun.porkbun File.join('dns/delete', domain, id)
  parse_response res
  self
end

#editObject

Raises:



59
60
61
# File 'lib/porkbun.rb', line 59

def edit
  raise Error, 'need id to edit' unless @id
end

#to_sObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/porkbun.rb', line 83

def to_s
  content_str = case type
                when /TXT|SPF/
                  "\"#{content}\""
                when /MX|CNAME|NS/
                  "#{content}."
                else
                  String(content)
                end

  prio_str = prio == '0' ? '' : prio
  "#{name}. #{ttl} IN #{type} #{prio_str} #{content_str}".tr_s(' ', ' ')
end