Class: Collins::Address

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/collins/address.rb

Constant Summary

Constants included from Util::Logging

Util::Logging::DEFAULT_LOG_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#deep_copy_hash, #get_asset_or_tag, included, #require_non_empty, #require_that, #stringify_hash, #symbolize_hash

Methods included from Util::Logging

#get_logger

Constructor Details

#initialize(model = {}) ⇒ Address

Returns a new instance of Address.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/collins/address.rb', line 33

def initialize model = {}
  hash = symbolize_hash(model).inject({}) do |result, (k,v)|
    result[k.downcase] = v
    result
  end
  @id = hash[:id].to_s.to_i
  @asset_id = hash[:asset_id].to_s.to_i
  @address = hash[:address].to_s
  @gateway = hash[:gateway].to_s
  @netmask = hash[:netmask].to_s
  @pool = hash[:pool].to_s
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



6
7
8
# File 'lib/collins/address.rb', line 6

def address
  @address
end

#asset_idObject

Returns the value of attribute asset_id.



6
7
8
# File 'lib/collins/address.rb', line 6

def asset_id
  @asset_id
end

#gatewayObject

Returns the value of attribute gateway.



6
7
8
# File 'lib/collins/address.rb', line 6

def gateway
  @gateway
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/collins/address.rb', line 6

def id
  @id
end

#netmaskObject

Returns the value of attribute netmask.



6
7
8
# File 'lib/collins/address.rb', line 6

def netmask
  @netmask
end

#poolObject

Returns the value of attribute pool.



6
7
8
# File 'lib/collins/address.rb', line 6

def pool
  @pool
end

Class Method Details

.from_json(json) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/collins/address.rb', line 9

def from_json json
  return [] if json.nil? || json.empty?
  if not json.is_a?(Array) then
    json = [json]
  end
  json.map { |j| Collins::Address.new j }
end

.is_private?(address) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/collins/address.rb', line 17

def is_private? address
  if address =~ /^10\./ then
    true
  elsif address =~ /^192\.168\./ then
    true
  elsif address =~ /^172\.(?:1[6-9]|2[0-9]|3[0-1])\./ then
    true
  else
    false
  end
end

.is_public?(address) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/collins/address.rb', line 28

def is_public? address
  not is_private?(address)
end

Instance Method Details

#is_addressable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/collins/address.rb', line 46

def is_addressable?
  @address.length > 0
end

#is_private?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/collins/address.rb', line 50

def is_private?
  Collins::Address.is_private? @address
end

#is_public?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/collins/address.rb', line 54

def is_public?
  is_addressable? and not is_private?
end

#to_hashObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/collins/address.rb', line 58

def to_hash
  {
    :address => @address,
    :gateway => @gateway,
    :netmask => @netmask,
    :pool => @pool,
    :is_private => is_private?,
    :is_public => is_public?
  }
end

#to_sObject



68
69
70
# File 'lib/collins/address.rb', line 68

def to_s
  "Collins::Address(address = %{address}, gateway = %{gateway}, netmask = %{netmask}, is_private = %{is_private})" % to_hash
end