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.



47
48
49
50
51
52
53
54
55
# File 'lib/porkbun.rb', line 47

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.



45
46
47
# File 'lib/porkbun.rb', line 45

def content
  @content
end

#domainObject

Returns the value of attribute domain.



45
46
47
# File 'lib/porkbun.rb', line 45

def domain
  @domain
end

#idObject

Returns the value of attribute id.



45
46
47
# File 'lib/porkbun.rb', line 45

def id
  @id
end

#nameObject

Returns the value of attribute name.



45
46
47
# File 'lib/porkbun.rb', line 45

def name
  @name
end

#notesObject

Returns the value of attribute notes.



45
46
47
# File 'lib/porkbun.rb', line 45

def notes
  @notes
end

#prioObject

Returns the value of attribute prio.



45
46
47
# File 'lib/porkbun.rb', line 45

def prio
  @prio
end

#ttlObject

Returns the value of attribute ttl.



45
46
47
# File 'lib/porkbun.rb', line 45

def ttl
  @ttl
end

#typeObject

Returns the value of attribute type.



45
46
47
# File 'lib/porkbun.rb', line 45

def type
  @type
end

Class Method Details

.create(options) ⇒ Object



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

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

.retrieve(domain, id = nil) ⇒ Object

Raises:



75
76
77
78
79
80
81
82
83
84
# File 'lib/porkbun.rb', line 75

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



120
121
122
123
124
125
# File 'lib/porkbun.rb', line 120

def create
  res = Porkbun.porkbun File.join('dns/create', domain), get_options
  parse_response res
  @id = res[:id]
  self
end

#deleteObject

Raises:



86
87
88
89
90
91
92
# File 'lib/porkbun.rb', line 86

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:



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

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

  res = Porkbun.porkbun File.join('dns/edit', domain, id), get_options
  parse_response res
  @id = res[:id]
  self
end

#saveObject



71
72
73
# File 'lib/porkbun.rb', line 71

def save
  edit
end

#to_hObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/porkbun.rb', line 108

def to_h
  {
    name: name,
    content: content,
    type: type,
    ttl: ttl,
    prio: prio,
    domain: domain,
    id: id
  }
end

#to_sObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/porkbun.rb', line 94

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