Class: Dnsruby::RR::GPOS

Inherits:
Dnsruby::RR show all
Defined in:
lib/dnsruby/resource/GPOS.rb

Overview

Class for Geographic Position (GPOS) resource records.

RFC 1712 (www.ietf.org/rfc/rfc1712.txt)

Constant Summary collapse

TypeValue =
Types::GPOS
ClassValue =
Classes::IN
REQUIRED_KEYS =
[:longitude, :latitude, :altitude]

Constants inherited from Dnsruby::RR

ClassInsensitiveTypes

Instance Attribute Summary collapse

Attributes inherited from Dnsruby::RR

#klass, #name, #rdata, #ttl, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dnsruby::RR

#<=>, #==, #clone, create, #eql?, find_class, get_class, get_num, #hash, implemented_rrs, #init_defaults, #rdlength, #sameRRset, #to_s

Instance Attribute Details

#altitudeObject

NOTE: these are strings, not numbers



14
15
16
# File 'lib/dnsruby/resource/GPOS.rb', line 14

def altitude
  @altitude
end

#latitudeObject

NOTE: these are strings, not numbers



14
15
16
# File 'lib/dnsruby/resource/GPOS.rb', line 14

def latitude
  @latitude
end

#longitudeObject

NOTE: these are strings, not numbers



14
15
16
# File 'lib/dnsruby/resource/GPOS.rb', line 14

def longitude
  @longitude
end

Class Method Details

.build_rdata(longitude, latitude, altitude) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dnsruby/resource/GPOS.rb', line 106

def self.build_rdata(longitude, latitude, altitude)
  binary_string = ''.b

  binary_string << longitude.length.chr
  binary_string << longitude
  binary_string << latitude.length.chr
  binary_string << latitude
  binary_string << altitude.length.chr
  binary_string << altitude
  binary_string
end

.decode_rdata(message) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dnsruby/resource/GPOS.rb', line 118

def self.decode_rdata(message)
  rdata_s = message.get_bytes.clone

  index = 0

  long_len = rdata_s[index].ord;         index += 1
  longitude = rdata_s[index, long_len];  index += long_len

  lat_len = rdata_s[index].ord;          index += 1
  latitude = rdata_s[index, lat_len];    index += lat_len

  alt_len = rdata_s[index].ord;          index += 1
  altitude = rdata_s[index, alt_len];    index += alt_len

  validate_latitude(latitude)
  validate_longitude(longitude)

  new([longitude, latitude, altitude].join(' '))  # e.g. "10.0 20.0 30.0"
end

.new_from_data(*gpos_params_data) ⇒ Object

Create an instance from an ordered parameter list, e.g.: EXAMPLE_GPOS_DATA = begin

rdata = RR::GPOS.build_rdata(EXAMPLE_LONGITUDE, EXAMPLE_LATITUDE, EXAMPLE_ALTITUDE)
[EXAMPLE_HOSTNAME, Types::GPOS, Classes::IN, EXAMPLE_TTL, rdata.length, rdata, 0]

end self.from_data(*EXAMPLE_GPOS_DATA)



58
59
60
# File 'lib/dnsruby/resource/GPOS.rb', line 58

def self.new_from_data(*gpos_params_data)
  RR.new_from_data(*gpos_params_data)
end

.new_from_hash(gpos_params_hash) ⇒ Object

Create an instance from a hash of parameters, e.g.:

{
   name:       'techhumans.com',
   type:       Types::GPOS,
   ttl:        1234,
   longitude:  '10.0',
   latitude:   '20.0',
   altitude:   '30.0',
}

Since the type is assumed to be GPOS, it will be assigned automatially, and any other value will be overwritten. Therefore, having it present in the hash is not necessary.



39
40
41
42
# File 'lib/dnsruby/resource/GPOS.rb', line 39

def self.new_from_hash(gpos_params_hash)
  gpos_params_hash[:type] = Types::GPOS
  RR.new_from_hash(gpos_params_hash)
end

.new_from_string(gpos_params_string) ⇒ Object

Create an instance from a string containing parameters, e.g.: ‘a.dnsruby.com. 10800 IN GPOS 10.0 20.0 30.0’



47
48
49
# File 'lib/dnsruby/resource/GPOS.rb', line 47

def self.new_from_string(gpos_params_string)
  RR.new_from_string(gpos_params_string)
end

.valid_float?(object) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
# File 'lib/dnsruby/resource/GPOS.rb', line 150

def self.valid_float?(object)
  begin
    Float(object)
    true
  rescue
    false
  end
end

.validate_float_in_range(label, object, bound) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/dnsruby/resource/GPOS.rb', line 159

def self.validate_float_in_range(label, object, bound)
  number = Float(object)
  valid_range = (-Float(bound)..Float(bound))
  unless valid_range.include?(number)
    raise "Value of #{label} (#{number}) was not in the range #{valid_range}."
  end
end

.validate_floats(init_data) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dnsruby/resource/GPOS.rb', line 175

def self.validate_floats(init_data)
  bad_float_keys = REQUIRED_KEYS.reject { |key| valid_float?(init_data[key]) }
  unless bad_float_keys.empty?
    message = "The following key value pair(s) do not have valid floats or float strings:\n"
    bad_float_keys.each do |key|
      message << "%:-12.12s => %s\n" % [init_data[key]]
    end
    raise message
  end

  validate_longitude(init_data[:longitude])
  validate_latitude(init_data[:latitude])
end

.validate_latitude(value) ⇒ Object



171
172
173
# File 'lib/dnsruby/resource/GPOS.rb', line 171

def self.validate_latitude(value)
  validate_float_in_range('latitude',  value, 90)
end

.validate_longitude(value) ⇒ Object



167
168
169
# File 'lib/dnsruby/resource/GPOS.rb', line 167

def self.validate_longitude(value)
  validate_float_in_range('longitude', value, 180)
end

Instance Method Details

#build_rdataObject



102
103
104
# File 'lib/dnsruby/resource/GPOS.rb', line 102

def build_rdata
  self.class.build_rdata(longitude, latitude, altitude)
end

#encode_rdata(msg, _canonical) ⇒ Object



98
99
100
# File 'lib/dnsruby/resource/GPOS.rb', line 98

def encode_rdata(msg, _canonical)
  msg.put_bytes(build_rdata)
end

#from_data(array) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dnsruby/resource/GPOS.rb', line 63

def from_data(array)
  unless array.size == 3
    raise "Array size for creating GPOS record must be 3 (long, lat, alt). Array was:\n#{array.inspect}"
  end

  from_hash({
                longitude: array[0],
                latitude:  array[1],
                altitude:  array[2]
            })
end

#from_hash(init_data) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/dnsruby/resource/GPOS.rb', line 75

def from_hash(init_data)
  self.class.validate_floats(init_data)
  @longitude = init_data[:longitude].to_s
  @latitude  = init_data[:latitude].to_s
  @altitude  = init_data[:altitude].to_s
  self.rdata = build_rdata
  self
end

#from_string(string) ⇒ Object



84
85
86
87
# File 'lib/dnsruby/resource/GPOS.rb', line 84

def from_string(string)
  # Convert commas to spaces, then split by spaces:
  from_data(string.gsub(',', ' ').split(' '))
end

#ownerObject

‘name’ is used in the RR superclass, but ‘owner’ is the term referred to in the RFC, so we’ll make owner an alias for name.



140
141
142
# File 'lib/dnsruby/resource/GPOS.rb', line 140

def owner
  name
end

#owner=(owner_string) ⇒ Object

‘name’ is used in the RR superclass, but ‘owner’ is the term referred to in the RFC, so we’ll make owner an alias for name.



146
147
148
# File 'lib/dnsruby/resource/GPOS.rb', line 146

def owner=(owner_string)
  self.name = owner_string
end

#rdata_to_stringObject

From the RFC:

GPOS has the following format:

<owner> <ttl> <class> GPOS <longitude> <latitude> <altitude>

We handle the rdata, the RR superclass does the rest.



94
95
96
# File 'lib/dnsruby/resource/GPOS.rb', line 94

def rdata_to_string
  [longitude, latitude, altitude].join(' ')
end