Module: OUI
Overview
Organizationally Unique Identifier
Constant Summary collapse
- @@local_db =
LOCAL_DB_DEFAULT
- @@debugging =
DEBUGGING_DEFAULT
- @@import_local_txt_file =
IMPORT_LOCAL_TXT_FILE_DEFAULT
- @@in_memory_only =
IN_MEMORY_ONLY_DEFAULT
- @@db =
nil
Instance Method Summary collapse
- #clear_table ⇒ Object
-
#close_db ⇒ Object
Release backend resources.
- #debugging ⇒ Object
- #debugging=(v) ⇒ Object
- #find(oui) ⇒ Hash?
- #import_local_txt_file ⇒ Object
- #import_local_txt_file=(v) ⇒ Object
- #in_memory_only ⇒ Object
- #in_memory_only=(v) ⇒ Object
- #local_db ⇒ Object
- #local_db=(v) ⇒ Object
-
#to_i(oui) ⇒ Integer
Converts an OUI string to an integer of equal value.
-
#to_s(id, sep = '-') ⇒ String
Convert an id to OUI.
-
#update_db(local = nil, db_file = nil) ⇒ Integer
Update database from fetched URL.
Instance Method Details
#clear_table ⇒ Object
138 139 140 141 |
# File 'lib/oui.rb', line 138 def clear_table debug 'clear_table' table.delete_sql end |
#close_db ⇒ Object
Release backend resources
128 129 130 131 132 133 134 135 136 |
# File 'lib/oui.rb', line 128 def close_db semaphore.synchronize do debug 'Closing database' if @@db @@db.disconnect @@db = nil end end end |
#debugging ⇒ Object
59 60 61 |
# File 'lib/oui.rb', line 59 def debugging @@debugging end |
#debugging=(v) ⇒ Object
63 64 65 |
# File 'lib/oui.rb', line 63 def debugging=(v) @@debugging = (v.nil?) ? DEBUGGING_DEFAULT : v end |
#find(oui) ⇒ Hash?
94 95 96 97 98 99 100 101 |
# File 'lib/oui.rb', line 94 def find(oui) semaphore.synchronize do update_db unless table? && table.count > 0 r = table.where(id: self.to_i(oui)).first r.delete :create_table if r # not sure why this is here, but nuking it r end end |
#import_local_txt_file ⇒ Object
70 71 72 |
# File 'lib/oui.rb', line 70 def import_local_txt_file @@import_local_txt_file end |
#import_local_txt_file=(v) ⇒ Object
74 75 76 |
# File 'lib/oui.rb', line 74 def import_local_txt_file=(v) @@import_local_txt_file = (v.nil?) ? IMPORT_LOCAL_TXT_FILE_DEFAULT : v end |
#in_memory_only ⇒ Object
81 82 83 |
# File 'lib/oui.rb', line 81 def in_memory_only @@in_memory_only end |
#in_memory_only=(v) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/oui.rb', line 85 def in_memory_only=(v) if v != @@in_memory_only @@in_memory_only = (v.nil?) ? IN_MEMORY_ONLY_DEFAULT : v close_db end end |
#local_db ⇒ Object
48 49 50 |
# File 'lib/oui.rb', line 48 def local_db @@local_db end |
#local_db=(v) ⇒ Object
52 53 54 |
# File 'lib/oui.rb', line 52 def local_db=(v) @@local_db = v || LOCAL_DB_DEFAULT end |
#to_i(oui) ⇒ Integer
Converts an OUI string to an integer of equal value
107 108 109 110 111 112 113 |
# File 'lib/oui.rb', line 107 def to_i(oui) return oui if oui.is_a? Integer oui = oui.strip.gsub(/[:\- .]/, '') if oui =~ /([[:xdigit:]]{6})/ $1.to_i(16) end end |
#to_s(id, sep = '-') ⇒ String
Convert an id to OUI
118 119 120 121 122 123 124 |
# File 'lib/oui.rb', line 118 def to_s(id, sep = '-') return id if id.is_a? String unless id >= 0x000000 && id <= 0xFFFFFF raise ArgumentError, "#{id} is not a valid 24-bit OUI" end format('%06x', id).scan(/../).join(sep) end |
#update_db(local = nil, db_file = nil) ⇒ Integer
Update database from fetched URL
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/oui.rb', line 146 def update_db(local = nil, db_file = nil) semaphore.synchronize do debug "update_db(local = #{local}, db_file = #{db_file})" close_db old_import_local_txt_file = self.import_local_txt_file self.import_local_txt_file = local old_local_db = self.local_db self.local_db = db_file ## Sequel debug '--- close db ---' debug '--- close db ---' debug '--- drop table ---' drop_table # debug '--- drop table ---' # debug '--- create table ---' create_table # debug '--- create table ---' db.transaction do debug '--- clear table ---' clear_table debug '--- clear table ---' debug '--- install manual ---' install_manual debug '--- install manual ---' debug '--- install updates ---' install_updates debug '--- install updates ---' end debug '--- close db ---' close_db debug '--- close db ---' self.local_db = old_local_db self.import_local_txt_file = old_import_local_txt_file ## AR # self.transaction do # self.delete_all # self.install_manual # self.install_updates # end debug "update_db(local = #{local}, db_file = #{db_file}) finish" end end |