Module: Macker
- Extended by:
- Macker
- Included in:
- Macker
- Defined in:
- lib/macker.rb,
lib/macker/config.rb,
lib/macker/address.rb,
lib/macker/version.rb
Overview
Macker namespace
Defined Under Namespace
Classes: Address, Config, InvalidAddress, InvalidCache, InvalidOptions, InvalidRawData, NotFoundOuiVendor
Constant Summary collapse
- VERSION =
Macker version
'0.1.5'.freeze
Instance Attribute Summary collapse
-
#mem_timestamp ⇒ Time
readonly
Get the timestamp of vendor list in memory.
-
#proc_timestamp ⇒ Time
Proc timestamp accessor to set external timestamp.
Instance Method Summary collapse
-
#config ⇒ OpenStruct
Get or initialize Macker config.
-
#configure {|config| ... } ⇒ OpenStruct
Set configuration of Macker in a block.
-
#expire! ⇒ Boolean
Fetch new vendor list if cached list is expired.
-
#expired? ⇒ Boolean
Check if vendor list is expired.
-
#generate(opts = {}) ⇒ Address
Generate a MAC address.
-
#generate!(opts = {}) ⇒ Address
Generate a MAC address.
-
#iso_code_table ⇒ Hash
Vendor table with all country iso codes as keys.
-
#lapsed! ⇒ Boolean
Fetch new vendor list if cached list is lapsed (expired or stale).
-
#lookup(mac, opts = {}) ⇒ Address
Lookup for a vendor with given MAC address.
-
#lookup!(mac) ⇒ Address
Lookup for a vendor with given MAC address.
-
#prefix_table ⇒ Hash
Vendor table with all base16 MAC prefixes as keys.
-
#stale! ⇒ Boolean
Fetch new vendor list if cached list is stale.
-
#stale? ⇒ Boolean
Check if vendor list is stale Stale is true if vendor list is updated straight by another thread.
-
#update(straight = false) ⇒ Time
Update all OUI tables.
-
#vendor_table ⇒ Hash
Vendor table with all country vendor names as keys.
-
#vendors_expiration ⇒ Time
Get vendor list expiration time based on ttl.
Instance Attribute Details
#mem_timestamp ⇒ Time (readonly)
Get the timestamp of vendor list in memory
30 31 32 |
# File 'lib/macker.rb', line 30 def @mem_timestamp end |
#proc_timestamp ⇒ Time
Proc timestamp accessor to set external timestamp
26 27 28 |
# File 'lib/macker.rb', line 26 def @proc_timestamp end |
Instance Method Details
#config ⇒ OpenStruct
Get or initialize Macker config.
34 35 36 |
# File 'lib/macker.rb', line 34 def config @config ||= Macker::Config.new end |
#configure {|config| ... } ⇒ OpenStruct
Set configuration of Macker in a block.
40 41 42 |
# File 'lib/macker.rb', line 40 def configure yield config if block_given? end |
#expire! ⇒ Boolean
Fetch new vendor list if cached list is expired
172 173 174 175 176 177 178 179 |
# File 'lib/macker.rb', line 172 def expire! if expired? update(true) true else false end end |
#expired? ⇒ Boolean
Check if vendor list is expired
194 195 196 |
# File 'lib/macker.rb', line 194 def expired? Time.now > vendors_expiration end |
#generate(opts = {}) ⇒ Address
Generate a MAC address.
-
No options for random MAC.
-
Vendor option to get a valid OUI MAC.
-
Vendor Name option to get a random MAC from vendor.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/macker.rb', line 102 def generate(opts = {}) lapsed! return generate_by_iso_code(opts.delete(:iso_code).upcase, opts) if opts[:iso_code] vendor = opts.delete(:vendor) case vendor when nil, false Address.new(rand(2**48)) when true generate_by_vendor(prefix_table[prefix_table.keys.shuffle.sample][:name], opts) when String generate_by_vendor(vendor, opts) else raise(InvalidOptions, "Incompatible option vendor for generate: #{vendor.class}") end end |
#generate!(opts = {}) ⇒ Address
Generate a MAC address.
-
No options for random MAC address.
-
Vendor option to get a valid OUI MAC address.
-
Vendor name option to get a random MAC address from vendor.
Raises an error if an error occurs.
131 132 133 |
# File 'lib/macker.rb', line 131 def generate!(opts = {}) generate(opts.merge(raising: true)) end |
#iso_code_table ⇒ Hash
Vendor table with all country iso codes as keys
144 145 146 147 |
# File 'lib/macker.rb', line 144 def iso_code_table update unless @iso_code_table @iso_code_table end |
#lapsed! ⇒ Boolean
Fetch new vendor list if cached list is lapsed (expired or stale)
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/macker.rb', line 158 def lapsed! if config.auto_expire && expired? update(true) true elsif config.auto_stale && stale? update true else false end end |
#lookup(mac, opts = {}) ⇒ Address
Lookup for a vendor with given MAC address.
68 69 70 71 72 73 74 75 |
# File 'lib/macker.rb', line 68 def lookup(mac, opts = {}) lapsed! data = prefix_table[Address.new(mac).prefix] if data.nil? opts[:raising] ? raise(NotFoundOuiVendor, "OUI not found for MAC: #{mac}") : (return nil) end Address.new(mac, data) end |
#lookup!(mac) ⇒ Address
Lookup for a vendor with given MAC address. Raises an error if no vendor found.
86 87 88 |
# File 'lib/macker.rb', line 86 def lookup!(mac) lookup(mac, raising: true) end |
#prefix_table ⇒ Hash
Vendor table with all base16 MAC prefixes as keys
137 138 139 140 |
# File 'lib/macker.rb', line 137 def prefix_table update unless @prefix_table @prefix_table end |
#stale! ⇒ Boolean
Fetch new vendor list if cached list is stale
183 184 185 186 187 188 189 190 |
# File 'lib/macker.rb', line 183 def stale! if stale? update true else false end end |
#stale? ⇒ Boolean
Check if vendor list is stale Stale is true if vendor list is updated straight by another thread. The actual thread has always old vendor list in memory store.
202 203 204 205 206 207 208 |
# File 'lib/macker.rb', line 202 def stale? if config.cache.is_a?(Proc) != else != end end |
#update(straight = false) ⇒ Time
Update all OUI tables
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/macker.rb', line 47 def update(straight = false) @prefix_table = {} @iso_code_table = {} @vendor_table = {} vendor_list(straight) @mem_timestamp = if config.cache.is_a?(Proc) else end end |
#vendor_table ⇒ Hash
Vendor table with all country vendor names as keys
151 152 153 154 |
# File 'lib/macker.rb', line 151 def vendor_table update unless @vendor_table @vendor_table end |
#vendors_expiration ⇒ Time
Get vendor list expiration time based on ttl
212 213 214 215 216 217 218 |
# File 'lib/macker.rb', line 212 def vendors_expiration if config.cache.is_a?(Proc) + config.ttl_in_seconds else + config.ttl_in_seconds end end |