Class: Fission::Lease

Inherits:
Object show all
Defined in:
lib/fission/lease.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Lease

Public: Initialize a Lease object.

args - Hash of arguments:

:ip_address  - String which denotes the IP address of the lease.
:mac_address - String which denotes the MAC address of the lease.
:start       - DateTime which denotes the start of the lease.
:end         - DateTime which denotes the end of the lease.

Examples

Returns a new Lease instance.



27
28
29
30
31
32
# File 'lib/fission/lease.rb', line 27

def initialize(args={})
  @ip_address = args[:ip_address]
  @mac_address = args[:mac_address]
  @start = args[:start]
  @end = args[:end]
end

Instance Attribute Details

#endObject

Public: Get/set the end DateTime for the lease.



14
15
16
# File 'lib/fission/lease.rb', line 14

def end
  @end
end

#ip_addressObject

Public: Get/set the IP address for the lease.



5
6
7
# File 'lib/fission/lease.rb', line 5

def ip_address
  @ip_address
end

#mac_addressObject

Public: Get/set the MAC address for the lease.



8
9
10
# File 'lib/fission/lease.rb', line 8

def mac_address
  @mac_address
end

#startObject

Public: Get/set the start DateTime for the lease.



11
12
13
# File 'lib/fission/lease.rb', line 11

def start
  @start
end

Class Method Details

.allObject

Public: Provides all of the known leases.

Examples:

Fission::Lease.all
# => [#<Fission::Lease:0x000001008b6d88...>,
      #<Fission::Lease:0x000001008522c0...>]

Returns a Response with the result. If successful, the Response’s data attribute will be an Array of Lease objects. If no leases are found, then the Response’s data attribute will be an empty Array. If there is an error, an unsuccessful Response will be returned.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fission/lease.rb', line 60

def self.all
  response = Response.new

  if File.file? Fission.config['lease_file']
    content = File.read Fission.config['lease_file']

    response.data = content.split('}').collect do |entry|
      parse entry
    end

    content = nil

    response.code = 0
  else
    response.code = 1
    response.message = "Unable to find the lease file '#{Fission.config['lease_file']}'"
  end

  response
end

.find_by_mac_address(mac_address) ⇒ Object

Public: Get lease information for a specific MAC address.

mac_address - MAC address (String) to search for.

Examples

Fission::Lease.find_by_mac '00:11:AA:bb:cc:22'
# => #<Fission::Lease:0x000001008522c0...>

Returns a Response with the result. If successful, the Response’s data attribute will be a Lease object if the MAC address was found. The Response’s data attribute will be nil if the MAC address was not found. If there is an error, an unsuccessful Response will be returned.



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

def self.find_by_mac_address(mac_address)
  all_response = all

  if all_response.successful?
    response = Response.new :code => 0
    leases = all_response.data.find_all { |l| l.mac_address == mac_address }
    response.data = leases.sort_by { |l| l.end }.last
  else
    response = all_response
  end

  response
end

Instance Method Details

#expired?Boolean

Public: Determine if the lease has expired or not.

Examples:

@lease.expired?
# => true

Returns a Boolean. The Boolean is determined by comparing the end attribute to the current date/time.

Returns:

  • (Boolean)


43
44
45
# File 'lib/fission/lease.rb', line 43

def expired?
  @end < DateTime.now
end