Class: Dennis::Record
- Inherits:
-
Object
- Object
- Dennis::Record
- Defined in:
- lib/dennis/record.rb
Constant Summary collapse
- IRREGULAR_RECORD_TYPE_NAMES =
{ 'HTTPREDIRECT' => 'HTTPRedirect' }.freeze
Class Method Summary collapse
- .all(client, zone, **options) ⇒ Object
- .all_by_tag(client, tags, group: nil) ⇒ Object
- .create(client, zone:, **properties) ⇒ Object
- .create_or_update(client, zone_id, **properties) ⇒ Object
- .find_by(client, field, value) ⇒ Object
- .normalize_type(type) ⇒ Object
- .properties_to_argument(hash, type: nil) ⇒ Object
Instance Method Summary collapse
- #content ⇒ Object
- #created_at ⇒ Object
- #delete ⇒ Object
- #display_content ⇒ Object
- #external_reference ⇒ Object
- #full_name ⇒ Object
- #id ⇒ Object
-
#initialize(client, hash) ⇒ Record
constructor
A new instance of Record.
- #managed? ⇒ Boolean
- #name ⇒ Object
- #priority ⇒ Object
- #raw_content ⇒ Object
- #tags ⇒ Object
- #ttl ⇒ Object
- #type ⇒ Object
- #update(properties) ⇒ Object
- #updated_at ⇒ Object
- #zone ⇒ Object
Constructor Details
#initialize(client, hash) ⇒ Record
Returns a new instance of Record.
94 95 96 97 |
# File 'lib/dennis/record.rb', line 94 def initialize(client, hash) @client = client @hash = hash end |
Class Method Details
.all(client, zone, **options) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/dennis/record.rb', line 16 def all(client, zone, **) request = client.api.create_request(:get, 'zones/:zone/records') request.arguments[:zone] = zone .each do |field, value| request.arguments[field] = value end request.perform.hash['records'].map { |hash| new(client, hash) } end |
.all_by_tag(client, tags, group: nil) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/dennis/record.rb', line 25 def all_by_tag(client, , group: nil) request = client.api.create_request(:get, 'records/tagged') request.arguments[:tags] = request.arguments[:group] = group if group request.perform.hash['records'].map { |hash| new(client, hash) } end |
.create(client, zone:, **properties) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dennis/record.rb', line 40 def create(client, zone:, **properties) request = client.api.create_request(:post, 'records') request.arguments[:zone] = zone request.arguments[:properties] = properties_to_argument(properties) new(client, request.perform.hash['record']) rescue ApiaClient::RequestError => e raise ZoneNotFoundError if e.code == 'zone_not_found' raise ValidationError, e.detail['errors'] if e.code == 'validation_error' raise end |
.create_or_update(client, zone_id, **properties) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dennis/record.rb', line 52 def create_or_update(client, zone_id, **properties) if properties[:external_reference].nil? raise Dennis::ExternalReferenceRequiredError, 'An external_reference must be provided to use create_or_update' end record = find_by(client, :external_reference, properties[:external_reference]) if record.nil? create(client, zone: { id: zone_id }, **properties) else record.update(properties) record end end |
.find_by(client, field, value) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/dennis/record.rb', line 32 def find_by(client, field, value) request = client.api.create_request(:get, 'records/:record') request.arguments[:record] = { field => value } new(client, request.perform.hash['record']) rescue ApiaClient::RequestError => e e.code == 'record_not_found' ? nil : raise end |
.normalize_type(type) ⇒ Object
86 87 88 89 90 |
# File 'lib/dennis/record.rb', line 86 def normalize_type(type) type = type.to_s.upcase type = IRREGULAR_RECORD_TYPE_NAMES[type] if IRREGULAR_RECORD_TYPE_NAMES.key?(type) type end |
.properties_to_argument(hash, type: nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dennis/record.rb', line 66 def properties_to_argument(hash, type: nil) arguments = {} [:name, :type, :ttl, :priority, :external_reference, :tags].each do |field_name| arguments[field_name] = hash[field_name] if hash.key?(field_name) type = hash[field_name] if field_name == :type && hash.key?(field_name) end type = normalize_type(type) if hash.key?(:content) if type.nil? raise Error, 'Cannot generate record properties without a type' end arguments[:content] = { type => hash[:content] } end arguments end |
Instance Method Details
#content ⇒ Object
164 165 166 167 168 |
# File 'lib/dennis/record.rb', line 164 def content return nil if type.nil? @hash.dig('content', self.class.normalize_type(type))&.transform_keys(&:to_sym) end |
#created_at ⇒ Object
135 136 137 138 139 140 |
# File 'lib/dennis/record.rb', line 135 def created_at return nil if @hash['created_at'].nil? return @hash['created_at'] if @hash['created_at'].is_a?(Time) Time.at(@hash['created_at']) end |
#delete ⇒ Object
182 183 184 185 186 187 |
# File 'lib/dennis/record.rb', line 182 def delete req = @client.api.create_request(:delete, 'records/:record') req.arguments['record'] = { id: id } req.perform true end |
#display_content ⇒ Object
160 161 162 |
# File 'lib/dennis/record.rb', line 160 def display_content @hash['display_content'] end |
#external_reference ⇒ Object
123 124 125 |
# File 'lib/dennis/record.rb', line 123 def external_reference @hash['external_reference'] end |
#full_name ⇒ Object
107 108 109 |
# File 'lib/dennis/record.rb', line 107 def full_name @hash['full_name'] end |
#id ⇒ Object
99 100 101 |
# File 'lib/dennis/record.rb', line 99 def id @hash['id'] end |
#managed? ⇒ Boolean
127 128 129 |
# File 'lib/dennis/record.rb', line 127 def managed? @hash['managed'] end |
#name ⇒ Object
103 104 105 |
# File 'lib/dennis/record.rb', line 103 def name @hash['name'] end |
#priority ⇒ Object
119 120 121 |
# File 'lib/dennis/record.rb', line 119 def priority @hash['priority'] end |
#raw_content ⇒ Object
156 157 158 |
# File 'lib/dennis/record.rb', line 156 def raw_content @hash['raw_content'] end |
#tags ⇒ Object
131 132 133 |
# File 'lib/dennis/record.rb', line 131 def @hash['tags'] end |
#ttl ⇒ Object
115 116 117 |
# File 'lib/dennis/record.rb', line 115 def ttl @hash['ttl'] end |
#type ⇒ Object
111 112 113 |
# File 'lib/dennis/record.rb', line 111 def type @hash['type'] end |
#update(properties) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dennis/record.rb', line 170 def update(properties) req = @client.api.create_request(:patch, 'records/:record') req.arguments['record'] = { id: id } req.arguments['properties'] = self.class.properties_to_argument(properties, type: type) @hash = req.perform.hash['record'] true rescue ApiaClient::RequestError => e raise ValidationError, e.detail['errors'] if e.code == 'validation_error' raise end |
#updated_at ⇒ Object
142 143 144 145 146 147 |
# File 'lib/dennis/record.rb', line 142 def updated_at return nil if @hash['updated_at'].nil? return @hash['updated_at'] if @hash['updated_at'].is_a?(Time) Time.at(@hash['updated_at']) end |